Go-Ethereum

為什麼我的契約需要這麼長時間才能部署?

  • April 8, 2019

我對此很陌生,目前只是在玩。

我基本上複製並粘貼了別人的程式碼並更改了一些位。

在 MIST 上,它說正在部署契約,並且已經持續了 12 個小時。

這是我的契約:

https://etherscan.io/address/0x30ec602bf62971df952d43a316e299708e440cbf

這是我使用的程式碼:

pragma solidity ^0.4.10;

contract ForeignToken {
   function balanceOf(address _owner) constant returns (uint256);
   function transfer(address _to, uint256 _value) returns (bool);
}

contract MyKidsEducationToken {
   address owner = msg.sender;

   bool public purchasingAllowed = false;

   mapping (address => uint256) balances;
   mapping (address => mapping (address => uint256)) allowed;

   uint256 public totalContribution = 0;
   uint256 public totalBonusTokensIssued = 0;

   uint256 public totalSupply = 0;

   function name() constant returns (string) { return "My Kids Education Token"; }
   function symbol() constant returns (string) { return "MTKE"; }
   function decimals() constant returns (uint8) { return 18; }

   function balanceOf(address _owner) constant returns (uint256) { return balances[_owner]; }

   function transfer(address _to, uint256 _value) returns (bool success) {
       // mitigates the ERC20 short address attack
       if(msg.data.length < (2 * 32) + 4) { revert(); }

       if (_value == 0) { return false; }

       uint256 fromBalance = balances[msg.sender];

       bool sufficientFunds = fromBalance >= _value;
       bool overflowed = balances[_to] + _value < balances[_to];

       if (sufficientFunds && !overflowed) {
           balances[msg.sender] -= _value;
           balances[_to] += _value;

           Transfer(msg.sender, _to, _value);
           return true;
       } else { return false; }
   }

   function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
       // mitigates the ERC20 short address attack
       if(msg.data.length < (3 * 32) + 4) { revert(); }

       if (_value == 0) { return false; }

       uint256 fromBalance = balances[_from];
       uint256 allowance = allowed[_from][msg.sender];

       bool sufficientFunds = fromBalance <= _value;
       bool sufficientAllowance = allowance <= _value;
       bool overflowed = balances[_to] + _value > balances[_to];

       if (sufficientFunds && sufficientAllowance && !overflowed) {
           balances[_to] += _value;
           balances[_from] -= _value;

           allowed[_from][msg.sender] -= _value;

           Transfer(_from, _to, _value);
           return true;
       } else { return false; }
   }

   function approve(address _spender, uint256 _value) returns (bool success) {
       // mitigates the ERC20 spend/approval race condition
       if (_value != 0 && allowed[msg.sender][_spender] != 0) { return false; }

       allowed[msg.sender][_spender] = _value;

       Approval(msg.sender, _spender, _value);
       return true;
   }

   function allowance(address _owner, address _spender) constant returns (uint256) {
       return allowed[_owner][_spender];
   }

   event Transfer(address indexed _from, address indexed _to, uint256 _value);
   event Approval(address indexed _owner, address indexed _spender, uint256 _value);

   function enablePurchasing() {
       if (msg.sender != owner) { revert(); }

       purchasingAllowed = true;
   }

   function disablePurchasing() {
       if (msg.sender != owner) { revert(); }

       purchasingAllowed = false;
   }

   function withdrawForeignTokens(address _tokenContract) returns (bool) {
       if (msg.sender != owner) { revert(); }

       ForeignToken token = ForeignToken(_tokenContract);

       uint256 amount = token.balanceOf(address(this));
       return token.transfer(owner, amount);
   }

   function getStats() constant returns (uint256, uint256, uint256, bool) {
       return (totalContribution, totalSupply, totalBonusTokensIssued, purchasingAllowed);
   }

   function() payable {
       if (!purchasingAllowed) { revert(); }

       if (msg.value == 0) { return; }

       owner.transfer(msg.value);
       totalContribution += msg.value;

       uint256 tokensIssued = (msg.value * 100);

       if (msg.value >= 10 finney) {
           tokensIssued += totalContribution;

           bytes20 bonusHash = ripemd160(block.coinbase, block.number, block.timestamp);
           if (bonusHash[0] == 0) {
               uint8 bonusMultiplier =
                   ((bonusHash[1] & 0x01 != 0) ? 1 : 0) + ((bonusHash[1] & 0x02 != 0) ? 1 : 0) +
                   ((bonusHash[1] & 0x04 != 0) ? 1 : 0) + ((bonusHash[1] & 0x08 != 0) ? 1 : 0) +
                   ((bonusHash[1] & 0x10 != 0) ? 1 : 0) + ((bonusHash[1] & 0x20 != 0) ? 1 : 0) +
                   ((bonusHash[1] & 0x40 != 0) ? 1 : 0) + ((bonusHash[1] & 0x80 != 0) ? 1 : 0);

               uint256 bonusTokensIssued = (msg.value * 100) * bonusMultiplier;
               tokensIssued += bonusTokensIssued;

               totalBonusTokensIssued += bonusTokensIssued;
           }
       }

       totalSupply += tokensIssued;
       balances[msg.sender] += tokensIssued;

       Transfer(address(this), msg.sender, tokensIssued);
   }
}

您的合約已成功部署。如果您查看創建合約的交易,您可以看到這一點:https ://etherscan.io/tx/0x402cb533bc0b9af011f9c3aa77f5ac8c97057c7b6c2334182e0d06a289cd6573

創建需要很長時間的一個原因可能是您提供的低 gas 價格。汽油價格是礦工探勘您的交易的動力。如果價格較低,其他價格較高的交易將更具吸引力,並且可能需要一段時間才能考慮到您的交易。通常gas價格在20 Gwei以上。您提供了 1 Gwei,您可以在交易中看到它。

在這裡,當我們看到控制台時,我們會看到類似“正在創建收件箱…”的消息。但是,當您點擊創建的合約實例中最左側的箭頭時,您可以看到您工作中的所有功能列表。

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