Issues running Kovan testnet Aave flash loans from Remix

I keep running into a persistent problem after I’ve deployed a test flash loan contract on the Kovan testnet and attempt to run a flash loan on it. I stripped everything down to the bare-bones basic functions of a flash loan, which involves calling the flash loan and paying it back. So, my contract has only two functions, one for initiating the flash loan, and the executeOperation function, both of which conform verbatim to those provided in the Aave docs. I do send test coins to my contract on Kovan to pay the premium of the flash loan as well, so that’s not an issue. However, when I try to call the flash loan function Remix throws a gas estimation error, and says it reverted due to an exception. The transaction fails if I force it.

I have no idea what to do. Google is proving very unhelpful, so I’m taking this issue to a forum. I don’t want to move on with courses until I can get flash loans running. This is the reason I took DeFi 201, and it’s not working! It doesn’t make any sense why it won’t work either. I’ve managed to narrow down that it fails when it calls Aave’s flashLoan function. I can comment out this block of code and it works fine, and I can call the executeOperation function and it works fine as well. Only when the LENDING_POOL.flashLoan() function is called does it throw this error.

Here’s my code:

// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.12;

import "./FlashLoanReceiverBase.sol";
import "./ILendingPool.sol";
import "./ILendingPoolAddressesProvider.sol";
import "./IERC20.sol";
import "./SafeMath.sol";
import "./IUniswap.sol";
import "./Ownable.sol";

//Aave ILendingPoolAddressesProvider provider (Kovan): 0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5

//Dai contract: 0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD

contract BasicFlashLoan is FlashLoanReceiverBase, Ownable {
    using SafeMath for uint256;
    
    ILendingPool public lendingPool;
  
    constructor(ILendingPoolAddressesProvider provider) FlashLoanReceiverBase(provider) public {
        lendingPool = ILendingPool(provider.getLendingPool());
    }

    address tokenAddress;

    function runFlashLoan(address asset, uint256 amount) public {
        tokenAddress = asset;
        address receiverAddress = address(this);
        
        address[] memory assets = new address[](1);
        assets[0] = asset;
        
        uint[] memory amounts = new uint[](1);
        amounts[0] = amount;
        
        uint[] memory modes = new uint[](1);
        modes[0] = 0;
        
        address onBehalfOf = address(this);
        
        bytes memory params = "";
        uint16 referralCode = 0;
        
        lendingPool.flashLoan(
            receiverAddress,
            assets,
            amounts,
            modes,
            onBehalfOf,
            params,
            referralCode
        );
    }

    function executeOperation(
        address[] calldata assets,
        uint256[] calldata amounts,
        uint256[] calldata premiums,
        address initiator,
        bytes calldata params
    )
        external
        override
        returns (bool)
    {
        for (uint i = 0; i < assets.length; i++) {
            uint amountOwing = amounts[i].add(premiums[i]);
            IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
        }
        return true;
    }
}

Here’s the error code that pops up in Remix: “Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? The execution failed due to an exception. Reverted.”

When I call runFlashLoan it throws the error, when I force the transaction it fails. I can’t get any useful information after that. From what I’m seeing, there are no reason codes provided, just a failed transaction receipt.

What is going on?

Hey @CryptoPhoenix, hope you are ok.

Could you please specify on which lesson exactly did you got the error? i want try to replicate your issue by myself and check the lesson also (from past coding experiences, i face that error in truffle some time ago and i do remember that my error was a syntax error)

Carlos Z

It started with “Program an Arbitrage Flash Loan Part 2”. The code they used in that lesson threw the error I’m having along with a lot of syntax errors that took some time to work out (did they not have auto-compile running when they recorded that lesson?). I wanted to eliminate all possible sources of failure so I could figure out if it’s their code or the flash loan functions themselves, so I scrapped the entire code and stripped it down to the absolute basic code provided in the Aave Flash Loan docs (provided in my post here). However, it’s still throwing the same error with the basic code.

