-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathsend-signed-tx.ts
More file actions
88 lines (81 loc) · 2.73 KB
/
Copy pathsend-signed-tx.ts
File metadata and controls
88 lines (81 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
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";
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(),
});
const responseBodySchema = Type.Object({
result: Type.Object({
transactionHash: TransactionHashSchema,
}),
});
export async function sendSignedTransaction(fastify: FastifyInstance) {
fastify.route<{
Params: Static<typeof walletChainParamSchema>;
Body: Static<typeof requestBodySchema>;
Reply: Static<typeof responseBodySchema>;
}>({
method: "POST",
url: "/transaction/:chain/send-signed-transaction",
schema: {
summary: "Send a signed transaction",
description: "Send a signed transaction",
tags: ["Transaction"],
operationId: "sendRawTransaction",
params: walletChainParamSchema,
body: requestBodySchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: responseBodySchema,
},
},
handler: async (req, res) => {
const { chain } = req.params;
const { signedTransaction } = req.body;
const chainId = await getChainIdFromChain(chain);
if (!isHex(signedTransaction)) {
throw createCustomError(
"SignedTransaction is not a valid hex string.",
StatusCodes.BAD_REQUEST,
"SendSignedTxError",
);
}
const rpcRequest = getRpcClient({
client: thirdwebClient,
chain: await getChain(chainId),
});
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: {
transactionHash,
},
});
},
});
}