Skip to content

Commit

Permalink
fix PVG race case
Browse files Browse the repository at this point in the history
  • Loading branch information
mouseless0x committed Jan 1, 2025
1 parent 1340c8c commit 00d7e69
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 87 deletions.
10 changes: 5 additions & 5 deletions src/handlers/arbitrumGasPriceManager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { maxUint128 } from "viem"
import { TimedQueue } from "../utils/timedQueue"
import { SlidingWindowTimedQueue } from "../utils/slidingWindowTimedQueue"

export class ArbitrumManager {
private l1BaseFeeQueue: TimedQueue
private l2BaseFeeQueue: TimedQueue
private l1BaseFeeQueue: SlidingWindowTimedQueue
private l2BaseFeeQueue: SlidingWindowTimedQueue

constructor(queueValidity: number) {
this.l1BaseFeeQueue = new TimedQueue(queueValidity)
this.l2BaseFeeQueue = new TimedQueue(queueValidity)
this.l1BaseFeeQueue = new SlidingWindowTimedQueue(queueValidity)
this.l2BaseFeeQueue = new SlidingWindowTimedQueue(queueValidity)
}

public saveL1BaseFee(baseFee: bigint) {
Expand Down
16 changes: 9 additions & 7 deletions src/handlers/gasPriceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
polygonMumbai
} from "viem/chains"
import type { AltoConfig } from "../createConfig"
import { TimedQueue } from "../utils/timedQueue"
import { SlidingWindowTimedQueue } from "../utils/slidingWindowTimedQueue"
import { ArbitrumManager } from "./arbitrumGasPriceManager"
import { MantleManager } from "./mantleGasPriceManager"

Expand All @@ -41,9 +41,9 @@ function getGasStationUrl(chainId: ChainId.Polygon | ChainId.Mumbai): string {

export class GasPriceManager {
private readonly config: AltoConfig
private baseFeePerGasQueue: TimedQueue
private maxFeePerGasQueue: TimedQueue
private maxPriorityFeePerGasQueue: TimedQueue
private baseFeePerGasQueue: SlidingWindowTimedQueue
private maxFeePerGasQueue: SlidingWindowTimedQueue
private maxPriorityFeePerGasQueue: SlidingWindowTimedQueue
private logger: Logger

public arbitrumManager: ArbitrumManager
Expand All @@ -60,9 +60,11 @@ export class GasPriceManager {

const queueValidity = this.config.gasPriceExpiry * 1_000

this.baseFeePerGasQueue = new TimedQueue(queueValidity)
this.maxFeePerGasQueue = new TimedQueue(queueValidity)
this.maxPriorityFeePerGasQueue = new TimedQueue(queueValidity)
this.baseFeePerGasQueue = new SlidingWindowTimedQueue(queueValidity)
this.maxFeePerGasQueue = new SlidingWindowTimedQueue(queueValidity)
this.maxPriorityFeePerGasQueue = new SlidingWindowTimedQueue(
queueValidity
)

// Periodically update gas prices if specified
if (this.config.gasPriceRefreshInterval > 0) {
Expand Down
20 changes: 11 additions & 9 deletions src/handlers/mantleGasPriceManager.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { TimedQueue } from "../utils/timedQueue"
import { SlidingWindowTimedQueue } from "../utils/slidingWindowTimedQueue"

export class MantleManager {
private tokenRatioQueue: TimedQueue
private scalarQueue: TimedQueue
private rollupDataGasAndOverheadQueue: TimedQueue
private l1GasPriceQueue: TimedQueue
private tokenRatioQueue: SlidingWindowTimedQueue
private scalarQueue: SlidingWindowTimedQueue
private rollupDataGasAndOverheadQueue: SlidingWindowTimedQueue
private l1GasPriceQueue: SlidingWindowTimedQueue

constructor(queueValidity: number) {
this.tokenRatioQueue = new TimedQueue(queueValidity)
this.scalarQueue = new TimedQueue(queueValidity)
this.rollupDataGasAndOverheadQueue = new TimedQueue(queueValidity)
this.l1GasPriceQueue = new TimedQueue(queueValidity)
this.tokenRatioQueue = new SlidingWindowTimedQueue(queueValidity)
this.scalarQueue = new SlidingWindowTimedQueue(queueValidity)
this.l1GasPriceQueue = new SlidingWindowTimedQueue(queueValidity)
this.rollupDataGasAndOverheadQueue = new SlidingWindowTimedQueue(
queueValidity
)
}

public getMinMantleOracleValues() {
Expand Down
65 changes: 65 additions & 0 deletions src/utils/slidingWindowTimedQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export class SlidingWindowTimedQueue {
// Element 0 will always be the min.
private minDeque: { timestamp: number; value: bigint }[]
// Element 0 will always be the max.
private maxDeque: { timestamp: number; value: bigint }[]
private latestValue: bigint | null
private queueValidityMs: number

constructor(queueValidityMs: number) {
this.minDeque = []
this.maxDeque = []
this.latestValue = null
this.queueValidityMs = queueValidityMs
}

public saveValue(value: bigint) {
if (value === 0n) {
return
}

const timestamp = Date.now()

// Remove expired entries.
const cutoffTime = timestamp - this.queueValidityMs
this.minDeque = this.minDeque.filter(
(entry) => entry.timestamp >= cutoffTime
)
this.maxDeque = this.maxDeque.filter(
(entry) => entry.timestamp >= cutoffTime
)

// Maintain the min deque by removing all elements from the back that are larger then the new value.
while (
this.minDeque.length &&
this.minDeque[this.minDeque.length - 1].value >= value
) {
this.minDeque.pop()
}
this.minDeque.push({ value, timestamp })

// Maintain the max deque by removing all elements from the back that are smaller then the new value.
while (
this.maxDeque.length &&
this.maxDeque[this.maxDeque.length - 1].value <= value
) {
this.maxDeque.pop()
}
this.maxDeque.push({ value, timestamp })

// Record the latest value.
this.latestValue = value
}

public getLatestValue(): bigint | null {
return this.latestValue
}

public getMinValue() {
return this.minDeque.length ? this.minDeque[0].value : null
}

public getMaxValue() {
return this.maxDeque.length ? this.maxDeque[0].value : null
}
}
66 changes: 0 additions & 66 deletions src/utils/timedQueue.ts

This file was deleted.

0 comments on commit 00d7e69

Please sign in to comment.