|
1 | 1 | import { type Job, type Processor, Worker } from "bullmq";
|
2 | 2 | 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"; |
5 | 11 | import { logger } from "../../shared/utils/logger";
|
6 | 12 | import { redis } from "../../shared/utils/redis/redis";
|
7 | 13 | import { sendCancellationTransaction } from "../../shared/utils/transaction/cancel-transaction";
|
@@ -47,10 +53,18 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
|
47 | 53 | });
|
48 | 54 | success.push(nonce);
|
49 | 55 | } 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. |
52 | 65 | ignore.push(nonce);
|
53 | 66 | } else {
|
| 67 | + // Nonce is not used onchain. Recycle it. |
54 | 68 | job.log(`Recycling nonce: ${nonce}`);
|
55 | 69 | await recycleNonce(chainId, walletAddress, nonce);
|
56 | 70 | fail.push(nonce);
|
|
0 commit comments