@dynamic-labs-sdk/zerodev 0.1.0-alpha.17 → 0.1.0-alpha.19

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/index.cjs.js +463 -18
  3. package/index.esm.js +463 -22
  4. package/package.json +5 -5
  5. package/src/addZerodevExtension/addZerodevExtension.d.ts.map +1 -1
  6. package/src/canSponsorTransaction/canSponsorTransaction.d.ts +29 -0
  7. package/src/canSponsorTransaction/canSponsorTransaction.d.ts.map +1 -0
  8. package/src/canSponsorTransaction/index.d.ts +2 -0
  9. package/src/canSponsorTransaction/index.d.ts.map +1 -0
  10. package/src/constants.d.ts +5 -0
  11. package/src/constants.d.ts.map +1 -1
  12. package/src/createKernelClientForWalletAccount/createKernelClientForWalletAccount.d.ts +27 -0
  13. package/src/createKernelClientForWalletAccount/createKernelClientForWalletAccount.d.ts.map +1 -0
  14. package/src/createKernelClientForWalletAccount/index.d.ts +2 -0
  15. package/src/createKernelClientForWalletAccount/index.d.ts.map +1 -0
  16. package/src/estimateTransactionGas/estimateTransactionGas.d.ts +18 -0
  17. package/src/estimateTransactionGas/estimateTransactionGas.d.ts.map +1 -0
  18. package/src/estimateTransactionGas/index.d.ts +2 -0
  19. package/src/estimateTransactionGas/index.d.ts.map +1 -0
  20. package/src/exports/index.d.ts +5 -0
  21. package/src/exports/index.d.ts.map +1 -1
  22. package/src/isGasSponsorshipError/index.d.ts +2 -0
  23. package/src/isGasSponsorshipError/index.d.ts.map +1 -0
  24. package/src/isGasSponsorshipError/isGasSponsorshipError.d.ts +2 -0
  25. package/src/isGasSponsorshipError/isGasSponsorshipError.d.ts.map +1 -0
  26. package/src/isZerodevWalletAccount/index.d.ts +2 -0
  27. package/src/isZerodevWalletAccount/index.d.ts.map +1 -0
  28. package/src/isZerodevWalletAccount/isZerodevWalletAccount.d.ts +9 -0
  29. package/src/isZerodevWalletAccount/isZerodevWalletAccount.d.ts.map +1 -0
  30. package/src/utils/calculateGasForUserOperation/calculateGasForUserOperation.d.ts +7 -0
  31. package/src/utils/calculateGasForUserOperation/calculateGasForUserOperation.d.ts.map +1 -0
  32. package/src/utils/calculateGasForUserOperation/index.d.ts +2 -0
  33. package/src/utils/calculateGasForUserOperation/index.d.ts.map +1 -0
  34. package/src/utils/createKernelAccount/createKernelAccount.d.ts +1 -1
  35. package/src/utils/createKernelAccount/createKernelAccount.d.ts.map +1 -1
  36. package/src/utils/createZerodevWalletProvider/createZerodevWalletProvider.d.ts +4 -0
  37. package/src/utils/createZerodevWalletProvider/createZerodevWalletProvider.d.ts.map +1 -0
  38. package/src/utils/createZerodevWalletProvider/index.d.ts +2 -0
  39. package/src/utils/createZerodevWalletProvider/index.d.ts.map +1 -0
  40. package/src/utils/getAllUserZerodevAddresses/getAllUserZerodevAddresses.d.ts +3 -0
  41. package/src/utils/getAllUserZerodevAddresses/getAllUserZerodevAddresses.d.ts.map +1 -0
  42. package/src/utils/getAllUserZerodevAddresses/index.d.ts +2 -0
  43. package/src/utils/getAllUserZerodevAddresses/index.d.ts.map +1 -0
  44. package/src/utils/prepareUserOperationWithKernelClient/index.d.ts +2 -0
  45. package/src/utils/prepareUserOperationWithKernelClient/index.d.ts.map +1 -0
  46. package/src/utils/prepareUserOperationWithKernelClient/prepareUserOperationWithKernelClient.d.ts +466 -0
  47. package/src/utils/prepareUserOperationWithKernelClient/prepareUserOperationWithKernelClient.d.ts.map +1 -0
  48. package/src/utils/signMessage/index.d.ts +2 -0
  49. package/src/utils/signMessage/index.d.ts.map +1 -0
  50. package/src/utils/signMessage/signMessage.d.ts +11 -0
  51. package/src/utils/signMessage/signMessage.d.ts.map +1 -0
package/index.esm.js CHANGED
@@ -1,31 +1,74 @@
1
1
  import { assertPackageVersion } from '@dynamic-labs-sdk/assert-package-version';
