@account-kit/infra 4.0.0-alpha.0 → 4.0.0-alpha.10

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 (123) hide show
  1. package/dist/cjs/actions/simulateUserOperationChanges.js.map +1 -1
  2. package/dist/cjs/chains.d.ts +25 -0
  3. package/dist/cjs/chains.js +200 -1
  4. package/dist/cjs/chains.js.map +1 -1
  5. package/dist/cjs/client/decorators/alchemyEnhancedApis.d.ts +5393 -1
  6. package/dist/cjs/client/decorators/alchemyEnhancedApis.js.map +1 -1
  7. package/dist/cjs/client/decorators/smartAccount.js.map +1 -1
  8. package/dist/cjs/client/internal/smartAccountClientFromRpc.d.ts +2 -2
  9. package/dist/cjs/client/internal/smartAccountClientFromRpc.js +3 -14
  10. package/dist/cjs/client/internal/smartAccountClientFromRpc.js.map +1 -1
  11. package/dist/cjs/client/isAlchemySmartAccountClient.d.ts +1173 -1317
  12. package/dist/cjs/client/isAlchemySmartAccountClient.js.map +1 -1
  13. package/dist/cjs/client/rpcClient.js.map +1 -1
  14. package/dist/cjs/client/smartAccountClient.d.ts +2 -3
  15. package/dist/cjs/client/smartAccountClient.js +2 -2
  16. package/dist/cjs/client/smartAccountClient.js.map +1 -1
  17. package/dist/cjs/client/types.d.ts +1 -48
  18. package/dist/cjs/client/types.js.map +1 -1
  19. package/dist/cjs/defaults.js +7 -7
  20. package/dist/cjs/defaults.js.map +1 -1
  21. package/dist/cjs/gas-manager.js +16 -16
  22. package/dist/cjs/gas-manager.js.map +1 -1
  23. package/dist/cjs/index.d.ts +2 -1
  24. package/dist/cjs/index.js +22 -1
  25. package/dist/cjs/index.js.map +1 -1
  26. package/dist/cjs/middleware/feeEstimator.js.map +1 -1
  27. package/dist/cjs/middleware/gasManager.d.ts +2 -29
  28. package/dist/cjs/middleware/gasManager.js +4 -141
  29. package/dist/cjs/middleware/gasManager.js.map +1 -1
  30. package/dist/cjs/middleware/userOperationSimulator.d.ts +2 -2
  31. package/dist/cjs/middleware/userOperationSimulator.js.map +1 -1
  32. package/dist/cjs/schema.js +3 -10
  33. package/dist/cjs/schema.js.map +1 -1
  34. package/dist/cjs/version.d.ts +1 -1
  35. package/dist/cjs/version.js +1 -1
  36. package/dist/cjs/version.js.map +1 -1
  37. package/dist/esm/actions/simulateUserOperationChanges.js.map +1 -1
  38. package/dist/esm/chains.d.ts +25 -0
  39. package/dist/esm/chains.js +199 -0
  40. package/dist/esm/chains.js.map +1 -1
  41. package/dist/esm/client/decorators/alchemyEnhancedApis.d.ts +5393 -1
  42. package/dist/esm/client/decorators/alchemyEnhancedApis.js.map +1 -1
  43. package/dist/esm/client/decorators/smartAccount.js.map +1 -1
  44. package/dist/esm/client/internal/smartAccountClientFromRpc.d.ts +2 -2
  45. package/dist/esm/client/internal/smartAccountClientFromRpc.js +3 -14
  46. package/dist/esm/client/internal/smartAccountClientFromRpc.js.map +1 -1
  47. package/dist/esm/client/isAlchemySmartAccountClient.d.ts +1173 -1317
  48. package/dist/esm/client/isAlchemySmartAccountClient.js.map +1 -1
  49. package/dist/esm/client/rpcClient.js.map +1 -1
  50. package/dist/esm/client/smartAccountClient.d.ts +2 -3
  51. package/dist/esm/client/smartAccountClient.js +2 -3
  52. package/dist/esm/client/smartAccountClient.js.map +1 -1
  53. package/dist/esm/client/types.d.ts +1 -48
  54. package/dist/esm/client/types.js.map +1 -1
  55. package/dist/esm/defaults.js +2 -1
  56. package/dist/esm/defaults.js.map +1 -1
  57. package/dist/esm/gas-manager.js +1 -1
  58. package/dist/esm/gas-manager.js.map +1 -1
  59. package/dist/esm/index.d.ts +2 -1
  60. package/dist/esm/index.js +1 -1
  61. package/dist/esm/index.js.map +1 -1
  62. package/dist/esm/middleware/feeEstimator.js.map +1 -1
  63. package/dist/esm/middleware/gasManager.d.ts +2 -29
  64. package/dist/esm/middleware/gasManager.js +5 -142
  65. package/dist/esm/middleware/gasManager.js.map +1 -1
  66. package/dist/esm/middleware/userOperationSimulator.d.ts +2 -2
  67. package/dist/esm/middleware/userOperationSimulator.js.map +1 -1
  68. package/dist/esm/schema.js +4 -11
  69. package/dist/esm/schema.js.map +1 -1
  70. package/dist/esm/version.d.ts +1 -1
  71. package/dist/esm/version.js +1 -1
  72. package/dist/esm/version.js.map +1 -1
  73. package/dist/types/actions/simulateUserOperationChanges.d.ts +17 -0
  74. package/dist/types/actions/simulateUserOperationChanges.d.ts.map +1 -1
  75. package/dist/types/chains.d.ts +44 -0
  76. package/dist/types/chains.d.ts.map +1 -1
  77. package/dist/types/client/decorators/alchemyEnhancedApis.d.ts +5410 -1
  78. package/dist/types/client/decorators/alchemyEnhancedApis.d.ts.map +1 -1
  79. package/dist/types/client/decorators/smartAccount.d.ts +15 -0
  80. package/dist/types/client/decorators/smartAccount.d.ts.map +1 -1
  81. package/dist/types/client/internal/smartAccountClientFromRpc.d.ts +4 -4
  82. package/dist/types/client/internal/smartAccountClientFromRpc.d.ts.map +1 -1
  83. package/dist/types/client/isAlchemySmartAccountClient.d.ts +1188 -1317
  84. package/dist/types/client/isAlchemySmartAccountClient.d.ts.map +1 -1
  85. package/dist/types/client/rpcClient.d.ts +22 -0
  86. package/dist/types/client/rpcClient.d.ts.map +1 -1
  87. package/dist/types/client/smartAccountClient.d.ts +2 -3
  88. package/dist/types/client/smartAccountClient.d.ts.map +1 -1
  89. package/dist/types/client/types.d.ts +1 -48
  90. package/dist/types/client/types.d.ts.map +1 -1
  91. package/dist/types/defaults.d.ts +14 -0
  92. package/dist/types/defaults.d.ts.map +1 -1
  93. package/dist/types/gas-manager.d.ts +13 -0
  94. package/dist/types/gas-manager.d.ts.map +1 -1
  95. package/dist/types/index.d.ts +2 -1
  96. package/dist/types/index.d.ts.map +1 -1
  97. package/dist/types/middleware/feeEstimator.d.ts +19 -0
  98. package/dist/types/middleware/feeEstimator.d.ts.map +1 -1
  99. package/dist/types/middleware/gasManager.d.ts +15 -87
  100. package/dist/types/middleware/gasManager.d.ts.map +1 -1
  101. package/dist/types/middleware/userOperationSimulator.d.ts +21 -2
  102. package/dist/types/middleware/userOperationSimulator.d.ts.map +1 -1
  103. package/dist/types/schema.d.ts.map +1 -1
  104. package/dist/types/version.d.ts +1 -1
  105. package/dist/types/version.d.ts.map +1 -1
  106. package/package.json +5 -4
  107. package/src/actions/simulateUserOperationChanges.ts +17 -0
  108. package/src/chains.ts +255 -0
  109. package/src/client/decorators/alchemyEnhancedApis.ts +50 -33
  110. package/src/client/decorators/smartAccount.ts +15 -0
  111. package/src/client/internal/smartAccountClientFromRpc.ts +36 -32
  112. package/src/client/isAlchemySmartAccountClient.ts +15 -0
  113. package/src/client/rpcClient.ts +22 -0
  114. package/src/client/smartAccountClient.ts +21 -5
  115. package/src/client/types.ts +1 -54
  116. package/src/defaults.ts +17 -3
  117. package/src/gas-manager.ts +17 -4
  118. package/src/index.ts +25 -1
  119. package/src/middleware/feeEstimator.ts +19 -0
  120. package/src/middleware/gasManager.ts +21 -424
  121. package/src/middleware/userOperationSimulator.ts +25 -2
  122. package/src/schema.ts +3 -11
  123. package/src/version.ts +1 -1
