Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/db/wallets/walletNonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,19 @@ export const inspectNonce = async (chainId: number, walletAddress: Address) => {
};

/**
* Delete all wallet nonces. Useful when they get out of sync.
* Delete nonce state for the provided wallets.
* @param backendWallets
*/
export const deleteAllNonces = async () => {
const keys = [
...(await redis.keys("nonce:*")),
...(await redis.keys("nonce-recycled:*")),
...(await redis.keys("sent-nonce:*")),
];
if (keys.length > 0) {
await redis.del(keys);
}
};
export async function deleteNoncesForBackendWallets(
backendWallets: { chainId: number; walletAddress: Address }[],
) {
const keys = backendWallets.flatMap(({ chainId, walletAddress }) => [
lastUsedNonceKey(chainId, walletAddress),
recycledNoncesKey(chainId, walletAddress),
sentNoncesKey(chainId, walletAddress),
]);
await redis.del(keys);
}

/**
* Resync the nonce to the higher of (db nonce, onchain nonce).
Expand Down
96 changes: 96 additions & 0 deletions src/server/routes/backend-wallet/reset-nonces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { getAddress } from "thirdweb";
import {
deleteNoncesForBackendWallets,
getUsedBackendWallets,
syncLatestNonceFromOnchain,
} from "../../../db/wallets/walletNonce";
import { AddressSchema } from "../../schemas/address";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";

const requestBodySchema = Type.Object({
chainId: Type.Optional(
Type.Number({
description: "The chain ID to reset nonces for.",
}),
),
walletAddress: Type.Optional({
...AddressSchema,
description:
"The backend wallet address to reset nonces for. Omit to reset all backend wallets.",
}),
});

const responseSchema = Type.Object({
result: Type.Object({
status: Type.String(),
count: Type.Number({
description: "The number of backend wallets processed.",
}),
}),
});

responseSchema.example = {
result: {
status: "success",
count: 1,
},
};

export const resetBackendWalletNoncesRoute = async (
fastify: FastifyInstance,
) => {
fastify.route<{
Reply: Static<typeof responseSchema>;
Body: Static<typeof requestBodySchema>;
}>({
method: "POST",
url: "/backend-wallet/reset-nonces",
schema: {
summary: "Reset nonces",
description:
"Reset nonces for all backend wallets. This is for debugging purposes and does not impact held tokens.",
tags: ["Backend Wallet"],
operationId: "resetNonces",
body: requestBodySchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: responseSchema,
},
},
handler: async (req, reply) => {
const { chainId, walletAddress: _walletAddress } = req.body;

// If chain+wallet are provided, only process that wallet.
// Otherwise process all used wallets that has nonce state.
const backendWallets =
chainId && _walletAddress
? [{ chainId, walletAddress: getAddress(_walletAddress) }]
: await getUsedBackendWallets();

const RESYNC_BATCH_SIZE = 50;
for (let i = 0; i < backendWallets.length; i += RESYNC_BATCH_SIZE) {
const batch = backendWallets.slice(i, i + RESYNC_BATCH_SIZE);

// Delete nonce state for these backend wallets.
await deleteNoncesForBackendWallets(backendWallets);

// Resync nonces for these backend wallets.
await Promise.allSettled(
batch.map(({ chainId, walletAddress }) =>
syncLatestNonceFromOnchain(chainId, walletAddress),
),
);
}

reply.status(StatusCodes.OK).send({
result: {
status: "success",
count: backendWallets.length,
},
});
},
});
};
78 changes: 0 additions & 78 deletions src/server/routes/backend-wallet/resetNonces.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getTransactionsForBackendWallet } from "./backend-wallet/getTransaction
import { getTransactionsForBackendWalletByNonce } from "./backend-wallet/getTransactionsByNonce";
import { importBackendWallet } from "./backend-wallet/import";
import { removeBackendWallet } from "./backend-wallet/remove";
import { resetBackendWalletNonces } from "./backend-wallet/resetNonces";
import { resetBackendWalletNoncesRoute } from "./backend-wallet/reset-nonces";
import { sendTransaction } from "./backend-wallet/sendTransaction";
import { sendTransactionBatch } from "./backend-wallet/sendTransactionBatch";
import { signMessageRoute } from "./backend-wallet/signMessage";
Expand Down Expand Up @@ -128,7 +128,7 @@ export async function withRoutes(fastify: FastifyInstance) {
await fastify.register(signTypedData);
await fastify.register(getTransactionsForBackendWallet);
await fastify.register(getTransactionsForBackendWalletByNonce);
await fastify.register(resetBackendWalletNonces);
await fastify.register(resetBackendWalletNoncesRoute);
await fastify.register(getBackendWalletNonce);
await fastify.register(simulateTransaction);

Expand Down
Loading