ethereumweb3jsnonce

web3.eth.Contract passing nonce


I use web3.eth.Contract to interact with smart contract. I need to execute method multiple times at the same moment asynchronously. Because of nonce things I have errors like replacement transaction underpriced so I need to increase nonce manually. But I can't find a way to do it, here is example how I call method:

contract.methods.foo()
            .send({
                from: address,
                gas: 100000,
                gasPrice: gasPrice
            });

How to pass nonce by going this way?


Solution

  • nonce is one of the allowed parameters overriding the transaction.

    So you can do

    contract.methods.foo().send({
        from: address,
        gas: 100000,
        gasPrice: gasPrice,
        nonce: 5
    });
    

    Source: https://web3js.readthedocs.io/en/v1.10.0/web3-eth-contract.html#methods-mymethod-send


    replacement transaction underpriced

    This error occurs when there's already another transaction with the same nonce in the mempool, and your new transaction (with the same nonce) has lower or the same gasPrice.