Search code examples
soliditysmartcontracts

TypeError: Invalid type for argument in modifier invocation


Issue Summary:

I encountered a TypeError in the code while trying to invoke a modifier in the flashloan.sol contract. The error message suggests an invalid type for the argument in the modifier invocation, specifically an invalid implicit conversion from contract IPoolAddressesProvider to contract IPoolAddressesProvider is being requested.

Steps to Reproduce:

  • Deploy the contract with the given code.
  • Attempt to invoke the modifier in the flashloan.sol contract.
  • Observe the TypeError mentioned in the error message.

Expected Behavior:

The code should compile without any errors, and the modifier should be invoked successfully without type conversion issues.

Actual Behavior:

Encountered a TypeError with the following details:

TypeError: Invalid type for argument in modifier invocation. Invalid implicit conversion from contract IPoolAddressesProvider to contract IPoolAddressesProvider requested.
  --> contracts/flashloan.sol:13:35:
   |
13 |     ) FlashLoanSimpleReceiverBase(IPoolAddressesProvider(_addressProvider)) {
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Environment:

  • Solidity Compiler version: "0.8.10"
  • Operating system: Windows x64

I removed the implicit type conversion from contract IPoolAddressesProvider to contract IPoolAddressesProvider

FlashLoanSimpleReceiverBase(_addressProvider)

also tried fixing the code with Remix Ide also, after spending times fixing it on local using Hardhat but no results unfortunately, please let me know how it can be fixed thanks!

FlashLoan.sol

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

import {FlashLoanSimpleReceiverBase} from "https://github.com/aave/aave-v3-core/contracts/flashloan/base/FlashLoanSimpleReceiverBase.sol";
import {IPoolAddressesProvider} from "@aave/core-v3/contracts/interfaces/IPoolAddressesProvider.sol";
import {IERC20} from "@aave/core-v3/contracts/dependencies/openzeppelin/contracts/IERC20.sol";

abstract contract FlashLoan is FlashLoanSimpleReceiverBase {
    address payable owner;

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

    function executeOperation(
        address[] calldata assets,
        uint256[] calldata amounts,
        uint256[] calldata premiums,
        address initiator,
        bytes calldata params
    ) external returns (bool){
        // We have the borrowed funds
        // custom logic to mock arbitrage
        uint256 amountOwed = amounts + premiums;
        IERC20(assets).approve(address(POOL),amountOwed);

    return true;
    }

    function requestFlashLoan(address _token, uint256 _amount) public {
        address receiverAddress = address(this);
        address asset = _token;
        uint256 amount = _amount;
        bytes memory params = ""; //empty
        uint16 referralCode = 0;

        POOL.flashLoanSimple(
            receiverAddress,
            asset,
            amount,
            params,
            referralCode
        );
    }

    function getBalance(address _tokenAddress) external view returns(uint256) {
        return IERC20(_tokenAddress).balanceOf(address(this));
    }

    function withdraw(address _tokenAddress) external onlyOwner{
        IERC20 token = IERC20(_tokenAddress);
        token.transfer(msg.sender, token.balanceOf(address(this)));
    }

    modifier onlyOwner() {
        require(msg.sender == owner, 
        "Only contract owner can call this function");
        _;
    }

    receive() external payable {}


}

Solution

  • The issue is that IPoolAddressesProvider(_addressProvider) is not of type of address, it should be _addressProvider instead to be passed as the correct type.

    This template demonstrates the format of imports, constructor, and function calls.