Skip to content

Commit 828aa01

Browse files
committed
feat(bot-oo): detect nonce backlog and exit early
Add detectNonceBacklog() helper to shared transactionClearing module. bot-oo now checks for pending transactions before each iteration and exits early if a backlog is detected to avoid compounding nonce issues. Signed-off-by: Pablo Maldonado <pablo@umaproject.org>
1 parent 8744689 commit 828aa01

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

packages/monitor-v2/src/bot-oo/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { delay, waitForLogger, GasEstimator } from "@uma/financial-templates-lib";
22
import { BotModes, initMonitoringParams, Logger, startupLogLevel } from "./common";
33
import { settleRequests } from "./SettleRequests";
4+
import { detectNonceBacklog } from "../bot-utils/transactionClearing";
45

56
const logger = Logger;
67

@@ -22,6 +23,19 @@ async function main() {
2223
};
2324

2425
for (;;) {
26+
// Check for nonce backlog before running settlements
27+
const nonceStatus = await detectNonceBacklog(params.provider, params.signer);
28+
if (nonceStatus.hasBacklog) {
29+
logger.error({
30+
at: "OracleBot",
31+
message: "Nonce backlog detected, skipping settlements to avoid compounding issues",
32+
latestNonce: nonceStatus.latestNonce,
33+
pendingNonce: nonceStatus.pendingNonce,
34+
backlog: nonceStatus.backlog,
35+
});
36+
break;
37+
}
38+
2539
await gasEstimator.update();
2640

2741
const runCmds = Object.entries(cmds)

packages/monitor-v2/src/bot-utils/transactionClearing.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ async function getNonces(provider: Provider, address: string): Promise<{ latestN
7373
return { latestNonce, pendingNonce };
7474
}
7575

76+
export interface NonceBacklogStatus {
77+
hasBacklog: boolean;
78+
latestNonce: number;
79+
pendingNonce: number;
80+
backlog: number;
81+
}
82+
83+
/**
84+
* Detects if there's a nonce backlog (pending transactions that haven't been mined).
85+
*/
86+
export async function detectNonceBacklog(provider: Provider, signer: Signer): Promise<NonceBacklogStatus> {
87+
const address = await signer.getAddress();
88+
const { latestNonce, pendingNonce } = await getNonces(provider, address);
89+
const backlog = pendingNonce - latestNonce;
90+
91+
return {
92+
hasBacklog: backlog > 0,
93+
latestNonce,
94+
pendingNonce,
95+
backlog,
96+
};
97+
}
98+
7699
/**
77100
* Clears stuck transactions by sending self-transactions with higher gas fees.
78101
* @returns true if a nonce backlog was detected and clearing was attempted

0 commit comments

Comments
 (0)