Solidity

未能從 Aave 的貸款池中藉款

  • June 29, 2021

我正在嘗試通過一個界面從 Aave v2 的借貸池中藉用 USDC,但我無法做到:

我的契約:

//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
   }
}

界面連接良好,我成功地使用deposit().

當我查詢MyILendingPool使用 - 並註釋掉borrow()- 時:

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

…我得到了所有正確的值,以及availableBorrowsETH大約 1500 萬美元(已轉換)。

我得到的錯誤是:

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

…因為這是來自代理的邏輯契約delegatecall(),但我認為問題不在代理上,因為當我註釋掉borrow()並從貸款池中查詢時,一切執行順利。

謝謝您的幫助!

以防萬一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
   );
}

AAVE v2 USDC 和 USDT 代幣使用 6 位小數,而不是 18 位。我的猜測是你試圖借入一萬億美元而不是一百萬美元。

這一行:

aaveUSDCloan = 1000000 * 10 ** 18;

應該是:

aaveUSDCloan = 1000000 * 10 ** 6;

此處不同代幣使用的小數文件: https ://docs.aave.com/developers/deployed-contracts/deployed-contracts

引用自:https://ethereum.stackexchange.com/questions/102667