Robert Oschler
Robert Oschler

Reputation: 14375

Getting "replacement transaction underpriced" errors when sending Ethereum transactions to the Rinkeby network?

I'm getting intermittent "replacement transaction underpriced" errors on the Rinkeby network on the server side of my Node.JS dApp. I am using the exact amount for estimated gas in my transaction send() call returned to me by the estimateGas() call. In my call options, I am adding both a gas and gasLimit field just to be safe with the estimated gas value returned by estimateGas() in the options object. Does anyone know how to fix this?

On an unrelated issue. Much to my dismay, just submitting a transaction through Metamask to the Rinkeby network takes about 16 to 30 seconds. Note, I mean from the time the Metamask extension pops up to the time my client side code regains control. I am not talking about the time it takes to get a transaction confirmed/mined by the network. Having said that, I am beginning to wonder if Metamask does not return control to you until the transaction has been mined. Is that the case?

Here is a code fragment of the code I use to send the transaction to Rinkeby (or whatever network I'm testing on):

contractMethodToCall.estimateGas(
    { from: publicAddr, gasPrice: 20000000000, gas: 1500000})
.then(function(estimatedGas) {
    if (estimatedGas <= 0)
        throw new Error("The estimated gas for the transaction is zero.");

    const rawTx = {
        nonce: fromNonce,
        gasPrice: gasPriceGwei,
        // Use the estimated gas.
        gasLimit: estimatedGas,
        // Adding both gas and gasLimit just in case.
        gas: estimatedGas,
        to: contractAddr,
        value: '0x00',
        data: encodedAbiForCall
    }

    let tx = new Tx(rawTx);

    // Sign the transaction using our server private key in Buffer format.
    tx.sign(privateKeyBuffer);

    let serializedTx = '0x' + tx.serialize().toString('hex');

    return web3.eth.sendSignedTransaction(serializedTx);
});

Upvotes: 5

Views: 5604

Answers (1)

Adam Kipnis
Adam Kipnis

Reputation: 10971

It sounds like you found the cause of your issue from your comment. But, to add clarity for others who see the same issue, the error is not solely because of a duplicate nonce. This error will occur when a transaction is submitted with a nonce that is already used in another pending transaction AND the gas price is the same (or less than) the pending transaction.

You can submit a transaction using the same nonce if you use a higher gas price. Miners will always pick the higher priced transaction for pending work, so this is a way to cancel a pending transaction or resubmit a transaction that is being ignored due to low gas prices.

Upvotes: 5

Related Questions