-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutils.ts
More file actions
727 lines (688 loc) · 25.5 KB
/
Copy pathutils.ts
File metadata and controls
727 lines (688 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
import {encodeAbiParameters, hashTypedData, id, keccak256} from "./ethereUtils";
import {ENTRYPOINT_V6, ENTRYPOINT_V7, ENTRYPOINT_V8, ENTRYPOINT_V9} from "./constants";
import {AbstractionKitError, ensureError} from "./errors";
import type {TypedData} from "./signer/types";
import {JsonRpcNode, type Transport, TransportRpcError} from "./transport";
import {
type AbiInputValue,
GasOption,
type GasPrice,
type JsonRpcError,
type JsonRpcParam,
type JsonRpcResponse,
type JsonRpcResult,
type PolygonChain,
type PolygonGasStationJsonRpcResponse,
type UserOperationV6,
type UserOperationV7,
type UserOperationV8,
type UserOperationV9,
} from "./types";
function buildDomainSeparator(chainId: bigint, entrypoint: string): string {
// DOMAIN_NAME = "ERC4337"
const hashed_name = "0x364da28a5c92bcc87fe97c8813a6c6b8a3a049b0ea0a328fcb0b4f0e00337586";
// DOMAIN_VERSION = "1"
const hashed_version = "0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6";
// TYPE_HASH = keccak("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
const type_hash = "0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f";
const encodedUserOperationHash = encodeAbiParameters(
["(bytes32,bytes32,bytes32,uint256,address)"],
[[type_hash, hashed_name, hashed_version, chainId, entrypoint]],
);
return keccak256(encodedUserOperationHash);
}
/**
* Compute the UserOperation hash for any supported EntryPoint version.
* This hash is what gets signed by the account owner(s).
* Automatically selects the correct packing format based on the entrypoint address.
*
* @param useroperation - UserOperation to hash
* @param entrypointAddress - EntryPoint contract address (determines hash format)
* @param chainId - Target chain ID
* @returns The UserOperation hash as a hex string
*/
export function createUserOperationHash(
useroperation: UserOperationV6 | UserOperationV7 | UserOperationV8 | UserOperationV9,
entrypointAddress: string,
chainId: bigint,
): string {
let packedUserOperationHash: string;
let userOperationHash: string;
if (entrypointAddress.toLowerCase() === ENTRYPOINT_V6.toLowerCase()) {
packedUserOperationHash = keccak256(
createPackedUserOperationV6(useroperation as UserOperationV6),
);
const encodedUserOperationHash = encodeAbiParameters(
["bytes32", "address", "uint256"],
[packedUserOperationHash, entrypointAddress, chainId],
);
userOperationHash = keccak256(encodedUserOperationHash);
} else if (entrypointAddress.toLowerCase() === ENTRYPOINT_V7.toLowerCase()) {
packedUserOperationHash = keccak256(
createPackedUserOperationV7(useroperation as UserOperationV7),
);
const encodedUserOperationHash = encodeAbiParameters(
["bytes32", "address", "uint256"],
[packedUserOperationHash, entrypointAddress, chainId],
);
userOperationHash = keccak256(encodedUserOperationHash);
} else if (entrypointAddress.toLowerCase() === ENTRYPOINT_V8.toLowerCase()) {
packedUserOperationHash = keccak256(
createPackedUserOperationV8(useroperation as UserOperationV8),
);
const domainSeparator = buildDomainSeparator(chainId, entrypointAddress);
userOperationHash = keccak256(
`0x1901${domainSeparator.slice(2)}${packedUserOperationHash.slice(2)}`,
);
} else if (entrypointAddress.toLowerCase() === ENTRYPOINT_V9.toLowerCase()) {
packedUserOperationHash = keccak256(
createPackedUserOperationV9(useroperation as UserOperationV8),
);
const domainSeparator = buildDomainSeparator(chainId, entrypointAddress);
userOperationHash = keccak256(
`0x1901${domainSeparator.slice(2)}${packedUserOperationHash.slice(2)}`,
);
} else {
throw new RangeError(`unsupported entrypoint address: ${entrypointAddress}`);
}
return userOperationHash;
}
/**
* @internal
* Reconstruct the packed `initCode` field for an EntryPoint v0.8/v0.9
* UserOperation. When `eip7702Auth.address` is set, the EIP-7702 delegatee
* address replaces the factory address in initCode (only `factoryData` is
* concatenated). Otherwise, behaves like v0.7 (`factory + factoryData`).
*
* Shared by {@link createPackedUserOperationV8} /
* {@link createPackedUserOperationV9} and Simple7702Account's typed-data
* builder. Not part of the public API; only `export`ed for cross-module
* use within the package and not re-exported from `src/abstractionkit.ts`.
*
* @param useroperation - V8 or V9 UserOperation to read fields from
* @returns Hex-encoded initCode
*/
export function buildPackedInitCodeV8V9(useroperation: UserOperationV8 | UserOperationV9): string {
if (useroperation.factory == null) return "0x";
const eip7702Auth = useroperation.eip7702Auth;
const head =
eip7702Auth != null && eip7702Auth.address != null
? eip7702Auth.address
: useroperation.factory;
const factoryData = useroperation.factoryData != null ? useroperation.factoryData.slice(2) : "";
return head + factoryData;
}
/**
* @internal
* Pack `(verificationGasLimit, callGasLimit)` into a single `bytes32`
* (two uint128 values, big-endian). Matches the on-chain
* `PackedUserOperation.accountGasLimits` layout used by EntryPoint v0.7+.
*/
function packAccountGasLimits(verificationGasLimit: bigint, callGasLimit: bigint): string {
return (
"0x" +
encodeAbiParameters(["uint128"], [verificationGasLimit]).slice(34) +
encodeAbiParameters(["uint128"], [callGasLimit]).slice(34)
);
}
/**
* @internal
* Pack `(maxPriorityFeePerGas, maxFeePerGas)` into a single `bytes32`
* (two uint128 values, big-endian). Matches the on-chain
* `PackedUserOperation.gasFees` layout used by EntryPoint v0.7+.
*/
function packGasFees(maxPriorityFeePerGas: bigint, maxFeePerGas: bigint): string {
return (
"0x" +
encodeAbiParameters(["uint128"], [maxPriorityFeePerGas]).slice(34) +
encodeAbiParameters(["uint128"], [maxFeePerGas]).slice(34)
);
}
/**
* @internal
* Reconstruct the packed `paymasterAndData` field from a UserOperation's
* separate paymaster fields. Returns `0x` when no paymaster is set.
*
* For EntryPoint v0.9, when `paymasterData` ends with the parallel-paymaster
* signature magic suffix (`0x22e325a297439656`), the embedded signature is
* stripped so the userOpHash does not commit to the paymaster's signature
* over itself. Pass `stripV9PaymasterSig=false` to preserve the wire format.
*
* Shared by v7/v8/v9 packers and Simple7702Account's typed-data builder.
* Not part of the public API; only `export`ed for cross-module use within
* the package and not re-exported from `src/abstractionkit.ts`.
*
* @param useroperation - V7/V8/V9 UserOperation to read fields from
* @param stripV9PaymasterSig - Whether to strip the v0.9 paymaster signature suffix
* @returns Hex-encoded paymasterAndData
*/
export function buildPaymasterAndData(
useroperation: UserOperationV7 | UserOperationV8 | UserOperationV9,
stripV9PaymasterSig: boolean = false,
): string {
if (useroperation.paymaster == null) return "0x";
let paymasterAndData = useroperation.paymaster;
if (useroperation.paymasterVerificationGasLimit != null) {
paymasterAndData += encodeAbiParameters(
["uint128"],
[useroperation.paymasterVerificationGasLimit],
).slice(34);
}
if (useroperation.paymasterPostOpGasLimit != null) {
paymasterAndData += encodeAbiParameters(
["uint128"],
[useroperation.paymasterPostOpGasLimit],
).slice(34);
}
if (useroperation.paymasterData != null) {
const PAYMASTER_SIG_MAGIC = "22e325a297439656";
if (
stripV9PaymasterSig &&
useroperation.paymasterData.toLowerCase().endsWith(PAYMASTER_SIG_MAGIC)
) {
const sigLenHex = useroperation.paymasterData.slice(
useroperation.paymasterData.length - 16 - 4,
useroperation.paymasterData.length - 16,
);
const sigLen = parseInt(sigLenHex, 16);
const prefixEnd = useroperation.paymasterData.length - 16 - 4 - sigLen * 2;
paymasterAndData +=
useroperation.paymasterData.slice(0, prefixEnd).replaceAll("0x", "") + PAYMASTER_SIG_MAGIC;
} else {
paymasterAndData += useroperation.paymasterData.slice(2);
}
}
return paymasterAndData;
}
/**
* Build the EIP-712 typed data payload for a UserOperation under the
* EntryPoint v0.8 / v0.9 domain. The digest of the returned payload equals
* the `userOpHash` from {@link createUserOperationHash}, so signing it via
* `signTypedData` produces a signature that validates against the same hash
* as raw ECDSA over the `userOpHash`.
*
* Shared by EIP-7702 accounts (Simple7702, Calibur). Earlier EntryPoints
* define the userOpHash differently and don't fit this typed-data shape.
*
* @param userOperation - Unsigned UserOperation to wrap
* @param entrypointAddress - The EntryPoint contract address (must be v0.8 or v0.9)
* @param chainId - Target chain ID (must match the chain that will validate the signature)
* @returns EIP-712 {@link TypedData} payload ready for `signTypedData`
* @throws {AbstractionKitError} if `entrypointAddress` is not v0.8 / v0.9
*/
export function getUserOperationEip712DataV8V9(
userOperation: UserOperationV8 | UserOperationV9,
entrypointAddress: string,
chainId: bigint,
): TypedData {
const ep = entrypointAddress.toLowerCase();
const isV9 = ep === ENTRYPOINT_V9.toLowerCase();
if (ep !== ENTRYPOINT_V8.toLowerCase() && !isV9) {
throw new AbstractionKitError(
"BAD_DATA",
`getUserOperationEip712Data supports EntryPoint v0.8 / v0.9 only; ` +
`got ${entrypointAddress}. Earlier EntryPoints define the userOpHash ` +
`differently and require raw-hash signing.`,
);
}
const initCode = buildPackedInitCodeV8V9(userOperation);
const accountGasLimits = packAccountGasLimits(
userOperation.verificationGasLimit,
userOperation.callGasLimit,
);
const gasFees = packGasFees(userOperation.maxPriorityFeePerGas, userOperation.maxFeePerGas);
const paymasterAndData = buildPaymasterAndData(userOperation, isV9);
const types = {
PackedUserOperation: [
{ name: "sender", type: "address" },
{ name: "nonce", type: "uint256" },
{ name: "initCode", type: "bytes" },
{ name: "callData", type: "bytes" },
{ name: "accountGasLimits", type: "bytes32" },
{ name: "preVerificationGas", type: "uint256" },
{ name: "gasFees", type: "bytes32" },
{ name: "paymasterAndData", type: "bytes" },
],
};
return {
domain: {
name: "ERC4337",
version: "1",
chainId,
verifyingContract: entrypointAddress as `0x${string}`,
},
types,
primaryType: "PackedUserOperation",
message: {
sender: userOperation.sender,
nonce: userOperation.nonce,
initCode,
callData: userOperation.callData,
accountGasLimits,
preVerificationGas: userOperation.preVerificationGas,
gasFees,
paymasterAndData,
},
};
}
/**
* Compute the EIP-712 digest of a UserOperation under the EntryPoint
* v0.8 / v0.9 domain. For these EntryPoints this digest IS the
* `userOpHash` ({@link createUserOperationHash}).
*
* @param userOperation - Unsigned UserOperation to hash
* @param entrypointAddress - The EntryPoint contract address (must be v0.8 or v0.9)
* @param chainId - Target chain ID
* @returns The EIP-712 digest as a hex string
* @throws {AbstractionKitError} if `entrypointAddress` is not v0.8 / v0.9
*/
export function getUserOperationEip712HashV8V9(
userOperation: UserOperationV8 | UserOperationV9,
entrypointAddress: string,
chainId: bigint,
): string {
const data = getUserOperationEip712DataV8V9(userOperation, entrypointAddress, chainId);
return hashTypedData(data.domain, data.types, data.message);
}
/**
* ABI-encode and pack a UserOperation for hashing (EntryPoint v0.6 format).
* Bytes fields (initCode, callData, paymasterAndData) are keccak256-hashed before packing.
*
* @param useroperation - UserOperation to pack
* @returns ABI-encoded packed UserOperation as a hex string
*/
export function createPackedUserOperationV6(useroperation: UserOperationV6): string {
const useroperationValuesArrayWithHashedByteValues = [
useroperation.sender,
useroperation.nonce,
keccak256(useroperation.initCode),
keccak256(useroperation.callData),
useroperation.callGasLimit,
useroperation.verificationGasLimit,
useroperation.preVerificationGas,
useroperation.maxFeePerGas,
useroperation.maxPriorityFeePerGas,
keccak256(useroperation.paymasterAndData),
];
const packedUserOperation = encodeAbiParameters(
[
"address",
"uint256",
"bytes32",
"bytes32",
"uint256",
"uint256",
"uint256",
"uint256",
"uint256",
"bytes32",
],
useroperationValuesArrayWithHashedByteValues,
);
return packedUserOperation;
}
/**
* ABI-encode and pack a UserOperation for hashing (EntryPoint v0.7 format).
* Reconstructs initCode, accountGasLimits, gasFees, and paymasterAndData from separate fields.
*
* @param useroperation - UserOperation to pack
* @returns ABI-encoded packed UserOperation as a hex string
*/
export function createPackedUserOperationV7(useroperation: UserOperationV7): string {
let initCode = "0x";
if (useroperation.factory != null) {
initCode = useroperation.factory;
if (useroperation.factoryData != null) {
initCode += useroperation.factoryData.slice(2);
}
}
const accountGasLimits = packAccountGasLimits(
useroperation.verificationGasLimit,
useroperation.callGasLimit,
);
const gasFees = packGasFees(useroperation.maxPriorityFeePerGas, useroperation.maxFeePerGas);
const paymasterAndData = buildPaymasterAndData(useroperation);
const useroperationValuesArrayWithHashedByteValues = [
useroperation.sender,
useroperation.nonce,
keccak256(initCode),
keccak256(useroperation.callData),
accountGasLimits,
useroperation.preVerificationGas,
gasFees,
keccak256(paymasterAndData),
];
const packedUserOperation = encodeAbiParameters(
["address", "uint256", "bytes32", "bytes32", "bytes32", "uint256", "bytes32", "bytes32"],
useroperationValuesArrayWithHashedByteValues,
);
return packedUserOperation;
}
/**
* ABI-encode and pack a UserOperation for hashing (EntryPoint v0.9 format).
*
* @param useroperation - UserOperation to pack
* @returns ABI-encoded packed UserOperation as a hex string
*/
export function createPackedUserOperationV9(useroperation: UserOperationV8): string {
return baseCreatePackedUserOperationV8V9(useroperation, true);
}
/**
* ABI-encode and pack a UserOperation for hashing (EntryPoint v0.8 format).
*
* @param useroperation - UserOperation to pack
* @returns ABI-encoded packed UserOperation as a hex string
*/
export function createPackedUserOperationV8(useroperation: UserOperationV8): string {
return baseCreatePackedUserOperationV8V9(useroperation, false);
}
/**
* Shared packer for EntryPoint v0.8 and v0.9 UserOperations.
* @param useroperation - UserOperation to pack
* @param is_v9 - If true, strips the paymaster signature suffix (v0.9-specific)
* @returns ABI-encoded packed UserOperation as a hex string
*/
function baseCreatePackedUserOperationV8V9(
useroperation: UserOperationV8 | UserOperationV9,
is_v9: boolean,
): string {
const initCode = buildPackedInitCodeV8V9(useroperation);
const accountGasLimits = packAccountGasLimits(
useroperation.verificationGasLimit,
useroperation.callGasLimit,
);
const gasFees = packGasFees(useroperation.maxPriorityFeePerGas, useroperation.maxFeePerGas);
const paymasterAndData = buildPaymasterAndData(useroperation, is_v9);
const useroperationValuesArrayWithHashedByteValues = [
// PACKED_USEROP_TYPEHASH
"0x29a0bca4af4be3421398da00295e58e6d7de38cb492214754cb6a47507dd6f8e",
useroperation.sender,
useroperation.nonce,
keccak256(initCode),
keccak256(useroperation.callData),
accountGasLimits,
useroperation.preVerificationGas,
gasFees,
keccak256(paymasterAndData),
];
const packedUserOperation = encodeAbiParameters(
[
"bytes32",
"address",
"uint256",
"bytes32",
"bytes32",
"bytes32",
"uint256",
"bytes32",
"bytes32",
],
useroperationValuesArrayWithHashedByteValues,
);
return packedUserOperation;
}
/**
* Encode a function call into ABI-encoded calldata.
*
* @param functionSelector - 4-byte hex function selector (e.g., "0xa9059cbb" for ERC-20 transfer)
* @param functionInputAbi - Array of ABI type strings (e.g., ["address", "uint256"])
* @param functionInputParameters - Array of parameter values matching the ABI types
* @returns ABI-encoded calldata as a hex string (selector + encoded parameters)
*
* @example
* const transferCallData = createCallData(
* "0xa9059cbb",
* ["address", "uint256"],
* ["0xRecipientAddress", 1000000n],
* );
*/
export function createCallData(
functionSelector: string,
functionInputAbi: string[],
functionInputParameters: AbiInputValue[],
): string {
const params: string = encodeAbiParameters(functionInputAbi, functionInputParameters);
const callData = functionSelector + params.slice(2);
return callData;
}
/**
* Send a JSON-RPC 2.0 request to the specified endpoint.
*
* **External escape hatch.** Most SDK consumers should use one of the
* high-level service classes ({@link Bundler}, {@link CandidePaymaster},
* {@link Erc7677Paymaster}, {@link JsonRpcNode}) which translate errors into
* the SDK's domain vocabulary. This function is intentionally low-level: it
* speaks plain JSON-RPC and throws {@link TransportRpcError}
* (an EIP-1193-shaped `ProviderRpcError`) on RPC errors.
*
* Two execution modes:
* - **URL string:** issues a `fetch` POST directly. Supports custom `headers`
* and a custom `paramsKeyName` for non-standard servers (e.g. APIs that
* expect `"simulations"` instead of `"params"`).
* - **Transport / JsonRpcNode:** delegates to `.request({ method, params })`.
* `headers` and `paramsKeyName` are ignored (those concerns belong to the
* transport implementation).
*
* Automatically serializes `bigint` values in `params` as `0x`-prefixed hex.
*
* @param rpc - JSON-RPC endpoint URL, {@link Transport}, or {@link JsonRpcNode}
* @param method - The JSON-RPC method name (e.g., `"eth_call"`)
* @param params - The JSON-RPC parameters
* @param headers - Custom HTTP headers (URL inputs only; default Content-Type: application/json)
* @param paramsKeyName - Override the params key in the envelope (URL inputs only; default `"params"`)
* @returns The `result` (or non-standard equivalent) field from the JSON-RPC response
* @throws {@link TransportRpcError} when the response contains an `error` field
*/
export async function sendJsonRpcRequest(
rpc: string | Transport | JsonRpcNode,
method: string,
params: JsonRpcParam,
headers: Record<string, string> = { "Content-Type": "application/json" },
paramsKeyName: string = "params",
): Promise<JsonRpcResult> {
// Normalize bigints to 0x-hex so user-supplied transports
// and the fetch body both receive
// JSON-RPC-safe values.
const normalizedParams = JSON.parse(
JSON.stringify(params, (_key, value) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
typeof value === "bigint" ? `0x${value.toString(16)}` : value,
),
) as JsonRpcParam;
if (typeof rpc !== "string") {
return JsonRpcNode.from(rpc).request<JsonRpcResult>({
method,
params: normalizedParams as readonly unknown[] | object,
});
}
// URL path — preserve the historical fetch-based implementation so callers
// passing custom headers or a custom paramsKeyName continue to work.
const raw = JSON.stringify({
method,
[paramsKeyName]: normalizedParams,
id: Date.now(),
jsonrpc: "2.0",
});
const fetchResult = await fetch(rpc, {
method: "POST",
headers,
body: raw,
redirect: "follow",
});
const responseText = await fetchResult.text();
let response: JsonRpcResponse;
try {
response = JSON.parse(responseText) as JsonRpcResponse;
} catch {
// non-JSON body (HTML error page, plain text) — the HTTP status is the
// real diagnostic, so surface it instead of a JSON parse error
throw new TransportRpcError(
-32603,
`HTTP ${fetchResult.status} ${fetchResult.statusText}: response body is not JSON`.trim(),
responseText.slice(0, 1000),
);
}
if (typeof response !== "object" || response === null) {
// scalar or null payload — the `in` checks below would throw a
// TypeError; report it as a malformed response with the HTTP status
throw new TransportRpcError(
-32603,
`HTTP ${fetchResult.status} ${fetchResult.statusText}: malformed JSON-RPC response`.trim(),
response,
);
}
if ("result" in response) {
return response.result as JsonRpcResult;
}
// Non-standard servers (e.g. Tenderly's simulate-bundle API) return the
// payload under `simulation_results` instead of `result`.
if ("simulation_results" in response) {
return response.simulation_results as JsonRpcResult;
}
const err = response.error as JsonRpcError;
if (err == null || typeof err !== "object") {
// no result and no error object — report the HTTP status rather than
// crashing on err.code
throw new TransportRpcError(
-32603,
`HTTP ${fetchResult.status} ${fetchResult.statusText}: malformed JSON-RPC response`.trim(),
response,
);
}
throw new TransportRpcError(err.code, err.message);
}
/**
* Get a 4-byte function selector from a Solidity-style function signature.
* Computed as the first 4 bytes of keccak256(signature).
* @param functionSignature - Solidity-style function signature, e.g. "mint(address)"
* @returns Function selector as a 0x-prefixed 10-character hex string
*
* @example
* getFunctionSelector("getNonce(address,uint192)"); // "0x35567e1a"
*/
export function getFunctionSelector(functionSignature: string): string {
return id(functionSignature).slice(0, 10);
}
/**
* Fetch the account's nonce from the EntryPoint contract.
*
* Equivalent to `JsonRpcNode.from(rpc).getEntryPointNonce(entryPoint, account, key)`.
* Kept as a top-level export for backward compatibility with existing call
* sites; the first parameter widens from `string` to
* `string | Transport | JsonRpcNode` so users can pass a configured transport
* (with custom headers, retries, etc.) without restructuring their code.
*
* @param rpc - Ethereum JSON-RPC node URL, {@link Transport}, or {@link JsonRpcNode}
* @param entryPoint - EntryPoint contract address
* @param account - Smart account address to query
* @param key - Nonce key (default 0). Different keys allow parallel nonce channels.
* @returns The current nonce as a bigint
*/
export async function fetchAccountNonce(
rpc: string | Transport | JsonRpcNode,
entryPoint: string,
account: string,
key: number = 0,
): Promise<bigint> {
return JsonRpcNode.from(rpc).getEntryPointNonce(entryPoint, account, BigInt(key));
}
/**
* Fetch current gas prices from the Polygon Gas Station API.
*
* @param polygonChain - Target Polygon chain (Mainnet, Amoy, etc.)
* @param gasLevel - Gas price level (Slow, Medium, Fast)
* @returns A tuple of [maxFeePerGas, maxPriorityFeePerGas] as bigints
*/
export async function fetchGasPricePolygon(
polygonChain: PolygonChain,
gasLevel: GasOption = GasOption.Medium,
): Promise<[bigint, bigint]> {
const gasStationUrl = `https://gasstation.polygon.technology/${polygonChain}`;
try {
const fetchResult = await fetch(gasStationUrl);
const response = (await fetchResult.json()) as PolygonGasStationJsonRpcResponse;
let gasPrice: GasPrice;
if (gasLevel === GasOption.Slow) {
gasPrice = response.safeLow;
} else if (gasLevel === GasOption.Medium) {
gasPrice = response.standard;
} else {
gasPrice = response.fast;
}
let maxFeePerGas = BigInt(Math.ceil(Number(gasPrice.maxFee) * 1000000000));
let maxPriorityFeePerGas = BigInt(Math.ceil(Number(gasPrice.maxPriorityFee) * 1000000000));
if (maxFeePerGas === 0n) {
maxFeePerGas = 1n;
}
if (maxPriorityFeePerGas === 0n) {
maxPriorityFeePerGas = 1n;
}
return [maxFeePerGas, maxPriorityFeePerGas];
} catch (err) {
const error = ensureError(err);
throw new AbstractionKitError("BAD_DATA", `fetching gas prices from ${gasStationUrl} failed.`, {
cause: error,
});
}
}
/**
* Calculate the maximum gas cost (in wei) that a UserOperation could consume.
* Uses different formulas for v0.6 (with paymaster multiplier) and v0.7+ UserOperations.
*
* @param useroperation - The UserOperation to calculate the max gas cost for
* @returns Maximum possible gas cost in wei as a bigint
*/
export function calculateUserOperationMaxGasCost(
useroperation: UserOperationV6 | UserOperationV7,
): bigint {
if ("initCode" in useroperation) {
const isPaymasterAndData =
useroperation.paymasterAndData !== "0x" && useroperation.paymasterAndData != null;
const mul = isPaymasterAndData ? 3n : 1n;
const requiredGas =
useroperation.callGasLimit +
useroperation.verificationGasLimit * mul +
useroperation.preVerificationGas;
return requiredGas * useroperation.maxFeePerGas;
} else {
const requiredGas =
useroperation.verificationGasLimit +
useroperation.callGasLimit +
(useroperation.paymasterVerificationGasLimit ?? 0n) +
(useroperation.paymasterPostOpGasLimit ?? 0n) +
useroperation.preVerificationGas;
return requiredGas * useroperation.maxFeePerGas;
}
}
/**
* Deposit information for an address in the EntryPoint contract.
*/
export type DepositInfo = {
deposit: bigint;
staked: boolean;
stake: bigint;
unstakeDelaySec: bigint;
withdrawTime: bigint;
};
/**
* @internal
* Internal dispatcher: routes to the Polygon Gas Station when a chain is
* provided, otherwise delegates to {@link JsonRpcNode.getFeeData}. Used by
* the account classes when assembling base UserOperation gas fields.
*/
export async function handlefetchGasPrice(
providerRpc: string | Transport | JsonRpcNode | undefined,
polygonGasStation: PolygonChain | undefined,
gasLevel: GasOption = GasOption.Medium,
): Promise<[bigint, bigint]> {
if (polygonGasStation != null) {
return fetchGasPricePolygon(polygonGasStation, gasLevel);
}
if (providerRpc != null) {
return JsonRpcNode.from(providerRpc).getFeeData(gasLevel);
}
throw new AbstractionKitError(
"BAD_DATA",
"providerRpc can't be null if maxFeePerGas and maxPriorityFeePerGas are not overridden",
);
}