I'm using hardhat to doing AAVE flash loan, when I borrow one token, is successful, but borrow > 1 tokens, always error.
* deploy on eth mainnet-fork
flash-loans-test.js
const { ethers } = require("hardhat");
const hre = require("hardhat");
describe("AaveFlashLoans", function () {
it("Excute flashloans", async function () {
const contractAddress = "0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5";
const flashLoansFactory = await ethers.getContractFactory("AaveFlashLoans");
const flashLoans = await flashLoansFactory.deploy(contractAddress);
await flashLoans.deployed();
const token = await ethers.getContractAt("IERC20", "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"); //weth
const BALANCE_AMOUNT = ethers.utils.parseEther("2");
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: ["0xf07704777d6bc182bf2c67fbda48913169b84983"],
});
const signer = await ethers.getSigner("0xf07704777d6bc182bf2c67fbda48913169b84983");
await token
.connect(signer)
.transfer(flashLoans.address, BALANCE_AMOUNT);
await flashLoans.myFlashLoanCall();
const balance = await token.balanceOf(flashLoans.address)
console.log(balance);
});
});
AaveFlashLoans.sol
pragma solidity ^0.8.0;
import {SafeMath} from "@openzeppelin/contracts/utils/math/SafeMath.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {FlashLoanReceiverBase} from "./FlashLoanReceiverBase.sol";
import {ILendingPoolAddressesProvider} from "./ILendingPoolAddressesProvider.sol";
import "hardhat/console.sol";
contract AaveFlashLoans is FlashLoanReceiverBase {
using SafeMath for uint256;
constructor(ILendingPoolAddressesProvider _addressProvider)
FlashLoanReceiverBase(_addressProvider)
{}
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address,
bytes calldata
) external override returns (bool) {
for (uint256 i = 0; i < assets.length; i++) {
uint256 amountOwing = amounts[i].add(premiums[i]);
IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
console.log(amounts[i]);
console.log(premiums[i]);
}
return true;
}
function myFlashLoanCall() public {
address receiverAddress = address(this);
address[] memory assets = new address[](1);
assets[0] = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); // weth
uint256[] memory amounts = new uint256[](1);
amounts[0] = 1 ether;
uint256[] memory modes = new uint256[](1);
modes[0] = 0;
address onBehalfOf = address(this);
bytes memory params = "";
uint16 referralCode = 0;
LENDING_POOL.flashLoan(
receiverAddress,
assets,
amounts,
modes,
onBehalfOf,
params,
referralCode
);
}
}
above code is successful successful image1 successful image2
but when I add 2 tokens, it will error
AaveFlashLoans.sol
pragma solidity ^0.8.0;
import {SafeMath} from "@openzeppelin/contracts/utils/math/SafeMath.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {FlashLoanReceiverBase} from "./FlashLoanReceiverBase.sol";
import {ILendingPoolAddressesProvider} from "./ILendingPoolAddressesProvider.sol";
import "hardhat/console.sol";
contract AaveFlashLoans is FlashLoanReceiverBase {
using SafeMath for uint256;
constructor(ILendingPoolAddressesProvider _addressProvider)
FlashLoanReceiverBase(_addressProvider)
{}
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address,
bytes calldata
) external override returns (bool) {
for (uint256 i = 0; i < assets.length; i++) {
uint256 amountOwing = amounts[i].add(premiums[i]);
IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
console.log(amounts[i]);
console.log(premiums[i]);
}
return true;
}
function myFlashLoanCall() public {
address receiverAddress = address(this);
address[] memory assets = new address[](2);
assets[0] = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); // weth
assets[1] = address(0xdAC17F958D2ee523a2206206994597C13D831ec7); // usdt
uint256[] memory amounts = new uint256[](2);
amounts[0] = 1 ether;
amounts[1] = 1 ether;
uint256[] memory modes = new uint256[](2);
modes[0] = 0;
modes[1] = 0;
address onBehalfOf = address(this);
bytes memory params = "";
uint16 referralCode = 0;
LENDING_POOL.flashLoan(
receiverAddress,
assets,
amounts,
modes,
onBehalfOf,
params,
referralCode
);
}
}
above code is error error image
Please help, thanks a lot lot lot lot...
Since the USDT token has 6 decimals, by inputting the amount as 1 ether, this corresponds to the amount: 1000000000000 USDT. There is not enough availableLiquidity in the pool to borrow this amount, so this is why the tx is reverting. If you reduce the amount, then it should succeed.