1

I'm trying to borrow USDC from Aave v2's lending pool through an interface, but I can't manage to do it:

My contract:

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import './interfaces/MyILendingPool.sol';
import './interfaces/MyIERC20.sol';


contract FlashLoaner {

    struct MyCustomData {
        address token;
        uint256 repayAmount;
    }

// **** storage variables from the proxy ****
    address public logicContract;
    address public deployer;
    uint public borrowed;
// ********  
  
    function execute(address _weth, address _contract, uint256 _borrowed) external {

        MyILendingPool lendingPoolAAVE = MyILendingPool(0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9);

        MyIERC20 weth = MyIERC20(_weth);

        address usdc = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
        uint aaveUSDCloan = 1000000 * 10 ** 18;

        weth.approve(address(lendingPoolAAVE), _borrowed);

       lendingPoolAAVE.deposit(_weth, _borrowed, _contract, 0);

       lendingPoolAAVE.borrow(usdc, aaveUSDCloan, 2, 0, _contract);  //---> issue
    }
}

The interface is well connected and I managed to successfully deposit some collateral (6400 ETH) using deposit().

When I query from MyILendingPool using -and commenting out borrow()-:

function getUserAccountData(address user)
    external
    view
    returns (
      uint256 totalCollateralETH,
      uint256 totalDebtETH,
      uint256 availableBorrowsETH,
      uint256 currentLiquidationThreshold,
      uint256 ltv,
      uint256 healthFactor
    );

...I get all the correct values, and a availableBorrowsETH of around 15M USD (converted).

The error that I get is:

Error: VM Exception while processing transaction: reverted with reason string 'Delegate Call failed'

...since this is the logic contract from a proxy where I delegatecall(), but I don't think that the issue is on the proxy, since when I comment out borrow() and just query from the Lending pool, everything runs smoothly.

Thanks for the help!

Just in case, MyILendingPool:

// // SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

interface MyILendingPool {

    function deposit(
    address asset,
    uint256 amount,
    address onBehalfOf,
    uint16 referralCode
  ) external;

  function borrow(
    address asset,
    uint256 amount,
    uint256 interestRateMode,
    uint16 referralCode,
    address onBehalfOf
  ) external;

  function getUserAccountData(address user)
    external
    view
    returns (
      uint256 totalCollateralETH,
      uint256 totalDebtETH,
      uint256 availableBorrowsETH,
      uint256 currentLiquidationThreshold,
      uint256 ltv,
      uint256 healthFactor
    );
}

1 Answer 1

2

The AAVE v2 USDC and USDT tokens use 6 decimals rather than 18. My guess is that you are trying to borrow a trillion million dollars rather than just a million.

This line:

aaveUSDCloan = 1000000 * 10 ** 18;

should probably be:

aaveUSDCloan = 1000000 * 10 ** 6;

Documentation on decimals used by the different atokens here: https://docs.aave.com/developers/deployed-contracts/deployed-contracts

1
  • 1
    Awesome, man! That was it. Thanks a lot!
    – dNyrM
    Jun 29, 2021 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.