@smoothsend/sdk 2.2.10 → 2.3.0

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.
package/dist/index.js CHANGED
@@ -2797,12 +2797,49 @@ class SmoothSendAvaxSubmitter {
2797
2797
  }
2798
2798
  return data;
2799
2799
  }
2800
+ /**
2801
+ * Preflight fee estimate for `user-pays-erc20` mode.
2802
+ *
2803
+ * This performs:
2804
+ * 1) `eth_estimateUserOperationGas`
2805
+ * 2) `paymaster/sign` in `user-pays-erc20` mode
2806
+ * and returns `feePreview` when available.
2807
+ */
2808
+ async estimateUserPaysFee(opts) {
2809
+ const entryPoint = opts.entryPoint ?? (await this.getSupportedEntryPoints())[0];
2810
+ if (!entryPoint) {
2811
+ throw new Error('[SmoothSendAvaxSubmitter] no EntryPoint from bundler');
2812
+ }
2813
+ let userOp = {
2814
+ ...opts.userOp,
2815
+ signature: opts.userOp.signature ?? '0x',
2816
+ };
2817
+ const gas = await this.estimateUserOperationGas(userOp, entryPoint);
2818
+ userOp = SmoothSendAvaxSubmitter.applyGasEstimate(userOp, gas);
2819
+ const signedPm = await this.paymasterSign({
2820
+ mode: 'user-pays-erc20',
2821
+ userOp,
2822
+ ...opts.paymaster,
2823
+ });
2824
+ return {
2825
+ entryPoint,
2826
+ userOp,
2827
+ gas,
2828
+ exchangeRate: signedPm.exchangeRate,
2829
+ feePreview: signedPm.feePreview,
2830
+ };
2831
+ }
2800
2832
  async getBundlerHealth() {
2801
2833
  const res = await this.http.get('/api/v1/bundler/health');
2802
2834
  if (!res.success)
2803
2835
  throw new Error('[SmoothSendAvaxSubmitter] health failed');
2804
2836
  return res.data;
2805
2837
  }
2838
+ /** Fetch public AA addresses (Factory, Paymaster) from gateway. */
2839
+ async getPublicAaDefaults() {
2840
+ const { fetchAvaxAaPublicDefaults } = await Promise.resolve().then(function () { return publicAaDefaults; });
2841
+ return fetchAvaxAaPublicDefaults(this.config.gatewayUrl ?? DEFAULT_GATEWAY);
2842
+ }
2806
2843
  /**
2807
2844
  * One call: estimate gas → paymaster/sign → merge → your sign → send → (optional) wait for receipt.
2808
2845
  */
@@ -3032,6 +3069,164 @@ function encodeCreateAccountFactoryData(owner, salt) {
3032
3069
  });
3033
3070
  }
3034
3071
 
