Truffle

請求的從地址到應付地址的隱式轉換無效

  • October 15, 2021

首先真的很高興找到這個論壇,我開始編寫 Solidity 程式碼。

這是我從本教程中編譯眾籌智能合約 ExampleTokenCrowdsale.sol 的模板時遇到的問題。

截圖:

在此處輸入圖像描述

TypeError: Invalid type for argument in modifier invocation. Invalid implicit conversion from address to address payable requested.
   Crowdsale(_rate, _wallet, _token)

程式碼:

   pragma solidity ^0.5.0;


   import "openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol";
   import "openzeppelin-solidity/contracts/crowdsale/emission/MintedCrowdsale.sol";
   import "openzeppelin-solidity/contracts/crowdsale/validation/CappedCrowdsale.sol";


   contract ExampleTokenCrowdsale is Crowdsale, MintedCrowdsale, CappedCrowdsale{

   //minimum investor Contribution - 20000000000000000000
   //minimum investor Contribution - 50000000000000000000
   uint256 public investorMinCap = 20000000000000000000;
   uint256 public investorHardCap = 50000000000000000000;

   mapping(address => uint256) public contributions;

   constructor(uint256 _rate,
     address _wallet,
     ERC20 _token,
     uint256 _cap)
   Crowdsale(_rate, _wallet, _token)
   CappedCrowdsale(_cap)
   public{
   }


   function _preValidatePurchase(
   address _beneficiary,
   uint256 _weiAmount
    )
   internal view
   {
   super._preValidatePurchase(_beneficiary, _weiAmount);
   uint256 _existingContribution = contributions[_beneficiary];
   uint256 _newContribution = _existingContribution.add(_weiAmount);
   require(_newContribution >= investorMinCap && _newContribution <= investorHardCap);
   contributions[_beneficiary] = _newContribution;
  }

  }

謝謝!

$$ edited $$:

好的,我知道教程程式碼有效而您的程式碼無效的原因是因為在新的solidity 中,CrowdSale 實際上是:

constructor (uint256 rate, address payable wallet, IERC20 token) public {
   require(rate > 0);
   require(wallet != address(0));
   require(address(token) != address(0));

   _rate = rate;
   _wallet = wallet;
   _token = token;
}

但是用於教程的舊開放式齊柏林飛艇是:

constructor (uint256 rate, address wallet, IERC20 token) public {
   require(rate > 0);
   require(wallet != address(0));
   require(address(token) != address(0));

   _rate = rate;
   _wallet = wallet;
   _token = token;
}

如您所見,舊變數的 _wallet 類型是地址,但來自solidity 0.5.0 地址的新變數被拆分為地址和應付地址,因此如果舊範例合約已過期。

您的程式碼中斷的主要原因是因為在solidity 0.5.0 中您無法將地址轉換為像solidity 0.4.24 那樣的應付地址。更多資訊可以在這裡找到:https ://solidity.readthedocs.io/en/v0.5.0/050-break-changes.html#explicitness-requirements

應付地址可以直接轉換為地址,但不允許反之。

所以要解決這個問題簡單address _wallet變成address payable _wallet

在較新版本的solidity 中,您必須在預期的任何地方明確提及應付。

眾籌(_rate,應付(_wallet),_token)

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