package/src/defaults.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { type UserOperationFeeOptions } from "@aa-sdk/core";
2
+ import type { Chain } from "viem";
1
3
  import {
2
4
  arbitrum,
3
5
  arbitrumGoerli,
@@ -5,10 +7,22 @@ import {
5
7
  optimism,
6
8
  optimismGoerli,
7
9
  optimismSepolia,
8
- type UserOperationFeeOptions,
9
- } from "@aa-sdk/core";
10
- import type { Chain } from "viem";
10
+ } from "./chains.js";
11
11
 
12
+ /**
13
+ * Retrieves the default user operation fee options for a given chain. Adjusts fees for specific chains like Arbitrum and Optimism.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { getDefaultUserOperationFeeOptions } from "@account-kit/infra";
18
+ * import { arbitrum } from "@account-kit/infra";
19
+ *
20
+ * const feeOpts = getDefaultUserOperationFeeOptions(arbitrum);
21
+ * ```
22
+ *
23
+ * @param {Chain} chain The blockchain chain for which to get the fee options
24
+ * @returns {UserOperationFeeOptions} An object containing the default fee options for user operations on the specified chain
25
+ */
12
26
  export const getDefaultUserOperationFeeOptions = (
13
27
  chain: Chain
14
28
  ): UserOperationFeeOptions => {
@@ -4,18 +4,18 @@ import {
4
4
  arbitrumSepolia,
5
5
  base,
6
6
  baseSepolia,
7
+ fraxtal,
8
+ fraxtalSepolia,
7
9
  mainnet,
8
- sepolia,
9
10
  optimism,
10
11
  optimismSepolia,
11
12
  polygon,
12
13
  polygonAmoy,
13
14
  polygonMumbai,
14
- fraxtal,
15
- fraxtalSepolia,
15
+ sepolia,
16
16
  zora,
17
17
  zoraSepolia,
18
- } from "@aa-sdk/core";
18
+ } from "./chains.js";
19
19
 
20
20
  export const AlchemyPaymasterAddressV3 =
21
21
  "0x4f84a207A80c39E9e8BaE717c1F25bA7AD1fB08F";
@@ -26,6 +26,19 @@ export const ArbSepoliaPaymasterAddress =
26
26
  export const AlchemyPaymasterAddressV1 =
27
27
  "0xc03aac639bb21233e0139381970328db8bceeb67";
28
28
 
29
+ /**
30
+ * Retrieves the Alchemy paymaster address for the given chain. Returns different addresses based on the chain ID.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { sepolia, getAlchemyPaymasterAddress } from "@account-kit/infra";
35
+ *
36
+ * const paymasterAddress = getAlchemyPaymasterAddress(sepolia);
37
+ * ```
38
+ *
39
+ * @param {Chain} chain The chain for which the paymaster address is required
40
+ * @returns {Address} The Alchemy paymaster address corresponding to the specified chain
41
+ */
29
42
  export const getAlchemyPaymasterAddress = (chain: Chain): Address => {
30
43
  switch (chain.id) {
31
44
  case polygonAmoy.id:
package/src/index.ts CHANGED
@@ -1,7 +1,31 @@
1
1
  export type * from "./actions/simulateUserOperationChanges.js";
2
2
  export { simulateUserOperationChanges } from "./actions/simulateUserOperationChanges.js";
3
3
  export type * from "./actions/types.js";
4
- export { defineAlchemyChain } from "./chains.js";
4
+ export type * from "./chains.js";
5
+ export {
6
+ arbitrum,
7
+ arbitrumGoerli,
8
+ arbitrumSepolia,
9
+ base,
10
+ baseGoerli,
11
+ baseSepolia,
12
+ defineAlchemyChain,
13
+ fraxtal,
14
+ fraxtalSepolia,
15
+ goerli,
16
+ mainnet,
17
+ optimism,
18
+ optimismGoerli,
19
+ optimismSepolia,
20
+ polygon,
21
+ polygonAmoy,
22
+ polygonMumbai,
23
+ sepolia,
24
+ zora,
25
+ zoraSepolia,
26
+ worldChain,
27
+ worldChainSepolia,
28
+ } from "./chains.js";
5
29
  export type * from "./client/decorators/alchemyEnhancedApis.js";
6
30
  export { alchemyEnhancedApiActions } from "./client/decorators/alchemyEnhancedApis.js";
7
31
  export type * from "./client/decorators/smartAccount.js";
@@ -2,6 +2,25 @@ import type { ClientMiddlewareFn } from "@aa-sdk/core";
2
2
  import { applyUserOpOverrideOrFeeOption } from "@aa-sdk/core";
3
3
  import type { ClientWithAlchemyMethods } from "../client/types";
4
4
 
5
+ /**
6
+ * Function that estimates the transaction fees using Alchemy methods for a given client.
7
+ * It fetches the latest block and estimates the max priority fee per gas, applying any overrides or fee options provided.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { alchemyFeeEstimator, createAlchemyPublicRpcClient } from "@account-kit/infra";
12
+ * import { createSmartAccountClient } from "@aa-sdk/core";
13
+ *
14
+ * const bundlerClient = createAlchemyPublicRpcClient(...);
15
+ * const client = createSmartAccountClient({
16
+ * feeEstimator: alchemyFeeEstimator(bundlerClient),
17
+ * ...otherParams
18
+ * });
19
+ * ```
20
+ *
21
+ * @param {ClientWithAlchemyMethods} client The client with Alchemy methods
22
+ * @returns {ClientMiddlewareFn} A middleware function that takes a transaction structure and fee options, and returns the augmented structure with estimated fees
23
+ */
5
24
  export const alchemyFeeEstimator: <C extends ClientWithAlchemyMethods>(
6
25
  client: C
7
26
  ) => ClientMiddlewareFn =
@@ -1,432 +1,29 @@
1
- import type {
2
- Address,
3
- ClientMiddlewareConfig,
4
- ClientMiddlewareFn,
5
- EntryPointVersion,
6
- Multiplier,
7
- UserOperationFeeOptions,
8
- UserOperationOverrides,
9
- UserOperationRequest,
10
- } from "@aa-sdk/core";
11
- import {
12
- bypassPaymasterAndDataEmptyHex,
13
- deepHexlify,
14
- defaultGasEstimator,
15
- filterUndefined,
16
- isBigNumberish,
17
- isMultiplier,
18
- resolveProperties,
19
- } from "@aa-sdk/core";
20
- import { concat, fromHex, isHex, type Hex } from "viem";
21
- import type { ClientWithAlchemyMethods } from "../client/types";
22
- import { getAlchemyPaymasterAddress } from "../gas-manager.js";
23
- import { alchemyFeeEstimator } from "./feeEstimator.js";
1
+ import type { ClientMiddlewareConfig } from "@aa-sdk/core";
2
+ import { erc7677Middleware } from "@aa-sdk/core";
24
3
 
25
4
  /**
26
- * overrides value for [`alchemy_requestGasAndPaymasterData`](https://docs.alchemy.com/reference/alchemy-requestgasandpaymasteranddata)
5
+ * Paymaster middleware factory that uses Alchemy's Gas Manager for sponsoring transactions.
27
6
  *
28
- * @template {EntryPointVersion} TEntryPointVersion entry point version type
29
- */
30
- export type RequestGasAndPaymasterAndDataOverrides<
31
- TEntryPointVersion extends EntryPointVersion = EntryPointVersion
32
- > = Partial<
33
- {
34
- maxFeePerGas:
35
- | UserOperationRequest<TEntryPointVersion>["maxFeePerGas"]
36
- | Multiplier;
37
- maxPriorityFeePerGas:
38
- | UserOperationRequest<TEntryPointVersion>["maxPriorityFeePerGas"]
39
- | Multiplier;
40
- callGasLimit:
41
- | UserOperationRequest<TEntryPointVersion>["callGasLimit"]
42
- | Multiplier;
43
- verificationGasLimit:
44
- | UserOperationRequest<TEntryPointVersion>["verificationGasLimit"]
45
- | Multiplier;
46
- preVerificationGas:
47
- | UserOperationRequest<TEntryPointVersion>["preVerificationGas"]
48
- | Multiplier;
49
- } & TEntryPointVersion extends "0.7.0"
50
- ? {
51
- paymasterVerificationGasLimit:
52
- | UserOperationRequest<"0.7.0">["paymasterVerificationGasLimit"]
53
- | Multiplier;
54
- paymasterPostOpGasLimit:
55
- | UserOperationRequest<"0.7.0">["paymasterPostOpGasLimit"]
56
- | Multiplier;
57
- }
58
- : {}
59
- >;
60
-
61
- /**
62
- * [`alchemy-requestpaymasteranddata`](https://docs.alchemy.com/reference/alchemy-requestpaymasteranddata)
63
- * response type
7
+ * @example
8
+ * ```ts
64
9
  *
65
- * @template {EntryPointVersion} TEntryPointVersion entry point version type
66
- */
67
- export type RequestPaymasterAndDataResponse<
68
- TEntryPointVersion extends EntryPointVersion = EntryPointVersion
69
- > = TEntryPointVersion extends "0.6.0"
70
- ? {
71
- paymasterAndData: UserOperationRequest<"0.6.0">["paymasterAndData"];
72
- }
73
- : TEntryPointVersion extends "0.7.0"
74
- ? Pick<UserOperationRequest<"0.7.0">, "paymaster" | "paymasterData">
75
- : {};
76
-
77
- /**
78
- * [`alchemy_requestGasAndPaymasterData`](https://docs.alchemy.com/reference/alchemy-requestgasandpaymasteranddata)
79
- * response type
10
+ * import { sepolia } from "@account-kit/infra";
11
+ * import { http } from "viem";
80
12
  *
81
- * @template {EntryPointVersion} TEntryPointVersion entry point version type
82
- */
83
- export type RequestGasAndPaymasterAndDataResponse<
84
- TEntryPointVersion extends EntryPointVersion = EntryPointVersion
85
- > = Pick<
86
- UserOperationRequest,
87
- | "callGasLimit"
88
- | "preVerificationGas"
89
- | "verificationGasLimit"
90
- | "maxFeePerGas"
91
- | "maxPriorityFeePerGas"
92
- > &
93
- RequestPaymasterAndDataResponse<TEntryPointVersion>;
94
-
95
- /**
96
- * Alchemy gas manager configuration with gas policy id and optional gas estimation options
97
- *
98
- * To create a Gas Manager Policy, go to the [gas manager](https://dashboard.alchemy.com/gas-manager?a=embedded-accounts-get-started)
99
- * page of the Alchemy dashboard and click the “Create new policy” button.
100
- */
101
- export interface AlchemyGasManagerConfig {
102
- /**
103
- * the policy id of the gas manager you want to use.
104
- *
105
- */
106
- policyId: string;
107
- /**
108
- * optional option configurable for the gas estimation portion of the Alchemy gas manager
109
- *
110
- */
111
- gasEstimationOptions?: AlchemyGasEstimationOptions;
112
- /**
113
- * paymaster address to use for the gas estimation.
114
- * If not provided, the default paymaster address for the chain will be used.
115
- *
116
- */
117
- paymasterAddress?: Address;
118
- /**
119
- * dummy paymaster data to use for the gas estimation.
120
- *
121
- */
122
- dummyData?: Hex;
123
- }
124
-
125
- /**
126
- * Alchemy gas manager configuration option configurable for the gas estimation portion of the Alchemy gas manager
13
+ * const client = createSmartAccountClient({
14
+ * http("rpc-url"),
15
+ * sepolia,
16
+ * alchemyErc7677Middleware("policyId")
17
+ * );
18
+ * ```
127
19
  *
20
+ * @param {string} policyId the policyId for Alchemy's gas manager
21
+ * @returns {Pick<ClientMiddlewareConfig, "dummyPaymasterAndData" | "paymasterAndData">} partial client middleware configuration containing `dummyPaymasterAndData` and `paymasterAndData`
128
22
  */
129
- export interface AlchemyGasEstimationOptions {
130
- /**
131
- * disable gas estimation and fallback to the default gas estimation.
132
- *
133
- */
134
- disableGasEstimation: boolean;
135
- /**
136
- * optional fallback gas estimator to use when gas estimation is disabled.
137
- *
138
- */
139
- fallbackGasEstimator?: ClientMiddlewareFn;
140
- /**
141
- * optional fallback fee estimator to use when gas estimation is disabled.
142
- *
143
- */
144
- fallbackFeeDataGetter?: ClientMiddlewareFn;
23
+ export function alchemyGasManagerMiddleware(
24
+ policyId: string
25
+ ): Pick<ClientMiddlewareConfig, "dummyPaymasterAndData" | "paymasterAndData"> {
26
+ return erc7677Middleware<{ policyId: string }>({
27
+ context: { policyId: policyId },
28
+ });
145
29
  }
146
-
147
- /**
148
- * dummy paymaster and data middleware for the alchemy gas manager
149
- *
150
- * @template {ClientWithAlchemyMethods} C
151
- * @param client client with alchemy methods
152
- * @param config alchemy gas manager configuration
153
- * @returns the dummyPaymasterAndData middleware for Alchemy gas manager
154
- */
155
- const dummyPaymasterAndData =
156
- <C extends ClientWithAlchemyMethods>(
157
- client: C,
158
- config: AlchemyGasManagerConfig
159
- ) =>
160
- () => {
161
- const paymaster =
162
- config.paymasterAddress ?? getAlchemyPaymasterAddress(client.chain);
163
- const paymasterData =
164
- config.dummyData ??
165
- "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
166
-
167
- return concat([paymaster, paymasterData]); // or you can also return { paymaster, paymasterData }
168
- };
169
-
170
- /**
171
- * Alchemy gas manager middleware used as the paymaster middleware overrides param to the client middleware config
172
- *
173
- * @template {ClientWithAlchemyMethods} C
174
- * @param client client with alchemy methods
175
- * @param config alchemy gas manager configuration
176
- * @returns the gas estimator, fee estimator, and paymasterAndData middleware for Alchemy gas manager
177
- */
178
- export function alchemyGasManagerMiddleware<C extends ClientWithAlchemyMethods>(
179
- client: C,
180
- config: AlchemyGasManagerConfig
181
- ): Pick<
182
- ClientMiddlewareConfig,
183
- "paymasterAndData" | "feeEstimator" | "gasEstimator"
184
- > {
185
- const gasEstimationOptions = config.gasEstimationOptions;
186
- const disableGasEstimation =
187
- gasEstimationOptions?.disableGasEstimation ?? false;
188
- const fallbackFeeDataGetter =
189
- gasEstimationOptions?.fallbackFeeDataGetter ?? alchemyFeeEstimator(client);
190
- const fallbackGasEstimator =
191
- gasEstimationOptions?.fallbackGasEstimator ??
192
- defaultGasEstimator<C>(client);
193
-
194
- return {
195
- gasEstimator: disableGasEstimation
196
- ? fallbackGasEstimator
197
- : async (struct, { overrides, account, feeOptions }) => {
198
- // if user is bypassing paymaster to fallback to having the account to pay the gas (one-off override),
199
- // we cannot delegate gas estimation to the bundler because paymaster middleware will not be called
200
- if (bypassPaymasterAndDataEmptyHex(overrides)) {
201
- return {
202
- ...struct,
203
- ...(await fallbackGasEstimator(struct, {
204
- overrides,
205
- account,
206
- feeOptions,
207
- client,
208
- })),
209
- };
210
- }
211
-
212
- // essentially noop, because the gas estimation will happen in the backend
213
- return struct;
214
- },
215
- feeEstimator: disableGasEstimation
216
- ? fallbackFeeDataGetter
217
- : async (struct, { overrides, account, feeOptions }) => {
218
- let maxFeePerGas = await struct.maxFeePerGas;
219
- let maxPriorityFeePerGas = await struct.maxPriorityFeePerGas;
220
-
221
- // if user is bypassing paymaster to fallback to having the account to pay the gas (one-off override),
222
- // we cannot delegate gas estimation to the bundler because paymaster middleware will not be called
223
- if (bypassPaymasterAndDataEmptyHex(overrides)) {
224
- const result = await fallbackFeeDataGetter(struct, {
225
- overrides,
226
- feeOptions,
227
- account,
228
- client,
229
- });
230
- maxFeePerGas = (await result.maxFeePerGas) ?? maxFeePerGas;
231
- maxPriorityFeePerGas =
232
- (await result.maxPriorityFeePerGas) ?? maxPriorityFeePerGas;
233
- }
234
-
235
- return {
236
- ...struct,
237
- maxFeePerGas,
238
- maxPriorityFeePerGas,
239
- };
240
- },
241
- paymasterAndData: disableGasEstimation
242
- ? requestPaymasterAndData(client, config)
243
- : requestGasAndPaymasterData(client, config),
244
- };
245
- }
246
-
247
- /**
248
- * Utility function to override a field in the user operation request with the overrides or fee options
249
- *
250
- * @template {EntryPointVersion} TEntryPointVersion
251
- * @param field the field to override
252
- * @param overrides the overrides object
253
- * @param feeOptions the fee options object from the client
254
- * @param userOperation the user operation request
255
- * @returns the overridden field value
256
- */
257
- const overrideField = <
258
- TEntryPointVersion extends EntryPointVersion = EntryPointVersion
259
- >(
260
- field: keyof UserOperationFeeOptions<TEntryPointVersion>,
261
- overrides: UserOperationOverrides<TEntryPointVersion> | undefined,
262
- feeOptions: UserOperationFeeOptions<TEntryPointVersion> | undefined,
263
- userOperation: UserOperationRequest<TEntryPointVersion>
264
- ): Hex | Multiplier | undefined => {
265
- let _field = field as keyof UserOperationOverrides<TEntryPointVersion>;
266
-
267
- if (overrides?.[_field] != null) {
268
- // one-off absolute override
269
- if (isBigNumberish(overrides[_field])) {
270
- return deepHexlify(overrides[_field]);
271
- }
272
- // one-off multiplier overrides
273
- else {
274
- return {
275
- multiplier: Number((overrides[_field] as Multiplier).multiplier),
276
- };
277
- }
278
- }
279
-
280
- // provider level fee options with multiplier
281
- if (isMultiplier(feeOptions?.[field])) {
282
- return {
283
- multiplier: Number((feeOptions![field] as Multiplier).multiplier),
284
- };
285
- }
286
-
287
- const userOpField =
288
- userOperation[field as keyof UserOperationRequest<TEntryPointVersion>];
289
- if (isHex(userOpField) && fromHex(userOpField as Hex, "bigint") > 0n) {
290
- return userOpField;
291
- }
292
- return undefined;
293
- };
294
-
295
- /**
296
- * Alchemy gas manager middleware function that returns the paymaster middleware for the client middleware config
297
- * that calls the [`alchemy_requestGasAndPaymasterAndData`](https://docs.alchemy.com/reference/alchemy-requestgasandpaymasteranddata)
298
- *
299
- * @template {ClientWithAlchemyMethods} C
300
- * @param client client with alchemy methods
301
- * @param config alchemy gas manager configuration
302
- * @returns the paymasterAndData middleware for Alchemy gas manager
303
- */
304
- function requestGasAndPaymasterData<C extends ClientWithAlchemyMethods>(
305
- client: C,
306
- config: AlchemyGasManagerConfig
307
- ): ClientMiddlewareConfig["paymasterAndData"] {
308
- return {
309
- dummyPaymasterAndData: dummyPaymasterAndData(client, config),
310
-
311
- paymasterAndData: async (
312
- struct,
313
- { overrides: overrides_, feeOptions, account }
314
- ) => {
315
- const userOperation: UserOperationRequest = deepHexlify(
316
- await resolveProperties(struct)
317
- );
318
-
319
- const overrides: RequestGasAndPaymasterAndDataOverrides = filterUndefined(
320
- {
321
- maxFeePerGas: overrideField(
322
- "maxFeePerGas",
323
- overrides_ as UserOperationOverrides,
324
- feeOptions,
325
- userOperation
326
- ),
327
- maxPriorityFeePerGas: overrideField(
328
- "maxPriorityFeePerGas",
329
- overrides_ as UserOperationOverrides,
330
- feeOptions,
331
- userOperation
332
- ),
333
- callGasLimit: overrideField(
334
- "callGasLimit",
335
- overrides_ as UserOperationOverrides,
336
- feeOptions,
337
- userOperation
338
- ),
339
- verificationGasLimit: overrideField(
340
- "verificationGasLimit",
341
- overrides_ as UserOperationOverrides,
342
- feeOptions,
343
- userOperation
344
- ),
345
- preVerificationGas: overrideField(
346
- "preVerificationGas",
347
- overrides_ as UserOperationOverrides,
348
- feeOptions,
349
- userOperation
350
- ),
351
- }
352
- );
353
-
354
- if (account.getEntryPoint().version === "0.7.0") {
355
- const paymasterVerificationGasLimit = overrideField<"0.7.0">(
356
- "paymasterVerificationGasLimit",
357
- overrides_ as UserOperationOverrides<"0.7.0">,
358
- feeOptions,
359
- userOperation
360
- );
361
- if (paymasterVerificationGasLimit != null) {
362
- (
363
- overrides as RequestGasAndPaymasterAndDataOverrides<"0.7.0">
364
- ).paymasterVerificationGasLimit = paymasterVerificationGasLimit;
365
- }
366
-
367
- const paymasterPostOpGasLimit = overrideField<"0.7.0">(
368
- "paymasterPostOpGasLimit",
369
- overrides_ as UserOperationOverrides<"0.7.0">,
370
- feeOptions,
371
- userOperation
372
- );
373
- if (paymasterPostOpGasLimit != null) {
374
- (
375
- overrides as RequestGasAndPaymasterAndDataOverrides<"0.7.0">
376
- ).paymasterPostOpGasLimit = paymasterPostOpGasLimit;
377
- }
378
- }
379
-
380
- const result = await client.request({
381
- method: "alchemy_requestGasAndPaymasterAndData",
382
- params: [
383
- {
384
- policyId: config.policyId,
385
- entryPoint: account.getEntryPoint().address,
386
- userOperation,
387
- dummySignature: userOperation.signature,
388
- overrides,
389
- },
390
- ],
391
- });
392
-
393
- return {
394
- ...struct,
395
- ...result,
396
- };
397
- },
398
- };
399
- }
400
-
401
- /**
402
- * Alchemy gas manager middleware function that returns the paymaster middleware for the client middleware config
403
- * that calls the [`alchemy_requestPaymasterAndData`](https://docs.alchemy.com/reference/alchemy-requestpaymasteranddata)
404
- * with gas estimation disabled.
405
- *
406
- * @param client client with alchemy methods
407
- * @param config alchemy gas manager configuration
408
- * @returns the paymasterAndData middleware for Alchemy gas manager with gas estimation disabled
409
- */
410
- const requestPaymasterAndData: <C extends ClientWithAlchemyMethods>(
411
- client: C,
412
- config: AlchemyGasManagerConfig
413
- ) => ClientMiddlewareConfig["paymasterAndData"] = (client, config) => ({
414
- dummyPaymasterAndData: dummyPaymasterAndData(client, config),
415
- paymasterAndData: async (struct, { account }) => {
416
- const result = await client.request({
417
- method: "alchemy_requestPaymasterAndData",
418
- params: [
419
- {
420
- policyId: config.policyId,
421
- entryPoint: account.getEntryPoint().address,
422
- userOperation: deepHexlify(await resolveProperties(struct)),
423
- },
424
- ],
425
- });
426
-
427
- return {
428
- ...struct,
429
- ...result,
430
- };
431
- },
432
- });
@@ -2,12 +2,35 @@ import {
2
2
  deepHexlify,
3
3
  resolveProperties,
4
4
  type ClientMiddlewareFn,
5
+ type UserOperationContext,
5
6
  } from "@aa-sdk/core";
6
7
  import type { ClientWithAlchemyMethods } from "../client/types";
7
8
 
9
+ /**
10
+ * A middleware function to be used during simulation of user operations which leverages Alchemy's RPC uo simulation method.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { alchemyUserOperationSimulator, createAlchemyPublicRpcClient } from "@account-kit/infra";
15
+ * import { createSmartAccountClient } from "@aa-sdk/core";
16
+ *
17
+ * const bundlerClient = createAlchemyPublicRpcClient(...);
18
+ * const client = createSmartAccountClient({
19
+ * userOperationSimulator: alchemyUserOperationSimulator(bundlerClient),
20
+ * ...otherParams
21
+ * });
22
+ * ```
23
+ *
24
+ * @template C The client object with Alchemy methods
25
+ * @param {C} client The client object with Alchemy methods
26
+ * @returns {ClientMiddlewareFn} A middleware function to simulate and process user operations
27
+ */
8
28
  export function alchemyUserOperationSimulator<
9
- C extends ClientWithAlchemyMethods
10
- >(client: C): ClientMiddlewareFn {
29
+ C extends ClientWithAlchemyMethods,
30
+ TContext extends UserOperationContext | undefined =
31
+ | UserOperationContext
32
+ | undefined
33
+ >(client: C): ClientMiddlewareFn<TContext> {
11
34
  return async (struct, { account }) => {
12
35
  const uoSimResult = await client.request({
13
36
  method: "alchemy_simulateUserOperationAssetChanges",
package/src/schema.ts CHANGED
@@ -2,24 +2,16 @@ import {
2
2
  ChainSchema,
3
3
  ConnectionConfigSchema,
4
4
  SmartAccountClientOptsSchema,
5
- getChain,
6
5
  } from "@aa-sdk/core";
7
6
  import { Alchemy } from "alchemy-sdk";
8
7
  import type { Chain } from "viem";
9
8
  import z from "zod";
10
9
 
11
10
  export const AlchemyChainSchema = z.custom<Chain>((chain) => {
12
- const _chain = ChainSchema.parse(chain);
11
+ const chain_ = ChainSchema.parse(chain);
13
12
 
14
- let chainObject;
15
- try {
16
- chainObject = getChain(_chain.id);
17
- } catch {
18
- return false;
19
- }
20
-
21
- return chainObject.rpcUrls.alchemy != null;
22
- }, "chain must include an alchemy rpc url. See `createAlchemyChain` or use the `AlchemyChainMap` exported from `@aa-sdk/core`");
13
+ return chain_.rpcUrls.alchemy != null;
14
+ }, "chain must include an alchemy rpc url. See `createAlchemyChain` or import a chain from `@account-kit/infra`.");
23
15
 
24
16
  export const AlchemyProviderConfigSchema = ConnectionConfigSchema.and(
25
17
  z.object({
package/src/version.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  // This file is autogenerated by inject-version.ts. Any changes will be
2
2
  // overwritten on commit!
3
- export const VERSION = "4.0.0-alpha.0";
3
+ export const VERSION = "4.0.0-alpha.10";