I'm trying to get a flashloan from Aave using Ganache fork and truffle with javascript web.js and when I request the loan through the flashloan(...) function it shows this error:
'SafeERC20: low-level call failed',
Curious that outside the fork it works well and right, alias I don't know if I'm doing the fork correctly, if you have the knowledge and can help me I'd appreciate it immensely.
Ganache --->
ganache --fork "https://polygon-mainnet.g.alchemy.com/v2/..." -p 8545 --gasLimit 0x2DC6C0 --account_keys_path mypath -u 0xd05e3E715d945B59290df0ae8eF85c1BdB684744
The address 0xd05e3E715d945B59290df0ae8eF85c1BdB684744 is equivalent to LendingPoolAddressesProvider
https://docs.aave.com/developers/v/2.0/deployed-contracts/matic-polygon-market
Truffle --->
truffle deploy --network polygon
Deploy contracts correctly
Source Contract Solidity
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,
modest,
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);
}
}