Skip to content

Commit

Permalink
Add endpoint to send raw transactions (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-maj authored Oct 12, 2023
1 parent 7a4bb0c commit ba29679
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
85 changes: 85 additions & 0 deletions server/api/transaction/send.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ParamsSchema>;
Body: Static<typeof requestBodySchema>;
Reply: Static<typeof transactionWritesResponseSchema>;
}>({
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,
},
});
},
});
}

0 comments on commit ba29679

Please sign in to comment.