@smoothsend/sdk 2.3.1 → 2.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,459 @@
1
+ import { PublicClient, WalletClient, Address, Hex } from 'viem';
2
+ import { UserOperation } from 'viem/account-abstraction';
3
+ import * as react_jsx_runtime from 'react/jsx-runtime';
4
+ import { ReactNode } from 'react';
5
+
6
+ /**
7
+ * AVAX / ERC-4337 v0.7 types aligned with `chains/avax/bundler` API shapes.
8
+ */
9
+ type AvaxSponsorshipMode = 'developer-sponsored' | 'user-pays-erc20';
10
+ /** Unpacked UserOperation as accepted by the bundler JSON-RPC + paymaster/sign */
11
+ interface UserOperationAvax {
12
+ sender: string;
13
+ nonce: string;
14
+ factory?: string;
15
+ factoryData?: string;
16
+ initCode?: string;
17
+ callData: string;
18
+ callGasLimit?: string;
19
+ verificationGasLimit?: string;
20
+ preVerificationGas?: string;
21
+ maxFeePerGas?: string;
22
+ maxPriorityFeePerGas?: string;
23
+ paymaster?: string;
24
+ paymasterVerificationGasLimit?: string;
25
+ paymasterPostOpGasLimit?: string;
26
+ paymasterData?: string;
27
+ accountGasLimits?: string;
28
+ gasFees?: string;
29
+ paymasterAndData?: string;
30
+ /** Empty (`0x`) until the wallet/account signs the hash UserOp */
31
+ signature: string;
32
+ }
33
+ interface GasEstimateAvax {
34
+ preVerificationGas: string;
35
+ verificationGasLimit: string;
36
+ callGasLimit: string;
37
+ paymasterVerificationGasLimit?: string;
38
+ paymasterPostOpGasLimit?: string;
39
+ }
40
+ interface PaymasterSignRequestAvax {
41
+ mode: AvaxSponsorshipMode;
42
+ userOp: UserOperationAvax;
43
+ token?: string;
44
+ receiver?: string;
45
+ precheckBalance?: boolean;
46
+ prepaymentRequired?: boolean;
47
+ allowAnyBundler?: boolean;
48
+ sponsorUUID?: string;
49
+ }
50
+ interface AvaxFeePreview {
51
+ predictedGasCostWei: string;
52
+ predictedTokenFee: string;
53
+ minTokenFee: string;
54
+ variableTokenFee: string;
55
+ floorApplied: boolean;
56
+ }
57
+ interface AvaxHashTypes {
58
+ sendUserOperationResult?: 'userOpHash';
59
+ explorerTxLinkRequires?: 'transactionHash';
60
+ userOpHash?: 'userOpHash';
61
+ transactionHash?: 'transactionHash';
62
+ }
63
+ interface PaymasterSignResponseAvax {
64
+ success: boolean;
65
+ requestId?: string;
66
+ network?: string;
67
+ hashTypes?: AvaxHashTypes;
68
+ paymasterAndData: string;
69
+ paymasterData?: string;
70
+ signature?: string;
71
+ exchangeRate?: string;
72
+ feePreview?: AvaxFeePreview;
73
+ paymasterDataParts?: Record<string, string>;
74
+ error?: string;
75
+ }
76
+ interface UserOperationReceiptAvax {
77
+ userOpHash: string;
78
+ sender: string;
79
+ nonce: string;
80
+ paymaster: string;
81
+ success: boolean;
82
+ actualGasCost: string;
83
+ actualGasUsed: string;
84
+ reason?: string;
85
+ logs: unknown[];
86
+ receipt: {
87
+ transactionHash: string;
88
+ blockNumber: number;
89
+ blockHash: string;
90
+ };
91
+ hashTypes?: AvaxHashTypes;
92
+ }
93
+ /** Partial UserOp before estimate/paymaster (signature optional, usually `0x` until wallet signs). */
94
+ type SponsoredUserOpDraftAvax = Omit<UserOperationAvax, 'signature'> & {
95
+ signature?: string;
96
+ };
97
+
98
+ /**
99
+ * Public gateway defaults for Avalanche AA (no API key).
100
+ */
101
+ /** Canonical ERC-4337 EntryPoint v0.7 on Fuji / Avalanche C-Chain */
102
+ declare const ENTRY_POINT_V07_ADDRESS: "0x0000000071727De22E5E9d8BAf0edAc6f37da032";
103
+ type AvaxAaPublicDefaults = {
104
+ success?: boolean;
105
+ simpleAccountFactoryFuji: `0x${string}` | null;
106
+ simpleAccountFactoryMainnet: `0x${string}` | null;
107
+ entryPointV07: typeof ENTRY_POINT_V07_ADDRESS;
108
+ paymasterFuji: `0x${string}` | null;
109
+ paymasterMainnet: `0x${string}` | null;
110
+ };
111
+ /**
112
+ * Fetch SimpleAccount factory and Paymaster hints from SmoothSend gateway (GET; no auth).
113
+ */
114
+ declare function fetchAvaxAaPublicDefaults(gatewayUrl?: string): Promise<AvaxAaPublicDefaults>;
115
+
116
+ /** Options for {@link SmoothSendAvaxSubmitter.submitSponsoredUserOperation} */
117
+ interface SubmitSponsoredAvaxUserOpOptions {
118
+ /** Sender must match your smart account; nonce/callData/fees from your app */
119
+ userOp: SponsoredUserOpDraftAvax;
120
+ /** Wallet / viem / ethers signs the UserOp hash your stack expects */
121
+ signUserOp: (userOp: UserOperationAvax) => Promise<string>;
122
+ mode?: AvaxSponsorshipMode;
123
+ /** Defaults to first entry point from `eth_supportedEntryPoints` */
124
+ entryPoint?: string;
125
+ /** Extra `paymaster/sign` fields (`token`, `receiver`, …) for user-pays-ERC20 */
126
+ paymaster?: Omit<PaymasterSignRequestAvax, 'mode' | 'userOp'>;
127
+ /** @default true */
128
+ waitForReceipt?: boolean;
129
+ receiptPoll?: {
130
+ pollMs?: number;
131
+ timeoutMs?: number;
132
+ };
133
+ }
134
+ interface EstimateUserPaysFeeAvaxOptions {
135
+ /** Draft UserOperation (same shape as submit flow before signing) */
136
+ userOp: SponsoredUserOpDraftAvax;
137
+ /** Optional entrypoint, defaults to eth_supportedEntryPoints()[0] */
138
+ entryPoint?: string;
139
+ /** Optional paymaster params such as token/receiver overrides */
140
+ paymaster?: Omit<PaymasterSignRequestAvax, 'mode' | 'userOp'>;
141
+ }
142
+ interface SmoothSendAvaxSubmitterConfig {
143
+ apiKey: string;
144
+ network?: 'testnet' | 'mainnet';
145
+ gatewayUrl?: string;
146
+ timeout?: number;
147
+ /**
148
+ * Gateway chain routing bucket. Default `avalanche` matches `core/gateway` routing.
149
+ */
150
+ chain?: string;
151
+ /**
152
+ * Public keys (`pk_nogas_*`) require an `Origin` header matching your project's allowed CORS origins.
153
+ * Set this for Node/scripts (e.g. `http://localhost:5173`). Browsers send Origin automatically.
154
+ */
155
+ corsOrigin?: string;
156
+ }
157
+ declare class SmoothSendAvaxSubmitter {
158
+ private readonly config;
159
+ private readonly http;
160
+ private rpcId;
161
+ constructor(config: SmoothSendAvaxSubmitterConfig);
162
+ getConfig(): Readonly<SmoothSendAvaxSubmitterConfig>;
163
+ /** Merge gas estimate fields into a partial UserOp */
164
+ static applyGasEstimate(userOp: UserOperationAvax, gas: GasEstimateAvax): UserOperationAvax;
165
+ /** Attach packed paymaster data returned from {@link paymasterSign} */
166
+ static applyPaymasterAndData(userOp: UserOperationAvax, paymasterAndData: string): UserOperationAvax;
167
+ getChainId(): Promise<string>;
168
+ getSupportedEntryPoints(): Promise<string[]>;
169
+ estimateUserOperationGas(userOp: UserOperationAvax, entryPoint: string): Promise<GasEstimateAvax>;
170
+ sendUserOperation(userOp: UserOperationAvax, entryPoint: string): Promise<string>;
171
+ getUserOperationReceipt(userOpHash: string): Promise<UserOperationReceiptAvax | null>;
172
+ waitForUserOperationReceipt(userOpHash: string, options?: {
173
+ pollMs?: number;
174
+ timeoutMs?: number;
175
+ }): Promise<UserOperationReceiptAvax | null>;
176
+ paymasterSign(body: PaymasterSignRequestAvax): Promise<PaymasterSignResponseAvax>;
177
+ /**
178
+ * Preflight fee estimate for `user-pays-erc20` mode.
179
+ *
180
+ * This performs:
181
+ * 1) `eth_estimateUserOperationGas`
182
+ * 2) `paymaster/sign` in `user-pays-erc20` mode
183
+ * and returns `feePreview` when available.
184
+ */
185
+ estimateUserPaysFee(opts: EstimateUserPaysFeeAvaxOptions): Promise<{
186
+ entryPoint: string;
187
+ userOp: UserOperationAvax;
188
+ gas: GasEstimateAvax;
189
+ exchangeRate?: string;
190
+ feePreview?: AvaxFeePreview;
191
+ }>;
192
+ getBundlerHealth(): Promise<unknown>;
193
+ /** Fetch public AA addresses (Factory, Paymaster) from gateway. */
194
+ getPublicAaDefaults(): Promise<AvaxAaPublicDefaults>;
195
+ /**
196
+ * One call: estimate gas → paymaster/sign → merge → your sign → send → (optional) wait for receipt.
197
+ */
198
+ submitSponsoredUserOperation(opts: SubmitSponsoredAvaxUserOpOptions): Promise<{
199
+ userOpHash: string;
200
+ receipt: UserOperationReceiptAvax | null;
201
+ }>;
202
+ private rpc;
203
+ }
204
+ /**
205
+ * @alias {@link SmoothSendAvaxSubmitter}
206
+ */
207
+ declare const AvaxSubmitter: typeof SmoothSendAvaxSubmitter;
208
+ declare function createSmoothSendAvaxSubmitter(config: SmoothSendAvaxSubmitterConfig): SmoothSendAvaxSubmitter;
209
+
210
+ interface SmoothSendAvaxClientConfig {
211
+ apiKey: string;
212
+ network?: 'testnet' | 'mainnet';
213
+ gatewayUrl?: string;
214
+ timeout?: number;
215
+ chain?: string;
216
+ corsOrigin?: string;
217
+ publicClient: PublicClient;
218
+ walletClient: WalletClient;
219
+ smartAccountAddress?: Address;
220
+ accountFactory?: Address;
221
+ accountSalt?: bigint;
222
+ }
223
+ interface SimpleCallInput {
224
+ to: Address;
225
+ data?: Hex;
226
+ value?: bigint;
227
+ }
228
+ declare class SmoothSendAvaxClient {
229
+ readonly submitter: SmoothSendAvaxSubmitter;
230
+ readonly config: SmoothSendAvaxClientConfig;
231
+ constructor(config: SmoothSendAvaxClientConfig);
232
+ submitCall(args: {
233
+ call: SimpleCallInput;
234
+ mode?: AvaxSponsorshipMode;
235
+ paymaster?: Omit<PaymasterSignRequestAvax, 'mode' | 'userOp'>;
236
+ smartAccountAddress?: Address;
237
+ accountFactory?: Address;
238
+ accountSalt?: bigint;
239
+ waitForReceipt?: boolean;
240
+ }): Promise<{
241
+ userOpHash: string;
242
+ transactionHash: string | undefined;
243
+ receipt: UserOperationReceiptAvax | null;
244
+ }>;
245
+ submitCalls(args: {
246
+ calls: SimpleCallInput[];
247
+ mode?: AvaxSponsorshipMode;
248
+ paymaster?: Omit<PaymasterSignRequestAvax, 'mode' | 'userOp'>;
249
+ smartAccountAddress?: Address;
250
+ accountFactory?: Address;
251
+ accountSalt?: bigint;
252
+ waitForReceipt?: boolean;
253
+ }): Promise<{
254
+ userOpHash: string;
255
+ transactionHash: string | undefined;
256
+ receipt: UserOperationReceiptAvax | null;
257
+ }>;
258
+ estimateUserPaysFee(args: {
259
+ calls: SimpleCallInput[];
260
+ paymaster?: Omit<PaymasterSignRequestAvax, 'mode' | 'userOp'>;
261
+ smartAccountAddress?: Address;
262
+ accountFactory?: Address;
263
+ accountSalt?: bigint;
264
+ }): Promise<{
265
+ feePreview?: AvaxFeePreview;
266
+ exchangeRate?: string;
267
+ entryPoint: string;
268
+ }>;
269
+ private buildDraft;
270
+ private signUserOpHash;
271
+ }
272
+ declare function createSmoothSendAvaxClient(config: SmoothSendAvaxClientConfig): SmoothSendAvaxClient;
273
+
274
+ /**
275
+ * Viem helpers for ERC-4337 v0.7 + common smart-account `execute` encoding.
276
+ * Peer dependency: `viem` (optional install).
277
+ */
278
+
279
+ /** Typical SimpleAccount-style `execute(dest,value,func)`. */
280
+ declare const avaxExecuteAbi: readonly [{
281
+ readonly name: "execute";
282
+ readonly type: "function";
283
+ readonly stateMutability: "nonpayable";
284
+ readonly inputs: readonly [{
285
+ readonly name: "dest";
286
+ readonly type: "address";
287
+ }, {
288
+ readonly name: "value";
289
+ readonly type: "uint256";
290
+ }, {
291
+ readonly name: "func";
292
+ readonly type: "bytes";
293
+ }];
294
+ readonly outputs: readonly [];
295
+ }, {
296
+ readonly name: "executeBatch";
297
+ readonly type: "function";
298
+ readonly stateMutability: "nonpayable";
299
+ readonly inputs: readonly [{
300
+ readonly name: "dest";
301
+ readonly type: "address[]";
302
+ }, {
303
+ readonly name: "value";
304
+ readonly type: "uint256[]";
305
+ }, {
306
+ readonly name: "func";
307
+ readonly type: "bytes[]";
308
+ }];
309
+ readonly outputs: readonly [];
310
+ }];
311
+ declare function encodeAvaxExecuteCalldata(to: Address, value: bigint, data?: Hex): Hex;
312
+ /** Convert API UserOp → viem shape for {@link getUserOperationHash}. */
313
+ declare function userOperationAvaxToViem(u: UserOperationAvax): UserOperation<'0.7'>;
314
+ declare function hashUserOperationAvax(params: {
315
+ chainId: number;
316
+ entryPointAddress: Address;
317
+ userOperation: UserOperationAvax;
318
+ }): Hex;
319
+ /** EntryPoint v0.7 `getNonce(sender, key)` with key `0`. */
320
+ declare function readAvaxSenderNonce(params: {
321
+ publicClient: PublicClient;
322
+ entryPointAddress: Address;
323
+ sender: Address;
324
+ key?: bigint;
325
+ }): Promise<bigint>;
326
+
327
+ /**
328
+ * Standard SimpleAccountFactory (EntryPoint v0.7) helpers for counterfactual address + init calldata.
329
+ */
330
+
331
+ /** Minimal ABI for eth-infinitism–style SimpleAccountFactory. */
332
+ declare const SIMPLE_ACCOUNT_FACTORY_ABI: readonly [{
333
+ readonly inputs: readonly [{
334
+ readonly name: "owner";
335
+ readonly type: "address";
336
+ }, {
337
+ readonly name: "salt";
338
+ readonly type: "uint256";
339
+ }];
340
+ readonly name: "createAccount";
341
+ readonly outputs: readonly [{
342
+ readonly name: "account";
343
+ readonly type: "address";
344
+ }];
345
+ readonly stateMutability: "nonpayable";
346
+ readonly type: "function";
347
+ }, {
348
+ readonly inputs: readonly [{
349
+ readonly name: "owner";
350
+ readonly type: "address";
351
+ }, {
352
+ readonly name: "salt";
353
+ readonly type: "uint256";
354
+ }];
355
+ readonly name: "getAddress";
356
+ readonly outputs: readonly [{
357
+ readonly name: "";
358
+ readonly type: "address";
359
+ }];
360
+ readonly stateMutability: "view";
361
+ readonly type: "function";
362
+ }];
363
+ declare function predictSimpleAccountAddress(params: {
364
+ publicClient: PublicClient;
365
+ factory: Address;
366
+ owner: Address;
367
+ salt?: bigint;
368
+ }): Promise<Address>;
369
+ declare function encodeCreateAccountFactoryData(owner: Address, salt: bigint): Hex;
370
+
371
+ type SmoothSendAvaxContextValue = {
372
+ apiKey: string;
373
+ network: 'testnet' | 'mainnet';
374
+ /** Set when the SCW is already known. If you use `accountFactory`, you can omit this. */
375
+ smartAccountAddress?: Address;
376
+ /**
377
+ * SimpleAccountFactory (v0.7). When set, the hook derives the counterfactual sender via
378
+ * `getAddress(owner, salt)` and, if the account has no code, adds `factory` + `factoryData` so the
379
+ * first sponsored UserOp deploys the SCW and runs `callData` in the same op.
380
+ */
381
+ accountFactory?: Address;
382
+ accountSalt?: bigint;
383
+ };
384
+ declare function SmoothSendAvaxProvider({ apiKey, network, smartAccountAddress, accountFactory, accountSalt, children, }: {
385
+ apiKey: string;
386
+ network?: 'testnet' | 'mainnet';
387
+ smartAccountAddress?: Address;
388
+ accountFactory?: Address;
389
+ accountSalt?: bigint;
390
+ children: ReactNode;
391
+ }): react_jsx_runtime.JSX.Element;
392
+ declare function useSmoothSendAvaxContext(): SmoothSendAvaxContextValue | null;
393
+ type UseSmoothSendAvaxParams = {
394
+ apiKey?: string;
395
+ network?: 'testnet' | 'mainnet';
396
+ smartAccountAddress?: Address;
397
+ accountFactory?: Address;
398
+ /** @default 0n */
399
+ accountSalt?: bigint;
400
+ publicClient: PublicClient | null | undefined;
401
+ walletClient: WalletClient | null | undefined;
402
+ };
403
+ declare function useSmoothSendAvax(params: UseSmoothSendAvaxParams): {
404
+ submitter: SmoothSendAvaxSubmitter;
405
+ paymasterAddress: Address | null;
406
+ discoveredFactory: Address | null;
407
+ isPending: boolean;
408
+ submitCall: (args: {
409
+ to?: Address;
410
+ data?: Hex;
411
+ value?: bigint;
412
+ call?: {
413
+ to: Address;
414
+ data?: Hex;
415
+ value?: bigint;
416
+ };
417
+ calls?: {
418
+ to: Address;
419
+ data?: Hex;
420
+ value?: bigint;
421
+ }[];
422
+ mode?: AvaxSponsorshipMode;
423
+ paymaster?: Omit<PaymasterSignRequestAvax, 'mode' | 'userOp'>;
424
+ waitForReceipt?: boolean;
425
+ }) => ReturnType<SmoothSendAvaxSubmitter['submitSponsoredUserOperation']>;
426
+ submitSponsoredUserOp: (args: {
427
+ calls: Array<{
428
+ to: Address;
429
+ data?: Hex;
430
+ value?: bigint;
431
+ }>;
432
+ sponsorshipMode?: AvaxSponsorshipMode;
433
+ paymaster?: Omit<PaymasterSignRequestAvax, 'mode' | 'userOp'>;
434
+ waitForReceipt?: boolean;
435
+ }) => ReturnType<SmoothSendAvaxSubmitter['submitSponsoredUserOperation']>;
436
+ estimateUserPaysFee: (args: {
437
+ to?: Address;
438
+ data?: Hex;
439
+ value?: bigint;
440
+ call?: {
441
+ to: Address;
442
+ data?: Hex;
443
+ value?: bigint;
444
+ };
445
+ calls?: {
446
+ to: Address;
447
+ data?: Hex;
448
+ value?: bigint;
449
+ }[];
450
+ paymaster?: Omit<PaymasterSignRequestAvax, 'mode' | 'userOp'>;
451
+ }) => Promise<{
452
+ entryPoint: string;
453
+ feePreview?: AvaxFeePreview;
454
+ exchangeRate?: string;
455
+ }>;
456
+ };
457
+
458
+ export { AvaxSubmitter as A, ENTRY_POINT_V07_ADDRESS as E, SmoothSendAvaxClient as S, SmoothSendAvaxSubmitter as a, createSmoothSendAvaxSubmitter as b, createSmoothSendAvaxClient as c, SmoothSendAvaxProvider as d, useSmoothSendAvaxContext as e, encodeAvaxExecuteCalldata as f, userOperationAvaxToViem as g, hashUserOperationAvax as h, avaxExecuteAbi as i, SIMPLE_ACCOUNT_FACTORY_ABI as j, encodeCreateAccountFactoryData as k, fetchAvaxAaPublicDefaults as l, predictSimpleAccountAddress as p, readAvaxSenderNonce as r, useSmoothSendAvax as u };
459
+ export type { SponsoredUserOpDraftAvax as B, UserOperationAvax as C, UserOperationReceiptAvax as D, GasEstimateAvax as G, PaymasterSignRequestAvax as P, UseSmoothSendAvaxParams as U, AvaxAaPublicDefaults as m, SmoothSendAvaxSubmitterConfig as n, SmoothSendAvaxClientConfig as o, SimpleCallInput as q, EstimateUserPaysFeeAvaxOptions as s, SubmitSponsoredAvaxUserOpOptions as t, SmoothSendAvaxContextValue as v, AvaxFeePreview as w, AvaxHashTypes as x, AvaxSponsorshipMode as y, PaymasterSignResponseAvax as z };
package/dist/index.cjs CHANGED
@@ -3311,6 +3311,7 @@ function useSmoothSendAvax(params) {
3311
3311
  }
