Define the Goal
Do you want to:
- Send a new transaction, after all your previous transactions have completed, or...
- Replace a pending (unmined) transaction, with the new raw transaction.
Send a New Transaction
The error message implies that you're trying to replace a pending transaction. That's because the raw transaction you're trying to send has the same
nonce as another transaction that you have pending.
Since replacing a transaction is not your goal, simply increase the nonce to be one higher than your last pending transaction. You may need to track this internally, rather than relying on
Replace a Pending Transaction
The 10% Minumum
Since your goal is to replace a transaction that is pending, you must try to convince the miners to use your new transaction. To do that, you must use a gas price that is 10% higher* than the
gasPrice of the pending transaction.
const gasPrice = web3.eth.gasPrice.toNumber() * 1.40
note I'm already adding 40% to gasPrice
The quoted code adds 40% to
web3.eth.gasPrice. This may not be 10% higher than the pending transaction's gas price.
web3.eth.gasPrice may vary over time, and/or you might have set any arbitrary gas price on the pending transaction.
* 10% isn't defined in the protocol, it's just how most nodes & miners implement it.
Determining the Minimum
If you have the hash of the pending transaction, you can determine the required gas price with something like:
replacement_price = web3.eth.getTransaction(pending_txn_hash).gasPrice * 1.101
Note that this is floating point math, which will have rounding errors, so I threw in an extra 10th of a percent to be sure it was over the minimum.