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

Force latest gas price when bundling #293

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading