@zerodev/sdk 4.1.2 → 4.1.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.
@@ -135,7 +135,7 @@ class ZeroDevProvider extends aa_core_1.SmartAccountProvider {
135
135
  if (!(0, ERC4337_utils_js_1.isValidRequest)(request)) {
136
136
  throw new Error(`Request is missing parameters. All properties on UserOperationStruct must be set. uo: ${JSON.stringify(request, null, 2)}`);
137
137
  }
138
- this.account.approvePlugin();
138
+ await this.account.approvePlugin();
139
139
  request.signature = await this.account.validator.getSignature(request);
140
140
  try {
141
141
  hash = await this.rpcClient.sendUserOperation(request, this.entryPointAddress);
@@ -1 +1 @@
1
- {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../../src/kernel-zerodev/provider.ts"],"names":[],"mappings":";;;AAAA,+BAQc;AACd,8CAe0B;AAC1B,iDAMwB;AACxB,6CAA2E;AAC3E,oEAAwE;AACxE,+DAA0D;AAC1D,2CAA+C;AAC/C,4DAAwE;AACxE,gEAA6E;AAoB7E,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,yCAAQ,CAAA;IACR,yDAAgB,CAAA;AAClB,CAAC,EAHW,SAAS,yBAAT,SAAS,QAGpB;AAQD,MAAa,eAAgB,SAAQ,8BAAmC;IAKtE,YAAY,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,GAAG,iCAAkB,EACtC,MAAM,GAAG,0BAAW,EACpB,OAAO,EACP,eAAe,EACf,IAAI,GACkB;QACtB,MAAM,MAAM,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAA,kBAAQ,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QACnE,MAAM,SAAS,GAAG,IAAA,mDAAgC,EAAC;YACjD,KAAK,EAAE,MAAM;YACb,MAAM;YACN,SAAS;YACT,eAAe;SAChB,CAAC,CAAC;QAEH,KAAK,CAAC,SAAS,EAAE,iBAAiB,EAAE,MAAM,EAA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AA,0CAA2B,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACnD,CAAC;CAgCF;AApRD,0CAoRC","sourcesContent":["import {\n fromHex,\n type Address,\n type Chain,\n type Hash,\n type Hex,\n type HttpTransport,\n type RpcTransactionRequest,\n} from \"viem\";\nimport {\n deepHexlify,\n resolveProperties,\n type SmartAccountProviderOpts,\n getChain,\n type UserOperationCallData,\n type BatchUserOperationCallData,\n type SendUserOperationResult,\n asyncPipe,\n noOpMiddleware,\n type UserOperationStruct,\n type BytesLike,\n SmartAccountProvider,\n type AccountMiddlewareFn,\n type UserOperationOverrides,\n} from \"@alchemy/aa-core\";\nimport {\n BUNDLER_URL,\n DEFAULT_SEND_TX_MAX_RETRIES,\n DEFAULT_SEND_TX_RETRY_INTERVAL_MS,\n ENTRYPOINT_ADDRESS,\n minPriorityFeePerBidDefaults,\n} from \"./constants.js\";\nimport { KernelSmartContractAccount, isKernelAccount } from \"./account.js\";\nimport { withZeroDevGasEstimator } from \"./middleware/gas-estimator.js\";\nimport { isValidRequest } from \"./utils/ERC4337-utils.js\";\nimport { InvalidOperation } from \"./errors.js\";\nimport { withZeroDevPaymasterAndData } from \"./middleware/paymaster.js\";\nimport { createZeroDevPublicErc4337Client } from \"./client/create-client.js\";\nimport type {\n PaymasterAndBundlerProviders,\n PaymasterConfig,\n PaymasterPolicy,\n} from \"./paymaster/types.js\";\n\nexport type ZeroDevProviderConfig = {\n projectId: string;\n chain: Chain | number;\n entryPointAddress?: Address;\n rpcUrl?: string;\n account?: KernelSmartContractAccount;\n bundlerProvider?: PaymasterAndBundlerProviders;\n opts?: SmartAccountProviderOpts & {\n sendTxMaxRetries?: number;\n sendTxRetryIntervalMs?: number;\n };\n};\n\nexport enum Operation {\n Call = 0,\n DelegateCall = 1,\n}\n\ntype UserOpDataOperationTypes<T> = T extends UserOperationCallData\n ? Operation.Call | Operation.DelegateCall\n : T extends BatchUserOperationCallData\n ? Operation.Call\n : never;\n\nexport class ZeroDevProvider extends SmartAccountProvider<HttpTransport> {\n protected projectId: string;\n protected sendTxMaxRetries: number;\n protected sendTxRetryIntervalMs: number;\n\n constructor({\n projectId,\n chain,\n entryPointAddress = ENTRYPOINT_ADDRESS,\n rpcUrl = BUNDLER_URL,\n account,\n bundlerProvider,\n opts,\n }: ZeroDevProviderConfig) {\n const _chain = typeof chain === \"number\" ? getChain(chain) : chain;\n const rpcClient = createZeroDevPublicErc4337Client({\n chain: _chain,\n rpcUrl,\n projectId,\n bundlerProvider,\n });\n\n super(rpcClient, entryPointAddress, _chain, account, {\n ...opts,\n txMaxRetries: opts?.txMaxRetries ?? 10,\n txRetryIntervalMs: opts?.txRetryIntervalMs ?? 10000,\n minPriorityFeePerBid:\n opts?.minPriorityFeePerBid ??\n minPriorityFeePerBidDefaults.get(_chain.id),\n });\n\n this.projectId = projectId;\n this.sendTxMaxRetries =\n opts?.sendTxMaxRetries ?? DEFAULT_SEND_TX_MAX_RETRIES;\n this.sendTxRetryIntervalMs =\n opts?.sendTxRetryIntervalMs ?? DEFAULT_SEND_TX_RETRY_INTERVAL_MS;\n\n withZeroDevGasEstimator(this);\n }\n\n getProjectId = (): string => this.projectId;\n\n sendTransaction = async (\n request: RpcTransactionRequest,\n operation: UserOpDataOperationTypes<UserOperationCallData> = Operation.Call\n ): Promise<Hash> => {\n if (!request.to) {\n throw new Error(\"transaction is missing to address\");\n }\n\n const overrides: UserOperationOverrides = {};\n if (request.maxFeePerGas) {\n overrides.maxFeePerGas = request.maxFeePerGas;\n }\n if (request.maxPriorityFeePerGas) {\n overrides.maxPriorityFeePerGas = request.maxPriorityFeePerGas;\n }\n\n const { hash } = await this.sendUserOperation(\n {\n target: request.to,\n data: request.data ?? \"0x\",\n value: request.value ? fromHex(request.value, \"bigint\") : 0n,\n },\n overrides,\n operation\n );\n\n return await this.waitForUserOperationTransaction(hash as Hash);\n };\n\n sendUserOperation = async <\n T extends UserOperationCallData | BatchUserOperationCallData\n >(\n data: T,\n overrides?: UserOperationOverrides,\n operation: UserOpDataOperationTypes<T> = Operation.Call as UserOpDataOperationTypes<T>\n ): Promise<SendUserOperationResult> => {\n if (!isKernelAccount(this.account)) {\n throw new Error(\"account not connected!\");\n }\n if (!this.account.validator) {\n throw new Error(\"validator not connected!\");\n }\n\n let callData: BytesLike = \"0x\";\n\n if (Array.isArray(data)) {\n if (operation === Operation.Call) {\n callData = await this.account.encodeBatchExecute(data);\n } else {\n throw InvalidOperation;\n }\n } else if (isKernelAccount(this.account)) {\n if (operation === Operation.DelegateCall) {\n callData = await this.account.encodeExecuteDelegate(\n data.target,\n data.value ?? 0n,\n data.data\n );\n } else if (operation === Operation.Call) {\n callData = await this.account.encodeExecute(\n data.target,\n data.value ?? 0n,\n data.data\n );\n } else {\n throw InvalidOperation;\n }\n } else {\n throw InvalidOperation;\n }\n\n const initCode = await this.account.getInitCode();\n let hash: string = \"\";\n let i = 0;\n const nonce = await this.account.getNonce();\n let uoStruct: UserOperationStruct;\n let request: UserOperationStruct;\n let maxFeePerGas, maxPriorityFeePerGas;\n do {\n uoStruct = await asyncPipe(\n this.dummyPaymasterDataMiddleware,\n this.feeDataGetter,\n this.paymasterDataMiddleware,\n this.gasEstimator,\n this.customMiddleware ?? noOpMiddleware,\n async (struct) => ({ ...struct, ...overrides })\n )({\n initCode,\n sender: this.getAddress(),\n nonce,\n callData,\n signature: await this.account\n .getValidator()\n .getDynamicDummySignature(await this.getAddress(), callData as Hex),\n maxFeePerGas,\n maxPriorityFeePerGas,\n } as UserOperationStruct);\n request = deepHexlify(await resolveProperties(uoStruct));\n\n if (!isValidRequest(request)) {\n // this pretty prints the uo\n throw new Error(\n `Request is missing parameters. All properties on UserOperationStruct must be set. uo: ${JSON.stringify(\n request,\n null,\n 2\n )}`\n );\n }\n\n this.account.approvePlugin();\n\n request.signature = await this.account.validator.getSignature(request);\n try {\n hash = await this.rpcClient.sendUserOperation(\n request,\n this.entryPointAddress\n );\n } catch (error: any) {\n if (this.isReplacementOpError(error) && i++ < this.sendTxMaxRetries) {\n maxFeePerGas = (BigInt(request.maxFeePerGas) * 113n) / 100n;\n maxPriorityFeePerGas =\n (BigInt(request.maxPriorityFeePerGas) * 113n) / 100n;\n\n console.log(\n `After ${\n this.sendTxRetryIntervalMs / 60000\n } minutes, resending tx with Increased Gas fees: maxFeePerGas: ${maxFeePerGas}, maxPriorityFeePerGas: ${maxPriorityFeePerGas}`\n );\n await new Promise((resolve) =>\n setTimeout(resolve, this.sendTxRetryIntervalMs)\n );\n continue;\n }\n throw this.unwrapError(error);\n }\n } while (hash === \"\");\n\n return {\n hash,\n request,\n };\n };\n\n isReplacementOpError(errorIn: any): boolean {\n if (errorIn.cause != null) {\n const failedOpMessage: string | undefined = errorIn?.cause?.message;\n return (\n (failedOpMessage?.includes(\n \"replacement op must increase maxFeePerGas and MaxPriorityFeePerGas\"\n ) ||\n failedOpMessage?.match(/.*replacement.*underpriced.*/) !== null) ??\n false\n );\n }\n return false;\n }\n\n unwrapError(errorIn: any): Error {\n if (errorIn?.cause != null) {\n let paymasterInfo: string = \"\";\n let failedOpMessage: string | undefined = errorIn?.cause?.message;\n if (failedOpMessage?.includes(\"FailedOp\") === true) {\n // TODO: better error extraction methods will be needed\n const matched = failedOpMessage.match(/FailedOp\\((.*)\\)/);\n if (matched != null) {\n const split = matched[1].split(\",\");\n paymasterInfo = `(paymaster address: ${split[1]})`;\n failedOpMessage = split[2];\n }\n }\n const error = new Error(\n `The bundler has failed to include UserOperation in a batch: ${failedOpMessage} ${paymasterInfo}`\n );\n error.stack = errorIn.stack;\n return error;\n }\n return errorIn;\n }\n\n getAccount: () => KernelSmartContractAccount = () => {\n if (!isKernelAccount(this.account)) {\n throw new Error(\"account not connected!\");\n }\n return this.account;\n };\n\n dummyPaymasterDataMiddleware: AccountMiddlewareFn = async (\n struct: UserOperationStruct\n ): Promise<UserOperationStruct> => {\n struct.paymasterAndData = \"0x\";\n return struct;\n };\n\n withZeroDevPaymasterAndData(config: PaymasterConfig<PaymasterPolicy>) {\n if (!this.isConnected()) {\n throw new Error(\n \"ZeroDevProvider: account is not set, did you call `connect` first?\"\n );\n }\n\n return withZeroDevPaymasterAndData(this, config);\n }\n\n request: (args: { method: string; params?: any[] }) => Promise<any> = async (\n args\n ) => {\n const { method, params } = args;\n switch (method) {\n case \"eth_chainId\":\n return this.chain.id;\n case \"eth_sendTransaction\":\n const [tx] = params as [RpcTransactionRequest];\n return this.sendTransaction(tx);\n case \"eth_signTypedData_v4\":\n //@ts-expect-error\n return this.signTypedData(JSON.parse(params[1]));\n case \"personal_sign\":\n if (!this.account) {\n throw new Error(\"account not connected!\");\n }\n const [data, address] = params!;\n if (address.toLowerCase() !== (await this.getAddress()).toLowerCase()) {\n throw new Error(\n \"cannot sign for address that is not the current account\"\n );\n }\n // @ts-ignore\n return this.account.signMessageWith6492(data);\n default:\n // @ts-ignore\n return this.rpcClient.request(args);\n }\n };\n}\n"]}
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../../src/kernel-zerodev/provider.ts"],"names":[],"mappings":";;;AAAA,+BAQc;AACd,8CAe0B;AAC1B,iDAMwB;AACxB,6CAA2E;AAC3E,oEAAwE;AACxE,+DAA0D;AAC1D,2CAA+C;AAC/C,4DAAwE;AACxE,gEAA6E;AAoB7E,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,yCAAQ,CAAA;IACR,yDAAgB,CAAA;AAClB,CAAC,EAHW,SAAS,yBAAT,SAAS,QAGpB;AAQD,MAAa,eAAgB,SAAQ,8BAAmC;IAKtE,YAAY,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,GAAG,iCAAkB,EACtC,MAAM,GAAG,0BAAW,EACpB,OAAO,EACP,eAAe,EACf,IAAI,GACkB;QACtB,MAAM,MAAM,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAA,kBAAQ,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QACnE,MAAM,SAAS,GAAG,IAAA,mDAAgC,EAAC;YACjD,KAAK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,MAAwC;QAClE,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE;YACvB,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAC;SACH;QAED,OAAO,IAAA,0CAA2B,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACnD,CAAC;CAgCF;AApRD,0CAoRC","sourcesContent":["import {\n fromHex,\n type Address,\n type Chain,\n type Hash,\n type Hex,\n type HttpTransport,\n type RpcTransactionRequest,\n} from \"viem\";\nimport {\n deepHexlify,\n resolveProperties,\n type SmartAccountProviderOpts,\n getChain,\n type UserOperationCallData,\n type BatchUserOperationCallData,\n type SendUserOperationResult,\n asyncPipe,\n noOpMiddleware,\n type UserOperationStruct,\n type BytesLike,\n SmartAccountProvider,\n type AccountMiddlewareFn,\n type UserOperationOverrides,\n} from \"@alchemy/aa-core\";\nimport {\n BUNDLER_URL,\n DEFAULT_SEND_TX_MAX_RETRIES,\n DEFAULT_SEND_TX_RETRY_INTERVAL_MS,\n ENTRYPOINT_ADDRESS,\n minPriorityFeePerBidDefaults,\n} from \"./constants.js\";\nimport { KernelSmartContractAccount, isKernelAccount } from \"./account.js\";\nimport { withZeroDevGasEstimator } from \"./middleware/gas-estimator.js\";\nimport { isValidRequest } from \"./utils/ERC4337-utils.js\";\nimport { InvalidOperation } from \"./errors.js\";\nimport { withZeroDevPaymasterAndData } from \"./middleware/paymaster.js\";\nimport { createZeroDevPublicErc4337Client } from \"./client/create-client.js\";\nimport type {\n PaymasterAndBundlerProviders,\n PaymasterConfig,\n PaymasterPolicy,\n} from \"./paymaster/types.js\";\n\nexport type ZeroDevProviderConfig = {\n projectId: string;\n chain: Chain | number;\n entryPointAddress?: Address;\n rpcUrl?: string;\n account?: KernelSmartContractAccount;\n bundlerProvider?: PaymasterAndBundlerProviders;\n opts?: SmartAccountProviderOpts & {\n sendTxMaxRetries?: number;\n sendTxRetryIntervalMs?: number;\n };\n};\n\nexport enum Operation {\n Call = 0,\n DelegateCall = 1,\n}\n\ntype UserOpDataOperationTypes<T> = T extends UserOperationCallData\n ? Operation.Call | Operation.DelegateCall\n : T extends BatchUserOperationCallData\n ? Operation.Call\n : never;\n\nexport class ZeroDevProvider extends SmartAccountProvider<HttpTransport> {\n protected projectId: string;\n protected sendTxMaxRetries: number;\n protected sendTxRetryIntervalMs: number;\n\n constructor({\n projectId,\n chain,\n entryPointAddress = ENTRYPOINT_ADDRESS,\n rpcUrl = BUNDLER_URL,\n account,\n bundlerProvider,\n opts,\n }: ZeroDevProviderConfig) {\n const _chain = typeof chain === \"number\" ? getChain(chain) : chain;\n const rpcClient = createZeroDevPublicErc4337Client({\n chain: _chain,\n rpcUrl,\n projectId,\n bundlerProvider,\n });\n\n super(rpcClient, entryPointAddress, _chain, account, {\n ...opts,\n txMaxRetries: opts?.txMaxRetries ?? 10,\n txRetryIntervalMs: opts?.txRetryIntervalMs ?? 10000,\n minPriorityFeePerBid:\n opts?.minPriorityFeePerBid ??\n minPriorityFeePerBidDefaults.get(_chain.id),\n });\n\n this.projectId = projectId;\n this.sendTxMaxRetries =\n opts?.sendTxMaxRetries ?? DEFAULT_SEND_TX_MAX_RETRIES;\n this.sendTxRetryIntervalMs =\n opts?.sendTxRetryIntervalMs ?? DEFAULT_SEND_TX_RETRY_INTERVAL_MS;\n\n withZeroDevGasEstimator(this);\n }\n\n getProjectId = (): string => this.projectId;\n\n sendTransaction = async (\n request: RpcTransactionRequest,\n operation: UserOpDataOperationTypes<UserOperationCallData> = Operation.Call\n ): Promise<Hash> => {\n if (!request.to) {\n throw new Error(\"transaction is missing to address\");\n }\n\n const overrides: UserOperationOverrides = {};\n if (request.maxFeePerGas) {\n overrides.maxFeePerGas = request.maxFeePerGas;\n }\n if (request.maxPriorityFeePerGas) {\n overrides.maxPriorityFeePerGas = request.maxPriorityFeePerGas;\n }\n\n const { hash } = await this.sendUserOperation(\n {\n target: request.to,\n data: request.data ?? \"0x\",\n value: request.value ? fromHex(request.value, \"bigint\") : 0n,\n },\n overrides,\n operation\n );\n\n return await this.waitForUserOperationTransaction(hash as Hash);\n };\n\n sendUserOperation = async <\n T extends UserOperationCallData | BatchUserOperationCallData\n >(\n data: T,\n overrides?: UserOperationOverrides,\n operation: UserOpDataOperationTypes<T> = Operation.Call as UserOpDataOperationTypes<T>\n ): Promise<SendUserOperationResult> => {\n if (!isKernelAccount(this.account)) {\n throw new Error(\"account not connected!\");\n }\n if (!this.account.validator) {\n throw new Error(\"validator not connected!\");\n }\n\n let callData: BytesLike = \"0x\";\n\n if (Array.isArray(data)) {\n if (operation === Operation.Call) {\n callData = await this.account.encodeBatchExecute(data);\n } else {\n throw InvalidOperation;\n }\n } else if (isKernelAccount(this.account)) {\n if (operation === Operation.DelegateCall) {\n callData = await this.account.encodeExecuteDelegate(\n data.target,\n data.value ?? 0n,\n data.data\n );\n } else if (operation === Operation.Call) {\n callData = await this.account.encodeExecute(\n data.target,\n data.value ?? 0n,\n data.data\n );\n } else {\n throw InvalidOperation;\n }\n } else {\n throw InvalidOperation;\n }\n\n const initCode = await this.account.getInitCode();\n let hash: string = \"\";\n let i = 0;\n const nonce = await this.account.getNonce();\n let uoStruct: UserOperationStruct;\n let request: UserOperationStruct;\n let maxFeePerGas, maxPriorityFeePerGas;\n do {\n uoStruct = await asyncPipe(\n this.dummyPaymasterDataMiddleware,\n this.feeDataGetter,\n this.paymasterDataMiddleware,\n this.gasEstimator,\n this.customMiddleware ?? noOpMiddleware,\n async (struct) => ({ ...struct, ...overrides })\n )({\n initCode,\n sender: this.getAddress(),\n nonce,\n callData,\n signature: await this.account\n .getValidator()\n .getDynamicDummySignature(await this.getAddress(), callData as Hex),\n maxFeePerGas,\n maxPriorityFeePerGas,\n } as UserOperationStruct);\n request = deepHexlify(await resolveProperties(uoStruct));\n\n if (!isValidRequest(request)) {\n // this pretty prints the uo\n throw new Error(\n `Request is missing parameters. All properties on UserOperationStruct must be set. uo: ${JSON.stringify(\n request,\n null,\n 2\n )}`\n );\n }\n\n await this.account.approvePlugin();\n\n request.signature = await this.account.validator.getSignature(request);\n try {\n hash = await this.rpcClient.sendUserOperation(\n request,\n this.entryPointAddress\n );\n } catch (error: any) {\n if (this.isReplacementOpError(error) && i++ < this.sendTxMaxRetries) {\n maxFeePerGas = (BigInt(request.maxFeePerGas) * 113n) / 100n;\n maxPriorityFeePerGas =\n (BigInt(request.maxPriorityFeePerGas) * 113n) / 100n;\n\n console.log(\n `After ${\n this.sendTxRetryIntervalMs / 60000\n } minutes, resending tx with Increased Gas fees: maxFeePerGas: ${maxFeePerGas}, maxPriorityFeePerGas: ${maxPriorityFeePerGas}`\n );\n await new Promise((resolve) =>\n setTimeout(resolve, this.sendTxRetryIntervalMs)\n );\n continue;\n }\n throw this.unwrapError(error);\n }\n } while (hash === \"\");\n\n return {\n hash,\n request,\n };\n };\n\n isReplacementOpError(errorIn: any): boolean {\n if (errorIn.cause != null) {\n const failedOpMessage: string | undefined = errorIn?.cause?.message;\n return (\n (failedOpMessage?.includes(\n \"replacement op must increase maxFeePerGas and MaxPriorityFeePerGas\"\n ) ||\n failedOpMessage?.match(/.*replacement.*underpriced.*/) !== null) ??\n false\n );\n }\n return false;\n }\n\n unwrapError(errorIn: any): Error {\n if (errorIn?.cause != null) {\n let paymasterInfo: string = \"\";\n let failedOpMessage: string | undefined = errorIn?.cause?.message;\n if (failedOpMessage?.includes(\"FailedOp\") === true) {\n // TODO: better error extraction methods will be needed\n const matched = failedOpMessage.match(/FailedOp\\((.*)\\)/);\n if (matched != null) {\n const split = matched[1].split(\",\");\n paymasterInfo = `(paymaster address: ${split[1]})`;\n failedOpMessage = split[2];\n }\n }\n const error = new Error(\n `The bundler has failed to include UserOperation in a batch: ${failedOpMessage} ${paymasterInfo}`\n );\n error.stack = errorIn.stack;\n return error;\n }\n return errorIn;\n }\n\n getAccount: () => KernelSmartContractAccount = () => {\n if (!isKernelAccount(this.account)) {\n throw new Error(\"account not connected!\");\n }\n return this.account;\n };\n\n dummyPaymasterDataMiddleware: AccountMiddlewareFn = async (\n struct: UserOperationStruct\n ): Promise<UserOperationStruct> => {\n struct.paymasterAndData = \"0x\";\n return struct;\n };\n\n withZeroDevPaymasterAndData(config: PaymasterConfig<PaymasterPolicy>) {\n if (!this.isConnected()) {\n throw new Error(\n \"ZeroDevProvider: account is not set, did you call `connect` first?\"\n );\n }\n\n return withZeroDevPaymasterAndData(this, config);\n }\n\n request: (args: { method: string; params?: any[] }) => Promise<any> = async (\n args\n ) => {\n const { method, params } = args;\n switch (method) {\n case \"eth_chainId\":\n return this.chain.id;\n case \"eth_sendTransaction\":\n const [tx] = params as [RpcTransactionRequest];\n return this.sendTransaction(tx);\n case \"eth_signTypedData_v4\":\n //@ts-expect-error\n return this.signTypedData(JSON.parse(params[1]));\n case \"personal_sign\":\n if (!this.account) {\n throw new Error(\"account not connected!\");\n }\n const [data, address] = params!;\n if (address.toLowerCase() !== (await this.getAddress()).toLowerCase()) {\n throw new Error(\n \"cannot sign for address that is not the current account\"\n );\n }\n // @ts-ignore\n return this.account.signMessageWith6492(data);\n default:\n // @ts-ignore\n return this.rpcClient.request(args);\n }\n };\n}\n"]}
@@ -132,7 +132,7 @@ export class ZeroDevProvider extends SmartAccountProvider {
132
132
  if (!isValidRequest(request)) {
133
133
  throw new Error(`Request is missing parameters. All properties on UserOperationStruct must be set. uo: ${JSON.stringify(request, null, 2)}`);
134
134
  }
135
- this.account.approvePlugin();
135
+ await this.account.approvePlugin();
136
136
  request.signature = await this.account.validator.getSignature(request);
137
137
  try {
138
138
  hash = await this.rpcClient.sendUserOperation(request, this.entryPointAddress);
@@ -1 +1 @@
1
- {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../../src/kernel-zerodev/provider.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,GAOR,MAAM,MAAM,CAAC;AACd,OAAO,EACL,WAAW,EACX,iBAAiB,EAEjB,QAAQ,EAIR,SAAS,EACT,cAAc,EAGd,oBAAoB,GAGrB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,WAAW,EAC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D,OAAO,KAAK,CAAC;IACf,CAAC;IAED,WAAW,CAAC,OAAY;QACtB,IAAI,OAAO,EAAE,KAAK,IAAI,IAAI,EAAE;YAC1B,IAAI,aAAa,GAAW,EAAE,CAAC;YAC/B,IAAI,eAAe,GAAuB,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC;YAClE,IAAI,eAAe,EAAE,QAAQ,CAAC,UAAU,CAAC,KAAK,IAAI,EAAE;gBAElD,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;gBAC1D,IAAI,OAAO,IAAI,IAAI,EAAE;oBACnB,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;oBACpC,aAAa,GAAG,uBAAuB,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC;oBACnD,eAAe,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;iBAC5B;aACF;YACD,MAAM,KAAK,GAAG,IAAI,KAAK,CACrB,+DAA+D,eAAe,IAAI,aAAa,EAAE,CAClG,CAAC;YACF,KAAK,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;YAC5B,OAAO,KAAK,CAAC;SACd;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAgBD,2BAA2B,CAAC,MAAwC;QAClE,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE;YACvB,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAC;SACH;QAED,OAAO,2BAA2B,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACnD,CAAC;CAgCF","sourcesContent":["import {\n fromHex,\n type Address,\n type Chain,\n type Hash,\n type Hex,\n type HttpTransport,\n type RpcTransactionRequest,\n} from \"viem\";\nimport {\n deepHexlify,\n resolveProperties,\n type SmartAccountProviderOpts,\n getChain,\n type UserOperationCallData,\n type BatchUserOperationCallData,\n type SendUserOperationResult,\n asyncPipe,\n noOpMiddleware,\n type UserOperationStruct,\n type BytesLike,\n SmartAccountProvider,\n type AccountMiddlewareFn,\n type UserOperationOverrides,\n} from \"@alchemy/aa-core\";\nimport {\n BUNDLER_URL,\n DEFAULT_SEND_TX_MAX_RETRIES,\n DEFAULT_SEND_TX_RETRY_INTERVAL_MS,\n ENTRYPOINT_ADDRESS,\n minPriorityFeePerBidDefaults,\n} from \"./constants.js\";\nimport { KernelSmartContractAccount, isKernelAccount } from \"./account.js\";\nimport { withZeroDevGasEstimator } from \"./middleware/gas-estimator.js\";\nimport { isValidRequest } from \"./utils/ERC4337-utils.js\";\nimport { InvalidOperation } from \"./errors.js\";\nimport { withZeroDevPaymasterAndData } from \"./middleware/paymaster.js\";\nimport { createZeroDevPublicErc4337Client } from \"./client/create-client.js\";\nimport type {\n PaymasterAndBundlerProviders,\n PaymasterConfig,\n PaymasterPolicy,\n} from \"./paymaster/types.js\";\n\nexport type ZeroDevProviderConfig = {\n projectId: string;\n chain: Chain | number;\n entryPointAddress?: Address;\n rpcUrl?: string;\n account?: KernelSmartContractAccount;\n bundlerProvider?: PaymasterAndBundlerProviders;\n opts?: SmartAccountProviderOpts & {\n sendTxMaxRetries?: number;\n sendTxRetryIntervalMs?: number;\n };\n};\n\nexport enum Operation {\n Call = 0,\n DelegateCall = 1,\n}\n\ntype UserOpDataOperationTypes<T> = T extends UserOperationCallData\n ? Operation.Call | Operation.DelegateCall\n : T extends BatchUserOperationCallData\n ? Operation.Call\n : never;\n\nexport class ZeroDevProvider extends SmartAccountProvider<HttpTransport> {\n protected projectId: string;\n protected sendTxMaxRetries: number;\n protected sendTxRetryIntervalMs: number;\n\n constructor({\n projectId,\n chain,\n entryPointAddress = ENTRYPOINT_ADDRESS,\n rpcUrl = BUNDLER_URL,\n account,\n bundlerProvider,\n opts,\n }: ZeroDevProviderConfig) {\n const _chain = typeof chain === \"number\" ? getChain(chain) : chain;\n const rpcClient = createZeroDevPublicErc4337Client({\n chain: _chain,\n rpcUrl,\n projectId,\n bundlerProvider,\n });\n\n super(rpcClient, entryPointAddress, _chain, account, {\n ...opts,\n txMaxRetries: opts?.txMaxRetries ?? 10,\n txRetryIntervalMs: opts?.txRetryIntervalMs ?? 10000,\n minPriorityFeePerBid:\n opts?.minPriorityFeePerBid ??\n minPriorityFeePerBidDefaults.get(_chain.id),\n });\n\n this.projectId = projectId;\n this.sendTxMaxRetries =\n opts?.sendTxMaxRetries ?? DEFAULT_SEND_TX_MAX_RETRIES;\n this.sendTxRetryIntervalMs =\n opts?.sendTxRetryIntervalMs ?? DEFAULT_SEND_TX_RETRY_INTERVAL_MS;\n\n withZeroDevGasEstimator(this);\n }\n\n getProjectId = (): string => this.projectId;\n\n sendTransaction = async (\n request: RpcTransactionRequest,\n operation: UserOpDataOperationTypes<UserOperationCallData> = Operation.Call\n ): Promise<Hash> => {\n if (!request.to) {\n throw new Error(\"transaction is missing to address\");\n }\n\n const overrides: UserOperationOverrides = {};\n if (request.maxFeePerGas) {\n overrides.maxFeePerGas = request.maxFeePerGas;\n }\n if (request.maxPriorityFeePerGas) {\n overrides.maxPriorityFeePerGas = request.maxPriorityFeePerGas;\n }\n\n const { hash } = await this.sendUserOperation(\n {\n target: request.to,\n data: request.data ?? \"0x\",\n value: request.value ? fromHex(request.value, \"bigint\") : 0n,\n },\n overrides,\n operation\n );\n\n return await this.waitForUserOperationTransaction(hash as Hash);\n };\n\n sendUserOperation = async <\n T extends UserOperationCallData | BatchUserOperationCallData\n >(\n data: T,\n overrides?: UserOperationOverrides,\n operation: UserOpDataOperationTypes<T> = Operation.Call as UserOpDataOperationTypes<T>\n ): Promise<SendUserOperationResult> => {\n if (!isKernelAccount(this.account)) {\n throw new Error(\"account not connected!\");\n }\n if (!this.account.validator) {\n throw new Error(\"validator not connected!\");\n }\n\n let callData: BytesLike = \"0x\";\n\n if (Array.isArray(data)) {\n if (operation === Operation.Call) {\n callData = await this.account.encodeBatchExecute(data);\n } else {\n throw InvalidOperation;\n }\n } else if (isKernelAccount(this.account)) {\n if (operation === Operation.DelegateCall) {\n callData = await this.account.encodeExecuteDelegate(\n data.target,\n data.value ?? 0n,\n data.data\n );\n } else if (operation === Operation.Call) {\n callData = await this.account.encodeExecute(\n data.target,\n data.value ?? 0n,\n data.data\n );\n } else {\n throw InvalidOperation;\n }\n } else {\n throw InvalidOperation;\n }\n\n const initCode = await this.account.getInitCode();\n let hash: string = \"\";\n let i = 0;\n const nonce = await this.account.getNonce();\n let uoStruct: UserOperationStruct;\n let request: UserOperationStruct;\n let maxFeePerGas, maxPriorityFeePerGas;\n do {\n uoStruct = await asyncPipe(\n this.dummyPaymasterDataMiddleware,\n this.feeDataGetter,\n this.paymasterDataMiddleware,\n this.gasEstimator,\n this.customMiddleware ?? noOpMiddleware,\n async (struct) => ({ ...struct, ...overrides })\n )({\n initCode,\n sender: this.getAddress(),\n nonce,\n callData,\n signature: await this.account\n .getValidator()\n .getDynamicDummySignature(await this.getAddress(), callData as Hex),\n maxFeePerGas,\n maxPriorityFeePerGas,\n } as UserOperationStruct);\n request = deepHexlify(await resolveProperties(uoStruct));\n\n if (!isValidRequest(request)) {\n // this pretty prints the uo\n throw new Error(\n `Request is missing parameters. All properties on UserOperationStruct must be set. uo: ${JSON.stringify(\n request,\n null,\n 2\n )}`\n );\n }\n\n this.account.approvePlugin();\n\n request.signature = await this.account.validator.getSignature(request);\n try {\n hash = await this.rpcClient.sendUserOperation(\n request,\n this.entryPointAddress\n );\n } catch (error: any) {\n if (this.isReplacementOpError(error) && i++ < this.sendTxMaxRetries) {\n maxFeePerGas = (BigInt(request.maxFeePerGas) * 113n) / 100n;\n maxPriorityFeePerGas =\n (BigInt(request.maxPriorityFeePerGas) * 113n) / 100n;\n\n console.log(\n `After ${\n this.sendTxRetryIntervalMs / 60000\n } minutes, resending tx with Increased Gas fees: maxFeePerGas: ${maxFeePerGas}, maxPriorityFeePerGas: ${maxPriorityFeePerGas}`\n );\n await new Promise((resolve) =>\n setTimeout(resolve, this.sendTxRetryIntervalMs)\n );\n continue;\n }\n throw this.unwrapError(error);\n }\n } while (hash === \"\");\n\n return {\n hash,\n request,\n };\n };\n\n isReplacementOpError(errorIn: any): boolean {\n if (errorIn.cause != null) {\n const failedOpMessage: string | undefined = errorIn?.cause?.message;\n return (\n (failedOpMessage?.includes(\n \"replacement op must increase maxFeePerGas and MaxPriorityFeePerGas\"\n ) ||\n failedOpMessage?.match(/.*replacement.*underpriced.*/) !== null) ??\n false\n );\n }\n return false;\n }\n\n unwrapError(errorIn: any): Error {\n if (errorIn?.cause != null) {\n let paymasterInfo: string = \"\";\n let failedOpMessage: string | undefined = errorIn?.cause?.message;\n if (failedOpMessage?.includes(\"FailedOp\") === true) {\n // TODO: better error extraction methods will be needed\n const matched = failedOpMessage.match(/FailedOp\\((.*)\\)/);\n if (matched != null) {\n const split = matched[1].split(\",\");\n paymasterInfo = `(paymaster address: ${split[1]})`;\n failedOpMessage = split[2];\n }\n }\n const error = new Error(\n `The bundler has failed to include UserOperation in a batch: ${failedOpMessage} ${paymasterInfo}`\n );\n error.stack = errorIn.stack;\n return error;\n }\n return errorIn;\n }\n\n getAccount: () => KernelSmartContractAccount = () => {\n if (!isKernelAccount(this.account)) {\n throw new Error(\"account not connected!\");\n }\n return this.account;\n };\n\n dummyPaymasterDataMiddleware: AccountMiddlewareFn = async (\n struct: UserOperationStruct\n ): Promise<UserOperationStruct> => {\n struct.paymasterAndData = \"0x\";\n return struct;\n };\n\n withZeroDevPaymasterAndData(config: PaymasterConfig<PaymasterPolicy>) {\n if (!this.isConnected()) {\n throw new Error(\n \"ZeroDevProvider: account is not set, did you call `connect` first?\"\n );\n }\n\n return withZeroDevPaymasterAndData(this, config);\n }\n\n request: (args: { method: string; params?: any[] }) => Promise<any> = async (\n args\n ) => {\n const { method, params } = args;\n switch (method) {\n case \"eth_chainId\":\n return this.chain.id;\n case \"eth_sendTransaction\":\n const [tx] = params as [RpcTransactionRequest];\n return this.sendTransaction(tx);\n case \"eth_signTypedData_v4\":\n //@ts-expect-error\n return this.signTypedData(JSON.parse(params[1]));\n case \"personal_sign\":\n if (!this.account) {\n throw new Error(\"account not connected!\");\n }\n const [data, address] = params!;\n if (address.toLowerCase() !== (await this.getAddress()).toLowerCase()) {\n throw new Error(\n \"cannot sign for address that is not the current account\"\n );\n }\n // @ts-ignore\n return this.account.signMessageWith6492(data);\n default:\n // @ts-ignore\n return this.rpcClient.request(args);\n }\n };\n}\n"]}
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../../src/kernel-zerodev/provider.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,GAOR,MAAM,MAAM,CAAC;AACd,OAAO,EACL,WAAW,EACX,iBAAiB,EAEjB,QAAQ,EAIR,SAAS,EACT,cAAc,EAGd,oBAAoB,GAGrB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,WAAW,EACX,2BAA2B,EAC3B,iCAAiC,EACjC,kBAAkB,EAClB,4BAA4B,GAC7B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,0BAA0B,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC3E,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AACxE,OAAO,EAAE,gCAAgC,EAAE,MAAM,2BAA2B,CAAC;AAoB7E,MAAM,CAAN,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,yCAAQ,CAAA;IACR,yDAAgB,CAAA;AAClB,CAAC,EAHW,SAAS,KAAT,SAAS,QAGpB;AAQD,MAAM,OAAO,eAAgB,SAAQ,oBAAmC;IAKtE,YAAY,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,GAAG,kBAAkB,EACtC,MAAM,GAAG,WAAW,EACpB,OAAO,EACP,eAAe,EACf,IAAI,GACkB;QACtB,MAAM,MAAM,GAAG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;YAC5B,OAAO,KAAK,CAAC;SACd;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAgBD,2BAA2B,CAAC,MAAwC;QAClE,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE;YACvB,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAC;SACH;QAED,OAAO,2BAA2B,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACnD,CAAC;CAgCF","sourcesContent":["import {\n fromHex,\n type Address,\n type Chain,\n type Hash,\n type Hex,\n type HttpTransport,\n type RpcTransactionRequest,\n} from \"viem\";\nimport {\n deepHexlify,\n resolveProperties,\n type SmartAccountProviderOpts,\n getChain,\n type UserOperationCallData,\n type BatchUserOperationCallData,\n type SendUserOperationResult,\n asyncPipe,\n noOpMiddleware,\n type UserOperationStruct,\n type BytesLike,\n SmartAccountProvider,\n type AccountMiddlewareFn,\n type UserOperationOverrides,\n} from \"@alchemy/aa-core\";\nimport {\n BUNDLER_URL,\n DEFAULT_SEND_TX_MAX_RETRIES,\n DEFAULT_SEND_TX_RETRY_INTERVAL_MS,\n ENTRYPOINT_ADDRESS,\n minPriorityFeePerBidDefaults,\n} from \"./constants.js\";\nimport { KernelSmartContractAccount, isKernelAccount } from \"./account.js\";\nimport { withZeroDevGasEstimator } from \"./middleware/gas-estimator.js\";\nimport { isValidRequest } from \"./utils/ERC4337-utils.js\";\nimport { InvalidOperation } from \"./errors.js\";\nimport { withZeroDevPaymasterAndData } from \"./middleware/paymaster.js\";\nimport { createZeroDevPublicErc4337Client } from \"./client/create-client.js\";\nimport type {\n PaymasterAndBundlerProviders,\n PaymasterConfig,\n PaymasterPolicy,\n} from \"./paymaster/types.js\";\n\nexport type ZeroDevProviderConfig = {\n projectId: string;\n chain: Chain | number;\n entryPointAddress?: Address;\n rpcUrl?: string;\n account?: KernelSmartContractAccount;\n bundlerProvider?: PaymasterAndBundlerProviders;\n opts?: SmartAccountProviderOpts & {\n sendTxMaxRetries?: number;\n sendTxRetryIntervalMs?: number;\n };\n};\n\nexport enum Operation {\n Call = 0,\n DelegateCall = 1,\n}\n\ntype UserOpDataOperationTypes<T> = T extends UserOperationCallData\n ? Operation.Call | Operation.DelegateCall\n : T extends BatchUserOperationCallData\n ? Operation.Call\n : never;\n\nexport class ZeroDevProvider extends SmartAccountProvider<HttpTransport> {\n protected projectId: string;\n protected sendTxMaxRetries: number;\n protected sendTxRetryIntervalMs: number;\n\n constructor({\n projectId,\n chain,\n entryPointAddress = ENTRYPOINT_ADDRESS,\n rpcUrl = BUNDLER_URL,\n account,\n bundlerProvider,\n opts,\n }: ZeroDevProviderConfig) {\n const _chain = typeof chain === \"number\" ? getChain(chain) : chain;\n const rpcClient = createZeroDevPublicErc4337Client({\n chain: _chain,\n rpcUrl,\n projectId,\n bundlerProvider,\n });\n\n super(rpcClient, entryPointAddress, _chain, account, {\n ...opts,\n txMaxRetries: opts?.txMaxRetries ?? 10,\n txRetryIntervalMs: opts?.txRetryIntervalMs ?? 10000,\n minPriorityFeePerBid:\n opts?.minPriorityFeePerBid ??\n minPriorityFeePerBidDefaults.get(_chain.id),\n });\n\n this.projectId = projectId;\n this.sendTxMaxRetries =\n opts?.sendTxMaxRetries ?? DEFAULT_SEND_TX_MAX_RETRIES;\n this.sendTxRetryIntervalMs =\n opts?.sendTxRetryIntervalMs ?? DEFAULT_SEND_TX_RETRY_INTERVAL_MS;\n\n withZeroDevGasEstimator(this);\n }\n\n getProjectId = (): string => this.projectId;\n\n sendTransaction = async (\n request: RpcTransactionRequest,\n operation: UserOpDataOperationTypes<UserOperationCallData> = Operation.Call\n ): Promise<Hash> => {\n if (!request.to) {\n throw new Error(\"transaction is missing to address\");\n }\n\n const overrides: UserOperationOverrides = {};\n if (request.maxFeePerGas) {\n overrides.maxFeePerGas = request.maxFeePerGas;\n }\n if (request.maxPriorityFeePerGas) {\n overrides.maxPriorityFeePerGas = request.maxPriorityFeePerGas;\n }\n\n const { hash } = await this.sendUserOperation(\n {\n target: request.to,\n data: request.data ?? \"0x\",\n value: request.value ? fromHex(request.value, \"bigint\") : 0n,\n },\n overrides,\n operation\n );\n\n return await this.waitForUserOperationTransaction(hash as Hash);\n };\n\n sendUserOperation = async <\n T extends UserOperationCallData | BatchUserOperationCallData\n >(\n data: T,\n overrides?: UserOperationOverrides,\n operation: UserOpDataOperationTypes<T> = Operation.Call as UserOpDataOperationTypes<T>\n ): Promise<SendUserOperationResult> => {\n if (!isKernelAccount(this.account)) {\n throw new Error(\"account not connected!\");\n }\n if (!this.account.validator) {\n throw new Error(\"validator not connected!\");\n }\n\n let callData: BytesLike = \"0x\";\n\n if (Array.isArray(data)) {\n if (operation === Operation.Call) {\n callData = await this.account.encodeBatchExecute(data);\n } else {\n throw InvalidOperation;\n }\n } else if (isKernelAccount(this.account)) {\n if (operation === Operation.DelegateCall) {\n callData = await this.account.encodeExecuteDelegate(\n data.target,\n data.value ?? 0n,\n data.data\n );\n } else if (operation === Operation.Call) {\n callData = await this.account.encodeExecute(\n data.target,\n data.value ?? 0n,\n data.data\n );\n } else {\n throw InvalidOperation;\n }\n } else {\n throw InvalidOperation;\n }\n\n const initCode = await this.account.getInitCode();\n let hash: string = \"\";\n let i = 0;\n const nonce = await this.account.getNonce();\n let uoStruct: UserOperationStruct;\n let request: UserOperationStruct;\n let maxFeePerGas, maxPriorityFeePerGas;\n do {\n uoStruct = await asyncPipe(\n this.dummyPaymasterDataMiddleware,\n this.feeDataGetter,\n this.paymasterDataMiddleware,\n this.gasEstimator,\n this.customMiddleware ?? noOpMiddleware,\n async (struct) => ({ ...struct, ...overrides })\n )({\n initCode,\n sender: this.getAddress(),\n nonce,\n callData,\n signature: await this.account\n .getValidator()\n .getDynamicDummySignature(await this.getAddress(), callData as Hex),\n maxFeePerGas,\n maxPriorityFeePerGas,\n } as UserOperationStruct);\n request = deepHexlify(await resolveProperties(uoStruct));\n\n if (!isValidRequest(request)) {\n // this pretty prints the uo\n throw new Error(\n `Request is missing parameters. All properties on UserOperationStruct must be set. uo: ${JSON.stringify(\n request,\n null,\n 2\n )}`\n );\n }\n\n await this.account.approvePlugin();\n\n request.signature = await this.account.validator.getSignature(request);\n try {\n hash = await this.rpcClient.sendUserOperation(\n request,\n this.entryPointAddress\n );\n } catch (error: any) {\n if (this.isReplacementOpError(error) && i++ < this.sendTxMaxRetries) {\n maxFeePerGas = (BigInt(request.maxFeePerGas) * 113n) / 100n;\n maxPriorityFeePerGas =\n (BigInt(request.maxPriorityFeePerGas) * 113n) / 100n;\n\n console.log(\n `After ${\n this.sendTxRetryIntervalMs / 60000\n } minutes, resending tx with Increased Gas fees: maxFeePerGas: ${maxFeePerGas}, maxPriorityFeePerGas: ${maxPriorityFeePerGas}`\n );\n await new Promise((resolve) =>\n setTimeout(resolve, this.sendTxRetryIntervalMs)\n );\n continue;\n }\n throw this.unwrapError(error);\n }\n } while (hash === \"\");\n\n return {\n hash,\n request,\n };\n };\n\n isReplacementOpError(errorIn: any): boolean {\n if (errorIn.cause != null) {\n const failedOpMessage: string | undefined = errorIn?.cause?.message;\n return (\n (failedOpMessage?.includes(\n \"replacement op must increase maxFeePerGas and MaxPriorityFeePerGas\"\n ) ||\n failedOpMessage?.match(/.*replacement.*underpriced.*/) !== null) ??\n false\n );\n }\n return false;\n }\n\n unwrapError(errorIn: any): Error {\n if (errorIn?.cause != null) {\n let paymasterInfo: string = \"\";\n let failedOpMessage: string | undefined = errorIn?.cause?.message;\n if (failedOpMessage?.includes(\"FailedOp\") === true) {\n // TODO: better error extraction methods will be needed\n const matched = failedOpMessage.match(/FailedOp\\((.*)\\)/);\n if (matched != null) {\n const split = matched[1].split(\",\");\n paymasterInfo = `(paymaster address: ${split[1]})`;\n failedOpMessage = split[2];\n }\n }\n const error = new Error(\n `The bundler has failed to include UserOperation in a batch: ${failedOpMessage} ${paymasterInfo}`\n );\n error.stack = errorIn.stack;\n return error;\n }\n return errorIn;\n }\n\n getAccount: () => KernelSmartContractAccount = () => {\n if (!isKernelAccount(this.account)) {\n throw new Error(\"account not connected!\");\n }\n return this.account;\n };\n\n dummyPaymasterDataMiddleware: AccountMiddlewareFn = async (\n struct: UserOperationStruct\n ): Promise<UserOperationStruct> => {\n struct.paymasterAndData = \"0x\";\n return struct;\n };\n\n withZeroDevPaymasterAndData(config: PaymasterConfig<PaymasterPolicy>) {\n if (!this.isConnected()) {\n throw new Error(\n \"ZeroDevProvider: account is not set, did you call `connect` first?\"\n );\n }\n\n return withZeroDevPaymasterAndData(this, config);\n }\n\n request: (args: { method: string; params?: any[] }) => Promise<any> = async (\n args\n ) => {\n const { method, params } = args;\n switch (method) {\n case \"eth_chainId\":\n return this.chain.id;\n case \"eth_sendTransaction\":\n const [tx] = params as [RpcTransactionRequest];\n return this.sendTransaction(tx);\n case \"eth_signTypedData_v4\":\n //@ts-expect-error\n return this.signTypedData(JSON.parse(params[1]));\n case \"personal_sign\":\n if (!this.account) {\n throw new Error(\"account not connected!\");\n }\n const [data, address] = params!;\n if (address.toLowerCase() !== (await this.getAddress()).toLowerCase()) {\n throw new Error(\n \"cannot sign for address that is not the current account\"\n );\n }\n // @ts-ignore\n return this.account.signMessageWith6492(data);\n default:\n // @ts-ignore\n return this.rpcClient.request(args);\n }\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerodev/sdk",
3
- "version": "4.1.2",
3
+ "version": "4.1.3",
4
4
  "description": "A collection of ERC-4337 compliant smart contract account interfaces",
5
5
  "author": "ZeroDev",
6
6
  "license": "MIT",
@@ -218,7 +218,7 @@ export class ZeroDevProvider extends SmartAccountProvider<HttpTransport> {
218
218
  );
219
219
  }
220
220
 
221
- this.account.approvePlugin();
221
+ await this.account.approvePlugin();
222
222
 
223
223
  request.signature = await this.account.validator.getSignature(request);
224
224
  try {