2
- import { getDefaultClient, hasExtension, registerExtension } from '@dynamic-labs-sdk/client/core';
3
- import { getOwnerWalletAccountForSmartWalletAccount, NoSmartWalletAccountSignerFoundError } from '@dynamic-labs-sdk/client';
2
+ import { getChainFromVerifiedCredentialChain, assertDefined, getDefaultClient, getVerifiedCredentialForWalletAccount, formatWalletProviderKey, formatWalletProviderGroupKey, getActiveNetworkIdFromLastKnownRegistry, switchActiveNetworkInLastKnownRegistry, MethodNotImplementedError, hasExtension, registerExtension, getWalletProviderRegistry, WalletProviderPriority } from '@dynamic-labs-sdk/client/core';
3
+ import { getOwnerWalletAccountForSmartWalletAccount, NoSmartWalletAccountSignerFoundError, getActiveNetworkData, signMessage as signMessage$1, UnrecognizedNetworkError, InvalidParamError } from '@dynamic-labs-sdk/client';
4
4
  import { isEvmWalletAccount } from '@dynamic-labs-sdk/evm';
5
- import { createWalletClientForWalletAccount } from '@dynamic-labs-sdk/evm/viem';
5
+ import { ProviderKernelVersionEnum, WalletProviderEnum, ProviderEnum, ProviderEntryPointVersionEnum, ZerodevBundlerProvider } from '@dynamic-labs/sdk-api-core';
6
+ import { constants, createKernelAccount as createKernelAccount$1, createZeroDevPaymasterClient, createKernelAccountClient, getUserOperationGasPrice } from '@zerodev/sdk';
7
+ import { createWalletClientForWalletAccount, mapNetworkDataToViemChain } from '@dynamic-labs-sdk/evm/viem';
8
+ import { http, createPublicClient } from 'viem';
9
+ import { signerToEcdsaValidator, createEcdsaKernelMigrationAccount } from '@zerodev/ecdsa-validator';
10
+ import { toMultiChainECDSAValidator } from '@zerodev/multi-chain-ecdsa-validator';
6
11
 
7
12
  var name = "@dynamic-labs-sdk/zerodev";
8
- var version = "0.1.0-alpha.17";
13
+ var version = "0.1.0-alpha.19";
9
14
 
