2

I'm trying to get a flashloan using web3.py. I'm able to deploy the flashloan contract successfully but when I call the flashloan function it's giving me

execution reverted: SafeERC20: low-level call failed' failed error. I have enough ether in my account.

Do I need to send some ether to my flashloan contract? But I don't think so the error is because of lack of ether to pay the gas fees. (Let me know if this is the case!)

Below is my code for flashloan

pragma solidity ^0.6.6;

import "./aave/FlashLoanReceiverBaseV2.sol";
import "../../interfaces/v2/ILendingPoolAddressesProviderV2.sol";
import "../../interfaces/v2/ILendingPoolV2.sol";

contract FlashloanV2 is FlashLoanReceiverBaseV2, Withdrawable {
    constructor(address _addressProvider) FlashLoanReceiverBaseV2(_addressProvider) public {}

    function executeOperation(
        address[] calldata assets,
        uint256[] calldata amounts,
        uint256[] calldata premiums,
        address initiator,
        bytes calldata params
    )
        external
        override
        returns (bool)
    {

        // Approve the LendingPool contract allowance to *pull* the owed amount
        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;
    }

    function _flashloan(address[] memory assets, uint256[] memory amounts) internal {
        address receiverAddress = address(this);

        address onBehalfOf = address(this);
        bytes memory params = "";
        uint16 referralCode = 0;

        uint256[] memory modes = new uint256[](assets.length);

        // 0 = no debt (flash), 1 = stable, 2 = variable
        for (uint256 i = 0; i < assets.length; i++) {
            modes[i] = 0;
        }

        LENDING_POOL.flashLoan(
            receiverAddress,
            assets,
            amounts,
            modes,
            onBehalfOf,
            params,
            referralCode
        );
    }


    function flashloan(address[] memory assets, uint256[] memory amounts) public onlyOwner {
        _flashloan(assets, amounts);
    }


    function flashloan(address _asset) public onlyOwner {
        bytes memory data = "";
        uint amount = 1 ether;

        address[] memory assets = new address[](1);
        assets[0] = _asset;

        uint256[] memory amounts = new uint256[](1);
        amounts[0] = amount;

        _flashloan(assets, amounts);
   }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Rahulsinh
  • 41
  • 1
  • 6

1 Answers1

5

I think the error you are receiving is because you don't have the funds available to repay the flashloan + premiums at the end of the (flash) transaction.

Your logic doesn't generate any extra profit, and the funds you ask alone are not enought to pay the fee.

ace
  • 313
  • 1
  • 5
  • 19