Web3.js: getting error nonce too low even after getting correct nounce.

Created on 16 Jun 2018  路  5Comments  路  Source: ChainSafe/web3.js

Hello,
This is my code.

web3.eth.getTransactionCount(myAddress).then(function(lastCountOfTransaction){     
                        console.log(lastCountOfTransaction);
                        var rawTransaction = {
                            "from": myAddress,
                            "nonce": '0x' + lastCountOfTransaction.toString(16),      
                            "gasPrice": web3.utils.toHex(15 * 1e9), //1 can be changed to n gwei
                            "gasLimit": web3.utils.toHex(250000), // 1000000 can be to set to any n number
                            "to": ContractAddress,
                            "value": "0x0",
                            "data": ContractObject.methods.transfer(userAddress,noOfTokens).encodeABI(),
                            "chainId": chainId
                        };

                        var tx = new Tx(rawTransaction);
                        tx.sign(privKey);
                        var serializedTx = tx.serialize();

                        web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),function(err,hash){

but I am getting error as nonce too low.

When I console few things it gives me the following.

transfer token value 50
lastCountOfTransaction : 52
{ from: '0x1f0CDd8764443c66A9C8064297.........',
nonce: '0x34',
gasPrice: '0x37e11d600',
gasLimit: '0x3d090',
to: '0x3475B73b0b8c638BB5f8A405e5..........',
value: '0x0',
data: '0xa9059cbb00000000000000000000000045559a79195b5e16eb92694abd7eee2eb4b6507b0000000000000000000000000000000000000000000000000000000000000032',
chainId: 1 }

I am using web3js 1.0.0.^34 beta version of web3js and provider as infura main net.

Most helpful comment

it worked. there was my mistake with private key. I had kept incorrect private key hence it gived nounce too low.

All 5 comments

I have also tried using web3.utils.toHex(lastCountOfTransaction) but it didn't work too

it worked. there was my mistake with private key. I had kept incorrect private key hence it gived nounce too low.

but I have one more question. My contract has 18 decimals. When I make a transaction using web3js it shows 0.000000000000000000005 tokens on etherscan. that is 5 after 16 decimals. Why it is not showing me 50 tokens as it is.

@Hkk009 I had the same issue a few days ago:

When emitting an ERC20, try to think of it as ETH:

1 ETH = 1e18 WEI
1 ERC20 = 1 ETH

If you were to create a crowdsale and sell your token, you may be thinking of giving it a supply and price:

pragma solidity ^0.4.21;

import '../node_modules/openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
import '../node_modules/openzeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol';

contract SimpleToken is StandardToken, DetailedERC20 {

    constructor (
        string _name, 
        string _symbol, 
        uint8 _decimals,
        uint256 _totalSupply) public DetailedERC20 (_name, _symbol, _decimals) {

        uint256 supply = _totalSupply * 1 ether; // this is the solution to your 0.0000... decimals problem

        totalSupply_ = supply;
        balances[msg.sender] = supply;
        emit Transfer(0x0, msg.sender, supply);

    }

}

Then in the crowdsale:

pragma solidity ^0.4.21;

import '../node_modules/openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol';
import './SimpleToken.sol';

contract SimpleCrowdsale is Crowdsale {

    uint256 public price;

    uint256 internal weiAmount;

    event ProcessedRemainder(uint256 remainder);

    constructor (uint256 _price, uint256 _rate, address _wallet, SimpleToken _token)
        public Crowdsale(_rate, _wallet, _token) {

        price = _price; // price is an ETH value, say 0.1 ETH

    }

    /**
   * @dev low level token purchase ***DO NOT OVERRIDE***
   * @param _beneficiary Address performing the token purchase
   */
    function buyTokens(address _beneficiary) public payable {

        _preValidatePurchase(_beneficiary, msg.value);

        weiAmount = _processRemainder(_beneficiary, msg.value);

        uint256 tokens = _getTokenAmount(weiAmount);

        weiRaised = weiRaised.add(weiAmount);

        _processPurchase(_beneficiary, tokens);
        emit TokenPurchase(
            msg.sender,
            _beneficiary,
            weiAmount,
            tokens
        );

        _updatePurchasingState(_beneficiary, weiAmount);

        _forwardFunds();
        _postValidatePurchase(_beneficiary, weiAmount);
    }

    /**
       * @dev Transfers back the remainder of the weiAmount against the token price to the beneficiary
       * @param _beneficiary Address performing the token purchase
       * @param _weiAmount Value in wei involved in the purchase
       * @return _weiAmount Value without the remainder
    */
    function _processRemainder(address _beneficiary, uint256 _weiAmount) internal returns (uint256) {
        uint256 remainder = _weiAmount % price;

        emit ProcessedRemainder(remainder);

        if (remainder > 0) {
            _beneficiary.transfer(remainder);
        }

        return _weiAmount.sub(remainder);
    }

    /**
       * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use super to concatenate validations.
       * @param _beneficiary Address performing the token purchase
       * @param _weiAmount Value in wei involved in the purchase
    */
    function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
        require(_beneficiary != address(0));
        require(_weiAmount != 0);
        require(_weiAmount >= price);
    }

    /**
       * @dev Override to extend the way in which ether is converted to tokens.
       * @param _weiAmount Value in wei to be converted into tokens
       * @return Number of tokens that can be purchased with the specified _weiAmount
   */
    function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
        return _weiAmount.div(price).mul(1 ether); // and here is the other trick. You will give a token per the value of 1 ETH. After this you will see the correct value in etherscan
    }

Hope this helps.

thanks

Was this page helpful?
0 / 5 - 0 ratings