diff --git a/src/server/routes/backend-wallet/sendTransactionBatch.ts b/src/server/routes/backend-wallet/sendTransactionBatch.ts new file mode 100644 index 000000000..470198e9e --- /dev/null +++ b/src/server/routes/backend-wallet/sendTransactionBatch.ts @@ -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; + Body: Static; + Reply: Static; + }>({ + 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()), + }, + }); + }, + }); +} diff --git a/src/server/routes/index.ts b/src/server/routes/index.ts index f2291e2b8..55836470a 100644 --- a/src/server/routes/index.ts +++ b/src/server/routes/index.ts @@ -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"; @@ -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); diff --git a/src/server/utils/wallets/getLocalWallet.ts b/src/server/utils/wallets/getLocalWallet.ts index 414cd0b54..e9bbfb1a5 100644 --- a/src/server/utils/wallets/getLocalWallet.ts +++ b/src/server/utils/wallets/getLocalWallet.ts @@ -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) => - chainData.chainId === chainId, - ); + if (CHAIN_OVERRIDES) { + const parsedChainOverrides = JSON.parse(CHAIN_OVERRIDES); + const overrideChain = parsedChainOverrides.find( + (chainData: Static) => + chainData.chainId === chainId, + ); + + if (overrideChain) { + chain = overrideChain; } } diff --git a/src/utils/cache/getSdk.ts b/src/utils/cache/getSdk.ts index 9f8a3f1dc..db985a6bb 100644 --- a/src/utils/cache/getSdk.ts +++ b/src/utils/cache/getSdk.ts @@ -91,10 +91,14 @@ export const getSdk = async ({ } const parsedChainOverrides = JSON.parse(CHAIN_OVERRIDES); - chain = parsedChainOverrides.find( + const overrideChain = parsedChainOverrides.find( (chainData: Static) => chainData.chainId === chainId, ); + + if (overrideChain) { + chain = overrideChain; + } } if (!chain) {