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?