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 sliding window to arbitrum preVerificationGas calculation #303

Merged
merged 3 commits into from
Sep 26, 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
76 changes: 75 additions & 1 deletion src/handlers/gasPriceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@alto/types"
import { maxBigInt, minBigInt, type Logger } from "@alto/utils"
import * as sentry from "@sentry/node"
import { parseGwei, type Chain, type PublicClient } from "viem"
import { parseGwei, type Chain, type PublicClient, maxUint128 } from "viem"
import {
celo,
celoAlfajores,
Expand Down Expand Up @@ -36,6 +36,77 @@ function getGasStationUrl(chainId: ChainId.Polygon | ChainId.Mumbai): string {
}
}

class ArbitrumManager {
private queueL1BaseFee: { timestamp: number; baseFee: bigint }[]
private queueL2BaseFee: { timestamp: number; baseFee: bigint }[]

private maxQueueSize
private queueValidity = 15_000

constructor(maxQueueSize: number) {
this.maxQueueSize = maxQueueSize
this.queueL1BaseFee = []
this.queueL2BaseFee = []
}

public saveL1BaseFee(baseFee: bigint) {
const queue = this.queueL1BaseFee
const last = queue.length > 0 ? queue[queue.length - 1] : null
const timestamp = Date.now()

if (!last || timestamp - last.timestamp >= this.queueValidity) {
if (queue.length >= this.maxQueueSize) {
queue.shift()
}
queue.push({ baseFee, timestamp })
} else if (baseFee < last.baseFee) {
last.baseFee = baseFee
last.timestamp = timestamp
}
}

public saveL2BaseFee(baseFee: bigint) {
const queue = this.queueL2BaseFee
const last = queue.length > 0 ? queue[queue.length - 1] : null
const timestamp = Date.now()

if (!last || timestamp - last.timestamp >= this.queueValidity) {
if (queue.length >= this.maxQueueSize) {
queue.shift()
}
queue.push({ baseFee, timestamp })
} else if (baseFee < last.baseFee) {
last.baseFee = baseFee
last.timestamp = timestamp
}
}

public async getMinL1BaseFee() {
const queue = this.queueL1BaseFee

if (queue.length === 0) {
return 1n
}
return queue.reduce(
(acc: bigint, cur) => minBigInt(cur.baseFee, acc),
queue[0].baseFee
)
}

public async getMaxL2BaseFee() {
const queue = this.queueL2BaseFee

if (queue.length === 0) {
return maxUint128
}

return queue.reduce(
(acc: bigint, cur) => maxBigInt(cur.baseFee, acc),
queue[0].baseFee
)
}
}

export class GasPriceManager {
private chain: Chain
private publicClient: PublicClient
Expand All @@ -53,6 +124,7 @@ export class GasPriceManager {
private gasBumpMultiplier: bigint
private gasPriceRefreshIntervalInSeconds: number
private chainType: ChainType
public arbitrumManager: ArbitrumManager

constructor(
chain: Chain,
Expand Down Expand Up @@ -83,6 +155,8 @@ export class GasPriceManager {
this.updateGasPrice()
}, this.gasPriceRefreshIntervalInSeconds * 1000)
}

this.arbitrumManager = new ArbitrumManager(this.maxQueueSize)
}

public init() {
Expand Down
27 changes: 24 additions & 3 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ export async function calcPreVerificationGas(
publicClient,
userOperation,
entryPoint,
preVerificationGas
preVerificationGas,
gasPriceManager,
validate
)
}

Expand Down Expand Up @@ -583,7 +585,9 @@ export async function calcArbitrumPreVerificationGas(
publicClient: PublicClient<Transport, Chain | undefined>,
op: UserOperation,
entryPoint: Address,
staticFee: bigint
staticFee: bigint,
gasPriceManager: GasPriceManager,
validate: boolean
) {
let selector: Hex
let paramData: Hex
Expand Down Expand Up @@ -649,7 +653,24 @@ export async function calcArbitrumPreVerificationGas(
serializedTx
])

return result[0] + staticFee
let [gasForL1, l2BaseFee, l1BaseFeeEstimate] = result

gasPriceManager.arbitrumManager.saveL1BaseFee(l1BaseFeeEstimate)
gasPriceManager.arbitrumManager.saveL2BaseFee(l2BaseFee)

if (validate) {
// gasEstimateL1Component source: https://github.com/OffchainLabs/nitro/blob/5cd7d6913eb6b4dedb08f6ea49d7f9802d2cc5b9/execution/nodeInterface/NodeInterface.go#L515-L551
const feesForL1 = (gasForL1 * l2BaseFee) / l1BaseFeeEstimate

const minL1BaseFeeEstimate =
await gasPriceManager.arbitrumManager.getMinL1BaseFee()
const maxL2BaseFee =
await gasPriceManager.arbitrumManager.getMaxL2BaseFee()

gasForL1 = (feesForL1 * minL1BaseFeeEstimate) / maxL2BaseFee
}

return staticFee + gasForL1
}

export function parseViemError(err: unknown) {
Expand Down
Loading