From b08eec384dcbcdf10b6ce02b1cd2ad348d17f9f3 Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Sat, 17 May 2025 08:26:16 +0800 Subject: [PATCH] fix: return 400 for client-side errors for send-signed-transaction --- .../transaction/blockchain/send-signed-tx.ts | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/server/routes/transaction/blockchain/send-signed-tx.ts b/src/server/routes/transaction/blockchain/send-signed-tx.ts index 862fc261b..ae1791227 100644 --- a/src/server/routes/transaction/blockchain/send-signed-tx.ts +++ b/src/server/routes/transaction/blockchain/send-signed-tx.ts @@ -1,7 +1,7 @@ import { Type, type Static } from "@sinclair/typebox"; import type { FastifyInstance } from "fastify"; import { StatusCodes } from "http-status-codes"; -import { eth_sendRawTransaction, getRpcClient, isHex } from "thirdweb"; +import { eth_sendRawTransaction, getRpcClient, type Hex, isHex } from "thirdweb"; import { getChain } from "../../../../shared/utils/chain"; import { thirdwebClient } from "../../../../shared/utils/sdk"; import { createCustomError } from "../../../middleware/error"; @@ -9,6 +9,7 @@ import { TransactionHashSchema } from "../../../schemas/address"; import { standardResponseSchema } from "../../../schemas/shared-api-schemas"; import { walletChainParamSchema } from "../../../schemas/wallet"; import { getChainIdFromChain } from "../../../utils/chain"; +import { isInsufficientFundsError, prettifyError } from "../../../../shared/utils/error"; const requestBodySchema = Type.Object({ signedTransaction: Type.String(), @@ -57,10 +58,25 @@ export async function sendSignedTransaction(fastify: FastifyInstance) { client: thirdwebClient, chain: await getChain(chainId), }); - const transactionHash = await eth_sendRawTransaction( - rpcRequest, - signedTransaction, - ); + + let transactionHash: Hex; + try { + transactionHash = await eth_sendRawTransaction( + rpcRequest, + signedTransaction, + ); + } catch (error) { + // Return 400 for client errors. + const isClientError = isInsufficientFundsError(error); + if (isClientError) { + throw createCustomError( + prettifyError(error), + StatusCodes.BAD_REQUEST, + "CLIENT_RPC_ERROR", + ); + } + throw error; + } res.status(StatusCodes.OK).send({ result: {