10
- const ZERODEV_EXTENSION_KEY = 'zerodev';
11
- /**
12
- * Adds the ZeroDev extension to the Dynamic client.
13
- *
14
- * This extension enables Account Abstraction integration with ZeroDev
15
- *
16
- * @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
17
- */ const addZerodevExtension = (client = getDefaultClient())=>{
18
- if (hasExtension({
19
- extensionKey: ZERODEV_EXTENSION_KEY
20
- }, client)) {
21
- return;
22
- }
23
- registerExtension({
24
- extensionKey: ZERODEV_EXTENSION_KEY
25
- }, client);
26
- // TODO: implement
15
+ const ZERODEV_METADATA = {
16
+ displayName: 'ZeroDev',
17
+ icon: 'https://iconic.dynamic-static-assets.com/icons/sprite.svg#smartwallet',
18
+ normalizedWalletName: 'zerodev'
19
+ };
20
+ const ZERODEV_RPC_BASE_URL = 'https://rpc.zerodev.app/api/v2';
21
+ const KERNEL_VERSION_MAP = {
22
+ [ProviderKernelVersionEnum.V33]: constants.KERNEL_V3_3,
23
+ [ProviderKernelVersionEnum.V32]: constants.KERNEL_V3_2,
24
+ [ProviderKernelVersionEnum.V31]: constants.KERNEL_V3_1,
25
+ [ProviderKernelVersionEnum.V30]: constants.KERNEL_V3_0,
26
+ [ProviderKernelVersionEnum.V24]: constants.KERNEL_V2_4
27
+ };
28
+ const ENTRY_POINT_VERSION_MAP = {
29
+ 0.6: constants.KERNEL_V2_4,
30
+ 0.7: constants.KERNEL_V3_1,
31
+ 0.8: constants.KERNEL_V3_3
27
32
  };
28
33
 
34
+ const getAllUserZerodevAddresses = (client)=>{
35
+ var _client_user;
36
+ var _client_user_verifiedCredentials_filter;
37
+ const zerodevWalletCredentials = (_client_user_verifiedCredentials_filter = (_client_user = client.user) == null ? void 0 : _client_user.verifiedCredentials.filter((credential)=>{
38
+ var _credential_walletName;
39
+ return credential.walletProvider === WalletProviderEnum.SmartContractWallet && ((_credential_walletName = credential.walletName) == null ? void 0 : _credential_walletName.toLowerCase().startsWith(ZERODEV_METADATA.normalizedWalletName)) && credential.address && credential.chain && getChainFromVerifiedCredentialChain(credential.chain) === 'EVM';
40
+ })) != null ? _client_user_verifiedCredentials_filter : [];
41
+ const zerodevAddresses = zerodevWalletCredentials.map(// casting because we're already filtering out credentials without an address
42
+ (credential)=>credential.address);
43
+ return zerodevAddresses;
44
+ };
45
+
46
+ const getZerodevProviderFromSettings = (client)=>{
47
+ var _projectSettings_providers;
48
+ const projectSettings = client.projectSettings;
49
+ assertDefined(projectSettings, 'Project settings are not available');
50
+ const zerodevProvider = (_projectSettings_providers = projectSettings.providers) == null ? void 0 : _projectSettings_providers.find((provider)=>provider.provider === ProviderEnum.Zerodev);
51
+ assertDefined(zerodevProvider, 'Zerodev is not configured in project settings');
52
+ return zerodevProvider;
53
+ };
54
+
55
+ const getZerodevChainProviderForNetworkId = ({ networkId }, client)=>{
56
+ var _zerodevProvider_multichainAccountAbstractionProviders;
57
+ const zerodevProvider = getZerodevProviderFromSettings(client);
58
+ return (_zerodevProvider_multichainAccountAbstractionProviders = zerodevProvider.multichainAccountAbstractionProviders) == null ? void 0 : _zerodevProvider_multichainAccountAbstractionProviders.find((provider)=>provider.chain === networkId);
59
+ };
60
+
61
+ function _extends() {
62
+ _extends = Object.assign || function assign(target) {
63
+ for(var i = 1; i < arguments.length; i++){
64
+ var source = arguments[i];
65
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
66
+ }
67
+ return target;
68
+ };
69
+ return _extends.apply(this, arguments);
70
+ }
71
+
29
72
  /**
30
73
  * Gets a WalletClient instance for the EOA of the smart wallet account.
31
74
  * You can use this signer to sign EVM transactions.
@@ -46,6 +89,404 @@ const ZERODEV_EXTENSION_KEY = 'zerodev';
46
89
  }, client);
47
90
  };
48
91
 
92
+ const getEcdsaValidator = ({ ecdsaProviderType, publicClient, signer, entryPoint, kernelVersion })=>{
93
+ const params = {
94
+ entryPoint,
95
+ kernelVersion,
96
+ signer
97
+ };
98
+ if (ecdsaProviderType === 'zerodev_multi_chain') {
99
+ return toMultiChainECDSAValidator(publicClient, params);
100
+ }
101
+ return signerToEcdsaValidator(publicClient, params);
102
+ };
103
+
104
+ const getEntryPoint = (entryPoint)=>{
105
+ if (entryPoint === ProviderEntryPointVersionEnum.V6) {
106
+ return constants.getEntryPoint('0.6');
107
+ }
108
+ // default to v7
109
+ return constants.getEntryPoint('0.7');
110
+ };
111
+
112
+ const getKernelVersion = ({ kernelVersion, entryPoint })=>{
113
+ // If kernel version is explicitly provided, use it
114
+ if (kernelVersion && kernelVersion in KERNEL_VERSION_MAP) {
115
+ return KERNEL_VERSION_MAP[kernelVersion];
116
+ }
117
+ // If no kernel version provided, determine based on entry point version
118
+ const entryPointVersion = entryPoint.version;
119
+ if (entryPointVersion in ENTRY_POINT_VERSION_MAP) {
120
+ return ENTRY_POINT_VERSION_MAP[entryPointVersion];
121
+ }
122
+ // Fallback to latest version
123
+ return constants.KERNEL_V3_3;
124
+ };
125
+
126
+ const shouldUseEIP7702 = ({ smartWalletAccount }, client)=>{
127
+ const zerodevProvider = getZerodevProviderFromSettings(client);
128
+ assertDefined(zerodevProvider, 'Zerodev provider is not enabled in project settings');
129
+ const eoaWalletAccount = getOwnerWalletAccountForSmartWalletAccount({
130
+ smartWalletAccount
131
+ }, client);
132
+ assertDefined(eoaWalletAccount, 'EOA wallet account is not found');
133
+ return zerodevProvider.enableEIP7702 && // with EIP7702, the EOA wallet account address is the same as the smart wallet account address
134
+ smartWalletAccount.address === eoaWalletAccount.address;
135
+ };
136
+
137
+ const createKernelAccount = async ({ publicClient, smartWalletAccount }, client)=>{
138
+ var _getVerifiedCredentialForWalletAccount;
139
+ const zerodevProvider = getZerodevProviderFromSettings(client);
140
+ assertDefined(zerodevProvider, 'Zerodev provider is not enabled in project settings');
141
+ const signer = await getSignerForSmartWalletAccount({
142
+ smartWalletAccount
143
+ }, client);
144
+ const walletProperties = (_getVerifiedCredentialForWalletAccount = getVerifiedCredentialForWalletAccount({
145
+ walletAccount: smartWalletAccount
146
+ }, client)) == null ? void 0 : _getVerifiedCredentialForWalletAccount.walletProperties;
147
+ var _walletProperties_entryPointVersion;
148
+ const entryPointVersion = (_walletProperties_entryPointVersion = walletProperties == null ? void 0 : walletProperties.entryPointVersion) != null ? _walletProperties_entryPointVersion : zerodevProvider.entryPointVersion;
149
+ const entryPoint = getEntryPoint(entryPointVersion);
150
+ const useEIP7702 = shouldUseEIP7702({
151
+ smartWalletAccount
152
+ }, client);
153
+ if (useEIP7702) {
154
+ return createKernelAccount$1(publicClient, {
155
+ eip7702Account: signer,
156
+ entryPoint,
157
+ kernelVersion: constants.KERNEL_V3_3
158
+ });
159
+ }
160
+ var _walletProperties_kernelVersion;
161
+ const kernelVersionValue = (_walletProperties_kernelVersion = walletProperties == null ? void 0 : walletProperties.kernelVersion) != null ? _walletProperties_kernelVersion : zerodevProvider.kernelVersion;
162
+ const kernelVersion = getKernelVersion({
163
+ entryPoint,
164
+ kernelVersion: kernelVersionValue
165
+ });
166
+ var _zerodevProvider_enableKernelV3Migration;
167
+ const kernelV3MigrationEnabled = (_zerodevProvider_enableKernelV3Migration = zerodevProvider.enableKernelV3Migration) != null ? _zerodevProvider_enableKernelV3Migration : false;
168
+ if (kernelV3MigrationEnabled) {
169
+ const apiKernelVersion = getKernelVersion({
170
+ entryPoint,
171
+ kernelVersion: zerodevProvider.kernelVersion
172
+ });
173
+ return createEcdsaKernelMigrationAccount(publicClient, {
174
+ entryPoint,
175
+ migrationVersion: {
176
+ from: kernelVersion,
177
+ to: apiKernelVersion
178
+ },
179
+ signer
180
+ });
181
+ }
182
+ var _walletProperties_ecdsaProviderType;
183
+ const validator = await getEcdsaValidator({
184
+ ecdsaProviderType: (_walletProperties_ecdsaProviderType = walletProperties == null ? void 0 : walletProperties.ecdsaProviderType) != null ? _walletProperties_ecdsaProviderType : zerodevProvider.ecdsaProviderType,
185
+ entryPoint,
186
+ kernelVersion,
187
+ publicClient,
188
+ signer
189
+ });
190
+ return createKernelAccount$1(publicClient, {
191
+ entryPoint,
192
+ kernelVersion,
193
+ plugins: {
194
+ sudo: validator
195
+ }
196
+ });
197
+ };
198
+
199
+ const getPaymasterConfig = ({ chain, gasTokenAddress, paymasterRpc })=>{
200
+ const getPaymasterData = (params)=>{
201
+ const zerodevPaymaster = createZeroDevPaymasterClient({
202
+ chain,
203
+ transport: http(paymasterRpc)
204
+ });
205
+ return zerodevPaymaster.sponsorUserOperation({
206
+ gasToken: gasTokenAddress,
207
+ userOperation: params
208
+ });
209
+ };
210
+ return {
211
+ paymaster: {
212
+ getPaymasterData
213
+ }
214
+ };
215
+ };
216
+
217
+ const getZerodevRpc = ({ networkId, bundlerProvider, rpcType }, client)=>{
218
+ const zerodevProvider = getZerodevProviderFromSettings(client);
219
+ if (zerodevProvider == null ? void 0 : zerodevProvider.zerodevPaymasterRpcUrl) {
220
+ return zerodevProvider.zerodevPaymasterRpcUrl;
221
+ }
222
+ const zerodevChainProvider = getZerodevChainProviderForNetworkId({
223
+ networkId
224
+ }, client);
225
+ assertDefined(zerodevChainProvider, `No zerodev provider found for network id ${networkId}`);
226
+ var _ref;
227
+ const paymasterProvider = (_ref = bundlerProvider != null ? bundlerProvider : zerodevProvider.zerodevBundlerProvider) != null ? _ref : ZerodevBundlerProvider.Pimlico;
228
+ const paramName = rpcType === 'bundler' ? 'bundlerProvider' : 'paymasterProvider';
229
+ return `${ZERODEV_RPC_BASE_URL}/${rpcType}/${zerodevChainProvider.clientId}?${paramName}=${paymasterProvider}`;
230
+ };
231
+
232
+ /**
233
+ * Creates a KernelClient instance for a given smart wallet account.
234
+ *
235
+ * @param params.smartWalletAccount - The smart wallet account to create the KernelClient for.
236
+ * @param [params.bundlerProvider] - A custom bundler provider to use
237
+ * @param [params.bundlerRpc] - A custom bundler RPC to use
238
+ * @param [params.paymasterRpc] - A custom paymaster RPC to use
239
+ * @param [params.gasTokenAddress] - The address of a custom ERC20 token to use as a gas token
240
+ * @param [params.withSponsorship] - Whether to use sponsorship for the KernelClient or not (default is true).
241
+ * @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
242
+ * @returns A promise that resolves to a KernelClient instance.
243
+ */ const createKernelClientForWalletAccount = async ({ smartWalletAccount, withSponsorship = true, bundlerProvider, bundlerRpc: bundlerRpcOverride, paymasterRpc: paymasterRpcOverride, gasTokenAddress }, client = getDefaultClient())=>{
244
+ const { networkData: activeNetworkData } = await getActiveNetworkData({
245
+ walletAccount: smartWalletAccount
246
+ }, client);
247
+ assertDefined(activeNetworkData, 'No active network data found');
248
+ const viemChain = mapNetworkDataToViemChain(activeNetworkData);
249
+ const bundlerRpc = bundlerRpcOverride != null ? bundlerRpcOverride : getZerodevRpc({
250
+ bundlerProvider,
251
+ networkId: activeNetworkData.networkId,
252
+ rpcType: 'bundler'
253
+ }, client);
254
+ const bundlerTransport = http(bundlerRpc);
255
+ const publicClient = createPublicClient({
256
+ chain: viemChain,
257
+ transport: bundlerTransport
258
+ });
259
+ const account = await createKernelAccount({
260
+ publicClient,
261
+ smartWalletAccount
262
+ }, client);
263
+ const paymasterRpc = paymasterRpcOverride != null ? paymasterRpcOverride : getZerodevRpc({
264
+ bundlerProvider,
265
+ networkId: activeNetworkData.networkId,
266
+ rpcType: 'paymaster'
267
+ }, client);
268
+ const paymasterConfig = withSponsorship ? getPaymasterConfig({
269
+ chain: viemChain,
270
+ gasTokenAddress,
271
+ paymasterRpc
272
+ }) : {};
273
+ return createKernelAccountClient(_extends({
274
+ account,
275
+ bundlerTransport,
276
+ chain: viemChain,
277
+ client: publicClient,
278
+ userOperation: {
279
+ estimateFeesPerGas: async ({ bundlerClient })=>getUserOperationGasPrice(bundlerClient)
280
+ }
281
+ }, paymasterConfig));
282
+ };
283
+
284
+ const shouldSignWithEoa = async ({ kernelClient, smartWalletAccount }, client)=>{
285
+ const useEIP7702 = shouldUseEIP7702({
286
+ smartWalletAccount
287
+ }, client);
288
+ if (!useEIP7702) {
289
+ return false;
290
+ }
291
+ const isDeployed = await kernelClient.account.isDeployed();
292
+ return !isDeployed;
293
+ };
294
+
295
+ const signMessage = async ({ walletAccount, message }, client)=>{
296
+ const kernelClient = await createKernelClientForWalletAccount({
297
+ smartWalletAccount: walletAccount
298
+ }, client);
299
+ const shouldUseEoa = await shouldSignWithEoa({
300
+ kernelClient,
301
+ smartWalletAccount: walletAccount
302
+ }, client);
303
+ if (shouldUseEoa) {
304
+ const eoaWalletAccount = getOwnerWalletAccountForSmartWalletAccount({
305
+ smartWalletAccount: walletAccount
306
+ }, client);
307
+ assertDefined(eoaWalletAccount, 'Eoa wallet account not found');
308
+ const { signature } = await signMessage$1({
309
+ message,
310
+ walletAccount: eoaWalletAccount
311
+ });
312
+ return {
313
+ signature
314
+ };
315
+ }
316
+ const signature = await kernelClient.signMessage({
317
+ message
318
+ });
319
+ return {
320
+ signature
321
+ };
322
+ };
323
+
324
+ const createZerodevWalletProvider = (client)=>{
325
+ const chain = 'EVM';
326
+ const walletProviderType = WalletProviderEnum.SmartContractWallet;
327
+ const key = formatWalletProviderKey({
328
+ chain,
329
+ displayName: ZERODEV_METADATA.displayName,
330
+ walletProviderType
331
+ });
332
+ const getActiveNetworkId = async ()=>getActiveNetworkIdFromLastKnownRegistry({
333
+ client,
334
+ walletProviderKey: key
335
+ });
336
+ const getConnectedAddresses = async ()=>{
337
+ const zerodevAddresses = getAllUserZerodevAddresses(client);
338
+ return {
339
+ addresses: zerodevAddresses
340
+ };
341
+ };
342
+ const request = async ()=>{
343
+ throw new MethodNotImplementedError('request');
344
+ };
345
+ const signMessage$1 = async ({ walletAccount, message })=>{
346
+ assertDefined(walletAccount, 'Wallet account is required');
347
+ if (!isEvmWalletAccount(walletAccount)) {
348
+ throw new InvalidParamError(`walletAccount is not an EVM wallet account`);
349
+ }
350
+ return signMessage({
351
+ message,
352
+ walletAccount
353
+ }, client);
354
+ };
355
+ const switchActiveNetwork = async ({ networkId })=>{
356
+ const isNetworkEnabled = getZerodevChainProviderForNetworkId({
357
+ networkId
358
+ }, client);
359
+ if (!isNetworkEnabled) {
360
+ throw new UnrecognizedNetworkError({
361
+ networkId,
362
+ originalError: null,
363
+ walletProviderKey: key
364
+ });
365
+ }
366
+ return switchActiveNetworkInLastKnownRegistry({
367
+ client,
368
+ networkId,
369
+ walletProviderKey: key
370
+ });
371
+ };
372
+ return {
373
+ chain,
374
+ getActiveNetworkId,
375
+ getConnectedAddresses,
376
+ groupKey: formatWalletProviderGroupKey(ZERODEV_METADATA.displayName),
377
+ key,
378
+ metadata: {
379
+ displayName: ZERODEV_METADATA.displayName,
380
+ icon: ZERODEV_METADATA.icon
381
+ },
382
+ request,
383
+ signMessage: signMessage$1,
384
+ switchActiveNetwork,
385
+ walletProviderType
386
+ };
387
+ };
388
+
389
+ const ZERODEV_EXTENSION_KEY = 'zerodev';
390
+ /**
391
+ * Adds the ZeroDev extension to the Dynamic client.
392
+ *
393
+ * This extension enables Account Abstraction integration with ZeroDev
394
+ *
395
+ * @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
396
+ */ const addZerodevExtension = (client = getDefaultClient())=>{
397
+ if (hasExtension({
398
+ extensionKey: ZERODEV_EXTENSION_KEY
399
+ }, client)) {
400
+ return;
401
+ }
402
+ registerExtension({
403
+ extensionKey: ZERODEV_EXTENSION_KEY
404
+ }, client);
405
+ const walletProviderRegistry = getWalletProviderRegistry(client);
406
+ const walletProvider = createZerodevWalletProvider(client);
407
+ walletProviderRegistry.register({
408
+ priority: WalletProviderPriority.WALLET_SDK,
409
+ walletProvider
410
+ });
411
+ };
412
+
413
+ const prepareUserOperationWithKernelClient = async ({ kernelClient, transaction })=>{
414
+ var _transaction_data;
415
+ const callData = await kernelClient.account.encodeCalls([
416
+ {
417
+ data: (_transaction_data = transaction.data) != null ? _transaction_data : '0x',
418
+ to: transaction.to,
419
+ value: transaction.value
420
+ }
421
+ ]);
422
+ return kernelClient.prepareUserOperation({
423
+ callData
424
+ });
425
+ };
426
+
427
+ /**
428
+ * Checks if a transaction can be sponsored
429
+ * @param params.transaction - The transaction to check if it can be sponsored
430
+ * @param params.kernelClient - The kernel client to use to check if the transaction can be sponsored. If no provided, a walletAccount is required to create a kernel client with sponsorship.
431
+ * @param params.walletAccount - The wallet account that will be used to send the transaction, Only required if no kernel client is provided
432
+ * @returns True if the transaction can be sponsored, false otherwise
433
+ */ const canSponsorTransaction = async ({ walletAccount, transaction, kernelClient })=>{
434
+ let kernelClientToUse = kernelClient;
435
+ if (!kernelClientToUse) {
436
+ assertDefined(walletAccount, 'Please provide either a wallet account or a kernel client in the parameters');
437
+ kernelClientToUse = await createKernelClientForWalletAccount({
438
+ smartWalletAccount: walletAccount
439
+ });
440
+ }
441
+ try {
442
+ const sponsorResult = await prepareUserOperationWithKernelClient({
443
+ kernelClient: kernelClientToUse,
444
+ transaction
445
+ });
446
+ const hasPaymaster = sponsorResult.paymasterAndData !== '0x';
447
+ return hasPaymaster;
448
+ } catch (e) {
449
+ return false;
450
+ }
451
+ };
452
+
453
+ const calculateGasForUserOperation = ({ userOperationData })=>{
454
+ // Sum all gas units
455
+ const totalGasUnits = userOperationData.callGasLimit + userOperationData.verificationGasLimit + userOperationData.preVerificationGas;
456
+ // Multiply by maxFeePerGas to get the total gas cost in wei
457
+ const gasCost = totalGasUnits * userOperationData.maxFeePerGas;
458
+ return gasCost;
459
+ };
460
+
461
+ /**
462
+ * Estimates the total gas for a transaction
463
+ * @param params.transaction - The transaction to estimate the gas for
464
+ * @param params.walletAccount - The wallet account that will be used to send the transaction
465
+ * @returns The gas for the transaction in wei
466
+ */ const estimateTransactionGas = async ({ walletAccount, transaction })=>{
467
+ try {
468
+ const kernelClientWithoutSponsorship = await createKernelClientForWalletAccount({
469
+ smartWalletAccount: walletAccount,
470
+ withSponsorship: false
471
+ });
472
+ const unsponsoredUserOperation = await prepareUserOperationWithKernelClient({
473
+ kernelClient: kernelClientWithoutSponsorship,
474
+ transaction
475
+ });
476
+ return calculateGasForUserOperation({
477
+ userOperationData: unsponsoredUserOperation
478
+ });
479
+ } catch (e) {
480
+ return null;
481
+ }
482
+ };
483
+
484
+ const isGasSponsorshipError = (err)=>{
485
+ var _errorWithMessage_message;
486
+ const errorWithMessage = err;
487
+ return (errorWithMessage == null ? void 0 : (_errorWithMessage_message = errorWithMessage.message) == null ? void 0 : _errorWithMessage_message.includes('userOp did not match any gas sponsoring policies')) || false;
488
+ };
489
+
49
490
  assertPackageVersion(name, version);
50
491
 
51
- export { addZerodevExtension, getSignerForSmartWalletAccount };
492
+ export { addZerodevExtension, canSponsorTransaction, createKernelClientForWalletAccount, estimateTransactionGas, getSignerForSmartWalletAccount, isGasSponsorshipError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs-sdk/zerodev",
3
- "version": "0.1.0-alpha.17",
3
+ "version": "0.1.0-alpha.19",
4
4
  "type": "module",
5
5
  "main": "./index.cjs.js",
6
6
  "module": "./index.esm.js",
@@ -14,10 +14,10 @@
14
14
  }
15
15
  },
