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

Fix op-stack PVG calculations #397

Merged
merged 3 commits into from
Jan 10, 2025
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
24 changes: 24 additions & 0 deletions src/handlers/gasPriceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { AltoConfig } from "../createConfig"
import { SlidingWindowTimedQueue } from "../utils/slidingWindowTimedQueue"
import { ArbitrumManager } from "./arbitrumGasPriceManager"
import { MantleManager } from "./mantleGasPriceManager"
import { OptimismManager } from "./optimismManager"

enum ChainId {
Goerli = 5,
Expand Down Expand Up @@ -48,6 +49,7 @@ export class GasPriceManager {

public arbitrumManager: ArbitrumManager
public mantleManager: MantleManager
public optimismManager: OptimismManager

constructor(config: AltoConfig) {
this.config = config
Expand Down Expand Up @@ -79,6 +81,7 @@ export class GasPriceManager {

this.arbitrumManager = new ArbitrumManager(queueValidity)
this.mantleManager = new MantleManager(queueValidity)
this.optimismManager = new OptimismManager(queueValidity)
}

public init() {
Expand Down Expand Up @@ -432,6 +435,27 @@ export class GasPriceManager {
return maxBaseFeePerGas
}

public async getHighestMaxFeePerGas(): Promise<bigint> {
let highestMaxFeePerGas = this.maxFeePerGasQueue.getMaxValue()
if (!highestMaxFeePerGas) {
const gasPrice = await this.getGasPrice()
highestMaxFeePerGas = gasPrice.maxFeePerGas
}

return highestMaxFeePerGas
}

public async getHighestMaxPriorityFeePerGas(): Promise<bigint> {
let highestMaxPriorityFeePerGas =
this.maxPriorityFeePerGasQueue.getMaxValue()
if (!highestMaxPriorityFeePerGas) {
const gasPrice = await this.getGasPrice()
highestMaxPriorityFeePerGas = gasPrice.maxPriorityFeePerGas
}

return highestMaxPriorityFeePerGas
}

private async getMinMaxFeePerGas(): Promise<bigint> {
let minMaxFeePerGas = this.maxFeePerGasQueue.getMinValue()
if (!minMaxFeePerGas) {
Expand Down
17 changes: 17 additions & 0 deletions src/handlers/optimismManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SlidingWindowTimedQueue } from "../utils/slidingWindowTimedQueue"

export class OptimismManager {
private l1FeeQueue: SlidingWindowTimedQueue

constructor(queueValidity: number) {
this.l1FeeQueue = new SlidingWindowTimedQueue(queueValidity)
}

public getMinL1Fee() {
return this.l1FeeQueue.getMinValue() || 1n
}

public saveL1FeeValue(l1Fee: bigint) {
this.l1FeeQueue.saveValue(l1Fee)
}
}
18 changes: 15 additions & 3 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,22 +615,34 @@ export async function calcOptimismPreVerificationGas(
})

const [l1Fee, baseFeePerGas] = await Promise.all([
opGasPriceOracle.read.getL1Fee([serializedTx]),
validate
? gasPriceManager.optimismManager.getMinL1Fee()
: opGasPriceOracle.read.getL1Fee([serializedTx]),
validate
? gasPriceManager.getMaxBaseFeePerGas()
: gasPriceManager.getBaseFee()
])

if (!validate) {
gasPriceManager.optimismManager.saveL1FeeValue(l1Fee)
}

if (op.maxFeePerGas <= 1n || op.maxPriorityFeePerGas <= 1n) {
// if user didn't provide gasPrice values, fetch current going price rate
const gasPrices = await gasPriceManager.getGasPrice()
op.maxPriorityFeePerGas = gasPrices.maxPriorityFeePerGas
op.maxFeePerGas = gasPrices.maxFeePerGas
}

const l2MaxFee = op.maxFeePerGas
const l2MaxFee = validate
? await gasPriceManager.getHighestMaxFeePerGas()
: op.maxFeePerGas

const l2PriorityFee = baseFeePerGas + op.maxPriorityFeePerGas
const l2PriorityFee =
baseFeePerGas +
(validate
? await gasPriceManager.getHighestMaxPriorityFeePerGas()
: op.maxPriorityFeePerGas)

const l2price = minBigInt(l2MaxFee, l2PriorityFee)

Expand Down
Loading