Lets hack Flash Loans | Flash loan assignment/discussion

It seems you did all correct Good job :slight_smile:
The no-profit fail-safe is build in so you will not waste any gas but can also be removed.

hello @Andrewgaven, in both cases you executed the code to arbitrate between uniswap A to B and changed the same tokens (from DAI to BAT then to DAI) this will give you profit for the arbitrage opportunity there is. So that the “no profit fail” is activated it is because it is not enough to pay the flashloan back.
For example, if you change from DAI to MKR and then to DAI, it will not give you profit and it would activate the revert canceling the entire loan.

1 Like

I later noticed that with my first trade I change the pools liquidity, so went I swap Exchange A for Exchange B the rates the second trade also came profitable, I was just confused as to why it didn’t turn out like the video. But went I try again without swaping exchanges I got the “no profit fail”

2 Likes

Where can I see the 1M Dai of the flashloan?
And …
What is the actual code for
// INSERT YOUR USE CASE HERE

3 Likes

I was going to try a quick flashloan on the mainnet and the cost just to deploy the contract was €7!!! All these fees are kiling me! Will these prices go down? How much do I need to play around with in order to expect to make a profit? Is it worth dong this with €100-worth of crypto?

2 Likes

Depends on the profits mostly people with ± 100K token arbitrage opportunities.
With Flash Loans it makes it a bit more easy but GAS is still really high that is why there is a no profit revert option but you still pay part of the GAS costs

2 Likes

@amadeobrands Could this high price of gas be generated by so many Yield farming transactions?

1 Like

Hi @amadeobrands / everyone,

Great course, really enjoyed it and learned tons.

I was trying to get some arbitrage from Dai to Lend and the other way around, the Tx got executed, but as “did not profit”…though the exchange rate from exchange A is = 100 dai / 67.4589 lend & in exchange B is = 67.4589 lend / 115.8661 dai.

Can you take a look and just let me know if I did anything wrong, I would really appreciate the help, cheers.

SC:
pragma solidity ^0.5.0;

import “https://github.com/mrdavey/ez-flashloan/blob/remix/contracts/aave/FlashLoanReceiverBase.sol”;
import “https://github.com/mrdavey/ez-flashloan/blob/remix/contracts/aave/ILendingPool.sol”;
import “https://github.com/Robsonsjre/FlashloanUsecases/blob/master/contracts/interfaces/IUniswap.sol”;

