Skip to content

Commit

Permalink
Add send-tx-batch (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-maj authored Dec 2, 2023
1 parent 5775625 commit 9f1839e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
83 changes: 83 additions & 0 deletions src/server/routes/backend-wallet/sendTransactionBatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { v4 as uuidv4 } from "uuid";
import { prisma } from "../../../db/client";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
import { walletAuthSchema } from "../../schemas/wallet";
import { getChainIdFromChain } from "../../utils/chain";

const ParamsSchema = Type.Object({
chain: Type.String(),
});

const BodySchema = Type.Array(
Type.Object({
toAddress: Type.Optional(
Type.String({
examples: ["0x..."],
}),
),
data: Type.String({
examples: ["0x..."],
}),
value: Type.String({
examples: ["10000000"],
}),
}),
);

const ReplySchema = Type.Object({
result: Type.Object({
queueIds: Type.Array(Type.String()),
}),
});

export async function sendTransactionBatch(fastify: FastifyInstance) {
fastify.route<{
Params: Static<typeof ParamsSchema>;
Body: Static<typeof BodySchema>;
Reply: Static<typeof ReplySchema>;
}>({
method: "POST",
url: "/backend-wallet/:chain/send-transaction-batch",
schema: {
summary: "Send a batch of raw transactions",
description:
"Send a batch of raw transactions with transaction parameters",
tags: ["Backend Wallet"],
operationId: "sendTransactionBatch",
params: ParamsSchema,
body: BodySchema,
headers: Type.Omit(walletAuthSchema, ["x-account-address"]),
response: {
...standardResponseSchema,
[StatusCodes.OK]: ReplySchema,
},
},
handler: async (req, res) => {
const { chain } = req.params;
const txs = req.body;
const fromAddress = req.headers["x-backend-wallet-address"] as string;
const chainId = await getChainIdFromChain(chain);

const data = txs.map((tx) => ({
id: uuidv4(),
chainId: chainId.toString(),
fromAddress,
toAddress: tx.toAddress,
data: tx.data,
value: tx.value,
}));
await prisma.transactions.createMany({
data,
});

res.status(StatusCodes.OK).send({
result: {
queueIds: data.map((tx) => tx.id.toString()),
},
});
},
});
}
2 changes: 2 additions & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import { revokeRelayer } from "./relayer/revoke";

// System
import { getAllTransactions } from "./backend-wallet/getTransactions";
import { sendTransactionBatch } from "./backend-wallet/sendTransactionBatch";
import { healthCheck } from "./health";
import { home } from "./home";
import { updateRelayer } from "./relayer/update";
Expand All @@ -107,6 +108,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
await fastify.register(getAll);
await fastify.register(transfer);
await fastify.register(sendTransaction);
await fastify.register(sendTransactionBatch);
await fastify.register(signTransaction);
await fastify.register(signMessage);
await fastify.register(getAllTransactions);
Expand Down
16 changes: 9 additions & 7 deletions src/server/utils/wallets/getLocalWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export const getLocalWallet = async ({
chain = getChainByChainId(chainId);
} catch (error) {}

if (!chain) {
if (CHAIN_OVERRIDES) {
const parsedChainOverrides = JSON.parse(CHAIN_OVERRIDES);
chain = parsedChainOverrides.find(
(chainData: Static<typeof networkResponseSchema>) =>
chainData.chainId === chainId,
);
if (CHAIN_OVERRIDES) {
const parsedChainOverrides = JSON.parse(CHAIN_OVERRIDES);
const overrideChain = parsedChainOverrides.find(
(chainData: Static<typeof networkResponseSchema>) =>
chainData.chainId === chainId,
);

if (overrideChain) {
chain = overrideChain;
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/utils/cache/getSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@ export const getSdk = async ({
}

const parsedChainOverrides = JSON.parse(CHAIN_OVERRIDES);
chain = parsedChainOverrides.find(
const overrideChain = parsedChainOverrides.find(
(chainData: Static<typeof networkResponseSchema>) =>
chainData.chainId === chainId,
);

if (overrideChain) {
chain = overrideChain;
}
}

if (!chain) {
Expand Down

0 comments on commit 9f1839e

Please sign in to comment.