Skip to content

Commit 71fb8b0

Browse files
authored
fix: Reset nonce if out of funds (#807)
* fix: Reset nonce if out of funds * rename test files too
1 parent 04fdca0 commit 71fb8b0

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

src/shared/utils/error.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ const _parseMessage = (error: unknown): string | null => {
1616

1717
export const isNonceAlreadyUsedError = (error: unknown) => {
1818
const message = _parseMessage(error);
19-
const errorPhrases = ["nonce too low", "already known"];
2019

2120
if (message) {
22-
return errorPhrases.some((phrase) =>
23-
message.toLowerCase().includes(phrase),
21+
return (
22+
message.includes("nonce too low") || message.includes("already known")
2423
);
2524
}
2625

@@ -41,10 +40,7 @@ export const isReplacementGasFeeTooLow = (error: unknown) => {
4140
export const isInsufficientFundsError = (error: unknown) => {
4241
const message = _parseMessage(error);
4342
if (message) {
44-
return (
45-
message.includes("insufficient funds for gas * price + value") ||
46-
message.includes("insufficient funds for intrinsic transaction cost")
47-
);
43+
return message.includes("insufficient funds");
4844
}
4945
return isEthersErrorCode(error, ethers.errors.INSUFFICIENT_FUNDS);
5046
};

src/worker/tasks/cancel-recycled-nonces-worker.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { type Job, type Processor, Worker } from "bullmq";
22
import type { Address } from "thirdweb";
3-
import { recycleNonce } from "../../shared/db/wallets/wallet-nonce";
4-
import { isNonceAlreadyUsedError } from "../../shared/utils/error";
3+
import {
4+
deleteNoncesForBackendWallets,
5+
recycleNonce,
6+
} from "../../shared/db/wallets/wallet-nonce";
7+
import {
8+
isInsufficientFundsError,
9+
isNonceAlreadyUsedError,
10+
} from "../../shared/utils/error";
511
import { logger } from "../../shared/utils/logger";
612
import { redis } from "../../shared/utils/redis/redis";
713
import { sendCancellationTransaction } from "../../shared/utils/transaction/cancel-transaction";
@@ -47,10 +53,18 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
4753
});
4854
success.push(nonce);
4955
} catch (error: unknown) {
50-
// Release the nonce if it has not expired.
51-
if (isNonceAlreadyUsedError(error)) {
56+
if (isInsufficientFundsError(error)) {
57+
// Wallet is out of funds. Reset the nonce state.
58+
// After funded, the next transaction will resync the nonce.
59+
job.log(
60+
`Wallet ${chainId}:${walletAddress} out of funds. Resetting nonce.`,
61+
);
62+
await deleteNoncesForBackendWallets([{ chainId, walletAddress }]);
63+
} else if (isNonceAlreadyUsedError(error)) {
64+
// Nonce is used. Don't recycle it.
5265
ignore.push(nonce);
5366
} else {
67+
// Nonce is not used onchain. Recycle it.
5468
job.log(`Recycling nonce: ${nonce}`);
5569
await recycleNonce(chainId, walletAddress, nonce);
5670
fail.push(nonce);

0 commit comments

Comments
 (0)