@alchemy/aa-infra 0.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Alchemy Insights, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,8 @@
1
+ import { BaseError } from "@alchemy/common";
2
+ /**
3
+ * Error thrown when an invalid hex value is encountered during fee estimation.
4
+ */
5
+ export declare class InvalidHexValueError extends BaseError {
6
+ name: string;
7
+ constructor(value: unknown);
8
+ }
@@ -0,0 +1,16 @@
1
+ import { BaseError } from "@alchemy/common";
2
+ /**
3
+ * Error thrown when an invalid hex value is encountered during fee estimation.
4
+ */
5
+ export class InvalidHexValueError extends BaseError {
6
+ constructor(value) {
7
+ super(`Invalid hex value: ${value}`);
8
+ Object.defineProperty(this, "name", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: "InvalidHexValueError"
13
+ });
14
+ }
15
+ }
16
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,SAAS;IAGjD,YAAY,KAAc;QACxB,KAAK,CAAC,sBAAsB,KAAK,EAAE,CAAC,CAAC;QAH9B;;;;mBAAO,sBAAsB;WAAC;IAIvC,CAAC;CACF","sourcesContent":["import { BaseError } from \"@alchemy/common\";\n\n/**\n * Error thrown when an invalid hex value is encountered during fee estimation.\n */\nexport class InvalidHexValueError extends BaseError {\n override name = \"InvalidHexValueError\";\n\n constructor(value: unknown) {\n super(`Invalid hex value: ${value}`);\n }\n}\n"]}
@@ -0,0 +1,37 @@
1
+ import { type Client, type Transport, type Chain, type Account } from "viem";
2
+ import type { UserOperationRequest, SmartAccount } from "viem/account-abstraction";
3
+ import type { RundlerRpcSchema } from "./schema.js";
4
+ export type RundlerClient<transport extends Transport = Transport, chain extends Chain | undefined = Chain | undefined, account extends Account | undefined = Account | undefined> = Client<transport, chain, account, RundlerRpcSchema>;
5
+ /**
6
+ * A custom `estimateFeesPerGas` function for viem bundler clients to use `rundler_maxPriorityFeePerGas` for priority fee estimation.
7
+ *
8
+ * It fetches:
9
+ * 1. `baseFeePerGas` from the latest block.
10
+ * 2. `maxPriorityFeePerGas` via `rundler_maxPriorityFeePerGas`.
11
+ *
12
+ * It then returns `maxFeePerGas = baseFee * 1.5 + priority` (aligns with viem default).
13
+ *
14
+ * @param {RundlerClient} bundlerClient Bundler client with the rundler RPC method.
15
+ * @returns {Promise<{maxFeePerGas: bigint, maxPriorityFeePerGas: bigint}>} Estimated fee values.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { createBundlerClient } from "viem/account-abstraction";
20
+ * import { alchemyEstimateFeesPerGas } from "@alchemy/aa-infra";
21
+ *
22
+ * const bundler = createBundlerClient({
23
+ * transport: http("<rundler-url>"),
24
+ * userOperation: {
25
+ * estimateFeesPerGas: alchemyEstimateFeesPerGas,
26
+ * },
27
+ * });
28
+ * ```
29
+ */
30
+ export declare function estimateFeesPerGas<TTransport extends Transport = Transport, TChain extends Chain | undefined = Chain | undefined, TAccount extends Account | undefined = Account | undefined>({ bundlerClient, account: _account, userOperation: _userOperation, }: {
31
+ bundlerClient: RundlerClient<TTransport, TChain, TAccount>;
32
+ account?: SmartAccount;
33
+ userOperation?: UserOperationRequest;
34
+ }): Promise<{
35
+ maxFeePerGas: bigint;
36
+ maxPriorityFeePerGas: bigint;
37
+ }>;
@@ -0,0 +1,54 @@
1
+ import { getBlock } from "viem/actions";
2
+ import { isHex, hexToBigInt, } from "viem";
3
+ import { bigIntMultiply } from "@alchemy/common";
4
+ import { InvalidHexValueError } from "./errors.js";
5
+ /**
6
+ * A custom `estimateFeesPerGas` function for viem bundler clients to use `rundler_maxPriorityFeePerGas` for priority fee estimation.
7
+ *
8
+ * It fetches:
9
+ * 1. `baseFeePerGas` from the latest block.
10
+ * 2. `maxPriorityFeePerGas` via `rundler_maxPriorityFeePerGas`.
11
+ *
12
+ * It then returns `maxFeePerGas = baseFee * 1.5 + priority` (aligns with viem default).
13
+ *
14
+ * @param {RundlerClient} bundlerClient Bundler client with the rundler RPC method.
15
+ * @returns {Promise<{maxFeePerGas: bigint, maxPriorityFeePerGas: bigint}>} Estimated fee values.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { createBundlerClient } from "viem/account-abstraction";
20
+ * import { alchemyEstimateFeesPerGas } from "@alchemy/aa-infra";
21
+ *
22
+ * const bundler = createBundlerClient({
23
+ * transport: http("<rundler-url>"),
24
+ * userOperation: {
25
+ * estimateFeesPerGas: alchemyEstimateFeesPerGas,
26
+ * },
27
+ * });
28
+ * ```
29
+ */
30
+ export async function estimateFeesPerGas({ bundlerClient, account: _account, userOperation: _userOperation, }) {
31
+ const [block, maxPriorityFeePerGasHex] = await Promise.all([
32
+ getBlock(bundlerClient, { blockTag: "latest" }), // This is technically hitting the node rpc, not rundler.
33
+ bundlerClient.request({
34
+ method: "rundler_maxPriorityFeePerGas",
35
+ params: [],
36
+ }),
37
+ ]);
38
+ const baseFeePerGas = block.baseFeePerGas;
39
+ if (baseFeePerGas == null) {
40
+ throw new Error("baseFeePerGas is null");
41
+ }
42
+ if (maxPriorityFeePerGasHex == null) {
43
+ throw new Error("rundler_maxPriorityFeePerGas returned null or undefined");
44
+ }
45
+ if (!isHex(maxPriorityFeePerGasHex)) {
46
+ throw new InvalidHexValueError(maxPriorityFeePerGasHex);
47
+ }
48
+ const maxPriorityFeePerGas = hexToBigInt(maxPriorityFeePerGasHex);
49
+ return {
50
+ maxPriorityFeePerGas,
51
+ maxFeePerGas: bigIntMultiply(baseFeePerGas, 1.5) + maxPriorityFeePerGas,
52
+ };
53
+ }
54
+ //# sourceMappingURL=estimateFeesPerGas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"estimateFeesPerGas.js","sourceRoot":"","sources":["../../src/estimateFeesPerGas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,EAEL,KAAK,EAIL,WAAW,GACZ,MAAM,MAAM,CAAC;AAKd,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AASnD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAItC,EACA,aAAa,EACb,OAAO,EAAE,QAAQ,EACjB,aAAa,EAAE,cAAc,GAK9B;IAIC,MAAM,CAAC,KAAK,EAAE,uBAAuB,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QACzD,QAAQ,CAAC,aAAa,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,EAAE,yDAAyD;QAC1G,aAAa,CAAC,OAAO,CAAC;YACpB,MAAM,EAAE,8BAA8B;YACtC,MAAM,EAAE,EAAE;SACX,CAAC;KACH,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,KAAK,CAAC,aAAa,CAAC;IAC1C,IAAI,aAAa,IAAI,IAAI,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,uBAAuB,IAAI,IAAI,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;IAC7E,CAAC;IACD,IAAI,CAAC,KAAK,CAAC,uBAAuB,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,oBAAoB,CAAC,uBAAuB,CAAC,CAAC;IAC1D,CAAC;IACD,MAAM,oBAAoB,GAAG,WAAW,CAAC,uBAAuB,CAAC,CAAC;IAElE,OAAO;QACL,oBAAoB;QACpB,YAAY,EAAE,cAAc,CAAC,aAAa,EAAE,GAAG,CAAC,GAAG,oBAAoB;KACxE,CAAC;AACJ,CAAC","sourcesContent":["import { getBlock } from \"viem/actions\";\nimport {\n type Client,\n isHex,\n type Transport,\n type Chain,\n type Account,\n hexToBigInt,\n} from \"viem\";\nimport type {\n UserOperationRequest,\n SmartAccount,\n} from \"viem/account-abstraction\";\nimport { bigIntMultiply } from \"@alchemy/common\";\nimport type { RundlerRpcSchema } from \"./schema.js\";\nimport { InvalidHexValueError } from \"./errors.js\";\n\n// Extend client with Rundler rpc schema.\nexport type RundlerClient<\n transport extends Transport = Transport,\n chain extends Chain | undefined = Chain | undefined,\n account extends Account | undefined = Account | undefined,\n> = Client<transport, chain, account, RundlerRpcSchema>;\n\n/**\n * A custom `estimateFeesPerGas` function for viem bundler clients to use `rundler_maxPriorityFeePerGas` for priority fee estimation.\n *\n * It fetches:\n * 1. `baseFeePerGas` from the latest block.\n * 2. `maxPriorityFeePerGas` via `rundler_maxPriorityFeePerGas`.\n *\n * It then returns `maxFeePerGas = baseFee * 1.5 + priority` (aligns with viem default).\n *\n * @param {RundlerClient} bundlerClient Bundler client with the rundler RPC method.\n * @returns {Promise<{maxFeePerGas: bigint, maxPriorityFeePerGas: bigint}>} Estimated fee values.\n *\n * @example\n * ```ts\n * import { createBundlerClient } from \"viem/account-abstraction\";\n * import { alchemyEstimateFeesPerGas } from \"@alchemy/aa-infra\";\n *\n * const bundler = createBundlerClient({\n * transport: http(\"<rundler-url>\"),\n * userOperation: {\n * estimateFeesPerGas: alchemyEstimateFeesPerGas,\n * },\n * });\n * ```\n */\nexport async function estimateFeesPerGas<\n TTransport extends Transport = Transport,\n TChain extends Chain | undefined = Chain | undefined,\n TAccount extends Account | undefined = Account | undefined,\n>({\n bundlerClient,\n account: _account,\n userOperation: _userOperation,\n}: {\n bundlerClient: RundlerClient<TTransport, TChain, TAccount>;\n account?: SmartAccount;\n userOperation?: UserOperationRequest;\n}): Promise<{\n maxFeePerGas: bigint;\n maxPriorityFeePerGas: bigint;\n}> {\n const [block, maxPriorityFeePerGasHex] = await Promise.all([\n getBlock(bundlerClient, { blockTag: \"latest\" }), // This is technically hitting the node rpc, not rundler.\n bundlerClient.request({\n method: \"rundler_maxPriorityFeePerGas\",\n params: [],\n }),\n ]);\n\n const baseFeePerGas = block.baseFeePerGas;\n if (baseFeePerGas == null) {\n throw new Error(\"baseFeePerGas is null\");\n }\n if (maxPriorityFeePerGasHex == null) {\n throw new Error(\"rundler_maxPriorityFeePerGas returned null or undefined\");\n }\n if (!isHex(maxPriorityFeePerGasHex)) {\n throw new InvalidHexValueError(maxPriorityFeePerGasHex);\n }\n const maxPriorityFeePerGas = hexToBigInt(maxPriorityFeePerGasHex);\n\n return {\n maxPriorityFeePerGas,\n maxFeePerGas: bigIntMultiply(baseFeePerGas, 1.5) + maxPriorityFeePerGas,\n };\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export type * from "./schema.js";
2
+ export type * from "./estimateFeesPerGas.js";
3
+ export { estimateFeesPerGas } from "./estimateFeesPerGas.js";
@@ -0,0 +1,2 @@
1
+ export { estimateFeesPerGas } from "./estimateFeesPerGas.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC","sourcesContent":["// schema\nexport type * from \"./schema.js\";\n\nexport type * from \"./estimateFeesPerGas.js\";\nexport { estimateFeesPerGas } from \"./estimateFeesPerGas.js\";\n"]}
@@ -0,0 +1,2 @@
1
+ import type { DiagnosticsLogger } from "@alchemy/common";
2
+ export declare const LOGGER: DiagnosticsLogger;
@@ -0,0 +1,8 @@
1
+ import { createLogger } from "@alchemy/common";
2
+ import { VERSION } from "./version.js";
3
+ export const LOGGER = createLogger({
4
+ package: "@alchemy/aa-infra",
5
+ version: VERSION,
6
+ namespace: "aa-infra",
7
+ });
8
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,MAAM,CAAC,MAAM,MAAM,GAAsB,YAAY,CAAC;IACpD,OAAO,EAAE,mBAAmB;IAC5B,OAAO,EAAE,OAAO;IAChB,SAAS,EAAE,UAAU;CACtB,CAAC,CAAC","sourcesContent":["import { createLogger } from \"@alchemy/common\";\nimport type { DiagnosticsLogger } from \"@alchemy/common\";\nimport { VERSION } from \"./version.js\";\n\nexport const LOGGER: DiagnosticsLogger = createLogger({\n package: \"@alchemy/aa-infra\",\n version: VERSION,\n namespace: \"aa-infra\",\n});\n"]}
@@ -0,0 +1,8 @@
1
+ import type { Hex } from "viem";
2
+ type RundlerMaxPriorityFeePerGasSchema = {
3
+ Method: "rundler_maxPriorityFeePerGas";
4
+ Parameters: [];
5
+ ReturnType: Hex;
6
+ };
7
+ export type RundlerRpcSchema = [RundlerMaxPriorityFeePerGasSchema];
8
+ export {};
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"","sourcesContent":["import type { Hex } from \"viem\";\n\ntype RundlerMaxPriorityFeePerGasSchema = {\n Method: \"rundler_maxPriorityFeePerGas\";\n Parameters: [];\n ReturnType: Hex;\n};\n\nexport type RundlerRpcSchema = [RundlerMaxPriorityFeePerGasSchema];\n"]}
@@ -0,0 +1 @@
1
+ export declare const VERSION = "0.0.0";
@@ -0,0 +1,4 @@
1
+ // This file is autogenerated by inject-version.ts. Any changes will be
2
+ // overwritten on commit!
3
+ export const VERSION = "0.0.0";
4
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,yBAAyB;AACzB,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC","sourcesContent":["// This file is autogenerated by inject-version.ts. Any changes will be\n// overwritten on commit!\nexport const VERSION = \"0.0.0\";\n"]}
@@ -0,0 +1,9 @@
1
+ import { BaseError } from "@alchemy/common";
2
+ /**
3
+ * Error thrown when an invalid hex value is encountered during fee estimation.
4
+ */
5
+ export declare class InvalidHexValueError extends BaseError {
6
+ name: string;
7
+ constructor(value: unknown);
8
+ }
9
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,SAAS;IACxC,IAAI,SAA0B;gBAE3B,KAAK,EAAE,OAAO;CAG3B"}
@@ -0,0 +1,38 @@
1
+ import { type Client, type Transport, type Chain, type Account } from "viem";
2
+ import type { UserOperationRequest, SmartAccount } from "viem/account-abstraction";
3
+ import type { RundlerRpcSchema } from "./schema.js";
4
+ export type RundlerClient<transport extends Transport = Transport, chain extends Chain | undefined = Chain | undefined, account extends Account | undefined = Account | undefined> = Client<transport, chain, account, RundlerRpcSchema>;
5
+ /**
6
+ * A custom `estimateFeesPerGas` function for viem bundler clients to use `rundler_maxPriorityFeePerGas` for priority fee estimation.
7
+ *
8
+ * It fetches:
9
+ * 1. `baseFeePerGas` from the latest block.
10
+ * 2. `maxPriorityFeePerGas` via `rundler_maxPriorityFeePerGas`.
11
+ *
12
+ * It then returns `maxFeePerGas = baseFee * 1.5 + priority` (aligns with viem default).
13
+ *
14
+ * @param {RundlerClient} bundlerClient Bundler client with the rundler RPC method.
15
+ * @returns {Promise<{maxFeePerGas: bigint, maxPriorityFeePerGas: bigint}>} Estimated fee values.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { createBundlerClient } from "viem/account-abstraction";
20
+ * import { alchemyEstimateFeesPerGas } from "@alchemy/aa-infra";
21
+ *
22
+ * const bundler = createBundlerClient({
23
+ * transport: http("<rundler-url>"),
24
+ * userOperation: {
25
+ * estimateFeesPerGas: alchemyEstimateFeesPerGas,
26
+ * },
27
+ * });
28
+ * ```
29
+ */
30
+ export declare function estimateFeesPerGas<TTransport extends Transport = Transport, TChain extends Chain | undefined = Chain | undefined, TAccount extends Account | undefined = Account | undefined>({ bundlerClient, account: _account, userOperation: _userOperation, }: {
31
+ bundlerClient: RundlerClient<TTransport, TChain, TAccount>;
32
+ account?: SmartAccount;
33
+ userOperation?: UserOperationRequest;
34
+ }): Promise<{
35
+ maxFeePerGas: bigint;
36
+ maxPriorityFeePerGas: bigint;
37
+ }>;
38
+ //# sourceMappingURL=estimateFeesPerGas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"estimateFeesPerGas.d.ts","sourceRoot":"","sources":["../../src/estimateFeesPerGas.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,MAAM,EAEX,KAAK,SAAS,EACd,KAAK,KAAK,EACV,KAAK,OAAO,EAEb,MAAM,MAAM,CAAC;AACd,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACb,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAIpD,MAAM,MAAM,aAAa,CACvB,SAAS,SAAS,SAAS,GAAG,SAAS,EACvC,KAAK,SAAS,KAAK,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,EACnD,OAAO,SAAS,OAAO,GAAG,SAAS,GAAG,OAAO,GAAG,SAAS,IACvD,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,kBAAkB,CACtC,UAAU,SAAS,SAAS,GAAG,SAAS,EACxC,MAAM,SAAS,KAAK,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,EACpD,QAAQ,SAAS,OAAO,GAAG,SAAS,GAAG,OAAO,GAAG,SAAS,EAC1D,EACA,aAAa,EACb,OAAO,EAAE,QAAQ,EACjB,aAAa,EAAE,cAAc,GAC9B,EAAE;IACD,aAAa,EAAE,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC3D,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,aAAa,CAAC,EAAE,oBAAoB,CAAC;CACtC,GAAG,OAAO,CAAC;IACV,YAAY,EAAE,MAAM,CAAC;IACrB,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC,CAyBD"}
@@ -0,0 +1,4 @@
1
+ export type * from "./schema.js";
2
+ export type * from "./estimateFeesPerGas.js";
3
+ export { estimateFeesPerGas } from "./estimateFeesPerGas.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,mBAAmB,aAAa,CAAC;AAEjC,mBAAmB,yBAAyB,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { DiagnosticsLogger } from "@alchemy/common";
2
+ export declare const LOGGER: DiagnosticsLogger;
3
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAGzD,eAAO,MAAM,MAAM,EAAE,iBAInB,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { Hex } from "viem";
2
+ type RundlerMaxPriorityFeePerGasSchema = {
3
+ Method: "rundler_maxPriorityFeePerGas";
4
+ Parameters: [];
5
+ ReturnType: Hex;
6
+ };
7
+ export type RundlerRpcSchema = [RundlerMaxPriorityFeePerGasSchema];
8
+ export {};
9
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAEhC,KAAK,iCAAiC,GAAG;IACvC,MAAM,EAAE,8BAA8B,CAAC;IACvC,UAAU,EAAE,EAAE,CAAC;IACf,UAAU,EAAE,GAAG,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,CAAC,iCAAiC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const VERSION = "0.0.0";
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,UAAU,CAAC"}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@alchemy/aa-infra",
3
+ "version": "0.0.0-alpha.0",
4
+ "description": "Alchemy Account Abstraction Infrastructure",
5
+ "author": "Alchemy",
6
+ "license": "MIT",
7
+ "private": false,
8
+ "type": "module",
9
+ "main": "./dist/esm/index.js",
10
+ "module": "./dist/esm/index.js",
11
+ "types": "./dist/types/index.d.ts",
12
+ "typings": "./dist/types/index.d.ts",
13
+ "sideEffects": false,
14
+ "files": [
15
+ "dist",
16
+ "src/**/*.ts",
17
+ "!dist/**/*.tsbuildinfo",
18
+ "!vitest.config.ts",
19
+ "!.env",
20
+ "!src/**/*.test.ts",
21
+ "!src/**/*.test-d.ts",
22
+ "!src/__tests__/**/*"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/types/index.d.ts",
27
+ "import": "./dist/esm/index.js",
28
+ "default": "./dist/esm/index.js"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "scripts": {
33
+ "prebuild": "tsx ./inject-version.ts",
34
+ "build": "yarn clean && yarn build:esm && yarn build:types",
35
+ "build:esm": "tsc --project tsconfig.build.json --outDir ./dist/esm",
36
+ "build:types": "tsc --project tsconfig.build.json --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
37
+ "fern:gen": "node ../../doc-gen/dist/esm/cli.js generate --in ./src/index.ts --out ../../docs/pages/reference/alchemy/aa-infra",
38
+ "clean": "rm -rf ./dist",
39
+ "test": "vitest",
40
+ "test:run": "vitest run"
41
+ },
42
+ "devDependencies": {
43
+ "typescript-template": "*"
44
+ },
45
+ "dependencies": {
46
+ "@alchemy/common": "0.0.0-alpha.0",
47
+ "viem": "^2.32.0"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public",
51
+ "registry": "https://registry.npmjs.org/"
52
+ },
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/alchemyplatform/aa-sdk.git"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/alchemyplatform/aa-sdk/issues"
59
+ },
60
+ "homepage": "https://github.com/alchemyplatform/aa-sdk#readme",
61
+ "gitHead": "c0301f7bb5f4499fe35b5850293be6170057fae6"
62
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { BaseError } from "@alchemy/common";
2
+
3
+ /**
4
+ * Error thrown when an invalid hex value is encountered during fee estimation.
5
+ */
6
+ export class InvalidHexValueError extends BaseError {
7
+ override name = "InvalidHexValueError";
8
+
9
+ constructor(value: unknown) {
10
+ super(`Invalid hex value: ${value}`);
11
+ }
12
+ }
@@ -0,0 +1,90 @@
1
+ import { getBlock } from "viem/actions";
2
+ import {
3
+ type Client,
4
+ isHex,
5
+ type Transport,
6
+ type Chain,
7
+ type Account,
8
+ hexToBigInt,
9
+ } from "viem";
10
+ import type {
11
+ UserOperationRequest,
12
+ SmartAccount,
13
+ } from "viem/account-abstraction";
14
+ import { bigIntMultiply } from "@alchemy/common";
15
+ import type { RundlerRpcSchema } from "./schema.js";
16
+ import { InvalidHexValueError } from "./errors.js";
17
+
18
+ // Extend client with Rundler rpc schema.
19
+ export type RundlerClient<
20
+ transport extends Transport = Transport,
21
+ chain extends Chain | undefined = Chain | undefined,
22
+ account extends Account | undefined = Account | undefined,
23
+ > = Client<transport, chain, account, RundlerRpcSchema>;
24
+
25
+ /**
26
+ * A custom `estimateFeesPerGas` function for viem bundler clients to use `rundler_maxPriorityFeePerGas` for priority fee estimation.
27
+ *
28
+ * It fetches:
29
+ * 1. `baseFeePerGas` from the latest block.
30
+ * 2. `maxPriorityFeePerGas` via `rundler_maxPriorityFeePerGas`.
31
+ *
32
+ * It then returns `maxFeePerGas = baseFee * 1.5 + priority` (aligns with viem default).
33
+ *
34
+ * @param {RundlerClient} bundlerClient Bundler client with the rundler RPC method.
35
+ * @returns {Promise<{maxFeePerGas: bigint, maxPriorityFeePerGas: bigint}>} Estimated fee values.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { createBundlerClient } from "viem/account-abstraction";
40
+ * import { alchemyEstimateFeesPerGas } from "@alchemy/aa-infra";
41
+ *
42
+ * const bundler = createBundlerClient({
43
+ * transport: http("<rundler-url>"),
44
+ * userOperation: {
45
+ * estimateFeesPerGas: alchemyEstimateFeesPerGas,
46
+ * },
47
+ * });
48
+ * ```
49
+ */
50
+ export async function estimateFeesPerGas<
51
+ TTransport extends Transport = Transport,
52
+ TChain extends Chain | undefined = Chain | undefined,
53
+ TAccount extends Account | undefined = Account | undefined,
54
+ >({
55
+ bundlerClient,
56
+ account: _account,
57
+ userOperation: _userOperation,
58
+ }: {
59
+ bundlerClient: RundlerClient<TTransport, TChain, TAccount>;
60
+ account?: SmartAccount;
61
+ userOperation?: UserOperationRequest;
62
+ }): Promise<{
63
+ maxFeePerGas: bigint;
64
+ maxPriorityFeePerGas: bigint;
65
+ }> {
66
+ const [block, maxPriorityFeePerGasHex] = await Promise.all([
67
+ getBlock(bundlerClient, { blockTag: "latest" }), // This is technically hitting the node rpc, not rundler.
68
+ bundlerClient.request({
69
+ method: "rundler_maxPriorityFeePerGas",
70
+ params: [],
71
+ }),
72
+ ]);
73
+
74
+ const baseFeePerGas = block.baseFeePerGas;
75
+ if (baseFeePerGas == null) {
76
+ throw new Error("baseFeePerGas is null");
77
+ }
78
+ if (maxPriorityFeePerGasHex == null) {
79
+ throw new Error("rundler_maxPriorityFeePerGas returned null or undefined");
80
+ }
81
+ if (!isHex(maxPriorityFeePerGasHex)) {
82
+ throw new InvalidHexValueError(maxPriorityFeePerGasHex);
83
+ }
84
+ const maxPriorityFeePerGas = hexToBigInt(maxPriorityFeePerGasHex);
85
+
86
+ return {
87
+ maxPriorityFeePerGas,
88
+ maxFeePerGas: bigIntMultiply(baseFeePerGas, 1.5) + maxPriorityFeePerGas,
89
+ };
90
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ // schema
2
+ export type * from "./schema.js";
3
+
4
+ export type * from "./estimateFeesPerGas.js";
5
+ export { estimateFeesPerGas } from "./estimateFeesPerGas.js";
package/src/logger.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { createLogger } from "@alchemy/common";
2
+ import type { DiagnosticsLogger } from "@alchemy/common";
3
+ import { VERSION } from "./version.js";
4
+
5
+ export const LOGGER: DiagnosticsLogger = createLogger({
6
+ package: "@alchemy/aa-infra",
7
+ version: VERSION,
8
+ namespace: "aa-infra",
9
+ });
package/src/schema.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { Hex } from "viem";
2
+
3
+ type RundlerMaxPriorityFeePerGasSchema = {
4
+ Method: "rundler_maxPriorityFeePerGas";
5
+ Parameters: [];
6
+ ReturnType: Hex;
7
+ };
8
+
9
+ export type RundlerRpcSchema = [RundlerMaxPriorityFeePerGasSchema];
package/src/version.ts ADDED
@@ -0,0 +1,3 @@
1
+ // This file is autogenerated by inject-version.ts. Any changes will be
2
+ // overwritten on commit!
3
+ export const VERSION = "0.0.0";