3312
3312
  const submitter = react.useMemo(() => new SmoothSendAvaxSubmitter({ apiKey, network }), [apiKey, network]);
3313
3313
  const [discovered, setDiscovered] = react.useState(null);
3314
+ const [isPending, setIsPending] = react.useState(false);
3314
3315
  react.useEffect(() => {
3315
3316
  submitter.getPublicAaDefaults().then(setDiscovered).catch(err => {
3316
3317
  console.warn('[SmoothSend AVAX] Auto-discovery failed:', err);
@@ -3320,108 +3321,114 @@ function useSmoothSendAvax(params) {
3320
3321
  const paymasterAddress = (network === 'mainnet' ? discovered?.paymasterMainnet : discovered?.paymasterFuji);
3321
3322
  const { publicClient, walletClient } = params;
3322
3323
  const submitCall = react.useCallback(async (params) => {
3323
- if (!publicClient) {
3324
- throw new Error('[SmoothSend AVAX] publicClient missing (wagmi usePublicClient)');
3325
- }
3326
- if (!walletClient) {
3327
- throw new Error('[SmoothSend AVAX] walletClient missing (wagmi useWalletClient)');
3328
- }
3329
- if (!smartAccountAddressProp && !accountFactory) {
3330
- throw new Error('[SmoothSend AVAX] No smartAccountAddress or accountFactory provided. Pass them to SmoothSendAvaxProvider or useSmoothSendAvax hook.');
3331
- }
3332
- const account = walletClient.account;
3333
- if (!account) {
3334
- throw new Error('[SmoothSend AVAX] walletClient.account missing — connect wallet (wagmi useWalletClient)');
3335
- }
3336
- const ownerAddress = account.address;
3337
- const chainId = Number(publicClient.chain?.id ?? (await publicClient.getChainId()));
3338
- const entryPoint = (await submitter.getSupportedEntryPoints())[0];
3339
- let sender;
3340
- if (accountFactory) {
3341
- const predicted = await predictSimpleAccountAddress({
3342
- publicClient,
3343
- factory: accountFactory,
3344
- owner: ownerAddress,
3345
- salt: accountSalt,
3346
- });
3347
- if (smartAccountAddressProp &&
3348
- predicted.toLowerCase() !== smartAccountAddressProp.toLowerCase()) {
3349
- throw new Error(`[SmoothSend AVAX] smartAccountAddress ${smartAccountAddressProp} does not match factory prediction ${predicted}`);
3324
+ setIsPending(true);
3325
+ try {
3326
+ if (!publicClient) {
3327
+ throw new Error('[SmoothSend AVAX] publicClient missing (wagmi usePublicClient)');
3350
3328
  }
3351
- sender = predicted;
3352
- }
3353
- else {
3354
- sender = smartAccountAddressProp;
3355
- }
3356
- const code = await publicClient.getBytecode({ address: sender });
3357
- const deployed = typeof code === 'string' &&
3358
- code !== '0x' &&
3359
- code.length > 2;
3360
- let nonce;
3361
- let factory;
3362
- let factoryData;
3363
- if (!deployed) {
3364
- if (!accountFactory) {
3365
- throw new Error('[SmoothSend AVAX] No bytecode at sender — pass accountFactory so the paymaster can sponsor deploy + call in one UserOp');
3329
+ if (!walletClient) {
3330
+ throw new Error('[SmoothSend AVAX] walletClient missing (wagmi useWalletClient)');
3366
3331
  }
3367
- nonce = 0n;
3368
- factory = accountFactory;
3369
- factoryData = encodeCreateAccountFactoryData(ownerAddress, accountSalt);
3370
- }
3371
- else {
3372
- nonce = await readAvaxSenderNonce({
3373
- publicClient,
3374
- entryPointAddress: entryPoint,
3375
- sender,
3376
- });
3377
- }
3378
- let maxFeePerGas = 50n * 10n ** 9n;
3379
- let maxPriorityFeePerGas = 2n * 10n ** 9n;
3380
- try {
3381
- const est = await publicClient.estimateFeesPerGas();
3382
- if (est.maxFeePerGas)
3383
- maxFeePerGas = est.maxFeePerGas;
3384
- if (est.maxPriorityFeePerGas) {
3385
- maxPriorityFeePerGas = est.maxPriorityFeePerGas;
3332
+ if (!smartAccountAddressProp && !accountFactory) {
3333
+ throw new Error('[SmoothSend AVAX] No smartAccountAddress or accountFactory provided. Pass them to SmoothSendAvaxProvider or useSmoothSendAvax hook.');
3386
3334
  }
3387
- }
3388
- catch {
3389
- /* chain may not support EIP-1559 estimation */
3390
- }
3391
- let callData;
3392
- if (params.calls && params.calls.length > 0) {
3393
- callData = encodeAvaxExecuteBatchCalldata(params.calls.map(c => c.to), params.calls.map(c => c.value ?? 0n), params.calls.map(c => c.data ?? '0x'));
3394
- }
3395
- else {
3396
- const target = params.call?.to ?? params.to;
3397
- if (!target)
3398
- throw new Error('[SmoothSend AVAX] No target address (to) provided');
3399
- callData = encodeAvaxExecuteCalldata(target, params.call?.value ?? params.value ?? 0n, params.call?.data ?? params.data ?? '0x');
3400
- }
3401
- return submitter.submitSponsoredUserOperation({
3402
- userOp: {
3403
- sender,
3404
- nonce: viem.toHex(nonce),
3405
- callData,
3406
- maxFeePerGas: viem.toHex(maxFeePerGas),
3407
- maxPriorityFeePerGas: viem.toHex(maxPriorityFeePerGas),
3408
- ...(factory && factoryData ? { factory, factoryData } : {}),
3409
- },
3410
- mode: params.mode ?? 'developer-sponsored',
3411
- paymaster: params.paymaster,
3412
- waitForReceipt: params.waitForReceipt,
3413
- signUserOp: async (op) => {
3414
- const hash = hashUserOperationAvax({
3415
- chainId,
3416
- entryPointAddress: entryPoint,
3417
- userOperation: op,
3335
+ const account = walletClient.account;
3336
+ if (!account) {
3337
+ throw new Error('[SmoothSend AVAX] walletClient.account missing — connect wallet (wagmi useWalletClient)');
3338
+ }
3339
+ const ownerAddress = account.address;
3340
+ const chainId = Number(publicClient.chain?.id ?? (await publicClient.getChainId()));
3341
+ const entryPoint = (await submitter.getSupportedEntryPoints())[0];
3342
+ let sender;
3343
+ if (accountFactory) {
3344
+ const predicted = await predictSimpleAccountAddress({
3345
+ publicClient,
3346
+ factory: accountFactory,
3347
+ owner: ownerAddress,
3348
+ salt: accountSalt,
3418
3349
  });
3419
- return walletClient.signMessage({
3420
- account,
3421
- message: { raw: hash },
3350
+ if (smartAccountAddressProp &&
3351
+ predicted.toLowerCase() !== smartAccountAddressProp.toLowerCase()) {
3352
+ throw new Error(`[SmoothSend AVAX] smartAccountAddress ${smartAccountAddressProp} does not match factory prediction ${predicted}`);
3353
+ }
3354
+ sender = predicted;
3355
+ }
3356
+ else {
3357
+ sender = smartAccountAddressProp;
3358
+ }
3359
+ const code = await publicClient.getBytecode({ address: sender });
3360
+ const deployed = typeof code === 'string' &&
3361
+ code !== '0x' &&
3362
+ code.length > 2;
3363
+ let nonce;
3364
+ let factory;
3365
+ let factoryData;
3366
+ if (!deployed) {
3367
+ if (!accountFactory) {
3368
+ throw new Error('[SmoothSend AVAX] No bytecode at sender — pass accountFactory so the paymaster can sponsor deploy + call in one UserOp');
3369
+ }
3370
+ nonce = 0n;
3371
+ factory = accountFactory;
3372
+ factoryData = encodeCreateAccountFactoryData(ownerAddress, accountSalt);
3373
+ }
3374
+ else {
3375
+ nonce = await readAvaxSenderNonce({
3376
+ publicClient,
3377
+ entryPointAddress: entryPoint,
3378
+ sender,
3422
3379
  });
3423
- },
3424
- });
3380
+ }
3381
+ let maxFeePerGas = 50n * 10n ** 9n;
3382
+ let maxPriorityFeePerGas = 2n * 10n ** 9n;
3383
+ try {
3384
+ const est = await publicClient.estimateFeesPerGas();
3385
+ if (est.maxFeePerGas)
3386
+ maxFeePerGas = est.maxFeePerGas;
3387
+ if (est.maxPriorityFeePerGas) {
3388
+ maxPriorityFeePerGas = est.maxPriorityFeePerGas;
3389
+ }
3390
+ }
3391
+ catch {
3392
+ /* chain may not support EIP-1559 estimation */
3393
+ }
3394
+ let callData;
3395
+ if (params.calls && params.calls.length > 0) {
3396
+ callData = encodeAvaxExecuteBatchCalldata(params.calls.map(c => c.to), params.calls.map(c => c.value ?? 0n), params.calls.map(c => c.data ?? '0x'));
3397
+ }
3398
+ else {
3399
+ const target = params.call?.to ?? params.to;
3400
+ if (!target)
3401
+ throw new Error('[SmoothSend AVAX] No target address (to) provided');
3402
+ callData = encodeAvaxExecuteCalldata(target, params.call?.value ?? params.value ?? 0n, params.call?.data ?? params.data ?? '0x');
3403
+ }
3404
+ return await submitter.submitSponsoredUserOperation({
3405
+ userOp: {
3406
+ sender,
3407
+ nonce: viem.toHex(nonce),
3408
+ callData,
3409
+ maxFeePerGas: viem.toHex(maxFeePerGas),
3410
+ maxPriorityFeePerGas: viem.toHex(maxPriorityFeePerGas),
3411
+ ...(factory && factoryData ? { factory, factoryData } : {}),
3412
+ },
3413
+ mode: params.mode ?? 'developer-sponsored',
3414
+ paymaster: params.paymaster,
3415
+ waitForReceipt: params.waitForReceipt,
3416
+ signUserOp: async (op) => {
3417
+ const hash = hashUserOperationAvax({
3418
+ chainId,
3419
+ entryPointAddress: entryPoint,
3420
+ userOperation: op,
3421
+ });
3422
+ return walletClient.signMessage({
3423
+ account,
3424
+ message: { raw: hash },
3425
+ });
3426
+ },
3427
+ });
3428
+ }
3429
+ finally {
3430
+ setIsPending(false);
3431
+ }
3425
3432
  }, [
3426
3433
  accountFactory,
3427
3434
  accountSalt,
@@ -3549,6 +3556,7 @@ function useSmoothSendAvax(params) {
3549
3556
  estimateUserPaysFee,
3550
3557
  paymasterAddress,
3551
3558
  discoveredFactory: accountFactory ?? null,
3559
+ isPending,
3552
3560
  };
3553
3561
  }
3554
3562