3072
+ class SmoothSendAvaxClient {
3073
+ constructor(config) {
3074
+ this.config = config;
3075
+ this.submitter = new SmoothSendAvaxSubmitter({
3076
+ apiKey: config.apiKey,
3077
+ network: config.network,
3078
+ gatewayUrl: config.gatewayUrl,
3079
+ timeout: config.timeout,
3080
+ chain: config.chain,
3081
+ corsOrigin: config.corsOrigin
3082
+ });
3083
+ }
3084
+ async submitCall(args) {
3085
+ return this.submitCalls({
3086
+ calls: [args.call],
3087
+ mode: args.mode,
3088
+ paymaster: args.paymaster,
3089
+ waitForReceipt: args.waitForReceipt
3090
+ });
3091
+ }
3092
+ async submitCalls(args) {
3093
+ if (args.calls.length === 0) {
3094
+ throw new Error('[SmoothSend AVAX] No calls provided');
3095
+ }
3096
+ const built = await this.buildDraft(args.calls);
3097
+ const result = await this.submitter.submitSponsoredUserOperation({
3098
+ userOp: built.userOp,
3099
+ mode: args.mode ?? 'developer-sponsored',
3100
+ paymaster: args.paymaster,
3101
+ waitForReceipt: args.waitForReceipt,
3102
+ signUserOp: async (op) => {
3103
+ const hash = hashUserOperationAvax({
3104
+ chainId: built.chainId,
3105
+ entryPointAddress: built.entryPoint,
3106
+ userOperation: op
3107
+ });
3108
+ return this.signUserOpHash(hash);
3109
+ }
3110
+ });
3111
+ return {
3112
+ userOpHash: result.userOpHash,
3113
+ transactionHash: result.receipt?.receipt?.transactionHash,
3114
+ receipt: result.receipt
3115
+ };
3116
+ }
3117
+ async estimateUserPaysFee(args) {
3118
+ if (args.calls.length === 0) {
3119
+ throw new Error('[SmoothSend AVAX] No calls provided');
3120
+ }
3121
+ const built = await this.buildDraft(args.calls);
3122
+ const estimate = await this.submitter.estimateUserPaysFee({
3123
+ userOp: built.userOp,
3124
+ entryPoint: built.entryPoint,
3125
+ paymaster: args.paymaster
3126
+ });
3127
+ return {
3128
+ feePreview: estimate.feePreview,
3129
+ exchangeRate: estimate.exchangeRate,
3130
+ entryPoint: estimate.entryPoint
3131
+ };
3132
+ }
3133
+ async buildDraft(calls) {
3134
+ const { publicClient, walletClient } = this.config;
3135
+ const account = walletClient.account;
3136
+ if (!account) {
3137
+ throw new Error('[SmoothSend AVAX] walletClient.account missing');
3138
+ }
3139
+ const ownerAddress = account.address;
3140
+ const defaults = await this.submitter.getPublicAaDefaults().catch(() => null);
3141
+ const accountFactory = this.config.accountFactory ??
3142
+ ((this.config.network ?? 'testnet') === 'mainnet'
3143
+ ? defaults?.simpleAccountFactoryMainnet
3144
+ : defaults?.simpleAccountFactoryFuji) ??
3145
+ undefined;
3146
+ const chainId = Number(publicClient.chain?.id ?? (await publicClient.getChainId()));
3147
+ const entryPoint = (await this.submitter.getSupportedEntryPoints())[0];
3148
+ let sender;
3149
+ if (accountFactory) {
3150
+ const predicted = await predictSimpleAccountAddress({
3151
+ publicClient,
3152
+ factory: accountFactory,
3153
+ owner: ownerAddress,
3154
+ salt: this.config.accountSalt ?? 0n
3155
+ });
3156
+ if (this.config.smartAccountAddress &&
3157
+ predicted.toLowerCase() !== this.config.smartAccountAddress.toLowerCase()) {
3158
+ throw new Error(`[SmoothSend AVAX] smartAccountAddress ${this.config.smartAccountAddress} does not match factory prediction ${predicted}`);
3159
+ }
3160
+ sender = predicted;
3161
+ }
3162
+ else if (this.config.smartAccountAddress) {
3163
+ sender = this.config.smartAccountAddress;
3164
+ }
3165
+ else {
3166
+ throw new Error('[SmoothSend AVAX] Provide smartAccountAddress or accountFactory (or use gateway public defaults).');
3167
+ }
3168
+ const code = await publicClient.getBytecode({ address: sender });
3169
+ const deployed = typeof code === 'string' && code !== '0x' && code.length > 2;
3170
+ let nonce;
3171
+ let factory;
3172
+ let factoryData;
3173
+ if (!deployed) {
3174
+ if (!accountFactory) {
3175
+ throw new Error('[SmoothSend AVAX] Sender is not deployed and no accountFactory available');
3176
+ }
3177
+ nonce = 0n;
3178
+ factory = accountFactory;
3179
+ factoryData = encodeCreateAccountFactoryData(ownerAddress, this.config.accountSalt ?? 0n);
3180
+ }
3181
+ else {
3182
+ nonce = await readAvaxSenderNonce({
3183
+ publicClient,
3184
+ entryPointAddress: entryPoint,
3185
+ sender
3186
+ });
3187
+ }
3188
+ let maxFeePerGas = 50n * 10n ** 9n;
3189
+ let maxPriorityFeePerGas = 2n * 10n ** 9n;
3190
+ try {
3191
+ const est = await publicClient.estimateFeesPerGas();
3192
+ if (est.maxFeePerGas)
3193
+ maxFeePerGas = est.maxFeePerGas;
3194
+ if (est.maxPriorityFeePerGas)
3195
+ maxPriorityFeePerGas = est.maxPriorityFeePerGas;
3196
+ }
3197
+ catch {
3198
+ // ignore and use fallbacks
3199
+ }
3200
+ const callData = calls.length === 1
3201
+ ? encodeAvaxExecuteCalldata(calls[0].to, calls[0].value ?? 0n, calls[0].data ?? '0x')
3202
+ : encodeAvaxExecuteBatchCalldata(calls.map((c) => c.to), calls.map((c) => c.value ?? 0n), calls.map((c) => c.data ?? '0x'));
3203
+ return {
3204
+ userOp: {
3205
+ sender,
3206
+ nonce: toHex(nonce),
3207
+ callData,
3208
+ maxFeePerGas: toHex(maxFeePerGas),
3209
+ maxPriorityFeePerGas: toHex(maxPriorityFeePerGas),
3210
+ ...(factory && factoryData ? { factory, factoryData } : {})
3211
+ },
3212
+ chainId,
3213
+ entryPoint
3214
+ };
3215
+ }
3216
+ async signUserOpHash(hash) {
3217
+ const account = this.config.walletClient.account;
3218
+ if (!account)
3219
+ throw new Error('[SmoothSend AVAX] walletClient.account missing');
3220
+ return this.config.walletClient.signMessage({
3221
+ account,
3222
+ message: { raw: hash }
3223
+ });
3224
+ }
3225
+ }
3226
+ function createSmoothSendAvaxClient(config) {
3227
+ return new SmoothSendAvaxClient(config);
3228
+ }
3229
+
3035
3230
  /**
3036
3231
  * Public gateway defaults for Avalanche AA (no API key).
3037
3232
  */
