|
1 | 1 | import { Type, type Static } from "@sinclair/typebox";
|
2 | 2 | import type { FastifyInstance } from "fastify";
|
3 | 3 | import { StatusCodes } from "http-status-codes";
|
4 |
| -import { eth_sendRawTransaction, getRpcClient, isHex } from "thirdweb"; |
| 4 | +import { eth_sendRawTransaction, getRpcClient, type Hex, isHex } from "thirdweb"; |
5 | 5 | import { getChain } from "../../../../shared/utils/chain";
|
6 | 6 | import { thirdwebClient } from "../../../../shared/utils/sdk";
|
7 | 7 | import { createCustomError } from "../../../middleware/error";
|
8 | 8 | import { TransactionHashSchema } from "../../../schemas/address";
|
9 | 9 | import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
|
10 | 10 | import { walletChainParamSchema } from "../../../schemas/wallet";
|
11 | 11 | import { getChainIdFromChain } from "../../../utils/chain";
|
| 12 | +import { isInsufficientFundsError, prettifyError } from "../../../../shared/utils/error"; |
12 | 13 |
|
13 | 14 | const requestBodySchema = Type.Object({
|
14 | 15 | signedTransaction: Type.String(),
|
@@ -57,10 +58,25 @@ export async function sendSignedTransaction(fastify: FastifyInstance) {
|
57 | 58 | client: thirdwebClient,
|
58 | 59 | chain: await getChain(chainId),
|
59 | 60 | });
|
60 |
| - const transactionHash = await eth_sendRawTransaction( |
61 |
| - rpcRequest, |
62 |
| - signedTransaction, |
63 |
| - ); |
| 61 | + |
| 62 | + let transactionHash: Hex; |
| 63 | + try { |
| 64 | + transactionHash = await eth_sendRawTransaction( |
| 65 | + rpcRequest, |
| 66 | + signedTransaction, |
| 67 | + ); |
| 68 | + } catch (error) { |
| 69 | + // Return 400 for client errors. |
| 70 | + const isClientError = isInsufficientFundsError(error); |
| 71 | + if (isClientError) { |
| 72 | + throw createCustomError( |
| 73 | + prettifyError(error), |
| 74 | + StatusCodes.BAD_REQUEST, |
| 75 | + "CLIENT_RPC_ERROR", |
| 76 | + ); |
| 77 | + } |
| 78 | + throw error; |
| 79 | + } |
64 | 80 |
|
65 | 81 | res.status(StatusCodes.OK).send({
|
66 | 82 | result: {
|
|
0 commit comments