diff --git a/server/api/index.ts b/server/api/index.ts index 715ffd059..ac5ed2150 100644 --- a/server/api/index.ts +++ b/server/api/index.ts @@ -50,6 +50,7 @@ import { importWallet } from "./backend-wallet/import"; import { transfer } from "./backend-wallet/transfer"; import { accountRoutes } from "./contract/extensions/account"; import { accountFactoryRoutes } from "./contract/extensions/accountFactory"; +import { sendRawTransaction } from "./transaction/send"; export const apiRoutes = async (fastify: FastifyInstance) => { // Wallet @@ -96,6 +97,7 @@ export const apiRoutes = async (fastify: FastifyInstance) => { await fastify.register(checkTxStatus); await fastify.register(getAllTx); await fastify.register(getAllDeployedContracts); + await fastify.register(sendRawTransaction); await fastify.register(retryTransaction); await fastify.register(cancelTransaction); diff --git a/server/api/transaction/send.ts b/server/api/transaction/send.ts new file mode 100644 index 000000000..154b55f08 --- /dev/null +++ b/server/api/transaction/send.ts @@ -0,0 +1,85 @@ +import { Static, Type } from "@sinclair/typebox"; +import { FastifyInstance } from "fastify"; +import { StatusCodes } from "http-status-codes"; +import { prisma } from "../../../src/db/client"; +import { + standardResponseSchema, + transactionWritesResponseSchema, +} from "../../helpers/sharedApiSchemas"; +import { getChainIdFromChain } from "../../utilities/chain"; + +const ParamsSchema = Type.Object({ + chain: Type.String(), +}); + +const requestBodySchema = Type.Object({ + fromAddress: Type.String({ + examples: ["0x..."], + }), + toAddress: Type.Optional( + Type.String({ + examples: ["0x..."], + }), + ), + data: Type.String({ + examples: ["0x..."], + }), + value: Type.String({ + examples: ["10000000"], + }), +}); + +requestBodySchema.examples = [ + { + fromAddress: "0x43CAe0d7fe86C713530E679Ce02574743b2Ee9FC", + toAddress: "0x7a0ce8524bea337f0bee853b68fabde145dac0a0", + data: "0x449a52f800000000000000000000000043cae0d7fe86c713530e679ce02574743b2ee9fc0000000000000000000000000000000000000000000000000de0b6b3a7640000", + value: "0x00", + }, +]; + +export async function sendRawTransaction(fastify: FastifyInstance) { + fastify.route<{ + Params: Static; + Body: Static; + Reply: Static; + }>({ + method: "POST", + url: "/transaction/:chain/send-raw", + schema: { + summary: "Send a raw transaction", + description: "Send a raw transaction with transaction parameters", + tags: ["Transaction"], + operationId: "txSendRaw", + params: ParamsSchema, + body: requestBodySchema, + response: { + ...standardResponseSchema, + [StatusCodes.OK]: transactionWritesResponseSchema, + }, + }, + handler: async (request, reply) => { + const { chain } = request.params; + const { fromAddress, toAddress, data, value } = request.body; + const chainId = getChainIdFromChain(chain); + + // TODO: At some point we should simulate this first + // For now, it's okay not to since its a raw transaction + const { id: queueId } = await prisma.transactions.create({ + data: { + chainId, + fromAddress, + toAddress, + data, + value, + }, + }); + + reply.status(StatusCodes.OK).send({ + result: { + queueId, + }, + }); + }, + }); +}