|
| 1 | +import { Type, type Static } from "@sinclair/typebox"; |
| 2 | +import type { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import { getAddress } from "thirdweb"; |
| 5 | +import { |
| 6 | + deleteNoncesForBackendWallets, |
| 7 | + getUsedBackendWallets, |
| 8 | + syncLatestNonceFromOnchain, |
| 9 | +} from "../../../db/wallets/walletNonce"; |
| 10 | +import { AddressSchema } from "../../schemas/address"; |
| 11 | +import { standardResponseSchema } from "../../schemas/sharedApiSchemas"; |
| 12 | + |
| 13 | +const requestBodySchema = Type.Object({ |
| 14 | + chainId: Type.Optional( |
| 15 | + Type.Number({ |
| 16 | + description: "The chain ID to reset nonces for.", |
| 17 | + }), |
| 18 | + ), |
| 19 | + walletAddress: Type.Optional({ |
| 20 | + ...AddressSchema, |
| 21 | + description: |
| 22 | + "The backend wallet address to reset nonces for. Omit to reset all backend wallets.", |
| 23 | + }), |
| 24 | +}); |
| 25 | + |
| 26 | +const responseSchema = Type.Object({ |
| 27 | + result: Type.Object({ |
| 28 | + status: Type.String(), |
| 29 | + count: Type.Number({ |
| 30 | + description: "The number of backend wallets processed.", |
| 31 | + }), |
| 32 | + }), |
| 33 | +}); |
| 34 | + |
| 35 | +responseSchema.example = { |
| 36 | + result: { |
| 37 | + status: "success", |
| 38 | + count: 1, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +export const resetBackendWalletNoncesRoute = async ( |
| 43 | + fastify: FastifyInstance, |
| 44 | +) => { |
| 45 | + fastify.route<{ |
| 46 | + Reply: Static<typeof responseSchema>; |
| 47 | + Body: Static<typeof requestBodySchema>; |
| 48 | + }>({ |
| 49 | + method: "POST", |
| 50 | + url: "/backend-wallet/reset-nonces", |
| 51 | + schema: { |
| 52 | + summary: "Reset nonces", |
| 53 | + description: |
| 54 | + "Reset nonces for all backend wallets. This is for debugging purposes and does not impact held tokens.", |
| 55 | + tags: ["Backend Wallet"], |
| 56 | + operationId: "resetNonces", |
| 57 | + body: requestBodySchema, |
| 58 | + response: { |
| 59 | + ...standardResponseSchema, |
| 60 | + [StatusCodes.OK]: responseSchema, |
| 61 | + }, |
| 62 | + }, |
| 63 | + handler: async (req, reply) => { |
| 64 | + const { chainId, walletAddress: _walletAddress } = req.body; |
| 65 | + |
| 66 | + // If chain+wallet are provided, only process that wallet. |
| 67 | + // Otherwise process all used wallets that has nonce state. |
| 68 | + const backendWallets = |
| 69 | + chainId && _walletAddress |
| 70 | + ? [{ chainId, walletAddress: getAddress(_walletAddress) }] |
| 71 | + : await getUsedBackendWallets(); |
| 72 | + |
| 73 | + const RESYNC_BATCH_SIZE = 50; |
| 74 | + for (let i = 0; i < backendWallets.length; i += RESYNC_BATCH_SIZE) { |
| 75 | + const batch = backendWallets.slice(i, i + RESYNC_BATCH_SIZE); |
| 76 | + |
| 77 | + // Delete nonce state for these backend wallets. |
| 78 | + await deleteNoncesForBackendWallets(backendWallets); |
| 79 | + |
| 80 | + // Resync nonces for these backend wallets. |
| 81 | + await Promise.allSettled( |
| 82 | + batch.map(({ chainId, walletAddress }) => |
| 83 | + syncLatestNonceFromOnchain(chainId, walletAddress), |
| 84 | + ), |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + reply.status(StatusCodes.OK).send({ |
| 89 | + result: { |
| 90 | + status: "success", |
| 91 | + count: backendWallets.length, |
| 92 | + }, |
| 93 | + }); |
| 94 | + }, |
| 95 | + }); |
| 96 | +}; |
0 commit comments