Skip to content

Commit df30da0

Browse files
clean up poll
1 parent d6c4505 commit df30da0

File tree

2 files changed

+9
-81
lines changed

2 files changed

+9
-81
lines changed

packages/providers/account-abstraction-provider/src/providers/AccountAbstractionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class AccountAbstractionProvider extends BaseProvider<AccountAbstractionP
9797
// setup public client for viem smart account
9898
this._publicClient = createPublicClient({
9999
chain,
100-
transport: http(),
100+
transport: http(this.config.chainConfig.rpcTarget),
101101
}) as Client;
102102
this._smartAccount = await this.config.smartAccountInit.getSmartAccount({
103103
owner: eoaProvider,

packages/providers/account-abstraction-provider/src/providers/utils.ts

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,10 @@
11
import { addHexPrefix, isHexString } from "@ethereumjs/util";
2-
import { sleep } from "@toruslabs/base-controllers";
32
import { JRPCRequest, providerErrors } from "@web3auth/auth";
43
import { IProvider, log } from "@web3auth/base";
54
import { IProviderHandlers, MessageParams, SignTypedDataMessageV4, TransactionParams, TypedMessageParams } from "@web3auth/ethereum-provider";
65
import { Chain, createWalletClient, Hex, http } from "viem";
76
import { BundlerClient, SendUserOperationParameters, SmartAccount } from "viem/account-abstraction";
87

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-
518
export function getProviderHandlers({
529
bundlerClient,
5310
smartAccount,
@@ -105,43 +62,14 @@ export function getProviderHandlers({
10562
// @ts-expect-error viem types are too deep
10663
const userOpHash = await bundlerClient.sendUserOperation(userOperationParams);
10764

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;
14573
},
14674
processSignTransaction: async (txParams: TransactionParams): Promise<string> => {
14775
const { to, value, data } = txParams;

0 commit comments

Comments
 (0)