Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add try/catch around getGasPrice #370

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading