-
Notifications
You must be signed in to change notification settings - Fork 209
feat: adds a standalone transaction-clearer bot for recovering from nonce backlogs. #4915
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
Changes from 3 commits
fa2773f
e448f44
e7077cc
1e03e77
dd0c8b7
8744689
e706178
f4ba316
9eb7296
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import { BigNumber } from "ethers"; | ||
| import type { Logger as LoggerType } from "winston"; | ||
| import type { Provider } from "@ethersproject/abstract-provider"; | ||
| import { GasEstimator } from "@uma/financial-templates-lib"; | ||
| import { MonitoringParams, NonceBacklogConfig } from "./common"; | ||
|
|
||
| type FeeData = { maxFeePerGas: BigNumber; maxPriorityFeePerGas: BigNumber } | { gasPrice: BigNumber }; | ||
|
|
||
| function isLondonFeeData(feeData: FeeData): feeData is { maxFeePerGas: BigNumber; maxPriorityFeePerGas: BigNumber } { | ||
| return "maxFeePerGas" in feeData; | ||
| } | ||
|
|
||
| function bumpFeeData(baseFeeData: FeeData, attemptIndex: number, config: NonceBacklogConfig): FeeData { | ||
| // Calculate multiplier: (numerator/denominator)^(attemptIndex+1) | ||
| // For attempt 0: 1.2x, attempt 1: 1.44x, attempt 2: 1.73x (with default 12/10) | ||
| let numerator = BigNumber.from(config.feeBumpNumerator); | ||
| let denominator = BigNumber.from(config.feeBumpDenominator); | ||
|
|
||
| for (let i = 0; i < attemptIndex; i++) { | ||
| numerator = numerator.mul(config.feeBumpNumerator); | ||
| denominator = denominator.mul(config.feeBumpDenominator); | ||
| } | ||
|
|
||
| if (isLondonFeeData(baseFeeData)) { | ||
| return { | ||
| maxFeePerGas: baseFeeData.maxFeePerGas.mul(numerator).div(denominator), | ||
| maxPriorityFeePerGas: baseFeeData.maxPriorityFeePerGas.mul(numerator).div(denominator), | ||
| }; | ||
| } else { | ||
| return { | ||
| gasPrice: baseFeeData.gasPrice.mul(numerator).div(denominator), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async function getNonces( | ||
| provider: Provider, | ||
| address: string | ||
| ): Promise<{ latestNonce: number; pendingNonce: number }> { | ||
| const [latestNonce, pendingNonce] = await Promise.all([ | ||
| provider.getTransactionCount(address, "latest"), | ||
| provider.getTransactionCount(address, "pending"), | ||
| ]); | ||
| return { latestNonce, pendingNonce }; | ||
| } | ||
|
|
||
| export async function clearStuckTransactions( | ||
| logger: LoggerType, | ||
| params: MonitoringParams, | ||
| gasEstimator: GasEstimator | ||
| ): Promise<void> { | ||
| const { provider, signer, nonceBacklogConfig } = params; | ||
| const botAddress = await signer.getAddress(); | ||
|
|
||
| const { latestNonce, pendingNonce } = await getNonces(provider, botAddress); | ||
| const backlog = pendingNonce - latestNonce; | ||
|
|
||
| if (backlog < nonceBacklogConfig.nonceBacklogThreshold) { | ||
| logger.debug({ | ||
| at: "TransactionClearer", | ||
| message: "No nonce backlog detected", | ||
| botAddress, | ||
| latestNonce, | ||
| pendingNonce, | ||
| backlog, | ||
| threshold: nonceBacklogConfig.nonceBacklogThreshold, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| logger.warn({ | ||
| at: "TransactionClearer", | ||
| message: "Nonce backlog detected, attempting to clear stuck transactions", | ||
| botAddress, | ||
| latestNonce, | ||
| pendingNonce, | ||
| backlog, | ||
| threshold: nonceBacklogConfig.nonceBacklogThreshold, | ||
| }); | ||
|
|
||
| // Get base fee data from gas estimator | ||
| const baseFeeData = gasEstimator.getCurrentFastPriceEthers(); | ||
|
|
||
| // Clear all stuck nonces from latestNonce to pendingNonce - 1 | ||
| for (let nonce = latestNonce; nonce < pendingNonce; nonce++) { | ||
| let cleared = false; | ||
|
|
||
| for (let attempt = 0; attempt < nonceBacklogConfig.replacementAttempts; attempt++) { | ||
| const feeData = bumpFeeData(baseFeeData, attempt, nonceBacklogConfig); | ||
|
|
||
| try { | ||
| logger.info({ | ||
| at: "TransactionClearer", | ||
| message: `Attempting to clear stuck transaction (nonce ${nonce}, attempt ${attempt + 1})`, | ||
| botAddress, | ||
| nonce, | ||
| attempt: attempt + 1, | ||
| feeData: isLondonFeeData(feeData) | ||
| ? { | ||
| maxFeePerGas: feeData.maxFeePerGas.toString(), | ||
| maxPriorityFeePerGas: feeData.maxPriorityFeePerGas.toString(), | ||
| } | ||
| : { gasPrice: feeData.gasPrice.toString() }, | ||
| }); | ||
|
|
||
| const tx = await signer.sendTransaction({ | ||
| to: botAddress, // Self-transaction | ||
| value: 0, | ||
| nonce, | ||
| gasLimit: 21_000, | ||
| ...feeData, | ||
| }); | ||
|
|
||
| const receipt = await tx.wait(1); | ||
|
|
||
| logger.info({ | ||
| at: "TransactionClearer", | ||
| message: `Successfully cleared stuck transaction (nonce ${nonce})`, | ||
| botAddress, | ||
| nonce, | ||
| transactionHash: receipt.transactionHash, | ||
| gasUsed: receipt.gasUsed.toString(), | ||
| }); | ||
|
|
||
| cleared = true; | ||
| break; | ||
| } catch (error) { | ||
| logger.warn({ | ||
| at: "TransactionClearer", | ||
| message: `Failed to clear stuck transaction (nonce ${nonce}, attempt ${attempt + 1})`, | ||
| botAddress, | ||
| nonce, | ||
| attempt: attempt + 1, | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (!cleared) { | ||
| logger.error({ | ||
| at: "TransactionClearer", | ||
| message: `Failed to clear stuck transaction after all attempts (nonce ${nonce})`, | ||
| botAddress, | ||
| nonce, | ||
| maxAttempts: nonceBacklogConfig.replacementAttempts, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Verify final state | ||
| const { latestNonce: finalLatestNonce, pendingNonce: finalPendingNonce } = await getNonces(provider, botAddress); | ||
| const finalBacklog = finalPendingNonce - finalLatestNonce; | ||
|
|
||
| if (finalBacklog < nonceBacklogConfig.nonceBacklogThreshold) { | ||
| logger.info({ | ||
| at: "TransactionClearer", | ||
| message: "Successfully cleared nonce backlog", | ||
| botAddress, | ||
| previousBacklog: backlog, | ||
| finalBacklog, | ||
| }); | ||
| } else { | ||
| logger.warn({ | ||
| at: "TransactionClearer", | ||
| message: "Nonce backlog still present after clearing attempt", | ||
| botAddress, | ||
| previousBacklog: backlog, | ||
| finalBacklog, | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| export { Logger } from "@uma/financial-templates-lib"; | ||
| import { BaseMonitoringParams, initBaseMonitoringParams, startupLogLevel as baseStartup } from "../bot-utils/base"; | ||
|
|
||
| export interface NonceBacklogConfig { | ||
| // Minimum nonce difference (pending - latest) to trigger clearing | ||
| nonceBacklogThreshold: number; | ||
| // Fee bump multiplier: bumpedFee = fee * numerator / denominator | ||
| feeBumpNumerator: number; | ||
| feeBumpDenominator: number; | ||
| // Max attempts to replace a stuck transaction with increasing fees | ||
| replacementAttempts: number; | ||
| } | ||
|
|
||
| export interface MonitoringParams extends BaseMonitoringParams { | ||
| nonceBacklogConfig: NonceBacklogConfig; | ||
| } | ||
|
|
||
| export const initMonitoringParams = async (env: NodeJS.ProcessEnv): Promise<MonitoringParams> => { | ||
| const base = await initBaseMonitoringParams(env); | ||
|
|
||
| const nonceBacklogConfig: NonceBacklogConfig = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| nonceBacklogThreshold: Number(env.NONCE_BACKLOG_THRESHOLD) || 1, | ||
| feeBumpNumerator: Number(env.FEE_BUMP_NUMERATOR) || 12, | ||
| feeBumpDenominator: Number(env.FEE_BUMP_DENOMINATOR) || 10, | ||
| replacementAttempts: Number(env.REPLACEMENT_ATTEMPTS) || 3, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: these are named differently in |
||
| }; | ||
|
|
||
| return { | ||
| ...base, | ||
| nonceBacklogConfig, | ||
| }; | ||
| }; | ||
|
|
||
| export const startupLogLevel = baseStartup; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be more maintainable in the future if we have a single tx clearing module here and
bot-oowould just reuse it