|
| 1 | +import { Static, Type } from "@sinclair/typebox"; |
| 2 | +import { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import { |
| 5 | + Address, |
| 6 | + eth_getTransactionCount, |
| 7 | + getAddress, |
| 8 | + getRpcClient, |
| 9 | +} from "thirdweb"; |
| 10 | +import { |
| 11 | + getUsedBackendWallets, |
| 12 | + lastUsedNonceKey, |
| 13 | + recycledNoncesKey, |
| 14 | + sentNoncesKey, |
| 15 | +} from "../../../db/wallets/walletNonce"; |
| 16 | +import { getChain } from "../../../utils/chain"; |
| 17 | +import { redis } from "../../../utils/redis/redis"; |
| 18 | +import { thirdwebClient } from "../../../utils/sdk"; |
| 19 | +import { standardResponseSchema } from "../../schemas/sharedApiSchemas"; |
| 20 | +import { walletWithAddressParamSchema } from "../../schemas/wallet"; |
| 21 | + |
| 22 | +export const responseBodySchema = Type.Object({ |
| 23 | + result: Type.Array( |
| 24 | + Type.Object({ |
| 25 | + walletAddress: Type.String({ |
| 26 | + description: "Backend Wallet Address", |
| 27 | + examples: ["0xcedf3b4d8f7f1f7e0f7f0f7f0f7f0f7f0f7f0f7f"], |
| 28 | + }), |
| 29 | + chainId: Type.Number({ |
| 30 | + description: "Chain ID", |
| 31 | + examples: [80002], |
| 32 | + }), |
| 33 | + onchainNonce: Type.Integer({ |
| 34 | + description: "Last mined nonce", |
| 35 | + examples: [0], |
| 36 | + }), |
| 37 | + lastUsedNonce: Type.Integer({ |
| 38 | + description: "Last incremented nonce sent to the RPC", |
| 39 | + examples: [0], |
| 40 | + }), |
| 41 | + sentNonces: Type.Array(Type.Integer(), { |
| 42 | + description: |
| 43 | + "Nonces that were successfully sent to the RPC but not mined yet (in descending order)", |
| 44 | + examples: [[2, 1, 0]], |
| 45 | + }), |
| 46 | + recycledNonces: Type.Array(Type.Integer(), { |
| 47 | + examples: [[3, 2, 1]], |
| 48 | + description: |
| 49 | + "Nonces that were acquired but failed to be sent to the blockchain, waiting to be recycled or cancelled (in descending order)", |
| 50 | + }), |
| 51 | + }), |
| 52 | + ), |
| 53 | +}); |
| 54 | + |
| 55 | +responseBodySchema.example = { |
| 56 | + result: [ |
| 57 | + { |
| 58 | + walletAddress: "0xcedf3b4d8f7f1f7e0f7f0f7f0f7f0f7f0f7f0f7f", |
| 59 | + onchainNonce: 2, |
| 60 | + lastUsedNonce: 8, |
| 61 | + recycledNonces: [6, 7], |
| 62 | + chainId: 80002, |
| 63 | + }, |
| 64 | + ], |
| 65 | +}; |
| 66 | + |
| 67 | +const walletWithAddressQuerySchema = Type.Partial(walletWithAddressParamSchema); |
| 68 | + |
| 69 | +export async function getNonceDetailsRoute(fastify: FastifyInstance) { |
| 70 | + fastify.route<{ |
| 71 | + Querystring: Static<typeof walletWithAddressQuerySchema>; |
| 72 | + Reply: Static<typeof responseBodySchema>; |
| 73 | + }>({ |
| 74 | + method: "GET", |
| 75 | + url: "/admin/nonces", |
| 76 | + schema: { |
| 77 | + summary: "Get nonce status details for wallets", |
| 78 | + description: |
| 79 | + "Admin route to get nonce status details for all wallets filtered by address and chain ", |
| 80 | + tags: ["Admin"], |
| 81 | + operationId: "nonceDetails", |
| 82 | + querystring: walletWithAddressQuerySchema, |
| 83 | + response: { |
| 84 | + ...standardResponseSchema, |
| 85 | + [StatusCodes.OK]: responseBodySchema, |
| 86 | + }, |
| 87 | + hide: true, |
| 88 | + }, |
| 89 | + handler: async (request, reply) => { |
| 90 | + const { walletAddress, chain } = request.query; |
| 91 | + const result = await getNonceDetails({ |
| 92 | + walletAddress: walletAddress ? getAddress(walletAddress) : undefined, |
| 93 | + chainId: chain ? parseInt(chain) : undefined, |
| 94 | + }); |
| 95 | + |
| 96 | + reply.status(StatusCodes.OK).send({ |
| 97 | + result, |
| 98 | + }); |
| 99 | + }, |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +export const getNonceDetails = async ({ |
| 104 | + walletAddress, |
| 105 | + chainId, |
| 106 | +}: { |
| 107 | + walletAddress?: Address; |
| 108 | + chainId?: number; |
| 109 | +} = {}) => { |
| 110 | + const usedBackendWallets = await getUsedBackendWallets( |
| 111 | + chainId, |
| 112 | + walletAddress, |
| 113 | + ); |
| 114 | + |
| 115 | + const pipeline = redis.pipeline(); |
| 116 | + const onchainNoncePromises: Promise<number>[] = []; |
| 117 | + |
| 118 | + const keyMap = usedBackendWallets.map(({ chainId, walletAddress }) => { |
| 119 | + pipeline.get(lastUsedNonceKey(chainId, walletAddress)); |
| 120 | + pipeline.smembers(sentNoncesKey(chainId, walletAddress)); |
| 121 | + pipeline.smembers(recycledNoncesKey(chainId, walletAddress)); |
| 122 | + |
| 123 | + onchainNoncePromises.push(getLastUsedOnchainNonce(chainId, walletAddress)); |
| 124 | + |
| 125 | + return { chainId, walletAddress }; |
| 126 | + }); |
| 127 | + |
| 128 | + const [pipelineResults, onchainNonces] = await Promise.all([ |
| 129 | + pipeline.exec(), |
| 130 | + Promise.all(onchainNoncePromises), |
| 131 | + ]); |
| 132 | + |
| 133 | + if (!pipelineResults) { |
| 134 | + throw new Error("Failed to execute Redis pipeline"); |
| 135 | + } |
| 136 | + |
| 137 | + return keyMap.map((key, index) => { |
| 138 | + const pipelineOffset = index * 3; |
| 139 | + const [lastUsedNonceResult, sentNoncesResult, recycledNoncesResult] = |
| 140 | + pipelineResults.slice(pipelineOffset, pipelineOffset + 3); |
| 141 | + |
| 142 | + return { |
| 143 | + walletAddress: key.walletAddress, |
| 144 | + chainId: key.chainId, |
| 145 | + onchainNonce: onchainNonces[index], |
| 146 | + lastUsedNonce: parseInt(lastUsedNonceResult[1] as string) ?? 0, |
| 147 | + sentNonces: (sentNoncesResult[1] as string[]) |
| 148 | + .map((nonce) => parseInt(nonce)) |
| 149 | + .sort((a, b) => b - a), |
| 150 | + recycledNonces: (recycledNoncesResult[1] as string[]) |
| 151 | + .map((nonce) => parseInt(nonce)) |
| 152 | + .sort((a, b) => b - a), |
| 153 | + }; |
| 154 | + }); |
| 155 | +}; |
| 156 | + |
| 157 | +/* |
| 158 | + * Get the last used nonce onchain |
| 159 | + * @param chainId Chain ID |
| 160 | + * @param walletAddress Wallet address |
| 161 | + * @returns Next unused nonce |
| 162 | + */ |
| 163 | +export const getLastUsedOnchainNonce = async ( |
| 164 | + chainId: number, |
| 165 | + walletAddress: string, |
| 166 | +) => { |
| 167 | + const rpcRequest = getRpcClient({ |
| 168 | + client: thirdwebClient, |
| 169 | + chain: await getChain(chainId), |
| 170 | + }); |
| 171 | + |
| 172 | + // The next unused nonce = transactionCount. |
| 173 | + const transactionCount = await eth_getTransactionCount(rpcRequest, { |
| 174 | + address: walletAddress, |
| 175 | + blockTag: "latest", |
| 176 | + }); |
| 177 | + |
| 178 | + const onchainNonce = transactionCount - 1; |
| 179 | + return onchainNonce; |
| 180 | +}; |
0 commit comments