Solidity

部署智能合約時出錯 - 此合約未實現所有功能,因此無法創建

  • December 29, 2018

我已經編寫並編寫了一份用 Solidity 編寫的智能合約,用於代幣和眾籌。編譯成功,在 Remix IDE 中沒有錯誤。這是我的智能合約

pragma solidity ^0.4.25;


library SafeMath {

   /**
   * @dev Multiplies two numbers, reverts on overflow.
   */
   function mul(uint256 a, uint256 b) internal pure returns (uint256) {
   // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
   // benefit is lost if 'b' is also tested.
   // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
       if (a == 0) {
           return 0;
       }

       uint256 c = a * b;
       require(c / a == b);
       return c;
   }

   /**
   * @dev Integer division of two numbers truncating the quotient, reverts on division by zero.
   */
   function div(uint256 a, uint256 b) internal pure returns (uint256) {

       require(b > 0); // Solidity only automatically asserts when dividing by 0
       uint256 c = a / b;
       // assert(a == b * c + a % b); // There is no case in which this doesn't hold

       return c;
   }

   /**
   * @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend).
   */
   function sub(uint256 a, uint256 b) internal pure returns (uint256) {

       require(b <= a);
       uint256 c = a - b;

       return c;
   }

   /**
   * @dev Adds two numbers, reverts on overflow.
   */
   function add(uint256 a, uint256 b) internal pure returns (uint256) {

       uint256 c = a + b;
       require(c >= a);

       return c;
   }

   /**
   * @dev Divides two numbers and returns the remainder (unsigned integer modulo),
   * reverts when dividing by zero.
   */
   function mod(uint256 a, uint256 b) internal pure returns (uint256) {

       require(b != 0);
       return a % b;
   }
}

interface ERC20 {

   function balanceOf(address who) external view returns (uint256);
   function transferToken(address to, uint256 value) external returns (bool);
   // function allowance(address owner, address spender) external view returns (uint256);
   // function transferFrom(address from, address to, uint256 value) external returns (bool);
   // function approve(address spender, uint256 value) external returns (bool);
   event Transfer(address indexed from, address indexed to, uint256 value);
   event TransferEther(address indexed who,uint256 value);
   // event Approval(address indexed owner, address indexed spender, uint256 value);

}


contract Owned {

   address internal owner;

   address internal operator;

   function transferOwnership(address newOwner) public returns(bool success);

   function setOperator(address newoperator) public returns(bool success);

   function FreezeICO() public returns(bool success);

   function setRate(uint8 newRate) public returns(bool success);

   function addToWhitelist(address who) public returns(bool success);

   event Ownership_Transferred(address indexed previousOwner, address indexed newOwner);

   event Freeze_Ico(bool success);

   event change_Rate(uint8 newRate);

   event change_Operator(address operator);

   event Added_to_whitelist(bool success);

   constructor(address newoperator) public{

        owner = msg.sender;
        operator = newoperator;

   }


   modifier onlyOwner {

       require(msg.sender == owner);

       _;

   }

   modifier onlyOperator {

       require(msg.sender == operator);

       _;

   }
}


contract SafeToken is ERC20,Owned {

   using SafeMath for uint;

   string public _name = "DOHA";
   string public _symbol = "DO";
   uint8 public _decimals = 18;
   uint256 public _totalSupply;
   uint8 public _rate;
   uint256 public _deadline;
   uint256 public _icoCompleted;
   bool public _freeze;

   mapping (address => uint256) public balances;
   mapping (address => mapping (address => uint256)) public allowed;
   mapping (address => bool) public whiteList;

   constructor(uint256 totalSupply, uint8 rate,  uint duration) public {

       _rate=rate;
       _totalSupply = totalSupply * 10**uint(_decimals);
       _deadline = now + duration * 1 minutes;
       balances[msg.sender] = _totalSupply;
       _icoCompleted = 0;
       _freeze = false;


   }

   function name()public view returns (string) {
       return _name;
   }

   function symbol()public view returns (string) {
       return _symbol;
   }

   function deadline()public view returns (uint) {
       return _deadline;
   }

   function totalSupply()public view returns (uint) {
       return _totalSupply;
   }

   function currentOwner() public view returns (address) {
       return owner;
   }

   function icoStatus() public view returns (uint) {
       return _icoCompleted;
   }

   function freezeStatus() public view returns (bool) {
       return _freeze;
   }

   function white_List(address who) public view returns (bool) {

       return whiteList[who];
   }

   modifier icoFunctioning {

       require(_freeze == false);
       require(_icoCompleted < _totalSupply);
       _;
   }

   modifier deadlineCheck {

       require(now <= _deadline);
       _;
   }

   modifier whiteListed(address who) {

       require(whiteList[who]==true);
       _;
   }

   function() external payable icoFunctioning deadlineCheck whiteListed(msg.sender){

       require(msg.value>0);

       uint _tokens;
       _tokens = SafeMath.mul(msg.value,_rate);

       require(_tokens <= balances[owner]);

       balances[owner] = SafeMath.sub(balances[owner],_tokens);
       balances[msg.sender] = SafeMath.add(balances[msg.sender],_tokens);
       _icoCompleted = SafeMath.add(_icoCompleted,_tokens);

       owner.transfer(msg.value);
       emit TransferEther(msg.sender,_tokens);

   }

   function FreezeICO() public onlyOwner returns(bool success) {

       _freeze = !_freeze;
       emit Freeze_Ico(_freeze);
       return _freeze;
   }

   function addToWhitelist(address who) public onlyOperator deadlineCheck returns(bool success){

       whiteList[who] =true;
       emit Added_to_whitelist(whiteList[who]);
       return(true);


   }

   function balanceOf(address who) external view returns (uint256 balance) {

       return balances[who];
   }


  function transferToken(address _to, uint256 _value) external returns (bool) {

        require(_to != address(0));
        require(now>_deadline);
        require(_value <= balances[msg.sender]);

        balances[msg.sender] = SafeMath.sub(balances[msg.sender], _value);
        balances[_to] = SafeMath.add(balances[_to], _value);
        emit Transfer(msg.sender, _to, _value);
        return true;
  }

   function setRate(uint8 newRate) public onlyOwner icoFunctioning returns (bool success) {

       require(newRate != _rate);

       _rate = newRate;
       emit change_Rate(_rate); 
       return true;

   }

   function transferOwnership(address newOwner) public onlyOwner returns(bool success) {

       require(newOwner != address(0));
       balances[newOwner] = balances[owner];
       balances[owner] = 0;
       emit Ownership_Transferred(owner, newOwner);
       owner = newOwner;
       return true;

   }

   function setOperator(address newoperator) onlyOwner deadlineCheck public returns(bool success){

       require(newoperator != address(0));

       operator=newoperator;
       emit change_Operator(operator);
       return true;

   }

}

部署時我收到這樣的消息 在此處輸入圖像描述

我在乙太坊方面沒有太多經驗。需要幫忙

我發現這裡出了什麼問題,你contract Owned {有一個參數化的建構子,但你在這裡使用它作為預設建構子

contract SafeToken is ERC20,Owned {

同時繼承它的屬性。

因此,您沒有實現所有功能的錯誤!

解決方案:

您需要刪除參數化建構子,它的初始化如下:

constructor() public { // observe that the parameters are removed

    owner = msg.sender;
    // operator = newoperator; // comment or remove this line

}

或者

您可以**在繼承時添加參數,**如下所示:

contract SafeToken is ERC20, Owned(msg.sender) { // NOTE: You can add any address

選擇是你的。乾杯!

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