Invalid implicit conversion from address to contract IERC20 requested

Hello,

I have created a Crowdsale Deployer contract as per openzeppelin docs. But at compilation in remix, it throws me this error.

TypeError: Invalid type for argument in function call. Invalid implicit conversion from address to contract IERC20 requested.
address(token)

contract MyCrowdsaleDeployer {
constructor (uint256 initialSupply, string memory tokenName, string memory symbol, uint8 decimal, address payable ownerAddress)
public payable
{
ERC20Mintable token = new Ico(initialSupply, tokenName, symbol, decimal, ownerAddress);
Crowdsale crowdsale = new MyCrowdsale(
1,
ownerAddress,
address(token) // Error line tried removing address but still no success
);
token.addMinter(address(crowdsale));
token.renounceMinter();
}
}

1 Like

Hi @hardsuccess,

The example is from the following documentation: https://docs.openzeppelin.com/contracts/2.x/crowdsales#minted-crowdsale

The compiler is complaining about the conversion from address to IERC20, so instead of address(token) we can just leave as token which is IERC20.

I have created a Pull Request to update the documentation: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2026

Please note:
Appropriate (and thorough) testing and auditing would be required with any solution.
You may also need to seek appropriate advice on regulatory considerations.

The following example is what I used in Remix:

MyToken.sol

pragma solidity ^0.5.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/ERC20Detailed.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/ERC20Mintable.sol";

contract MyToken is ERC20, ERC20Detailed, ERC20Mintable {
    constructor (
        string memory name, 
        string memory symbol, 
        uint8 decimals
    ) 
        ERC20Detailed(name, symbol, decimals)
        public
    {
        
    }
}

MyCrowdsale.sol

pragma solidity ^0.5.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/crowdsale/Crowdsale.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/crowdsale/emission/MintedCrowdsale.sol";

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/IERC20.sol";

contract MyCrowdsale is Crowdsale, MintedCrowdsale {
    constructor(
        uint256 rate,    // rate in base units
        address payable wallet,
        IERC20 token
    )
        MintedCrowdsale()
        Crowdsale(rate, wallet, token)
        public
    {

    }
}

MyCrowdsaleDeployer.sol

pragma solidity ^0.5.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/crowdsale/Crowdsale.sol";

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/ERC20Mintable.sol";

import "./MyToken.sol";
import "./MyCrowdsale.sol";

contract MyCrowdsaleDeployer {
    constructor()
        public
    {
        // create a mintable token
        ERC20Mintable token = new MyToken("My Token", "TKN", 18);

        // create the crowdsale and tell it about the token
        Crowdsale crowdsale = new MyCrowdsale(
            1,               // rate, still in TKNbits
            msg.sender,      // send Ether to the deployer
            token            // the token
        );
        // transfer the minter role from this contract (the default)
        // to the crowdsale, so it can mint tokens
        token.addMinter(address(crowdsale));
        token.renounceMinter();
    }
}

Hi @hardsuccess,

Checking to see if the above answered your question. If so, please mark it as the solution, otherwise please ask additional questions.