@@ -3066,6 +3261,12 @@ async function fetchAvaxAaPublicDefaults(gatewayUrl = 'https://proxy.smoothsend.
3066
3261
  };
3067
3262
  }
3068
3263
 
3264
+ var publicAaDefaults = /*#__PURE__*/Object.freeze({
3265
+ __proto__: null,
3266
+ ENTRY_POINT_V07_ADDRESS: ENTRY_POINT_V07_ADDRESS,
3267
+ fetchAvaxAaPublicDefaults: fetchAvaxAaPublicDefaults
3268
+ });
3269
+
3069
3270
  const SmoothSendAvaxContext = createContext(null);
3070
3271
  function SmoothSendAvaxProvider({ apiKey, network = 'testnet', smartAccountAddress, accountFactory, accountSalt, children, }) {
3071
3272
  const value = useMemo(() => ({
@@ -3085,12 +3286,20 @@ function useSmoothSendAvax(params) {
3085
3286
  const apiKey = params.apiKey ?? ctx?.apiKey;
3086
3287
  const network = params.network ?? ctx?.network ?? 'testnet';
3087
3288
  const smartAccountAddressProp = params.smartAccountAddress ?? ctx?.smartAccountAddress;
3088
- const accountFactory = params.accountFactory ?? ctx?.accountFactory;
3289
+ const accountFactoryProp = params.accountFactory ?? ctx?.accountFactory;
3089
3290
  const accountSalt = params.accountSalt ?? ctx?.accountSalt ?? 0n;
3090
3291
  if (!apiKey) {
3091
3292
  throw new Error('[SmoothSend AVAX] apiKey required — pass useSmoothSendAvax({ apiKey }) or wrap SmoothSendAvaxProvider');
3092
3293
  }
3093
3294
  const submitter = useMemo(() => new SmoothSendAvaxSubmitter({ apiKey, network }), [apiKey, network]);
3295
+ const [discovered, setDiscovered] = useState(null);
3296
+ useEffect(() => {
3297
+ submitter.getPublicAaDefaults().then(setDiscovered).catch(err => {
3298
+ console.warn('[SmoothSend AVAX] Auto-discovery failed:', err);
3299
+ });
3300
+ }, [submitter]);
3301
+ const accountFactory = accountFactoryProp ?? (network === 'mainnet' ? discovered?.simpleAccountFactoryMainnet : discovered?.simpleAccountFactoryFuji);
3302
+ const paymasterAddress = (network === 'mainnet' ? discovered?.paymasterMainnet : discovered?.paymasterFuji);
3094
3303
  const { publicClient, walletClient } = params;
3095
3304
  const submitCall = useCallback(async (params) => {
3096
3305
  if (!publicClient) {
@@ -3214,7 +3423,115 @@ function useSmoothSendAvax(params) {
3214
3423
  waitForReceipt: args.waitForReceipt,
3215
3424
  });
3216
3425
  }, [submitCall]);
3217
- return { submitter, submitCall, submitSponsoredUserOp };
3426
+ const estimateUserPaysFee = useCallback(async (params) => {
3427
+ if (!publicClient) {
3428
+ throw new Error('[SmoothSend AVAX] publicClient missing (wagmi usePublicClient)');
3429
+ }
3430
+ if (!walletClient) {
3431
+ throw new Error('[SmoothSend AVAX] walletClient missing (wagmi useWalletClient)');
3432
+ }
3433
+ if (!smartAccountAddressProp && !accountFactory) {
3434
+ throw new Error('[SmoothSend AVAX] No smartAccountAddress or accountFactory provided. Pass them to SmoothSendAvaxProvider or useSmoothSendAvax hook.');
3435
+ }
3436
+ const account = walletClient.account;
3437
+ if (!account) {
3438
+ throw new Error('[SmoothSend AVAX] walletClient.account missing — connect wallet (wagmi useWalletClient)');
3439
+ }
3440
+ const ownerAddress = account.address;
3441
+ let sender;
3442
+ if (accountFactory) {
3443
+ const predicted = await predictSimpleAccountAddress({
3444
+ publicClient,
3445
+ factory: accountFactory,
3446
+ owner: ownerAddress,
3447
+ salt: accountSalt,
3448
+ });
3449
+ if (smartAccountAddressProp &&
3450
+ predicted.toLowerCase() !== smartAccountAddressProp.toLowerCase()) {
3451
+ throw new Error(`[SmoothSend AVAX] smartAccountAddress ${smartAccountAddressProp} does not match factory prediction ${predicted}`);
3452
+ }
3453
+ sender = predicted;
3454
+ }
3455
+ else {
3456
+ sender = smartAccountAddressProp;
3457
+ }
3458
+ const entryPoint = (await submitter.getSupportedEntryPoints())[0];
3459
+ const code = await publicClient.getBytecode({ address: sender });
3460
+ const deployed = typeof code === 'string' &&
3461
+ code !== '0x' &&
3462
+ code.length > 2;
3463
+ let nonce;
3464
+ let factory;
3465
+ let factoryData;
3466
+ if (!deployed) {
3467
+ if (!accountFactory) {
3468
+ throw new Error('[SmoothSend AVAX] No bytecode at sender — pass accountFactory so the paymaster can sponsor deploy + call in one UserOp');
3469
+ }
3470
+ nonce = 0n;
3471
+ factory = accountFactory;
3472
+ factoryData = encodeCreateAccountFactoryData(ownerAddress, accountSalt);
3473
+ }
3474
+ else {
3475
+ nonce = await readAvaxSenderNonce({
3476
+ publicClient,
3477
+ entryPointAddress: entryPoint,
3478
+ sender,
3479
+ });
3480
+ }
3481
+ let maxFeePerGas = 50n * 10n ** 9n;
3482
+ let maxPriorityFeePerGas = 2n * 10n ** 9n;
3483
+ try {
3484
+ const est = await publicClient.estimateFeesPerGas();
3485
+ if (est.maxFeePerGas)
3486
+ maxFeePerGas = est.maxFeePerGas;
3487
+ if (est.maxPriorityFeePerGas)
3488
+ maxPriorityFeePerGas = est.maxPriorityFeePerGas;
3489
+ }
3490
+ catch {
3491
+ // ignore and use fallback values
3492
+ }
3493
+ let callData;
3494
+ if (params.calls && params.calls.length > 0) {
3495
+ callData = encodeAvaxExecuteBatchCalldata(params.calls.map(c => c.to), params.calls.map(c => c.value ?? 0n), params.calls.map(c => c.data ?? '0x'));
3496
+ }
3497
+ else {
3498
+ const target = params.call?.to ?? params.to;
3499
+ if (!target)
3500
+ throw new Error('[SmoothSend AVAX] No target address (to) provided');
3501
+ callData = encodeAvaxExecuteCalldata(target, params.call?.value ?? params.value ?? 0n, params.call?.data ?? params.data ?? '0x');
3502
+ }
3503
+ const quote = await submitter.estimateUserPaysFee({
3504
+ userOp: {
3505
+ sender,
3506
+ nonce: toHex(nonce),
3507
+ callData,
3508
+ maxFeePerGas: toHex(maxFeePerGas),
3509
+ maxPriorityFeePerGas: toHex(maxPriorityFeePerGas),
3510
+ ...(factory && factoryData ? { factory, factoryData } : {}),
3511
+ },
3512
+ paymaster: params.paymaster,
3513
+ });
3514
+ return {
3515
+ entryPoint: quote.entryPoint,
3516
+ feePreview: quote.feePreview,
3517
+ exchangeRate: quote.exchangeRate,
3518
+ };
3519
+ }, [
3520
+ accountFactory,
3521
+ accountSalt,
3522
+ publicClient,
3523
+ smartAccountAddressProp,
3524
+ submitter,
3525
+ walletClient,
3526
+ ]);
3527
+ return {
3528
+ submitter,
3529
+ submitCall,
3530
+ submitSponsoredUserOp,
3531
+ estimateUserPaysFee,
3532
+ paymasterAddress,
3533
+ discoveredFactory: accountFactory ?? null,
3534
+ };
3218
3535
  }
3219
3536
 
3220
3537
  // ─── helpers ────────────────────────────────────────────────────────────────
@@ -3849,5 +4166,5 @@ if (typeof window !== 'undefined') {
3849
4166
  ' - import { ... } from "@smoothsend/sdk/stellar"');
3850
4167
  }
3851
4168
 
3852
- export { APTOS_ERROR_CODES, AptosAdapter, AuthenticationError, AvaxSubmitter, CHAIN_ECOSYSTEM_MAP, ENTRY_POINT_V07_ADDRESS, HttpClient, NetworkError, RateLimitError, SIMPLE_ACCOUNT_FACTORY_ABI, STELLAR_ERROR_CODES, ScriptComposerClient, SmoothSendAvaxProvider, SmoothSendAvaxSubmitter, SmoothSendError, SmoothSendSDK, SmoothSendSession, SmoothSendTransactionSubmitter, StellarAdapter, StellarCAddressAdapter, TrueGaslessClient, VERSION, ValidationError, avaxExecuteAbi, createErrorFromResponse, createNetworkError, createScriptComposerClient, createSmoothSendAvaxSubmitter, createSmoothSendSubmitter, createTrueGaslessClient, SmoothSendSDK as default, encodeAvaxExecuteCalldata, encodeCreateAccountFactoryData, fetchAvaxAaPublicDefaults, hashUserOperationAvax, predictSimpleAccountAddress, readAvaxSenderNonce, useSmoothSend, useSmoothSendAvax, useSmoothSendAvaxContext, userOperationAvaxToViem };
4169
+ export { APTOS_ERROR_CODES, AptosAdapter, AuthenticationError, AvaxSubmitter, CHAIN_ECOSYSTEM_MAP, ENTRY_POINT_V07_ADDRESS, HttpClient, NetworkError, RateLimitError, SIMPLE_ACCOUNT_FACTORY_ABI, STELLAR_ERROR_CODES, ScriptComposerClient, SmoothSendAvaxClient, SmoothSendAvaxProvider, SmoothSendAvaxSubmitter, SmoothSendError, SmoothSendSDK, SmoothSendSession, SmoothSendTransactionSubmitter, StellarAdapter, StellarCAddressAdapter, TrueGaslessClient, VERSION, ValidationError, avaxExecuteAbi, createErrorFromResponse, createNetworkError, createScriptComposerClient, createSmoothSendAvaxClient, createSmoothSendAvaxSubmitter, createSmoothSendSubmitter, createTrueGaslessClient, SmoothSendSDK as default, encodeAvaxExecuteCalldata, encodeCreateAccountFactoryData, fetchAvaxAaPublicDefaults, hashUserOperationAvax, predictSimpleAccountAddress, readAvaxSenderNonce, useSmoothSend, useSmoothSendAvax, useSmoothSendAvaxContext, userOperationAvaxToViem };
3853
4170
  //# sourceMappingURL=index.js.map