Skip to content

Commit 3a5dce9

Browse files
authored
fix: return 400 for client-side errors for send-signed-transaction (#892)
1 parent 6068902 commit 3a5dce9

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/server/routes/transaction/blockchain/send-signed-tx.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Type, type Static } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { eth_sendRawTransaction, getRpcClient, isHex } from "thirdweb";
4+
import { eth_sendRawTransaction, getRpcClient, type Hex, isHex } from "thirdweb";
55
import { getChain } from "../../../../shared/utils/chain";
66
import { thirdwebClient } from "../../../../shared/utils/sdk";
77
import { createCustomError } from "../../../middleware/error";
88
import { TransactionHashSchema } from "../../../schemas/address";
99
import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
1010
import { walletChainParamSchema } from "../../../schemas/wallet";
1111
import { getChainIdFromChain } from "../../../utils/chain";
12+
import { isInsufficientFundsError, prettifyError } from "../../../../shared/utils/error";
1213

1314
const requestBodySchema = Type.Object({
1415
signedTransaction: Type.String(),
@@ -57,10 +58,25 @@ export async function sendSignedTransaction(fastify: FastifyInstance) {
5758
client: thirdwebClient,
5859
chain: await getChain(chainId),
5960
});
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+
}
6480

6581
res.status(StatusCodes.OK).send({
6682
result: {

0 commit comments

Comments
 (0)