Skip to main content

Issue implementing and testing aave V3 flash loan

I'm trying to implement a flash loan using aave-v3 flash loan contracts. and when testing using hardhat i get this error:

Error:

Transaction reverted: function returned an unexpected amount of data
    at FlashLoan.constructor (contracts/utils/FlashLoanSimpleReceiverBase.sol:22)
    at EthModule._estimateGasAction (flashLoan/node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:425:7)
    at HardhatNetworkProvider.request (flashLoan/node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:118:18)
    at EthersProviderWrapper.send (flashLoan/node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)

& this is my implementation

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;

import "./utils/IERC20.sol";
import "./utils/FlashLoanSimpleReceiverBase.sol";

contract FlashLoan is FlashLoanSimpleReceiverBase {
address payable owner;
address assetAddress;

error NotOwner();
error NotEnoughFunds();

constructor (address _poolAddress) FlashLoanSimpleReceiverBase(IPoolAddressesProvider(_poolAddress)) {
    owner = payable(msg.sender);
}

function executeOperation(
    address asset,
    uint256 amount,
    uint256 premium,
    address initiator,
    bytes calldata params
) external returns (bool) {
    IERC20 token = IERC20(asset);
    uint256 debt = amount + premium;
    if (token.balanceOf(address(this)) < debt) revert NotEnoughFunds();
    token.approve(address(POOL), debt);
    return true;
}

function flashloan(address _assetAddress, uint256 amount) external {
    if (msg.sender != owner) revert NotOwner();
    assetAddress = _assetAddress;
    address recieverAddress = address(this);
    bytes memory params = '';
    uint16 referralCode = 0;

    POOL.flashLoanSimple(recieverAddress, assetAddress, amount, params, referralCode);
}

function withdraw() external {
    if (msg.sender != owner) revert NotOwner();
    IERC20 token = IERC20(assetAddress);
    token.transfer(owner, token.balanceOf(address(this)));
}
}