Skip to content

Commit

Permalink
Force latest gas price when bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
plusminushalf committed Sep 11, 2024
1 parent 9d7417a commit a9329f8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
16 changes: 12 additions & 4 deletions src/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ export class Executor {
): Promise<ReplaceTransactionResult> {
const newRequest = { ...transactionInfo.transactionRequest }

const gasPriceParameters = await this.gasPriceManager.getGasPrice()
const gasPriceParameters = await this.gasPriceManager.getGasPrice({
forceLatest: true
})

newRequest.maxFeePerGas = maxBigInt(
gasPriceParameters.maxFeePerGas,
Expand Down Expand Up @@ -496,7 +498,9 @@ export class Executor {

const wallets = Array.from(allWallets)

const gasPrice = await this.gasPriceManager.getGasPrice()
const gasPrice = await this.gasPriceManager.getGasPrice({
forceLatest: true
})
const promises = wallets.map((wallet) => {
flushStuckTransaction(
this.publicClient,
Expand Down Expand Up @@ -557,7 +561,9 @@ export class Executor {
})
childLogger.debug("bundling user operation")

const gasPriceParameters = await this.gasPriceManager.getGasPrice()
const gasPriceParameters = await this.gasPriceManager.getGasPrice({
forceLatest: true
})
childLogger.debug({ gasPriceParameters }, "got gas price")

const nonce = await this.publicClient.getTransactionCount({
Expand Down Expand Up @@ -842,7 +848,9 @@ export class Executor {
})
childLogger.debug("bundling compressed user operation")

const gasPriceParameters = await this.gasPriceManager.getGasPrice()
const gasPriceParameters = await this.gasPriceManager.getGasPrice({
forceLatest: true
})
childLogger.debug({ gasPriceParameters }, "got gas price")

const nonce = await this.publicClient.getTransactionCount({
Expand Down
59 changes: 30 additions & 29 deletions src/handlers/gasPriceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
type GasPriceParameters
} from "@alto/types"
import { maxBigInt, minBigInt, type Logger } from "@alto/utils"
// biome-ignore lint/style/noNamespaceImport: explicitly make it clear when sentry is used
import * as sentry from "@sentry/node"
import { parseGwei, type Chain, type PublicClient } from "viem"
import {
Expand Down Expand Up @@ -72,23 +71,22 @@ export class GasPriceManager {

// Periodically update gas prices if specified
if (this.gasPriceRefreshIntervalInSeconds > 0) {
setInterval(
() => {
if (this.legacyTransactions === false) {
this.updateBaseFee()
}
setInterval(() => {
if (this.legacyTransactions === false) {
this.updateBaseFee()
}

this.updateGasPrice()
},
this.gasPriceRefreshIntervalInSeconds * 1000
);
this.updateGasPrice()
}, this.gasPriceRefreshIntervalInSeconds * 1000)
}
}

public async init() {
public init() {
return Promise.all([
this.updateGasPrice(),
this.legacyTransactions === false ? this.updateBaseFee() : Promise.resolve()
this.legacyTransactions === false
? this.updateBaseFee()
: Promise.resolve()
])
}

Expand Down Expand Up @@ -452,18 +450,19 @@ export class GasPriceManager {

public async getBaseFee(): Promise<bigint> {
if (this.legacyTransactions) {
throw new RpcError("baseFee is not available for legacy transactions")
throw new RpcError(
"baseFee is not available for legacy transactions"
)
}

if (this.gasPriceRefreshIntervalInSeconds === 0) {
return this.updateBaseFee()
return await this.updateBaseFee()
}

const {
baseFeePerGas
} = this.queueBaseFeePerGas[this.queueBaseFeePerGas.length - 1];
const { baseFeePerGas } =
this.queueBaseFeePerGas[this.queueBaseFeePerGas.length - 1]

return baseFeePerGas;
return baseFeePerGas
}

private async updateGasPrice(): Promise<GasPriceParameters> {
Expand All @@ -478,24 +477,26 @@ export class GasPriceManager {
)

return gasPrice
}
}

public async getGasPrice(): Promise<GasPriceParameters> {
if (this.gasPriceRefreshIntervalInSeconds === 0) {
return this.updateGasPrice()
public async getGasPrice({
forceLatest = false
}: { forceLatest?: boolean } = {}): Promise<GasPriceParameters> {
if (this.gasPriceRefreshIntervalInSeconds === 0 || forceLatest) {
return await this.updateGasPrice()
}

const {
maxPriorityFeePerGas
} = this.queueMaxPriorityFeePerGas[this.queueMaxPriorityFeePerGas.length - 1];
const { maxPriorityFeePerGas } =
this.queueMaxPriorityFeePerGas[
this.queueMaxPriorityFeePerGas.length - 1
]

const {
maxFeePerGas
} = this.queueMaxFeePerGas[this.queueMaxFeePerGas.length - 1];
const { maxFeePerGas } =
this.queueMaxFeePerGas[this.queueMaxFeePerGas.length - 1]

return {
maxFeePerGas,
maxPriorityFeePerGas,
maxPriorityFeePerGas
}
}

Expand Down

0 comments on commit a9329f8

Please sign in to comment.