Lets hack Flash Loans | Flash loan assignment/discussion

Are you copying the address of the Smart Contract that Remix gives you?

1 Like

Hi, yes I’m copying the contract address. But I was under the impression that the flash loan fee is repayed by the dai that is available in the created contract (that I must transfer to it to make it available )? Hmmm after the transaction is reverted I don’t get any of the require "did not profit " msg , should I emit an event to check if the transaction is reverted for that reason or how else should I check if I am in profit ? Tnx

@tinyHamster the fee is paid with the Profit obtained from the operations. In the flashloan example, Dai was transferred to the Smart Contract because only the Loan was taken and it was paid without any other operation. Can you attach the Tx link?

1 Like

Hi, aha ok… So I checked the status of the failed transaction and it is ā€œFail with error 'Action requires an active reserveā€ when the transaction is reverted, this happens with with arbitrage or flashLoan contract…
I`m thinking that that is no DAI in the LendingPoolAddressesProvider (ā€œ0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5ā€), but after checking, the lending pool has DAI liquidity :face_with_raised_eyebrow:
txhash: 0x086e483f838443d8060254f5a99f93edfe9e0cf6e95278480d7df9aaca73d7ef

Thx

2 Likes

In arbitration are you exchanging Dai with which token? Because the address of the lending pool is correct

1 Like

Well, I had the wrong address for the DAI token, now the ā€œactive reserveā€ error is gone but I get the "did not profit " error when transaction is reverted. Is the order of the executeOperation function correct :

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 bat = ERC20(BAT_ADDRESS);

        // Buying ETH at Exchange A
        require(
            dai.approve(address(exchangeA), _amount),
            "Could not approve DAI sell"
        );

        uint256 tokenBought = exchangeA.tokenToTokenSwapInput( 
            _amount, // buy BAT TOKENS
            1,
            1,
            deadline,
            BAT_ADDRESS
        );

        require(
            bat.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);
    }

I`ve read in some posts that the order in the github code is incorrect and we must switch the pairs so that the trade is profitable ?

Tnx

2 Likes

@tinyHamster It gives you the error because when making the arbitrage you don’t get profits, remember that at the end of the swap the amount must allow you to pay the flashloan and the fees, otherwise the loan is reverted.
Cheking the swap you want to make from DAI to BAT and then BAT to DAI doesn’t give you any profit.
Try swapping DAI with MKR or rebalancing the uniswap pools for profits.

1 Like

@tinyHamster Or change the order of the exchanges, for example, if you create a arbitrage from Uniswap 2 to Uniswap 1 from DAI to MKR and then MKR to DAI you should be in profit.

1 Like

Yes, thank you, after switching the uniswap exchanges now the DAI-BAT or DAI-MKR are making a profit and the transaction goes trough.
I ve been reading a lot of negative comments about this course here :roll_eyes: ,but for myself I really don t mind these problems that I m encountering, because it forces me to really understand the code and really start to think of what Im actually doing in depth…didn`t enroll in this course just to make a profit with flash loans, I really want to learn the power of smart contracts. Thanks again for assistance :wink:

Tnx

3 Likes

Great!!! @tinyHamster it’s exciting to see how the first flashloan runs successfully.

Exactly those errors or problems make one analyze in depth and understand more the flow of the flashloans, in my case I included a third swap to the code and it isn“t working for me but I keep analyzing it to see what is failing and to be able to execute it.

DeFi is a whole world and flashloan is just the beginning of the future, we may see more tools.

I recommend you the smartcontract programming courses are very good and help you even more to understand this code, or to try to do new operations.

1 Like

I already managed to solve the problem I had with the code to generate a flashloan and arbitrage with 3 tokens. I attach the link to the Tx with the errors of ā€œOut of gasā€ and ā€œDid not profitā€ for them to see and so if they appear they can identify them.

With the ā€œOut of gasā€ error what they should do is increase the gas limit, and the ā€œDid not profitā€ error is because the result of the swap does not allow them to pay the loan and the fees for which it activates the revert.

What I learned from this problem I had from the code was:
1) Be patient, many times you have to calm your mind to see the error, which in my case was with a letter.
2) Review in advance the swap that I am going to carry out in the different Uniswap to verify if we are in the correct order of them, and if they would end in profit.

I also share the code in case you want to try it:

The result of the arbitrage

3 Likes

Amazing to see this :slight_smile:
Thank you all for helping out @Thaddeus19

3 Likes

Looking for a job :wink:
Nice work man.

2 Likes

@amadeobrands great to be able to help, and now to try to flash it with compound :+1: :+1:

1 Like

Hey, hehe was just trying to do the same damn thing, but with dai-bat-mkr, after a day of struggling with the code logic, came up here to get some help, and saw that you tried the same thing and got my code to work :slight_smile: Now I really have a good understanding of the code I must say :slight_smile: Thx again :wink:
Now I will experiment with adding and removing liquidity and see how that effects the end profit … Hmm with compound, u mean on the mainnet ? Was thinking of trying some small real world arbitrages myself :confused:

1 Like

I’m glad I helped you :+1: :+1:, I want to try the same compound on Kovan Testnet and practice more before going to the mainnet.

1 Like

so I simply bought a few BAL tokens with my Ethereum sized brain, lol, and on coingecko is appears as though there is an arbitrage opportunity here…

so the brain is growing…BUT, I bought from HotBit DeX and noticed that the ā€˜withdraw’ button is greyed out. So, I am thinking that this is basically limited functionality doe to using the Brave browser so simply solution, just switch back to Chrome…nope, still greyed out.

good luck getting an answer from an authority at HotBit, I don’t expect to get an answer so I thought I would withdraw some of my ETH on account…no greyed out button there…but no dice, 24 hour hold on ETH!

So I think that HotBit likely throw a wrench in arbitrage by freezing your holdings…I’ll get at them but a 24 hour delay which increases risk in arbitrage trading huh? HotBit stunts neural brain development is the conclusion.

3 Likes

I recommend you take the Ethereum coding course first. I checked your code and you have a mistake in line 22. I inserted contract Arbitrageur is FlashLoanReceiverBase(0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5) {
and updated the addresses and it worked fine.

1 Like

@moon_Time If you like you can try the code that I shared here where the Arbitrage is generated with 3 tokens

1 Like

Hello, just complete the Arbitrage flash loan sample but I’m confused: I ran this code https://github.com/Robsonsjre/FlashloanUsecases/blob/master/contracts/arbitrage/Arbitrageur.sol

Executed as it is (had to send some DAI to the contract) and got his results


with a profit of 183.37 DAI (a little high, but is the kovan network, so is fake money).

Then swap Uniswap A and B and got:


and got a profit of 279.94 DAI! It was suppose to activate the ā€œno profit failā€ can someone explain me what happen? (for this one I had to send some BAT to the contract.)
1 Like