From 7c7446d46ba60aa3e3f6a37eb6b90b3283040c72 Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Thu, 14 Jul 2022 00:28:26 +1000 Subject: [PATCH] fix: handleRpcError with web3 provider --- src/Transaction.js | 68 ++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/src/Transaction.js b/src/Transaction.js index e236b13..4a1eff3 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -11,12 +11,11 @@ const nonceErrors = [ /OldNonce/, 'invalid transaction nonce', ] -const nonceErrorCodes = ['SERVER_ERROR', 'NONCE_EXPIRED'] const gasPriceErrors = [ 'Transaction gas price supplied is too low. There is another transaction with same nonce in the queue. Try increasing the gas price or incrementing the nonce.', - /replacement transaction underpriced/i, /transaction underpriced/, + /fee too low/i, /Transaction gas price \d+wei is too low. There is another transaction with same nonce in the queue with gas price: \d+wei. Try increasing the gas price or incrementing the nonce./, /FeeTooLow/, /max fee per gas less than block base fee/, @@ -323,42 +322,41 @@ class Transaction { e = e.error } - if (e.error && nonceErrorCodes.includes(e.code)) { - const message = e.error.message + // web3 provider not wrapping message + const message = e.error?.message || e.message - // nonce is too low, trying to increase and resubmit - if (this._hasError(message, nonceErrors)) { - if (this.replaced) { - console.log('Transaction with the same nonce was mined') - return // do nothing - } - console.log(`Nonce ${this.tx.nonce} is too low, increasing and retrying`) - if (this.retries <= this.manager.config.MAX_RETRIES) { - this.tx.nonce++ - this.retries++ - return this[method]() - } - } - - // there is already a pending tx with higher gas price, trying to bump and resubmit - if (this._hasError(message, gasPriceErrors)) { - console.log( - `Gas price ${formatUnits( - this.tx.gasPrice || this.tx.maxFeePerGas, - 'gwei', - )} gwei is too low, increasing and retrying`, - ) - if (this._increaseGasPrice()) { - return this[method]() - } else { - throw new Error('Already at max gas price, but still not enough to submit the transaction') - } - } - - if (this._hasError(message, sameTxErrors)) { - console.log('Same transaction is already in mempool, skipping submit') + // nonce is too low, trying to increase and resubmit + if (this._hasError(message, nonceErrors)) { + if (this.replaced) { + console.log('Transaction with the same nonce was mined') return // do nothing } + console.log(`Nonce ${this.tx.nonce} is too low, increasing and retrying`) + if (this.retries <= this.manager.config.MAX_RETRIES) { + this.tx.nonce++ + this.retries++ + return this[method]() + } + } + + // there is already a pending tx with higher gas price, trying to bump and resubmit + if (this._hasError(message, gasPriceErrors)) { + console.log( + `Gas price ${formatUnits( + this.tx.gasPrice || this.tx.maxFeePerGas, + 'gwei', + )} gwei is too low, increasing and retrying`, + ) + if (this._increaseGasPrice()) { + return this[method]() + } else { + throw new Error('Already at max gas price, but still not enough to submit the transaction') + } + } + + if (this._hasError(message, sameTxErrors)) { + console.log('Same transaction is already in mempool, skipping submit') + return // do nothing } throw new Error(`Send error: ${e}`)