|
| 1 | +import { Static, Type } from "@sinclair/typebox"; |
| 2 | +import { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import { getSdk } from "../../../../utils/cache/getSdk"; |
| 5 | +import { standardResponseSchema } from "../../../schemas/sharedApiSchemas"; |
| 6 | +import { getChainIdFromChain } from "../../../utils/chain"; |
| 7 | + |
| 8 | +// INPUT |
| 9 | +const requestSchema = Type.Object({ |
| 10 | + txHash: Type.String({ |
| 11 | + description: "Transaction hash", |
| 12 | + examples: [ |
| 13 | + "0xd9bcba8f5bc4ce5bf4d631b2a0144329c1df3b56ddb9fc64637ed3a4219dd087", |
| 14 | + ], |
| 15 | + pattern: "^0x([A-Fa-f0-9]{64})$", |
| 16 | + }), |
| 17 | + chain: Type.String({ |
| 18 | + examples: ["mumbai"], |
| 19 | + description: "Chain ID or name", |
| 20 | + }), |
| 21 | +}); |
| 22 | + |
| 23 | +// OUTPUT |
| 24 | +export const responseBodySchema = Type.Object({ |
| 25 | + result: Type.Union([ |
| 26 | + Type.Partial( |
| 27 | + Type.Object({ |
| 28 | + to: Type.String(), |
| 29 | + from: Type.String(), |
| 30 | + contractAddress: Type.Union([Type.String(), Type.Null()]), |
| 31 | + transactionIndex: Type.Number(), |
| 32 | + root: Type.String(), |
| 33 | + gasUsed: Type.String(), |
| 34 | + logsBloom: Type.String(), |
| 35 | + blockHash: Type.String(), |
| 36 | + transactionHash: Type.String(), |
| 37 | + logs: Type.Array(Type.Any()), |
| 38 | + blockNumber: Type.Number(), |
| 39 | + confirmations: Type.Number(), |
| 40 | + cumulativeGasUsed: Type.String(), |
| 41 | + effectiveGasPrice: Type.String(), |
| 42 | + byzantium: Type.Boolean(), |
| 43 | + type: Type.Number(), |
| 44 | + status: Type.Number(), |
| 45 | + }), |
| 46 | + ), |
| 47 | + Type.Null(), |
| 48 | + ]), |
| 49 | +}); |
| 50 | + |
| 51 | +responseBodySchema.example = { |
| 52 | + result: { |
| 53 | + to: "0xd7419703c2D5737646525A8660906eCb612875BD", |
| 54 | + from: "0x9783Eb2a93A58b24CFeC56F94b30aB6e29fF4b38", |
| 55 | + contractAddress: null, |
| 56 | + transactionIndex: 69, |
| 57 | + gasUsed: "21000", |
| 58 | + logsBloom: |
| 59 | + "0x00000000000200000000000000000000000000000000000000000000000000000000000000000000000000100002000000008000000000000000000000002000000000000000000000000000000000800000000000000000000100000000040000000000000000000000000000000000008000000000000080000000000000000000000000000000000000000000000000000000000000040000000000000000200000000000004000800000000000000000000000000000000000000000004000000000000000000001000000000000000000000000800000108000000000000000000000000000000000000000000000000000000000000000008000100000", |
| 60 | + blockHash: |
| 61 | + "0x9be85de9e6a0717ed2e7c9035f7bd748a4b20bc9d6e04a6875fa69311421d971", |
| 62 | + transactionHash: |
| 63 | + "0xd9bcba8f5bc4ce5bf4d631b2a0144329c1df3b56ddb9fc64637ed3a4219dd087", |
| 64 | + logs: [ |
| 65 | + { |
| 66 | + transactionIndex: 69, |
| 67 | + blockNumber: 51048531, |
| 68 | + transactionHash: |
| 69 | + "0xd9bcba8f5bc4ce5bf4d631b2a0144329c1df3b56ddb9fc64637ed3a4219dd087", |
| 70 | + address: "0x0000000000000000000000000000000000001010", |
| 71 | + topics: [ |
| 72 | + "0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4", |
| 73 | + "0x0000000000000000000000000000000000000000000000000000000000001010", |
| 74 | + "0x0000000000000000000000009783eb2a93a58b24cfec56f94b30ab6e29ff4b38", |
| 75 | + "0x000000000000000000000000d7419703c2d5737646525a8660906ecb612875bd", |
| 76 | + ], |
| 77 | + data: "0x00000000000000000000000000000000000000000000000006a4d6a25acff49800000000000000000000000000000000000000000000000006b787e1bb9f06f80000000000000000000000000000000000000000000000051ac78aecb02246820000000000000000000000000000000000000000000000000012b13f60cf1260000000000000000000000000000000000000000000000005216c618f0af23b1a", |
| 78 | + logIndex: 181, |
| 79 | + blockHash: |
| 80 | + "0x9be85de9e6a0717ed2e7c9035f7bd748a4b20bc9d6e04a6875fa69311421d971", |
| 81 | + }, |
| 82 | + { |
| 83 | + transactionIndex: 69, |
| 84 | + blockNumber: 51048531, |
| 85 | + transactionHash: |
| 86 | + "0xd9bcba8f5bc4ce5bf4d631b2a0144329c1df3b56ddb9fc64637ed3a4219dd087", |
| 87 | + address: "0x0000000000000000000000000000000000001010", |
| 88 | + topics: [ |
| 89 | + "0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63", |
| 90 | + "0x0000000000000000000000000000000000000000000000000000000000001010", |
| 91 | + "0x0000000000000000000000009783eb2a93a58b24cfec56f94b30ab6e29ff4b38", |
| 92 | + "0x000000000000000000000000a8b52f02108aa5f4b675bdcc973760022d7c6020", |
| 93 | + ], |
| 94 | + data: "0x00000000000000000000000000000000000000000000000000046a2c9f9fd11800000000000000000000000000000000000000000000000006ce8dc1a70476680000000000000000000000000000000000000000000006df390338516c1391c300000000000000000000000000000000000000000000000006ca23950764a5500000000000000000000000000000000000000000000006df3907a27e0bb362db", |
| 95 | + logIndex: 182, |
| 96 | + blockHash: |
| 97 | + "0x9be85de9e6a0717ed2e7c9035f7bd748a4b20bc9d6e04a6875fa69311421d971", |
| 98 | + }, |
| 99 | + ], |
| 100 | + blockNumber: 51048531, |
| 101 | + confirmations: 3232643, |
| 102 | + cumulativeGasUsed: "5751865", |
| 103 | + effectiveGasPrice: "257158085297", |
| 104 | + status: 1, |
| 105 | + type: 2, |
| 106 | + byzantium: true, |
| 107 | + }, |
| 108 | +}; |
| 109 | + |
| 110 | +export async function getTxHashReceipt(fastify: FastifyInstance) { |
| 111 | + fastify.route<{ |
| 112 | + Params: Static<typeof requestSchema>; |
| 113 | + Reply: Static<typeof responseBodySchema>; |
| 114 | + }>({ |
| 115 | + method: "GET", |
| 116 | + url: "/transaction/:chain/tx-hash/:txHash", |
| 117 | + schema: { |
| 118 | + summary: "Get transaction receipt from transaction hash", |
| 119 | + description: "Get the transaction receipt from a transaction hash.", |
| 120 | + tags: ["Transaction"], |
| 121 | + operationId: "txHashReceipt", |
| 122 | + params: requestSchema, |
| 123 | + response: { |
| 124 | + ...standardResponseSchema, |
| 125 | + [StatusCodes.OK]: responseBodySchema, |
| 126 | + }, |
| 127 | + }, |
| 128 | + handler: async (request, reply) => { |
| 129 | + const { chain, txHash } = request.params; |
| 130 | + |
| 131 | + const chainId = await getChainIdFromChain(chain); |
| 132 | + const sdk = await getSdk({ chainId }); |
| 133 | + const receipt = await sdk.getProvider().getTransactionReceipt(txHash); |
| 134 | + |
| 135 | + reply.status(StatusCodes.OK).send({ |
| 136 | + result: receipt |
| 137 | + ? { |
| 138 | + ...receipt, |
| 139 | + gasUsed: receipt.gasUsed.toString(), |
| 140 | + cumulativeGasUsed: receipt.cumulativeGasUsed.toString(), |
| 141 | + effectiveGasPrice: receipt.effectiveGasPrice.toString(), |
| 142 | + } |
| 143 | + : null, |
| 144 | + }); |
| 145 | + }, |
| 146 | + }); |
| 147 | +} |
0 commit comments