|
| 1 | +import { Type, type Static } from "@sinclair/typebox"; |
| 2 | +import type { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import { eth_getTransactionCount, getRpcClient } from "thirdweb"; |
| 5 | +import { checksumAddress } from "thirdweb/utils"; |
| 6 | +import { getChain } from "../../../utils/chain"; |
| 7 | +import { thirdwebClient } from "../../../utils/sdk"; |
| 8 | +import { sendCancellationTransaction } from "../../../utils/transaction/cancelTransaction"; |
| 9 | +import { createCustomError } from "../../middleware/error"; |
| 10 | +import { |
| 11 | + requestQuerystringSchema, |
| 12 | + standardResponseSchema, |
| 13 | +} from "../../schemas/sharedApiSchemas"; |
| 14 | +import { |
| 15 | + walletHeaderSchema, |
| 16 | + walletWithAddressParamSchema, |
| 17 | +} from "../../schemas/wallet"; |
| 18 | +import { getChainIdFromChain } from "../../utils/chain"; |
| 19 | + |
| 20 | +const requestSchema = Type.Omit(walletWithAddressParamSchema, [ |
| 21 | + "walletAddress", |
| 22 | +]); |
| 23 | + |
| 24 | +const requestBodySchema = Type.Object({ |
| 25 | + toNonce: Type.Number({ |
| 26 | + description: |
| 27 | + "The nonce to cancel up to, inclusive. Example: If the onchain nonce is 10 and 'toNonce' is 15, this request will cancel nonces: 11, 12, 13, 14, 15", |
| 28 | + examples: ["42"], |
| 29 | + }), |
| 30 | +}); |
| 31 | + |
| 32 | +const responseBodySchema = Type.Object({ |
| 33 | + result: Type.Object( |
| 34 | + { |
| 35 | + cancelledNonces: Type.Array(Type.Number()), |
| 36 | + }, |
| 37 | + { |
| 38 | + examples: [ |
| 39 | + { |
| 40 | + result: { |
| 41 | + cancelledNonces: [11, 12, 13, 14, 15], |
| 42 | + }, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + ), |
| 47 | +}); |
| 48 | + |
| 49 | +export async function cancelBackendWalletNoncesRoute(fastify: FastifyInstance) { |
| 50 | + fastify.route<{ |
| 51 | + Params: Static<typeof requestSchema>; |
| 52 | + Reply: Static<typeof responseBodySchema>; |
| 53 | + Body: Static<typeof requestBodySchema>; |
| 54 | + Querystring: Static<typeof requestQuerystringSchema>; |
| 55 | + }>({ |
| 56 | + method: "POST", |
| 57 | + url: "/backend-wallet/:chain/cancel-nonces", |
| 58 | + schema: { |
| 59 | + summary: "Cancel nonces", |
| 60 | + description: |
| 61 | + "Cancel nonces from the next available onchain nonce to the provided nonce. This is useful to unblock a backend wallet that has transactions waiting for nonces to be mined.", |
| 62 | + tags: ["Backend Wallet"], |
| 63 | + operationId: "cancelNonces", |
| 64 | + params: requestSchema, |
| 65 | + body: requestBodySchema, |
| 66 | + headers: walletHeaderSchema, |
| 67 | + querystring: requestQuerystringSchema, |
| 68 | + response: { |
| 69 | + ...standardResponseSchema, |
| 70 | + [StatusCodes.OK]: responseBodySchema, |
| 71 | + }, |
| 72 | + }, |
| 73 | + handler: async (request, reply) => { |
| 74 | + const { chain } = request.params; |
| 75 | + const { toNonce } = request.body; |
| 76 | + const { "x-backend-wallet-address": walletAddress } = |
| 77 | + request.headers as Static<typeof walletHeaderSchema>; |
| 78 | + |
| 79 | + const chainId = await getChainIdFromChain(chain); |
| 80 | + const from = checksumAddress(walletAddress); |
| 81 | + |
| 82 | + const rpcRequest = getRpcClient({ |
| 83 | + client: thirdwebClient, |
| 84 | + chain: await getChain(chainId), |
| 85 | + }); |
| 86 | + |
| 87 | + // Cancel starting from the next unused onchain nonce. |
| 88 | + const transactionCount = await eth_getTransactionCount(rpcRequest, { |
| 89 | + address: walletAddress, |
| 90 | + blockTag: "latest", |
| 91 | + }); |
| 92 | + if (transactionCount > toNonce) { |
| 93 | + throw createCustomError( |
| 94 | + `"toNonce" (${toNonce}) is lower than the next unused onchain nonce (${transactionCount}).`, |
| 95 | + StatusCodes.BAD_REQUEST, |
| 96 | + "BAD_REQUEST", |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + const cancelledNonces: number[] = []; |
| 101 | + for (let nonce = transactionCount; nonce <= toNonce; nonce++) { |
| 102 | + await sendCancellationTransaction({ |
| 103 | + chainId, |
| 104 | + from, |
| 105 | + nonce, |
| 106 | + }); |
| 107 | + cancelledNonces.push(nonce); |
| 108 | + } |
| 109 | + |
| 110 | + reply.status(StatusCodes.OK).send({ |
| 111 | + result: { |
| 112 | + cancelledNonces, |
| 113 | + }, |
| 114 | + }); |
| 115 | + }, |
| 116 | + }); |
| 117 | +} |
0 commit comments