Tokens

嘗試在 Rinkeby 測試網上部署 ERC20 代幣合約時,不斷出現“氣體估算失敗”錯誤

  • December 23, 2021

我在下麵包含了契約的程式碼。嘗試通過元遮罩在混音中部署時遇到的錯誤是:

氣體估計錯誤並顯示以下消息(見下文)。事務執行可能會失敗。是否要強制發送?錯誤:在 chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:1391377 在 chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:927906 在 chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn 需要的氣體超過限額或始終失敗交易/background.js:1:346093 at o (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:364324) at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:348923 at chrome-extension:// nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:346093 at c (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:349199) at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background。js:1:349237 在 Ht (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:358606) 在 Object. (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:349326) 在 e.value (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:928821) 在 chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js :1:928388 在 n (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:364268) 在 o (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:364346) 在 chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn /background.js:1:348923 at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:928354 at n (chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:364268) at o (chrome-extension: //nkbihfbeogaeaoehlefnkodbefgpgknn/背景。

這是合約程式碼:

pragma solidity ^0.4.23;

library SafeMath {
//SafeMath library for preventing overflow when dealing with uint256 in solidity

 /**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
       return 0;
   }
   uint256 c = a * b;
   assert(c / a == b);
   return c;
}

/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
   // assert(b > 0); // Solidity automatically throws 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 Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
   assert(b <= a);
   return a - b;
}

/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
   uint256 c = a + b;
   assert(c >= a);
   return c;
}
}





contract ExampleToken {
using SafeMath for uint256;

string public constant name = "ExampleToken";
string public constant symbol = "EXT";
uint8 public constant decimals = 18; // 18 DECIMALS is the strongly suggested default, avoid changing it

//total supply (TOTALSUPPLY) is declared private and constant and can be accessed via totalSupply()
uint private constant TOTALSUPPLY = 200000000000 * (10**18);

// Balances for each account
mapping(address => uint256) balances;

// Owner of account approves the transfer of an amount to another account
//This is a mapping of a mapping
// This mapping keeps track of the allowances given
mapping(address => mapping (address => uint256)) allowed;

//Constructor
constructor () public {

   //sends all the tokens to the address of the contract creator
   balances[msg.sender] = TOTALSUPPLY;

   emit Transfer(address(0), msg.sender, TOTALSUPPLY);
}

            //*** ERC20 FUNCTIONS ***//
//1
/**
* @dev total number of tokens in existence
*/
function totalSupply() public pure returns (uint256 _totalSupply) {
   //set the named return variable as the global variable TOTALSUPPLY
   _totalSupply = TOTALSUPPLY;
}

//2
/**
* @dev Gets the balance of the specified address.
* @param _owner The address to query the the balance of.
* @return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address _owner) public view returns (uint256 balance) {
   return balances[_owner];
}

//3
/**
* @dev transfer token for a specified address
* @param _to The address to transfer to.
* @param _value The amount to be transferred.
*/
//Note: returns a boolean indicating whether transfer was successful
function transfer(address _to, uint256 _value) public returns (bool success) {
   require(_to != address(0)); //not sending to burn address
   require(_value <= balances[msg.sender]); // If the sender has sufficient funds to send
   require(_value>0);// and the amount is not zero or negative

   // SafeMath.sub will throw if there is not enough balance.
   balances[msg.sender] = balances[msg.sender].sub(_value); // subtract from sender
   balances[_to] = balances[_to].add(_value); // add same to recipient
   emit Transfer(msg.sender, _to, _value);
   return true;
}

//4
//Owner "approves" the given address to withdraw instances of the tokens from the owners address
/**
  * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
  *
  * Beware that changing an allowance with this method brings the risk that someone may use both the old
  * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
  * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
  * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  * @param _spender The address which will spend the funds.
  * @param _value The amount of tokens to be spent.
  */
function approve(address _spender, uint256 _value) public returns (bool) {
   allowed[msg.sender][_spender] = _value;
   emit Approval(msg.sender, _spender, _value);
   return true;
}

//5
//Lets an "approved" address transfer the approved amount from the address that called approve()
/**
* @dev Transfer tokens from one address to another
* @param _from address The address which you want to send tokens from
* @param _to address The address which you want to transfer to
* @param _value uint256 the amount of tokens to be transferred
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
   require(_to != address(0));
   require(_value <= balances[_from]);
   require(_value <= allowed[_from][msg.sender]);

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

//6
/**
* @dev Function to check the amount of tokens that an owner allowed to a spender.
* @param _owner address The address which owns the funds.
* @param _spender address The address which will spend the funds.
* @return A uint256 specifying the amount of tokens still available for the spender.
*/
function allowance(address _owner, address _spender) public view returns (uint256) {
   return allowed[_owner][_spender];
}

//additional functions for altering allowances
/**
* @dev Increase the amount of tokens that an owner allowed to a spender.
*
* approve should be called when allowed[_spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* @param _spender The address which will spend the funds.
* @param _addedValue The amount of tokens to increase the allowance by.
*/
function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
   allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
   emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
   return true;
}

/**
* @dev Decrease the amount of tokens that an owner allowed to a spender.
*
* approve should be called when allowed[_spender] == 0. To decrement
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* @param _spender The address which will spend the funds.
* @param _subtractedValue The amount of tokens to decrease the allowance by.
*/
function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
   uint oldValue = allowed[msg.sender][_spender];
   if (_subtractedValue > oldValue) {
       allowed[msg.sender][_spender] = 0;
   } else {
       allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
   }
   emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
   return true;
}

         //***ERC20 Events***//
//Event 1
// Triggered whenever approve(address _spender, uint256 _value) is called.
event Approval(address indexed _owner, address indexed _spender, uint256 _value);

//Event 2
// Triggered when tokens are transferred.
event Transfer(address indexed _from, address indexed _to, uint256 _value);

}

我很感激任何幫助。我已嘗試設置最大 gas 限制和 gas 價格,但合約不會部署。嘗試在 MEW 上使用字節碼部署它時,我遇到了同樣的問題。

我遇到了同樣的錯誤,現在我完全知道原因了。

  1. 如果你的程式碼行不大(少於 1000 行),你可以增加你的 gas,它會起作用。
  2. 如果你的程式碼行太大(超過 1000 行的導入程式碼),你應該把你的程式碼拆分成幾個合約或庫,並一個一個地部署它們。您可以通過主契約中的介面呼叫它們。我試過這個,它奏效了。

試著放

"config": {
   "chainId": 15,
   "homesteadBlock": 0,
"EIP150Block": 0,
   "eip155Block": 0,
   "eip158Block": 0,
**"ByzantiumBlock": 0**
}

“拜占庭塊”:0

你的 genesis.json 中的設置你需要創建一個新的鏈。

這就是上面solidity 0.4.22的問題。

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