//100 DAI = 100000000000000000000 (18 decimals)
/*

  • Arbitrageur is a contract to simulate the usage of flashloans

  • to make profit out of a market inbalacement

  • For this example we deployed 2 Uniswap instances which we’ll

  • call by ExchangeA and ExchangeB

  • The steps happens as following:

    1. Borrow DAI from Aave
    1. Buy LEND with DAI on ExchangeA
    1. Sell LEND for DAI on ExchangeB
    1. Repay Aave loan
    1. Keep the profits
      */
      contract Arbitrageur is
      FlashLoanReceiverBase(address(0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5) {
      address public constant DAI_ADDRESS = 0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD;
      address public constant LEND_ADDRESS = 0x1BCe8A0757B7315b74bA1C7A731197295ca4747a;
      address public constant UNISWAP_FACTORY_A = 0xECc6C0542710a0EF07966D7d1B10fA38bbb86523;
      address public constant UNISWAP_FACTORY_B = 0x54Ac34e5cE84C501165674782582ADce2FDdc8F4;

    ILendingPool public lendingPool;
    IUniswapExchange public exchangeA;
    IUniswapExchange public exchangeB;
    IUniswapFactory public uniswapFactoryA;
    IUniswapFactory public uniswapFactoryB;

    constructor() public {
    // Instantiate Uniswap Factory A
    uniswapFactoryA = IUniswapFactory(UNISWAP_FACTORY_A);
    // get Exchange A Address
    address exchangeA_address = uniswapFactoryA.getExchange(DAI_ADDRESS);
    // Instantiate Exchange A
    exchangeA = IUniswapExchange(exchangeA_address);

     //Instantiate Uniswap Factory B
     uniswapFactoryB = IUniswapFactory(UNISWAP_FACTORY_B);
     // get Exchange B Address
     address exchangeB_address = uniswapFactoryB.getExchange(LEND_ADDRESS);
     //Instantiate Exchange B
     exchangeB = IUniswapExchange(exchangeB_address);
     // get lendingPoolAddress
     address lendingPoolAddress = addressesProvider.getLendingPool();
     //Instantiate Aaave Lending Pool B
     lendingPool = ILendingPool(lendingPoolAddress);
    

    }

    /*

    • Start the arbitrage
      */
      function makeArbitrage(uint256 amount) public onlyOwner {
      bytes memory data = “”;

      ERC20 dai = ERC20(DAI_ADDRESS);
      lendingPool.flashLoan(address(this), DAI_ADDRESS, amount, data);

      // Any left amount of DAI is considered profit
      uint256 profit = dai.balanceOf(address(this));
      // Sending back the profits
      require(
      dai.transfer(msg.sender, profit),
      “Could not transfer back the profit”
      );
      }

    function executeOperation(
    address _reserve,
    uint256 _amount,
    uint256 _fee,
    bytes calldata _params
    ) external {
    // If transactions are not mined until deadline the transaction is reverted
    uint256 deadline = getDeadline();

     ERC20 dai = ERC20(DAI_ADDRESS);
     ERC20 lend = ERC20(0x1BCe8A0757B7315b74bA1C7A731197295ca4747a);
    
     // Buying ETH at Exchange A
     require(
         dai.approve(address(exchangeA), _amount
         "Could not approve DAI sell"
     );
    
     uint256 tokenBought = exchangeA.tokenToTokenSwapInput(
         _amount,
         1,
         1,
         deadline,
         LEND_ADDRESS_ADDRESS);
    
     require(
         lend.approve(address(exchangeB), tokenBought),
         "Could not approve DAI sell"
     );
    
     // Selling ETH at Exchange B
     uint256 daiBought = exchangeB.tokenToTokenSwapInput(
         tokenBought,
         1,
         1,
         deadline,
         DAI_ADDRESS
     );
    
     // Repay loan
     uint256 totalDebt = _amount.add(_fee);
    
     require(daiBought > totalDebt, "Did not profit");
    
     transferFundsBackToPoolInternal(_reserve, totalDebt);
    

    }

    function getDeadline() internal view returns (uint256) {
    return now + 3000;
    }
    }

2 Likes

Hi Amadeo and Rob!

Many thanks for putting together this great course to explain DeFi so well!

I wanted to let you know that I took the Arbitrageur.sol sample contract and deployed it to Kovan but I kept hitting the “Did not profit” transaction revert. So I checked out the two exchange web UIs and noticed that Exchange A and B needed to be swapped in the code. So I swapped the exchanges around in order to get a profit!

When I performed the flashloan for 1000 DAI, I made a profit of 196 DAI as shown in this transaction.

Is this because the prices have fluctuated in Uniswap V1 and V2 since the video was recorded?

I am looking forward to completing the rest of the course!

Many thanks.

Darren

1 Like

Yes always check the GAS station before doing transactions.
I think there is an dangerous development and the whole YIELD farming etc is becoming a WALE game … I hope we will see ways of making it also more accessible for smaller holders.

1 Like

Yes, since lately the cost of gas per transaction is quite high, including to test flashloan on the mainnet or place liquidity in compound or aave.
@amadeobrands could eip-1559 solve this problem?

1 Like

This would be fantastic. Looking to find more solutions to go bankless. Also of course yield hacking strategies etc to better understand DeFi moving forward.

1 Like

I am having a strange issue with Meta Mask lately where when I try interacting things like Uni swap some buttons flat out do nothing. I used to experience just in Brave browser but seems to be more common now. For example I was able to connect to Uni Swap contract add a pool and now adding liquidity to the pool I don’t get an error I just get nothing. Any one else experience that?

I was messing around with a way to add token information to the basic swap contract. My programming knowledge is beginner level. Open to feedback.

1 Like

Hi Everyone,

I’ve created an arbitrage opportunity using the two Uniswap instances in Kovan as follows:

  • Exchange A Exchange Rate 1 ETH = 298.5672 USDT
  • Exchange B Exchange Rate 1 ETH = 202.8706 USDT

USDT Token Address: 0x13512979ADE267AB5100878E2e0f485B568328a4

My Arbitrage results using a flash loan of 1 DAI: https://kovan.etherscan.io/tx/0x3ff304c2bf44fe295877c098ac0ec5c2bc9bd0fcf149af98aaae3bb3478c59a8

Feel free to add more liquidity to the pools and try out some arbitrage here!

Many thanks.

Darren

2 Likes

It is an issue but also a non issue since lot of contracts are just not well constructed and we need better standards etc … I do think EIP-1559 will help a bit but the main thing in my view is to of board transactions to layer 2 and group transfers etc

1 Like

What is the question?
You have added a coin?

Nice will inject it with some extra ETH :smiley:
Good job :muscle:

1 Like

To the code you posted on GIthub I added the ability to add contract addresses and link them to token names. Rather then look them up on etherscan I could look them up inside the same smart contract. Was just linking my code in the chat for any feedback anyone had. My solidity knowledge is very limited though always trying to learn.

Also thanks for making such a fantastic course.

2 Likes

Hi @amadeobrands, this course is great, thank you. I have (as usual) been trying to follow on exactly with the practical steps in the videos, but it seems that I can not create a new exchange (on https://kovan-uniswap.netlify.app/ at least) for “Create your own Arbitrage opportunity part 2” as all the tokens having an Aave faucet already seem to have had exchanges already created by other students. Is it possible to clear them down once in a while so that we are experimenting with our own “virgin” exchanges for these tokens? Thanks!

1 Like

Hi Amadeo,

Thanks for the amazing course… the DeFi 101 and 201 are very interesting. And its great that we are still in the beginning of the DeFi space. So we can develop a lot which will help mankind forward and eventually get rid of the old banking systems.

I’m working on a idea to bring real food to the DeFi space. I know its a longshot but somebody needs to start it. I’ll keep you updated!!

Thanks a lot!!

Yavuz

2 Likes