|
| 1 | +import { Static, Type } from "@sinclair/typebox"; |
| 2 | +import { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import { getContract } from "../../../utils/cache/getContract"; |
| 5 | +import { |
| 6 | + simulateResponseSchema, |
| 7 | + standardResponseSchema, |
| 8 | +} from "../../schemas/sharedApiSchemas"; |
| 9 | +import { walletAuthSchema } from "../../schemas/wallet"; |
| 10 | +import { getChainIdFromChain } from "../../utils/chain"; |
| 11 | +import { SimulateTxParams, simulateTx } from "../../utils/simulateTx"; |
| 12 | + |
| 13 | +// INPUT |
| 14 | +const ParamsSchema = Type.Object({ |
| 15 | + chain: Type.String(), |
| 16 | +}); |
| 17 | + |
| 18 | +const simulateRequestBodySchema = Type.Object({ |
| 19 | + toAddress: Type.String({ |
| 20 | + description: "Address of the contract", |
| 21 | + }), |
| 22 | + value: Type.Optional( |
| 23 | + Type.String({ |
| 24 | + examples: ["0"], |
| 25 | + description: "Native Currency Value", |
| 26 | + }), |
| 27 | + ), |
| 28 | + // Decoded transaction args |
| 29 | + functionName: Type.Optional( |
| 30 | + Type.String({ |
| 31 | + description: "Name of the function to call on Contract", |
| 32 | + }), |
| 33 | + ), |
| 34 | + args: Type.Optional( |
| 35 | + Type.Array( |
| 36 | + Type.Union([ |
| 37 | + Type.String({ |
| 38 | + description: "Arguments for the function. Comma Separated", |
| 39 | + }), |
| 40 | + Type.Tuple([Type.String(), Type.String()]), |
| 41 | + Type.Object({}), |
| 42 | + Type.Array(Type.Any()), |
| 43 | + Type.Any(), |
| 44 | + ]), |
| 45 | + ), |
| 46 | + ), |
| 47 | + // Raw transaction args |
| 48 | + data: Type.Optional( |
| 49 | + Type.String({ |
| 50 | + description: "Transaction Data", |
| 51 | + }), |
| 52 | + ), |
| 53 | +}); |
| 54 | + |
| 55 | +// LOGIC |
| 56 | +export async function simulateTransaction(fastify: FastifyInstance) { |
| 57 | + fastify.route<{ |
| 58 | + Params: Static<typeof ParamsSchema>; |
| 59 | + Body: Static<typeof simulateRequestBodySchema>; |
| 60 | + Reply: Static<typeof simulateResponseSchema>; |
| 61 | + }>({ |
| 62 | + method: "POST", |
| 63 | + url: "/backend-wallet/:chain/simulate-transaction", |
| 64 | + schema: { |
| 65 | + summary: "Simulate a transaction", |
| 66 | + description: "Simulate a transaction with transaction parameters", |
| 67 | + tags: ["Backend Wallet"], |
| 68 | + operationId: "simulateTransaction", |
| 69 | + params: ParamsSchema, |
| 70 | + body: simulateRequestBodySchema, |
| 71 | + headers: Type.Omit(walletAuthSchema, ["x-account-address"]), |
| 72 | + response: { |
| 73 | + ...standardResponseSchema, |
| 74 | + [StatusCodes.OK]: simulateResponseSchema, |
| 75 | + }, |
| 76 | + }, |
| 77 | + handler: async (request, reply) => { |
| 78 | + // Destruct core params |
| 79 | + const { chain } = request.params; |
| 80 | + const { toAddress, value, functionName, args, data } = request.body; |
| 81 | + const walletAddress = request.headers[ |
| 82 | + "x-backend-wallet-address" |
| 83 | + ] as string; |
| 84 | + const accountAddress = request.headers["x-account-address"] as string; |
| 85 | + const chainId = await getChainIdFromChain(chain); |
| 86 | + |
| 87 | + // Get decoded tx simulate args |
| 88 | + let simulateArgs: SimulateTxParams; |
| 89 | + if (functionName && args) { |
| 90 | + const contract = await getContract({ |
| 91 | + chainId, |
| 92 | + contractAddress: toAddress, |
| 93 | + walletAddress, |
| 94 | + accountAddress, |
| 95 | + }); |
| 96 | + const tx = contract.prepare(functionName, args, { |
| 97 | + value: value ?? "0", |
| 98 | + }); |
| 99 | + simulateArgs = { tx }; |
| 100 | + } |
| 101 | + // Get raw tx simulate args |
| 102 | + else { |
| 103 | + simulateArgs = { |
| 104 | + txRaw: { |
| 105 | + chainId: chainId.toString(), |
| 106 | + fromAddress: walletAddress, |
| 107 | + toAddress, |
| 108 | + data, |
| 109 | + value, |
| 110 | + }, |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + // Simulate raw tx |
| 115 | + await simulateTx(simulateArgs); |
| 116 | + |
| 117 | + // Return success |
| 118 | + reply.status(StatusCodes.OK).send({ |
| 119 | + result: { |
| 120 | + success: true, |
| 121 | + }, |
| 122 | + }); |
| 123 | + }, |
| 124 | + }); |
| 125 | +} |
0 commit comments