[Ethereum] Constantly getting “Gas estimation failed” error when attempting to deploy ERC20 Token contract on Rinkeby testnet

contract-deploymenterc-20gasgas-estimatetokens

I have included the code for the contract below. The error I get when attempting to deploy in remix via metamask is:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Error: gas required exceeds allowance or always failing transaction at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:1391377
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:927906
at
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
at Ht
(chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:358606)
at Object.
(chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:349326)
at e.value
(chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:928821)
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:928388
at n
(chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:364268)
at o
(chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:364346)
at
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/background.js:1:364346)
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:348923
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:928727
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:349733
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:346093
at c
(chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:349199)
at s
(chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:349121)
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:348923
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:349713
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:928609
at
chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:927046
at i
(chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/background.js:1:1066207)

Here is the contract code:

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);

}

I appreciate any help. I have tried setting maximum gas limit and gas prices but the contract won't deploy. I have the same problem when attempting to deply it using bytecode on MEW.

Best Answer

I have encought the same error, and now I absolutely know the reason.

  1. if you code lines is not big(less than 1000 lines),you can increase your gas, it would be worked.
  2. if you code lines is too big(more than 1000 lines with imported code),you should split you code into several contract or library ,and deploy them one by one. You can call them by interface in your main contract. I tried this, and it worked.
Related Topic