|
| 1 | +import { searchTransactions } from "@thirdweb-dev/engine"; |
| 2 | +import type { Chain } from "../chains/types.js"; |
| 3 | +import { getCachedChain } from "../chains/utils.js"; |
| 4 | +import type { ThirdwebClient } from "../client/client.js"; |
| 5 | +import type { WaitForReceiptOptions } from "../transaction/actions/wait-for-tx-receipt.js"; |
| 6 | +import { getThirdwebBaseUrl } from "../utils/domains.js"; |
| 7 | +import type { Hex } from "../utils/encoding/hex.js"; |
| 8 | +import { getClientFetch } from "../utils/fetch.js"; |
| 9 | +import { stringify } from "../utils/json.js"; |
| 10 | +import type { Prettify } from "../utils/type-utils.js"; |
| 11 | + |
| 12 | +export type RevertData = { |
| 13 | + errorName: string; |
| 14 | + errorArgs: Record<string, unknown>; |
| 15 | +}; |
| 16 | + |
| 17 | +type ExecutionResult4337Serialized = |
| 18 | + | { |
| 19 | + status: "QUEUED"; |
| 20 | + } |
| 21 | + | { |
| 22 | + status: "FAILED"; |
| 23 | + error: string; |
| 24 | + } |
| 25 | + | { |
| 26 | + status: "SUBMITTED"; |
| 27 | + monitoringStatus: "WILL_MONITOR" | "CANNOT_MONITOR"; |
| 28 | + userOpHash: string; |
| 29 | + } |
| 30 | + | ({ |
| 31 | + status: "CONFIRMED"; |
| 32 | + userOpHash: Hex; |
| 33 | + transactionHash: Hex; |
| 34 | + actualGasCost: string; |
| 35 | + actualGasUsed: string; |
| 36 | + nonce: string; |
| 37 | + } & ( |
| 38 | + | { |
| 39 | + onchainStatus: "SUCCESS"; |
| 40 | + } |
| 41 | + | { |
| 42 | + onchainStatus: "REVERTED"; |
| 43 | + revertData?: RevertData; |
| 44 | + } |
| 45 | + )); |
| 46 | + |
| 47 | +export type ExecutionResult = Prettify< |
| 48 | + ExecutionResult4337Serialized & { |
| 49 | + chain: Chain; |
| 50 | + from: string | undefined; |
| 51 | + id: string; |
| 52 | + } |
| 53 | +>; |
| 54 | + |
| 55 | +/** |
| 56 | + * Get the execution status of a transaction. |
| 57 | + * @param args - The arguments for the transaction. |
| 58 | + * @param args.client - The thirdweb client to use. |
| 59 | + * @param args.transactionId - The id of the transaction to get the status of. |
| 60 | + * @engine |
| 61 | + * @example |
| 62 | + * ```ts |
| 63 | + * import { Engine } from "thirdweb"; |
| 64 | + * |
| 65 | + * const executionResult = await Engine.getTransactionStatus({ |
| 66 | + * client, |
| 67 | + * transactionId, |
| 68 | + * }); |
| 69 | + * console.log(executionResult.status); |
| 70 | + * ``` |
| 71 | + */ |
| 72 | +export async function getTransactionStatus(args: { |
| 73 | + client: ThirdwebClient; |
| 74 | + transactionId: string; |
| 75 | +}): Promise<ExecutionResult> { |
| 76 | + const { client, transactionId } = args; |
| 77 | + const searchResult = await searchTransactions({ |
| 78 | + baseUrl: getThirdwebBaseUrl("engineCloud"), |
| 79 | + fetch: getClientFetch(client), |
| 80 | + body: { |
| 81 | + filters: [ |
| 82 | + { |
| 83 | + field: "id", |
| 84 | + values: [transactionId], |
| 85 | + operation: "OR", |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + if (searchResult.error) { |
| 92 | + throw new Error( |
| 93 | + `Error searching for transaction ${transactionId}: ${stringify( |
| 94 | + searchResult.error, |
| 95 | + )}`, |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + const data = searchResult.data?.result?.transactions?.[0]; |
| 100 | + |
| 101 | + if (!data) { |
| 102 | + throw new Error(`Transaction ${transactionId} not found`); |
| 103 | + } |
| 104 | + |
| 105 | + const executionResult = data.executionResult as ExecutionResult4337Serialized; |
| 106 | + return { |
| 107 | + ...executionResult, |
| 108 | + chain: getCachedChain(Number(data.chainId)), |
| 109 | + from: data.from ?? undefined, |
| 110 | + id: data.id, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Wait for a transaction to be submitted onchain and return the transaction hash. |
| 116 | + * @param args - The arguments for the transaction. |
| 117 | + * @param args.client - The thirdweb client to use. |
| 118 | + * @param args.transactionId - The id of the transaction to wait for. |
| 119 | + * @param args.timeoutInSeconds - The timeout in seconds. |
| 120 | + * @engine |
| 121 | + * @example |
| 122 | + * ```ts |
| 123 | + * import { Engine } from "thirdweb"; |
| 124 | + * |
| 125 | + * const { transactionHash } = await Engine.waitForTransactionHash({ |
| 126 | + * client, |
| 127 | + * transactionId, // the transaction id returned from enqueueTransaction |
| 128 | + * }); |
| 129 | + * ``` |
| 130 | + */ |
| 131 | +export async function waitForTransactionHash(args: { |
| 132 | + client: ThirdwebClient; |
| 133 | + transactionId: string; |
| 134 | + timeoutInSeconds?: number; |
| 135 | +}): Promise<WaitForReceiptOptions> { |
| 136 | + const startTime = Date.now(); |
| 137 | + const TIMEOUT_IN_MS = args.timeoutInSeconds |
| 138 | + ? args.timeoutInSeconds * 1000 |
| 139 | + : 5 * 60 * 1000; // 5 minutes in milliseconds |
| 140 | + |
| 141 | + while (Date.now() - startTime < TIMEOUT_IN_MS) { |
| 142 | + const executionResult = await getTransactionStatus(args); |
| 143 | + const status = executionResult.status; |
| 144 | + |
| 145 | + switch (status) { |
| 146 | + case "FAILED": { |
| 147 | + throw new Error( |
| 148 | + `Transaction failed: ${executionResult.error || "Unknown error"}`, |
| 149 | + ); |
| 150 | + } |
| 151 | + case "CONFIRMED": { |
| 152 | + const onchainStatus = |
| 153 | + executionResult && "onchainStatus" in executionResult |
| 154 | + ? executionResult.onchainStatus |
| 155 | + : null; |
| 156 | + if (onchainStatus === "REVERTED") { |
| 157 | + const revertData = |
| 158 | + "revertData" in executionResult |
| 159 | + ? executionResult.revertData |
| 160 | + : undefined; |
| 161 | + throw new Error( |
| 162 | + `Transaction reverted: ${revertData?.errorName || ""} ${revertData?.errorArgs ? stringify(revertData.errorArgs) : ""}`, |
| 163 | + ); |
| 164 | + } |
| 165 | + return { |
| 166 | + transactionHash: executionResult.transactionHash as Hex, |
| 167 | + client: args.client, |
| 168 | + chain: executionResult.chain, |
| 169 | + }; |
| 170 | + } |
| 171 | + default: { |
| 172 | + // wait for the transaction to be confirmed |
| 173 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + throw new Error( |
| 178 | + `Transaction timed out after ${TIMEOUT_IN_MS / 1000} seconds`, |
| 179 | + ); |
| 180 | +} |
0 commit comments