Skip to content

Commit d2a989e

Browse files
authored
feat: Reset nonces on server start, add endpoint (#366)
* feat: Reset nonces on server start * add route
1 parent 710bb5d commit d2a989e

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { PrismaTransaction } from "../../schema/prisma";
2+
import { getPrismaWithPostgresTx } from "../client";
3+
4+
interface DeleteAllWalletNoncesParams {
5+
pgtx?: PrismaTransaction;
6+
}
7+
8+
/**
9+
* Delete the cached value of all backend wallet nonces.
10+
* This is useful to require all backend wallets to resync their
11+
* nonce on the next txn.
12+
*/
13+
export const deleteAllWalletNonces = async ({
14+
pgtx,
15+
}: DeleteAllWalletNoncesParams) => {
16+
const prisma = getPrismaWithPostgresTx(pgtx);
17+
await prisma.walletNonce.deleteMany({});
18+
};

src/server/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fastify, { FastifyInstance } from "fastify";
33
import * as fs from "fs";
44
import path from "path";
55
import { URL } from "url";
6+
import { deleteAllWalletNonces } from "../db/wallets/deleteAllWalletNonces";
67
import { env } from "../utils/env";
78
import { logger } from "../utils/logger";
89
import { withAuth } from "./middleware/auth";
@@ -25,8 +26,12 @@ interface HttpsObject {
2526
}
2627

2728
const main = async () => {
28-
let httpsObject: HttpsObject | undefined = undefined;
29+
// Reset any server state that is safe to reset.
30+
// This allows the server to start in a predictable state.
31+
await deleteAllWalletNonces({});
2932

33+
// Enables the server to run on https://localhost:PORT, if ENABLE_HTTPS is provided.
34+
let httpsObject: HttpsObject | undefined = undefined;
3035
if (env.ENABLE_HTTPS) {
3136
httpsObject = {
3237
https: {
@@ -37,6 +42,7 @@ const main = async () => {
3742
};
3843
}
3944

45+
// Start the server with middleware.
4046
const server: FastifyInstance = fastify({
4147
disableRequestLogging: true,
4248
...(env.ENABLE_HTTPS ? httpsObject : {}),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Static, Type } from "@sinclair/typebox";
2+
import { FastifyInstance } from "fastify";
3+
import { StatusCodes } from "http-status-codes";
4+
import { deleteAllWalletNonces } from "../../../db/wallets/deleteAllWalletNonces";
5+
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
6+
7+
const responseSchema = Type.Object({
8+
result: Type.Object({
9+
status: Type.String(),
10+
}),
11+
});
12+
13+
responseSchema.example = {
14+
result: {
15+
status: "success",
16+
},
17+
};
18+
19+
export const resetBackendWalletNonces = async (fastify: FastifyInstance) => {
20+
fastify.route<{
21+
Reply: Static<typeof responseSchema>;
22+
}>({
23+
method: "POST",
24+
url: "/backend-wallet/reset-nonces",
25+
schema: {
26+
summary: "Reset all nonces",
27+
description:
28+
"Reset nonces for all backend wallets. This is for debugging purposes and does not impact held tokens.",
29+
tags: ["Backend Wallet"],
30+
operationId: "resetNonces",
31+
response: {
32+
...standardResponseSchema,
33+
[StatusCodes.OK]: responseSchema,
34+
},
35+
},
36+
handler: async (req, reply) => {
37+
await deleteAllWalletNonces({});
38+
39+
reply.status(StatusCodes.OK).send({
40+
result: {
41+
status: "success",
42+
},
43+
});
44+
},
45+
});
46+
};

src/server/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import { revokeRelayer } from "./relayer/revoke";
9494

9595
// System
9696
import { getAllTransactions } from "./backend-wallet/getTransactions";
97+
import { resetBackendWalletNonces } from "./backend-wallet/resetNonces";
9798
import { sendTransactionBatch } from "./backend-wallet/sendTransactionBatch";
9899
import { withdraw } from "./backend-wallet/withdraw";
99100
import { home } from "./home";
@@ -118,6 +119,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
118119
await fastify.register(signTransaction);
119120
await fastify.register(signMessage);
120121
await fastify.register(getAllTransactions);
122+
await fastify.register(resetBackendWalletNonces);
121123

122124
// Configuration
123125
await fastify.register(getWalletsConfiguration);

src/server/schemas/wallet/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export const walletAuthSchema = Type.Object({
1414
export const walletParamSchema = Type.Object({
1515
chain: Type.String({
1616
examples: ["mumbai"],
17-
description: "Chain ID name",
17+
description: "Chain name",
1818
}),
1919
walletAddress: Type.String({
2020
examples: ["0x..."],
21-
description: "Wallet Address",
21+
description: "Backend wallet address",
2222
}),
2323
});
2424

0 commit comments

Comments
 (0)