Skip to content

Commit de05929

Browse files
committed
unused nonce
1 parent 4df4eda commit de05929

File tree

7 files changed

+47
-132
lines changed

7 files changed

+47
-132
lines changed

src/server/routes/backend-wallet/transfer.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ import {
2929
} from "../../schemas/wallet";
3030
import { getChainIdFromChain } from "../../utils/chain";
3131

32-
// INPUTS
3332
const requestSchema = Type.Omit(walletWithAddressParamSchema, [
3433
"walletAddress",
3534
]);
3635
const requestBodySchema = Type.Object({
3736
to: Type.String({
38-
description: "Address of the wallet to transfer to",
37+
description: "The recipient wallet address.",
3938
}),
4039
currencyAddress: Type.String({
41-
description: "Address of the token to transfer",
40+
description:
41+
"The token address to transfer. Set to for the chain's native currency.",
4242
default: constants.AddressZero,
4343
}),
4444
amount: Type.String({
45-
description: "The amount of tokens to transfer",
45+
description:
46+
'The amount in ether to transfer. Example: "0.1" to send 0.1 ETH.',
4647
}),
4748
...txOverridesWithValueSchema.properties,
4849
});
@@ -59,7 +60,7 @@ export async function transfer(fastify: FastifyInstance) {
5960
schema: {
6061
summary: "Transfer tokens",
6162
description:
62-
"Transfer native or ERC20 tokens from this wallet to another wallet",
63+
"Transfer native currency or ERC20 tokens to another wallet.",
6364
tags: ["Backend Wallet"],
6465
operationId: "transfer",
6566
params: requestSchema,

src/worker/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from "./listeners/webhookListener";
1010

1111
// Init workers.
12-
import "./tasks/cancelTransactionWorker";
1312
import "./tasks/cancelUnusedNoncesWorker";
1413
import "./tasks/mineTransactionWorker";
1514
import "./tasks/processEventLogsWorker";

src/worker/queues/cancelTransactionQueue.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/worker/queues/mineTransactionQueue.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class MineTransactionQueue {
1717
// Delay confirming the tx by 500ms.
1818
delay: 500,
1919
// Check in 5s, 10s, 20s, 40s, 80s, 160s, 320s, 640s, 1280s, 2560s (~45 minutes)
20+
// This needs to be long enough to handle transactions stuck in mempool for a while.
2021
attempts: 10,
2122
backoff: { type: "exponential", delay: 5_000 },
2223
},

src/worker/tasks/cancelTransactionWorker.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/worker/tasks/cancelUnusedNoncesWorker.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ const handler: Processor<any, void, string> = async (_: Job<string>) => {
3030
nonce,
3131
});
3232
success.push(nonce);
33-
} catch (e) {
33+
} catch (e: unknown) {
34+
// If the error suggests the nonce is already used, do not release the nonce.
35+
if (isNonceUsedOnchain(e)) {
36+
success.push(nonce);
37+
continue;
38+
}
39+
40+
// Otherwise release the nonce so it can be re-used or cancelled again later.
3441
await releaseNonce(chainId, walletAddress, nonce);
3542
fail.push(nonce);
3643
}
@@ -65,6 +72,20 @@ const getAndDeleteUnusedNonces = async (key: string) => {
6572
return results.map(parseInt);
6673
};
6774

75+
/**
76+
* Returns true if the error suggests the nonce is used onchain.
77+
* @param error
78+
* @returns
79+
*/
80+
const isNonceUsedOnchain = (error: unknown) => {
81+
if (error instanceof Error) {
82+
if (error.message.toLowerCase().includes("nonce too low")) {
83+
return true;
84+
}
85+
}
86+
return false;
87+
};
88+
6889
// Worker
6990
const _worker = new Worker(CANCEL_UNUSED_NONCES_QUEUE_NAME, handler, {
7091
concurrency: 1,

src/worker/tasks/mineTransactionWorker.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { stringify } from "thirdweb/utils";
1010
import { getUserOpReceiptRaw } from "thirdweb/wallets/smart";
1111
import { TransactionDB } from "../../db/transactions/db";
12+
import { releaseNonce } from "../../db/wallets/walletNonce";
1213
import { getBlockNumberish } from "../../utils/block";
1314
import { getConfig } from "../../utils/cache/getConfig";
1415
import { getChain } from "../../utils/chain";
@@ -24,7 +25,6 @@ import {
2425
} from "../../utils/transaction/types";
2526
import { enqueueTransactionWebhook } from "../../utils/transaction/webhook";
2627
import { reportUsage } from "../../utils/usage";
27-
import { CancelTransactionQueue } from "../queues/cancelTransactionQueue";
2828
import {
2929
MineTransactionData,
3030
MineTransactionQueue,
@@ -227,23 +227,24 @@ _worker.on("failed", async (job: Job<string> | undefined) => {
227227
return;
228228
}
229229

230-
if (sentTransaction.isUserOp) {
231-
// If userOp, set as errored.
232-
job.log("Transaction is unmined after timeout. Erroring transaction...");
233-
const erroredTransaction: ErroredTransaction = {
234-
...sentTransaction,
235-
status: "errored",
236-
errorMessage: "Transaction Timed out.",
237-
};
238-
await TransactionDB.set(erroredTransaction);
239-
await enqueueTransactionWebhook(erroredTransaction);
240-
_reportUsageError(erroredTransaction);
241-
} else {
242-
// If EOA transaction, enqueue a cancel job.
243-
job.log(
244-
"Transaction is unmined after timeout. Cancelling transaction...",
230+
job.log("Transaction is unmined after timeout. Erroring transaction...");
231+
const erroredTransaction: ErroredTransaction = {
232+
...sentTransaction,
233+
status: "errored",
234+
errorMessage: "Transaction timed out.",
235+
};
236+
await TransactionDB.set(erroredTransaction);
237+
await enqueueTransactionWebhook(erroredTransaction);
238+
_reportUsageError(erroredTransaction);
239+
240+
if (!sentTransaction.isUserOp) {
241+
// Release the nonce to allow another transaction to acquire it.
242+
job.log(`Releasing nonce ${sentTransaction.nonce}.`);
243+
await releaseNonce(
244+
sentTransaction.chainId,
245+
sentTransaction.from,
246+
sentTransaction.nonce,
245247
);
246-
await CancelTransactionQueue.add({ queueId });
247248
}
248249
}
249250
});

0 commit comments

Comments
 (0)