|
1 | 1 | import { addHexPrefix, isHexString } from "@ethereumjs/util"; |
2 | | -import { sleep } from "@toruslabs/base-controllers"; |
3 | 2 | import { JRPCRequest, providerErrors } from "@web3auth/auth"; |
4 | 3 | import { IProvider, log } from "@web3auth/base"; |
5 | 4 | import { IProviderHandlers, MessageParams, SignTypedDataMessageV4, TransactionParams, TypedMessageParams } from "@web3auth/ethereum-provider"; |
6 | 5 | import { Chain, createWalletClient, Hex, http } from "viem"; |
7 | 6 | import { BundlerClient, SendUserOperationParameters, SmartAccount } from "viem/account-abstraction"; |
8 | 7 |
|
9 | | -type PollOptions<data> = { |
10 | | - // Whether or not to emit when the polling starts. |
11 | | - emitOnBegin?: boolean | undefined; |
12 | | - // The initial wait time (in ms) before polling. |
13 | | - initialWaitTime?: ((data: data | void) => Promise<number>) | undefined; |
14 | | - // The interval (in ms). |
15 | | - interval: number; |
16 | | -}; |
17 | | - |
18 | | -/** |
19 | | - * Polls a function at a specified interval. |
20 | | - * same poll function as viem/utils/poll |
21 | | - */ |
22 | | -export function poll<data>( |
23 | | - fn: ({ unpoll }: { unpoll: () => void }) => Promise<data | void>, |
24 | | - { emitOnBegin, initialWaitTime, interval }: PollOptions<data> |
25 | | -) { |
26 | | - let active = true; |
27 | | - |
28 | | - const unwatch = () => (active = false); |
29 | | - |
30 | | - const watch = async () => { |
31 | | - let data: data | void; |
32 | | - if (emitOnBegin) data = await fn({ unpoll: unwatch }); |
33 | | - |
34 | | - const initialWait = (await initialWaitTime?.(data)) ?? interval; |
35 | | - await sleep(initialWait); |
36 | | - |
37 | | - const _poll = async () => { |
38 | | - if (!active) return; |
39 | | - await fn({ unpoll: unwatch }); |
40 | | - await sleep(interval); |
41 | | - _poll(); |
42 | | - }; |
43 | | - |
44 | | - _poll(); |
45 | | - }; |
46 | | - watch(); |
47 | | - |
48 | | - return unwatch; |
49 | | -} |
50 | | - |
51 | 8 | export function getProviderHandlers({ |
52 | 9 | bundlerClient, |
53 | 10 | smartAccount, |
@@ -105,43 +62,14 @@ export function getProviderHandlers({ |
105 | 62 | // @ts-expect-error viem types are too deep |
106 | 63 | const userOpHash = await bundlerClient.sendUserOperation(userOperationParams); |
107 | 64 |
|
108 | | - const timeout = 120_000; |
109 | | - return new Promise((resolve, reject) => { |
110 | | - const done = (fn: () => void) => { |
111 | | - // eslint-disable-next-line @typescript-eslint/no-use-before-define, no-use-before-define |
112 | | - unpoll(); |
113 | | - fn(); |
114 | | - }; |
115 | | - |
116 | | - const unpoll = poll( |
117 | | - async () => { |
118 | | - // keep checking for user operation until it is online to return the transaction hash |
119 | | - // without needing to wait for the receipt |
120 | | - try { |
121 | | - const receipt = await bundlerClient.getUserOperation({ hash: userOpHash }); |
122 | | - done(() => resolve(receipt.transactionHash)); |
123 | | - } catch (error) { |
124 | | - if (!(error instanceof Error && error?.message?.toLowerCase()?.includes("could not be found"))) { |
125 | | - done(() => reject(error)); |
126 | | - } |
127 | | - } |
128 | | - }, |
129 | | - { |
130 | | - interval: 1000, |
131 | | - } |
132 | | - ); |
133 | | - |
134 | | - setTimeout(() => { |
135 | | - done(() => |
136 | | - reject( |
137 | | - providerErrors.custom({ |
138 | | - message: "Process transaction wait timeout.", |
139 | | - code: 4904, |
140 | | - }) |
141 | | - ) |
142 | | - ); |
143 | | - }, timeout); |
144 | | - }); |
| 65 | + const txReceipt = await bundlerClient.waitForUserOperationReceipt({ hash: userOpHash }); |
| 66 | + if (!txReceipt.success) { |
| 67 | + throw providerErrors.custom({ |
| 68 | + message: txReceipt.reason, |
| 69 | + code: 4905, |
| 70 | + }); |
| 71 | + } |
| 72 | + return txReceipt.receipt.transactionHash; |
145 | 73 | }, |
146 | 74 | processSignTransaction: async (txParams: TransactionParams): Promise<string> => { |
147 | 75 | const { to, value, data } = txParams; |
|
0 commit comments