16
16
  "dependencies": {
17
- "@dynamic-labs-sdk/assert-package-version": "0.1.0-alpha.17",
18
- "@dynamic-labs-sdk/client": "0.1.0-alpha.17",
19
- "@dynamic-labs-sdk/evm": "0.1.0-alpha.17",
20
- "@dynamic-labs/sdk-api-core": "0.0.791",
17
+ "@dynamic-labs-sdk/assert-package-version": "0.1.0-alpha.19",
18
+ "@dynamic-labs-sdk/client": "0.1.0-alpha.19",
19
+ "@dynamic-labs-sdk/evm": "0.1.0-alpha.19",
20
+ "@dynamic-labs/sdk-api-core": "0.0.805",
21
21
  "@zerodev/ecdsa-validator": "5.4.9",
22
22
  "@zerodev/multi-chain-ecdsa-validator": "5.4.5",
23
23
  "@zerodev/sdk": "5.4.36"
@@ -1 +1 @@
1
- {"version":3,"file":"addZerodevExtension.d.ts","sourceRoot":"","sources":["../../../../../packages/zerodev/src/addZerodevExtension/addZerodevExtension.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,qBAAqB,YAAY,CAAC;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,wBAQ/B,CAAC"}
1
+ {"version":3,"file":"addZerodevExtension.d.ts","sourceRoot":"","sources":["../../../../../packages/zerodev/src/addZerodevExtension/addZerodevExtension.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,qBAAqB,YAAY,CAAC;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,wBAe/B,CAAC"}
@@ -0,0 +1,29 @@
1
+ import type { EvmWalletAccount } from '@dynamic-labs-sdk/evm';
2
+ import type { Hex } from 'viem';
3
+ import type { KernelClient } from '../KernelClient.types';
4
+ type CanSponsorTransactionBaseParams = {
5
+ transaction: {
6
+ data?: Hex;
7
+ to: Hex;
8
+ value: bigint;
9
+ };
10
+ };
11
+ type CanSponsorTransactionWithWalletParams = CanSponsorTransactionBaseParams & {
12
+ kernelClient?: never;
13
+ walletAccount: EvmWalletAccount;
14
+ };
15
+ type CanSponsorTransactionWithClientParams = CanSponsorTransactionBaseParams & {
16
+ kernelClient: KernelClient;
17
+ walletAccount?: never;
18
+ };
19
+ export type CanSponsorTransactionParams = CanSponsorTransactionWithWalletParams | CanSponsorTransactionWithClientParams;
20
+ /**
21
+ * Checks if a transaction can be sponsored
22
+ * @param params.transaction - The transaction to check if it can be sponsored
23
+ * @param params.kernelClient - The kernel client to use to check if the transaction can be sponsored. If no provided, a walletAccount is required to create a kernel client with sponsorship.
24
+ * @param params.walletAccount - The wallet account that will be used to send the transaction, Only required if no kernel client is provided
25
+ * @returns True if the transaction can be sponsored, false otherwise
26
+ */
27
+ export declare const canSponsorTransaction: ({ walletAccount, transaction, kernelClient, }: CanSponsorTransactionParams) => Promise<boolean>;
28
+ export {};
29
+ //# sourceMappingURL=canSponsorTransaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canSponsorTransaction.d.ts","sourceRoot":"","sources":["../../../../../packages/zerodev/src/canSponsorTransaction/canSponsorTransaction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAGhC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG1D,KAAK,+BAA+B,GAAG;IACrC,WAAW,EAAE;QACX,IAAI,CAAC,EAAE,GAAG,CAAC;QACX,EAAE,EAAE,GAAG,CAAC;QACR,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;CACH,CAAC;AAEF,KAAK,qCAAqC,GAAG,+BAA+B,GAAG;IAC7E,YAAY,CAAC,EAAE,KAAK,CAAC;IACrB,aAAa,EAAE,gBAAgB,CAAC;CACjC,CAAC;AAEF,KAAK,qCAAqC,GAAG,+BAA+B,GAAG;IAC7E,YAAY,EAAE,YAAY,CAAC;IAC3B,aAAa,CAAC,EAAE,KAAK,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,2BAA2B,GACnC,qCAAqC,GACrC,qCAAqC,CAAC;AAE1C;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,kDAI/B,2BAA2B,KAAG,OAAO,CAAC,OAAO,CA0B/C,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { canSponsorTransaction, type CanSponsorTransactionParams, } from './canSponsorTransaction';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/zerodev/src/canSponsorTransaction/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,KAAK,2BAA2B,GACjC,MAAM,yBAAyB,CAAC"}
@@ -1,5 +1,10 @@
1
1
  import type { KERNEL_VERSION_TYPE } from '@zerodev/sdk/types';
2
2
  import type { EntryPointVersion } from 'viem/account-abstraction';
3
+ export declare const ZERODEV_METADATA: {
4
+ displayName: string;
5
+ icon: string;
6
+ normalizedWalletName: string;
7
+ };
3
8
  export declare const ZERODEV_RPC_BASE_URL = "https://rpc.zerodev.app/api/v2";
4
9
  export declare const KERNEL_VERSION_MAP: {
5
10
  readonly v3_3: import("@zerodev/sdk/types").KERNEL_V3_VERSION_TYPE;
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../packages/zerodev/src/constants.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE,eAAO,MAAM,oBAAoB,mCAAmC,CAAC;AAErE,eAAO,MAAM,kBAAkB;;;;;;CAMrB,CAAC;AAEX,eAAO,MAAM,uBAAuB,EAAE,MAAM,CAC1C,iBAAiB,EACjB,mBAAmB,CAKX,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../packages/zerodev/src/constants.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE,eAAO,MAAM,gBAAgB;;;;CAI5B,CAAC;AAEF,eAAO,MAAM,oBAAoB,mCAAmC,CAAC;AAErE,eAAO,MAAM,kBAAkB;;;;;;CAMrB,CAAC;AAEX,eAAO,MAAM,uBAAuB,EAAE,MAAM,CAC1C,iBAAiB,EACjB,mBAAmB,CAKX,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { EvmWalletAccount } from '@dynamic-labs-sdk/evm';
2
+ import type { ZerodevBundlerProvider } from '@dynamic-labs/sdk-api-core';
3
+ import type { Hex } from 'viem';
4
+ import type { KernelClient } from '../KernelClient.types';
5
+ type CreateKernelClientForWalletAccountParams = {
6
+ bundlerProvider?: ZerodevBundlerProvider;
7
+ bundlerRpc?: string;
8
+ gasTokenAddress?: Hex;
9
+ paymasterRpc?: string;
10
+ smartWalletAccount: EvmWalletAccount;
11
+ withSponsorship?: boolean;
12
+ };
13
+ /**
14
+ * Creates a KernelClient instance for a given smart wallet account.
15
+ *
16
+ * @param params.smartWalletAccount - The smart wallet account to create the KernelClient for.
17
+ * @param [params.bundlerProvider] - A custom bundler provider to use
18
+ * @param [params.bundlerRpc] - A custom bundler RPC to use
19
+ * @param [params.paymasterRpc] - A custom paymaster RPC to use
20
+ * @param [params.gasTokenAddress] - The address of a custom ERC20 token to use as a gas token
21
+ * @param [params.withSponsorship] - Whether to use sponsorship for the KernelClient or not (default is true).
22
+ * @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
23
+ * @returns A promise that resolves to a KernelClient instance.
24
+ */
25
+ export declare const createKernelClientForWalletAccount: ({ smartWalletAccount, withSponsorship, bundlerProvider, bundlerRpc: bundlerRpcOverride, paymasterRpc: paymasterRpcOverride, gasTokenAddress, }: CreateKernelClientForWalletAccountParams, client?: any) => Promise<KernelClient>;
26
+ export {};
27
+ //# sourceMappingURL=createKernelClientForWalletAccount.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createKernelClientForWalletAccount.d.ts","sourceRoot":"","sources":["../../../../../packages/zerodev/src/createKernelClientForWalletAccount/createKernelClientForWalletAccount.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAKzE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAGhC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAK1D,KAAK,wCAAwC,GAAG;IAC9C,eAAe,CAAC,EAAE,sBAAsB,CAAC;IACzC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,GAAG,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,kBAAkB,EAAE,gBAAgB,CAAC;IACrC,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,kCAAkC,mJAQ1C,wCAAwC,mBAE1C,OAAO,CAAC,YAAY,CAkEtB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { createKernelClientForWalletAccount } from './createKernelClientForWalletAccount';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/zerodev/src/createKernelClientForWalletAccount/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kCAAkC,EAAE,MAAM,sCAAsC,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { EvmWalletAccount } from '@dynamic-labs-sdk/evm';
2
+ import type { Hex } from 'viem';
3
+ export type EstimateTransactionGasParams = {
4
+ transaction: {
5
+ data?: Hex;
6
+ to: Hex;
7
+ value: bigint;
8
+ };
9
+ walletAccount: EvmWalletAccount;
10
+ };
11
+ /**
12
+ * Estimates the total gas for a transaction
13
+ * @param params.transaction - The transaction to estimate the gas for
14
+ * @param params.walletAccount - The wallet account that will be used to send the transaction
15
+ * @returns The gas for the transaction in wei
16
+ */
17
+ export declare const estimateTransactionGas: ({ walletAccount, transaction, }: EstimateTransactionGasParams) => Promise<bigint | null>;
18
+ //# sourceMappingURL=estimateTransactionGas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"estimateTransactionGas.d.ts","sourceRoot":"","sources":["../../../../../packages/zerodev/src/estimateTransactionGas/estimateTransactionGas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAMhC,MAAM,MAAM,4BAA4B,GAAG;IACzC,WAAW,EAAE;QACX,IAAI,CAAC,EAAE,GAAG,CAAC;QACX,EAAE,EAAE,GAAG,CAAC;QACR,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;IACF,aAAa,EAAE,gBAAgB,CAAC;CACjC,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,oCAGhC,4BAA4B,KAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAqBtD,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { estimateTransactionGas, type EstimateTransactionGasParams, } from './estimateTransactionGas';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/zerodev/src/estimateTransactionGas/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,KAAK,4BAA4B,GAClC,MAAM,0BAA0B,CAAC"}