Skip to content

Commit

Permalink
add try/catch around getGasPrice (#370)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mouseless0x and mouseless0x authored Dec 4, 2024
1 parent a8bfdc7 commit ea121ff
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
18 changes: 13 additions & 5 deletions src/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,15 @@ export class Executor {
): Promise<ReplaceTransactionResult> {
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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/executor/executorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/executor/senderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
13 changes: 8 additions & 5 deletions src/handlers/gasPriceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class GasPriceManager {
this.updateBaseFee()
}

this.updateGasPrice()
this.tryUpdateGasPrice()
}, this.config.gasPriceRefreshInterval * 1000)
}

Expand All @@ -81,7 +81,7 @@ export class GasPriceManager {

public init() {
return Promise.all([
this.updateGasPrice(),
this.tryUpdateGasPrice(),
this.config.legacyTransactions === false
? this.updateBaseFee()
: Promise.resolve()
Expand Down Expand Up @@ -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<GasPriceParameters> {
let maxFeePerGas = 0n
let maxPriorityFeePerGas = 0n
Expand Down Expand Up @@ -386,7 +387,8 @@ export class GasPriceManager {
return baseFee
}

private async updateGasPrice(): Promise<GasPriceParameters> {
// This method throws if it can't get a valid RPC response.
private async tryUpdateGasPrice(): Promise<GasPriceParameters> {
const gasPrice = await this.innerGetGasPrice()

this.maxFeePerGasQueue.saveValue(gasPrice.maxFeePerGas)
Expand All @@ -397,7 +399,7 @@ export class GasPriceManager {

public async getGasPrice(): Promise<GasPriceParameters> {
if (this.config.gasPriceRefreshInterval === 0) {
return await this.updateGasPrice()
return await this.tryUpdateGasPrice()
}

const maxFeePerGas = this.maxFeePerGasQueue.getLatestValue()
Expand All @@ -414,7 +416,8 @@ export class GasPriceManager {
}
}

public async getNetworkGasPrice(): Promise<GasPriceParameters> {
// This method throws if it can't get a valid RPC response.
public async tryGetNetworkGasPrice(): Promise<GasPriceParameters> {
return await this.innerGetGasPrice()
}

Expand Down

0 comments on commit ea121ff

Please sign in to comment.