|
| 1 | +import { Type, type Static } from "@sinclair/typebox"; |
| 2 | +import type { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import type { Address, Hex } from "thirdweb"; |
| 5 | +import { insertTransaction } from "../../../shared/utils/transaction/insert-transaction"; |
| 6 | +import { |
| 7 | + requestQuerystringSchema, |
| 8 | + standardResponseSchema, |
| 9 | + transactionWritesResponseSchema, |
| 10 | +} from "../../schemas/shared-api-schemas"; |
| 11 | +import { |
| 12 | + maybeAddress, |
| 13 | + walletChainParamSchema, |
| 14 | + walletWithAAHeaderSchema, |
| 15 | +} from "../../schemas/wallet"; |
| 16 | +import { getChainIdFromChain } from "../../utils/chain"; |
| 17 | +import { |
| 18 | + getWalletDetails, |
| 19 | + isSmartBackendWallet, |
| 20 | + type ParsedWalletDetails, |
| 21 | + WalletDetailsError, |
| 22 | +} from "../../../shared/db/wallets/get-wallet-details"; |
| 23 | +import { createCustomError } from "../../middleware/error"; |
| 24 | +import { RawTransactionParamsSchema } from "../../schemas/transaction/raw-transaction-parms"; |
| 25 | + |
| 26 | +const requestBodySchema = Type.Object({ |
| 27 | + transactions: Type.Array(RawTransactionParamsSchema, { |
| 28 | + minItems: 1, |
| 29 | + }), |
| 30 | +}); |
| 31 | + |
| 32 | +export async function sendTransactionBatchAtomicRoute(fastify: FastifyInstance) { |
| 33 | + fastify.route<{ |
| 34 | + Params: Static<typeof walletChainParamSchema>; |
| 35 | + Body: Static<typeof requestBodySchema>; |
| 36 | + Reply: Static<typeof transactionWritesResponseSchema>; |
| 37 | + Querystring: Static<typeof requestQuerystringSchema>; |
| 38 | + }>({ |
| 39 | + method: "POST", |
| 40 | + url: "/backend-wallet/:chain/send-transaction-batch-atomic", |
| 41 | + schema: { |
| 42 | + summary: "Send a batch of raw transactions atomically", |
| 43 | + description: |
| 44 | + "Send a batch of raw transactions in a single UserOp. Transactions will be sent in-order and atomically. Can only be used with smart wallets.", |
| 45 | + tags: ["Backend Wallet"], |
| 46 | + operationId: "sendTransactionBatchAtomic", |
| 47 | + params: walletChainParamSchema, |
| 48 | + body: requestBodySchema, |
| 49 | + headers: Type.Omit(walletWithAAHeaderSchema, ["x-transaction-mode"]), |
| 50 | + querystring: requestQuerystringSchema, |
| 51 | + response: { |
| 52 | + ...standardResponseSchema, |
| 53 | + [StatusCodes.OK]: transactionWritesResponseSchema, |
| 54 | + }, |
| 55 | + }, |
| 56 | + handler: async (request, reply) => { |
| 57 | + const { chain } = request.params; |
| 58 | + const { |
| 59 | + "x-backend-wallet-address": fromAddress, |
| 60 | + "x-idempotency-key": idempotencyKey, |
| 61 | + "x-account-address": accountAddress, |
| 62 | + "x-account-factory-address": accountFactoryAddress, |
| 63 | + "x-account-salt": accountSalt, |
| 64 | + } = request.headers as Static<typeof walletWithAAHeaderSchema>; |
| 65 | + const chainId = await getChainIdFromChain(chain); |
| 66 | + const shouldSimulate = request.query.simulateTx ?? false; |
| 67 | + const transactionRequests = request.body.transactions; |
| 68 | + |
| 69 | + const hasSmartHeaders = !!accountAddress; |
| 70 | + |
| 71 | + // check that we either use SBW, or send using EOA with smart wallet headers |
| 72 | + if (!hasSmartHeaders) { |
| 73 | + let backendWallet: ParsedWalletDetails | undefined; |
| 74 | + |
| 75 | + try { |
| 76 | + backendWallet = await getWalletDetails({ |
| 77 | + address: fromAddress, |
| 78 | + }); |
| 79 | + } catch (e: unknown) { |
| 80 | + if (e instanceof WalletDetailsError) { |
| 81 | + throw createCustomError( |
| 82 | + `Failed to get wallet details for backend wallet ${fromAddress}. ${e.message}`, |
| 83 | + StatusCodes.BAD_REQUEST, |
| 84 | + "WALLET_DETAILS_ERROR", |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (!backendWallet) { |
| 90 | + throw createCustomError( |
| 91 | + "Failed to get wallet details for backend wallet. See: https://portal.thirdweb.com/engine/troubleshooting", |
| 92 | + StatusCodes.INTERNAL_SERVER_ERROR, |
| 93 | + "WALLET_DETAILS_ERROR", |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + if (!isSmartBackendWallet(backendWallet)) { |
| 98 | + throw createCustomError( |
| 99 | + "Backend wallet is not a smart wallet, and x-account-address is not provided. Either use a smart backend wallet or provide x-account-address. This endpoint can only be used with smart wallets.", |
| 100 | + StatusCodes.BAD_REQUEST, |
| 101 | + "BACKEND_WALLET_NOT_SMART", |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (transactionRequests.length === 0) { |
| 107 | + throw createCustomError( |
| 108 | + "No transactions provided", |
| 109 | + StatusCodes.BAD_REQUEST, |
| 110 | + "NO_TRANSACTIONS_PROVIDED", |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + const queueId = await insertTransaction({ |
| 115 | + insertedTransaction: { |
| 116 | + transactionMode: undefined, |
| 117 | + isUserOp: false, |
| 118 | + chainId, |
| 119 | + from: fromAddress as Address, |
| 120 | + accountAddress: maybeAddress(accountAddress, "x-account-address"), |
| 121 | + accountFactoryAddress: maybeAddress( |
| 122 | + accountFactoryAddress, |
| 123 | + "x-account-factory-address", |
| 124 | + ), |
| 125 | + accountSalt: accountSalt, |
| 126 | + batchOperations: transactionRequests.map((transactionRequest) => ({ |
| 127 | + to: transactionRequest.toAddress as Address | undefined, |
| 128 | + data: transactionRequest.data as Hex, |
| 129 | + value: BigInt(transactionRequest.value), |
| 130 | + })), |
| 131 | + }, |
| 132 | + shouldSimulate, |
| 133 | + idempotencyKey, |
| 134 | + }); |
| 135 | + |
| 136 | + reply.status(StatusCodes.OK).send({ |
| 137 | + result: { |
| 138 | + queueId, |
| 139 | + }, |
| 140 | + }); |
| 141 | + }, |
| 142 | + }); |
| 143 | +} |
0 commit comments