This lesson is using Remix to connect to Kovan, as the course does not cover how to do this through Truffle. Since I copy-pasted the code directly from Aave’s developer docs (as well as double-checking it against numerous examples that are provided along with YouTube videos), I’m fairly confident there are no syntax errors in my code. Maybe this is a Remix problem? They didn’t cover how to use Truffle to connect to Kovan, are there any courses here that cover that, or do I need to learn it myself?

The exact block of code that throws the error is the call to flashLoan(). If I comment out this part of the function then the transaction passes. I can also manually run executeOperation() and it passes as well. It’s just the call to flashLoan() that fails the transaction.

Maybe there’s something wrong with the provider address I’m passing the constructor when I deploy it? I’m using the ILendingPoolAddressesProvider address for Kovan taken straight from the Aave developer docs (0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5). I feel like this could be a possible source of the problem as well.

I appreciate your insights, I just want to walk away from this course knowing how to run flash loans. That was why I took it. The only hurdle I need to get over is getting them to run at all, from there I will expand the code further and make it useful.

I am sorry man, but im digging into the video lesson and aave’s developers doc and I cant find the contract that you provide in your first topic. Maybe you can provide the link on where did you get that contract :face_with_monocle:

https://docs.aave.com/developers/guides/flash-loans#overview

Carlos Z.

The code I posted here came from modifying the Aave docs code so I could make it more flexible. I’ve already tried using the exact code in the developer docs and it has the same error. The code from the lesson is found here:

https://github.com/Robsonsjre/FlashloanUsecases/blob/master/contracts/arbitrage/Arbitrageur.sol

It also throws the same error. It’s the “gas estimation failed” pop-up that reads “Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? The execution failed due to an exception. Reverted.”

If I send the transaction it fails. This is the same error whether I’m using the code from the lesson, the code from the Aave developer docs, my own code that I posted here, or any example code I’ve come across. I’ve already tried resetting my Metamask, and I’m always remembering to send DAI to the contract after deploying it. I’ve also adjusted the gas sent, and on one of my contracts it actually stopped throwing the gas estimation error, but the transaction itself still failed. It’s the same error every time no matter which contract I’m using, so I doubt it’s a syntax error.

I also just tried creating a simple “hello world” contract that sets and returns a string on the Kovan testnet, and that deploys and runs just fine, so it’s not purely a Metamask issue. I think it’s something to do with the flashLoan() function itself.

I hope that helps.

Probably is not a problem from your side (remix or metamask), from my experience, when that message error shows in metamask, its more likely to be an error some where in the smart contract. (a missing parameter, bad syntax or typo error).

I will try run it my self and get back to you.

Carlos Z

So, the contract works as expected, the only inconvenience here is probably on the lending pool, might be that market prices are not in condition to profit.
https://github.com/Robsonsjre/FlashloanUsecases/issues/22

I tried my self and I think the error that is showed by remix comes from the flashloan will not make any profit (so it trigger the alert )
https://kovan.etherscan.io/tx/0xca0ce4420f41eaef455c3775d0aee604ce75cd5bb940c59b102700d086a281a4

The executeOperation() contains require(daiBought > totalDebt, "Did not profit"); which should calculate if the operation is profitable or not, and for the actual moment conditions, could be the case.

This is my contract address: https://kovan.etherscan.io/address/0xEAb13AB4E36565EEC6436d2Aa3Ff4F5661E85eAB

I also tried deposit eth, dai and bat to my contract so it could have funds to play around.

Carlos Z

I figured it out. The problem I was having was I was using Aave V1’s LendingPoolAddressesProvider address instead of Aave V2 for my contracts. It probably would have worked with the lesson’s contract if there were arbitrage opportunities available, since it uses V1. I figured there must have been something wrong with the address, and I checked it a few times to make sure I had the right one, but somehow I must have checked the wrong address each time lol. Well, thanks for your time, I appreciate it.

1 Like

Can you share the public hash to see the final result please?

Program Basics Flash Loan . Let’s start with the basics the goal is for you to initiate a Flash Loan Transnaction from the AAVE Kovan Test pool.

walgreenslistens

Interesting to see there’s absolutely zero commenting or support here from the creators of this course. What are we paying for again? Outdate material, no support. Hmmm, am I still within my 14 days?