Solidity

如何與已部署的令牌進行通信?

  • January 28, 2022

使用 Truffle,我正在嘗試部署以下合約:

pragma solidity ^0.4.13;

contract ERC20Events {
   event Approval(address indexed src, address indexed guy, uint wad);
   event Transfer(address indexed src, address indexed dst, uint wad);
}

contract ERC20 is ERC20Events {
   function totalSupply() public view returns (uint);
   function balanceOf(address guy) public view returns (uint);
   function allowance(address src, address guy) public view returns (uint);

   function approve(address guy, uint wad) public returns (bool);
   function transfer(address dst, uint wad) public returns (bool);
   function transferFrom(
       address src, address dst, uint wad
   ) public returns (bool);
}

contract TimedChest {

   /** The address allowed to do withdraws */
   address public withdrawer;

   /** Owner / creator of the contract */
   address public owner;

   /** Address of the token we're time-locking */
   ERC20 public token;
   // CryptoHuntToken public token;
   // StandardToken public token;

   /** Times at which specific amount becomes available */
   uint[] public releaseTimes;

   /** List of amounts which can be withdrawn after a specific timestamp */
   uint[] public amounts;

   modifier onlyAfter(uint _time) {
       require(now >= _time);
       _;
   }

   modifier onlyBy(address _account) {
       require(msg.sender == _account);
       _;
   }

   function changeOwner(address _newOwner) public onlyBy(owner) {
       owner = _newOwner;
   }

   function TimedChest(uint[] _releaseDelays, uint[] _amounts, address _withdrawer, address _tokenAddress) public {
       owner = msg.sender;

       require (address(_tokenAddress) != 0x0);
       require (address(_withdrawer) != 0x0);
       require (_releaseDelays.length == _amounts.length && _releaseDelays.length > 0);

       for (uint8 i = 0; i < _releaseDelays.length; i++) {
           require(_releaseDelays[i] > now);
           require(_amounts[i] > 0);
           if (i == 0) {
               releaseTimes[i] = now + _releaseDelays[i];
           } else {
               releaseTimes[i] = releaseTimes[i-1] + _releaseDelays[i];
           }
       }

       releaseTimes = _releaseDelays;
       amounts = _amounts;
       withdrawer = _withdrawer;

       token = ERC20(_tokenAddress);
       // token = CryptoHuntToken(_tokenAddress);
       // token = StandardToken(_tokenAddress);
   }

   function withdraw() onlyBy(withdrawer) external {
       uint256 amount = token.balanceOf(this);
       require (amount > 0);

       for (uint8 i = 0; i < releaseTimes.length; i++) {
           if (releaseTimes[i] < now && amounts[i] > 0) {
               token.transfer(withdrawer, amounts[i]);
           }
       }
   }

   function withdrawAll() onlyBy(owner) external {
       uint256 amount = token.balanceOf(this);
       require (amount > 0);

       token.transfer(owner, amount);
   }

}

這應該允許在部署時指定發布延遲和數量的初始列表,以及將任何 ERC20 代幣發送到該合約和提款人。提款人可以在特定時間過去後呼叫提款,並在該時間範圍內提取分配的金額。這就像任何預部署代幣的定期歸屬。

我收到錯誤:

Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: The contract code couldn't be stored, please check your gas amount.

經過廣泛搜尋,我測試了許多理論 - 部署抽象合約是一個問題(為了測試這一點,我將所有 ERC20 介面方法更改為存根,替換;{},氣體限制是問題,松露版本是問題(試過降級)無濟於事。

可能是什麼問題,或者我在哪裡可以找到更具體的錯誤消息?


編輯:

看起來建構子不是很喜歡循環。將循環移出那裡使其工作。我正在進一步調查以查明這個特定錯誤的根源並找出它發生的原因。

當契約中有錯誤時,這是一種一般錯誤。當我從父級獲得一個未實現的抽象函式時出現錯誤

也許這個問題會有所啟發:https ://github.com/trufflesuite/truffle/issues/476

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