Solidity

ERC-20 代幣智能合約重新部署

  • March 20, 2020

大家好,想知道如何重新部署已經部署的 ERC-20 代幣智能合約?

我已經在 Ropsten 測試網路和主 ETH 網路上部署了很少的合約,在這兩種情況下都是成功的,也就是說,我從頭開始編寫的所有合約都有清晰顯示的值,我的意思是:-名稱-總供應量-符號-小數點

使用 Sublime 編輯合約,然後將其放入 Remix 以使用 web3 注入錢包(例如 Metamask)編譯和部署它。

我正在嘗試重新部署一個已經在主 ETH 網路上的智能合約。總供應價值在哪裡?為什麼智能合約中沒有顯示值,我應該在哪裡編輯?

網上有很多答案,但沒有一個給出明確的答案!

來自 Etherscan 的智能合約程式碼:

pragma solidity ^0.4.8;

contract SafeMath {
 function safeMul(uint256 a, uint256 b) internal returns (uint256) {
   uint256 c = a * b;
   assert(a == 0 || c / a == b);
   return c;
 }

 function safeDiv(uint256 a, uint256 b) internal returns (uint256) {
   assert(b > 0);
   uint256 c = a / b;
   assert(a == b * c + a % b);
   return c;
 }

 function safeSub(uint256 a, uint256 b) internal returns (uint256) {
   assert(b <= a);
   return a - b;
 }

 function safeAdd(uint256 a, uint256 b) internal returns (uint256) {
   uint256 c = a + b;
   assert(c>=a && c>=b);
   return c;
 }

 function assert(bool assertion) internal {
   if (!assertion) {
     throw;
   }
 }
}
contract ERC20 is SafeMath{           
   string public name;
   string public symbol;
   uint8 public decimals;
   uint256 public totalSupply;
   address public owner;

   mapping (address => uint256) public balanceOf;
   mapping (address => uint256) public freezeOf;
   mapping (address => mapping (address => uint256)) public allowance;

   event Transfer(address indexed from, address indexed to, uint256 value);

   event Burn(address indexed from, uint256 value);

   event Freeze(address indexed from, uint256 value);

   event Unfreeze(address indexed from, uint256 value);

   function ERC20(
       uint256 initialSupply,
       string tokenName,
       uint8 decimalUnits,
       string tokenSymbol
       ) {
       balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
       totalSupply = initialSupply;                        // Update total supply
       name = tokenName;                                   // Set the name for display purposes
       symbol = tokenSymbol;                               // Set the symbol for display purposes
       decimals = decimalUnits;                            // Amount of decimals for display purposes
       owner = msg.sender;
   }

   /* Send coins */
   function transfer(address _to, uint256 _value) {
       if (_to == 0x0) throw;                               // Prevent transfer to 0x0 address. Use burn() instead
       if (_value <= 0) throw; 
       if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
       if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
       balanceOf[msg.sender] = SafeMath.safeSub(balanceOf[msg.sender], _value);                     // Subtract from the sender
       balanceOf[_to] = SafeMath.safeAdd(balanceOf[_to], _value);                            // Add the same to the recipient
       Transfer(msg.sender, _to, _value);                   // Notify anyone listening that this transfer took place
   }

   /* Allow another contract to spend some tokens in your behalf */
   function approve(address _spender, uint256 _value)
       returns (bool success) {
       if (_value <= 0) throw; 
       allowance[msg.sender][_spender] = _value;
       return true;
   }


   /* A contract attempts to get the coins */
   function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
       if (_to == 0x0) throw;                                // Prevent transfer to 0x0 address. Use burn() instead
       if (_value <= 0) throw; 
       if (balanceOf[_from] < _value) throw;                 // Check if the sender has enough
       if (balanceOf[_to] + _value < balanceOf[_to]) throw;  // Check for overflows
       if (_value > allowance[_from][msg.sender]) throw;     // Check allowance
       balanceOf[_from] = SafeMath.safeSub(balanceOf[_from], _value);                           // Subtract from the sender
       balanceOf[_to] = SafeMath.safeAdd(balanceOf[_to], _value);                             // Add the same to the recipient
       allowance[_from][msg.sender] = SafeMath.safeSub(allowance[_from][msg.sender], _value);
       Transfer(_from, _to, _value);
       return true;
   }

   function burn(uint256 _value) returns (bool success) {
       if (balanceOf[msg.sender] < _value) throw;            // Check if the sender has enough
       if (_value <= 0) throw; 
       balanceOf[msg.sender] = SafeMath.safeSub(balanceOf[msg.sender], _value);                      // Subtract from the sender
       totalSupply = SafeMath.safeSub(totalSupply,_value);                                // Updates totalSupply
       Burn(msg.sender, _value);
       return true;
   }

   function freeze(uint256 _value) returns (bool success) {
       if (balanceOf[msg.sender] < _value) throw;            // Check if the sender has enough
       if (_value <= 0) throw; 
       balanceOf[msg.sender] = SafeMath.safeSub(balanceOf[msg.sender], _value);                      // Subtract from the sender
       freezeOf[msg.sender] = SafeMath.safeAdd(freezeOf[msg.sender], _value);                                // Updates totalSupply
       Freeze(msg.sender, _value);
       return true;
   }

   function unfreeze(uint256 _value) returns (bool success) {
       if (freezeOf[msg.sender] < _value) throw;            // Check if the sender has enough
       if (_value <= 0) throw; 
       freezeOf[msg.sender] = SafeMath.safeSub(freezeOf[msg.sender], _value);                      // Subtract from the sender
       balanceOf[msg.sender] = SafeMath.safeAdd(balanceOf[msg.sender], _value);
       Unfreeze(msg.sender, _value);
       return true;
   }

   // transfer balance to owner
   function withdrawEther(uint256 amount) {
       if(msg.sender != owner)throw;
       owner.transfer(amount);
   }

   // can accept ether
   function() payable {
   }
}

當您使用 Remix(以及任何能夠部署合約的客戶端)部署合約時,您需要為合約提供所需的建構子參數。在您的情況下,所需的參數是:

uint256 initialSupply,
string tokenName,
uint8 decimalUnits,
string tokenSymbol

這些值只能在部署合約時給出,因此只能給出一次。如果合約沒有空的建構子(因為你的合約沒有),那麼你不能在不為這些參數提供值的情況下部署它。因此,只需提供正確的值,它們就會在成功部署後儲存在合約中。

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