|
| 1 | +import { Static, Type } from "@sinclair/typebox"; |
| 2 | +import { Value } from "@sinclair/typebox/value"; |
| 3 | +import { FastifyInstance } from "fastify"; |
| 4 | +import { StatusCodes } from "http-status-codes"; |
| 5 | +import { deriveClientId } from "../../../utils/api-keys"; |
| 6 | +import { env } from "../../../utils/env"; |
| 7 | +import { standardResponseSchema } from "../../schemas/sharedApiSchemas"; |
| 8 | +import { getChainIdFromChain } from "../../utils/chain"; |
| 9 | + |
| 10 | +const UserOp = Type.Object({ |
| 11 | + sender: Type.String(), |
| 12 | + nonce: Type.String(), |
| 13 | + initCode: Type.String(), |
| 14 | + callData: Type.String(), |
| 15 | + callGasLimit: Type.String(), |
| 16 | + verificationGasLimit: Type.String(), |
| 17 | + preVerificationGas: Type.String(), |
| 18 | + maxFeePerGas: Type.String(), |
| 19 | + maxPriorityFeePerGas: Type.String(), |
| 20 | + paymasterAndData: Type.String(), |
| 21 | + signature: Type.String(), |
| 22 | +}); |
| 23 | + |
| 24 | +const UserOpString = Type.Transform(Type.String()) |
| 25 | + .Decode((signedUserOp) => JSON.parse(signedUserOp) as Static<typeof UserOp>) |
| 26 | + .Encode((userOp) => JSON.stringify(userOp)); |
| 27 | + |
| 28 | +const ParamsSchema = Type.Object({ |
| 29 | + chain: Type.String(), |
| 30 | +}); |
| 31 | + |
| 32 | +const BodySchema = Type.Object({ |
| 33 | + // signedUserOp: Type.Union([UserOpString, UserOp]), |
| 34 | + signedUserOp: Type.Any(), |
| 35 | +}); |
| 36 | + |
| 37 | +const ReplySchema = Type.Union([ |
| 38 | + Type.Object({ |
| 39 | + result: Type.Object({ |
| 40 | + userOpHash: Type.String(), |
| 41 | + }), |
| 42 | + }), |
| 43 | + Type.Object({ |
| 44 | + error: Type.Object({ |
| 45 | + message: Type.String(), |
| 46 | + }), |
| 47 | + }), |
| 48 | +]); |
| 49 | + |
| 50 | +type RpcResponse = |
| 51 | + | { |
| 52 | + result: string; |
| 53 | + error: undefined; |
| 54 | + } |
| 55 | + | { |
| 56 | + result: undefined; |
| 57 | + error: { |
| 58 | + message: string; |
| 59 | + }; |
| 60 | + }; |
| 61 | + |
| 62 | +export async function sendSignedUserOp(fastify: FastifyInstance) { |
| 63 | + fastify.route<{ |
| 64 | + Params: Static<typeof ParamsSchema>; |
| 65 | + Body: Static<typeof BodySchema>; |
| 66 | + Reply: Static<typeof ReplySchema>; |
| 67 | + }>({ |
| 68 | + method: "POST", |
| 69 | + url: "/transaction/:chain/send-signed-user-op", |
| 70 | + schema: { |
| 71 | + summary: "Send a signed user operation", |
| 72 | + description: "Send a signed user operation", |
| 73 | + tags: ["Transaction"], |
| 74 | + operationId: "sendSignedUserOp", |
| 75 | + params: ParamsSchema, |
| 76 | + body: BodySchema, |
| 77 | + response: { |
| 78 | + ...standardResponseSchema, |
| 79 | + [StatusCodes.OK]: ReplySchema, |
| 80 | + }, |
| 81 | + }, |
| 82 | + handler: async (req, res) => { |
| 83 | + const { chain } = req.params; |
| 84 | + const { signedUserOp } = req.body; |
| 85 | + const chainId = await getChainIdFromChain(chain); |
| 86 | + |
| 87 | + let userOp: Static<typeof UserOp>; |
| 88 | + if (typeof signedUserOp === "string") { |
| 89 | + try { |
| 90 | + userOp = Value.Decode(UserOpString, signedUserOp); |
| 91 | + } catch (err: any) { |
| 92 | + return res.status(400).send({ |
| 93 | + error: { |
| 94 | + message: `Invalid signed user operation. - ${err.message || err}`, |
| 95 | + }, |
| 96 | + }); |
| 97 | + } |
| 98 | + } else { |
| 99 | + userOp = signedUserOp; |
| 100 | + } |
| 101 | + |
| 102 | + const entryPointAddress = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789"; |
| 103 | + const userOpRes = await fetch(`https://${chainId}.bundler.thirdweb.com`, { |
| 104 | + method: "POST", |
| 105 | + headers: { |
| 106 | + "Content-Type": "application/json", |
| 107 | + "x-client-id": deriveClientId(env.THIRDWEB_API_SECRET_KEY), |
| 108 | + "x-secret-key": env.THIRDWEB_API_SECRET_KEY, |
| 109 | + }, |
| 110 | + body: JSON.stringify({ |
| 111 | + id: 1, |
| 112 | + jsonrpc: "2.0", |
| 113 | + method: "eth_sendUserOperation", |
| 114 | + params: [userOp, entryPointAddress], |
| 115 | + }), |
| 116 | + }); |
| 117 | + |
| 118 | + const { result: userOpHash, error } = |
| 119 | + (await userOpRes.json()) as RpcResponse; |
| 120 | + |
| 121 | + if (error) { |
| 122 | + return res.status(400).send({ |
| 123 | + error: { |
| 124 | + message: `Failed to send - ${error.message || error}`, |
| 125 | + }, |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + return res.status(200).send({ |
| 130 | + result: { |
| 131 | + userOpHash, |
| 132 | + }, |
| 133 | + }); |
| 134 | + }, |
| 135 | + }); |
| 136 | +} |
0 commit comments