From ea121ffdd0ab989e852c225268706e9cfc09b52b Mon Sep 17 00:00:00 2001 From: mous <97399882+mouseless0x@users.noreply.github.com> Date: Thu, 5 Dec 2024 06:44:57 +0700 Subject: [PATCH] add try/catch around getGasPrice (#370) * add try/catch around getGasPrice * return 1gwei if fail * don't throw errors * add same case to legacy * handle cases where gas price can't be found * throw error --------- Co-authored-by: mouseless <97399882+mouseless-eth@users.noreply.github.com> --- src/executor/executor.ts | 18 +++++++++++++----- src/executor/executorManager.ts | 2 +- src/executor/senderManager.ts | 2 +- src/handlers/gasPriceManager.ts | 13 ++++++++----- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/executor/executor.ts b/src/executor/executor.ts index e576043a..b4b255e7 100644 --- a/src/executor/executor.ts +++ b/src/executor/executor.ts @@ -153,8 +153,15 @@ export class Executor { ): Promise { const newRequest = { ...transactionInfo.transactionRequest } - const gasPriceParameters = - await this.gasPriceManager.getNetworkGasPrice() + let gasPriceParameters + try { + gasPriceParameters = + await this.gasPriceManager.tryGetNetworkGasPrice() + } catch (err) { + this.logger.error({ error: err }, "Failed to get network gas price") + this.markWalletProcessed(transactionInfo.executor) + return { status: "failed" } + } newRequest.maxFeePerGas = maxBigInt( gasPriceParameters.maxFeePerGas, @@ -496,7 +503,8 @@ export class Executor { const wallets = Array.from(allWallets) - const gasPrice = await this.gasPriceManager.getNetworkGasPrice() + const gasPrice = await this.gasPriceManager.tryGetNetworkGasPrice() + const promises = wallets.map((wallet) => { try { flushStuckTransaction( @@ -722,7 +730,7 @@ export class Executor { let gasPriceParameters: GasPriceParameters try { ;[gasPriceParameters, nonce] = await Promise.all([ - this.gasPriceManager.getNetworkGasPrice(), + this.gasPriceManager.tryGetNetworkGasPrice(), this.config.publicClient.getTransactionCount({ address: wallet.address, blockTag: "pending" @@ -1021,7 +1029,7 @@ export class Executor { let gasPriceParameters: GasPriceParameters try { ;[gasPriceParameters, nonce] = await Promise.all([ - this.gasPriceManager.getNetworkGasPrice(), + this.gasPriceManager.tryGetNetworkGasPrice(), this.config.publicClient.getTransactionCount({ address: wallet.address, blockTag: "pending" diff --git a/src/executor/executorManager.ts b/src/executor/executorManager.ts index 6fcf22b6..a41f47f7 100644 --- a/src/executor/executorManager.ts +++ b/src/executor/executorManager.ts @@ -776,7 +776,7 @@ export class ExecutorManager { // for all still not included check if needs to be replaced (based on gas price) const gasPriceParameters = - await this.gasPriceManager.getNetworkGasPrice() + await this.gasPriceManager.tryGetNetworkGasPrice() this.logger.trace( { gasPriceParameters }, "fetched gas price parameters" diff --git a/src/executor/senderManager.ts b/src/executor/senderManager.ts index 35dfa07a..279c2015 100644 --- a/src/executor/senderManager.ts +++ b/src/executor/senderManager.ts @@ -138,7 +138,7 @@ export class SenderManager { if (Object.keys(balancesMissing).length > 0) { const { maxFeePerGas, maxPriorityFeePerGas } = - await this.gasPriceManager.getNetworkGasPrice() + await this.gasPriceManager.tryGetNetworkGasPrice() if (this.config.refillHelperContract) { const instructions = [] diff --git a/src/handlers/gasPriceManager.ts b/src/handlers/gasPriceManager.ts index 546bd0d9..c933c25a 100644 --- a/src/handlers/gasPriceManager.ts +++ b/src/handlers/gasPriceManager.ts @@ -71,7 +71,7 @@ export class GasPriceManager { this.updateBaseFee() } - this.updateGasPrice() + this.tryUpdateGasPrice() }, this.config.gasPriceRefreshInterval * 1000) } @@ -81,7 +81,7 @@ export class GasPriceManager { public init() { return Promise.all([ - this.updateGasPrice(), + this.tryUpdateGasPrice(), this.config.legacyTransactions === false ? this.updateBaseFee() : Promise.resolve() @@ -296,6 +296,7 @@ export class GasPriceManager { return { maxFeePerGas, maxPriorityFeePerGas } } + // This method throws if it can't get a valid RPC response. private async innerGetGasPrice(): Promise { let maxFeePerGas = 0n let maxPriorityFeePerGas = 0n @@ -386,7 +387,8 @@ export class GasPriceManager { return baseFee } - private async updateGasPrice(): Promise { + // This method throws if it can't get a valid RPC response. + private async tryUpdateGasPrice(): Promise { const gasPrice = await this.innerGetGasPrice() this.maxFeePerGasQueue.saveValue(gasPrice.maxFeePerGas) @@ -397,7 +399,7 @@ export class GasPriceManager { public async getGasPrice(): Promise { if (this.config.gasPriceRefreshInterval === 0) { - return await this.updateGasPrice() + return await this.tryUpdateGasPrice() } const maxFeePerGas = this.maxFeePerGasQueue.getLatestValue() @@ -414,7 +416,8 @@ export class GasPriceManager { } } - public async getNetworkGasPrice(): Promise { + // This method throws if it can't get a valid RPC response. + public async tryGetNetworkGasPrice(): Promise { return await this.innerGetGasPrice() }