Error

錯誤:應付類型地址不能隱式轉換為預期類型契約“IERC20””

  • February 8, 2022

所以我正在測試部署眾籌合約(使用混音)。

首先我部署了代幣合約(我嘗試了多個版本,但這裡有一個來自 openzeppelin 論壇的合約範例):

// contracts/SimpleToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.5;

import "@openzeppelin/contracts@2.5.0/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@2.5.0/token/ERC20/ERC20Detailed.sol";

/**
* @title SimpleToken
* @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
* Note they can later distribute these tokens as they wish using `transfer` and other
* `ERC20` functions.
*/
contract TestCoinForCrowdsale is Context, ERC20, ERC20Detailed {
   /**
    * @dev Constructor that gives _msgSender() all of existing tokens.
    */
   constructor() public ERC20Detailed("CoolToken", "CLTK", 18) {
       _mint(_msgSender(), 110000000000000000000);
   }
}

然後我想部署一個非常簡單的眾籌合約:

pragma solidity ^0.5.17;

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

contract HGTokenCrowdsale is Crowdsale {
   constructor (
   uint256 rate,
   address payable wallet,
   IERC20 token
   
       /**
        * @dev The rate is the conversion between wei and the smallest and indivisible
    * token unit. So, if you are using a rate of 1 with a ERC20Detailed token
    * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
    */
   ) public Crowdsale(rate=100, wallet=0x6b0962bc4dFf11B6f96934d777EA4b2088dDD33c, token=0x7e77c3277239F7D1d557cA98C295bDb63cC26ee8) {}
}

但是當我想為第二個合約編譯程式碼時,我得到了這個錯誤:

contracts/GratitudeCoinTCrowdsale.sol:17:91: TypeError: Type address payable is not implicitly convertible to expected type contract IERC20. ) public Crowdsale(rate=100, wallet=0x6b0962bc4dFf11B6f96934d777EA4b2088dDD33c, token=0x7e77c3277239F7D1d557cA98C295bDb63cC26ee8) {}

^----------------------------------------^

在使用 VM 以及在 Rinkeby 測試網路上嘗試編譯時會出現此錯誤我為令牌變數指定的合約地址是 Rinkeby 網路上的工作地址。任何人都可以幫助找出解決此錯誤的方法嗎?

謝謝!

您遇到此錯誤是因為您嘗試將應付地址分配給 IERC20 參數。這種隱式轉換不會由solidity本身完成,您必須顯式執行。這與您正在處理的網路無關。

這是問題:

Crowdsale(rate=100, wallet=0x6b0962bc4dFf11B6f96934d777EA4b2088dDD33c, token=0x7e77c3277239F7D1d557cA98C295bDb63cC26ee8)

由於CrowdSale期望令牌參數是 IERC20 類型。

嘗試顯式類型轉換:

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.5;

import "@openzeppelin/contracts@2.5.0/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@2.5.0/token/ERC20/ERC20Detailed.sol";
import "@openzeppelin/contracts@2.5.0/crowdsale/Crowdsale.sol";

contract HGTokenCrowdsale is Crowdsale {
   constructor (
   uint256 rate,
   address payable wallet,
   IERC20 token
   
       /**
        * @dev The rate is the conversion between wei and the smallest and indivisible
    * token unit. So, if you are using a rate of 1 with a ERC20Detailed token
    * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
    */
   ) public Crowdsale(rate=100, wallet=0x6b0962bc4dFf11B6f96934d777EA4b2088dDD33c, token=IERC20(0x7e77c3277239F7D1d557cA98C295bDb63cC26ee8)) {}
}

/**
* @title SimpleToken
* @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
* Note they can later distribute these tokens as they wish using `transfer` and other
* `ERC20` functions.
*/
contract TestCoinForCrowdsale is Context, ERC20, ERC20Detailed {
   /**
    * @dev Constructor that gives _msgSender() all of existing tokens.
    */
   constructor() public ERC20Detailed("CoolToken", "CLTK", 18) {
       _mint(_msgSender(), 110000000000000000000);
   }
}

引用自:https://ethereum.stackexchange.com/questions/113137