viem 0.0.1-alpha.8 → 0.0.1-cjs.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 (39) hide show
  1. package/actions/package.json +1 -1
  2. package/chains/package.json +1 -1
  3. package/clients/package.json +1 -1
  4. package/dist/actions/index.d.ts +5 -4
  5. package/dist/actions/index.js +125 -124
  6. package/dist/actions/index.mjs +125 -0
  7. package/dist/chains.d.ts +2 -2
  8. package/dist/chains.js +75 -76
  9. package/dist/chains.mjs +133 -0
  10. package/dist/chunk-2FDH6XP5.mjs +2616 -0
  11. package/dist/chunk-46ZFLVHC.js +1084 -0
  12. package/dist/chunk-5ZBNF5WM.js +2616 -0
  13. package/dist/{chunk-BIQ5KSX5.js → chunk-CWCWWGBC.mjs} +1 -1
  14. package/dist/{chunk-N6PIT2C5.js → chunk-HLVCJ7RV.mjs} +49 -12
  15. package/dist/chunk-SGTIBKHG.js +258 -0
  16. package/dist/clients/index.d.ts +3 -3
  17. package/dist/clients/index.js +23 -23
  18. package/dist/clients/index.mjs +23 -0
  19. package/dist/{createWalletClient-b5c3c89e.d.ts → createWalletClient-d612fe08.d.ts} +1 -1
  20. package/dist/{eip1193-100454ab.d.ts → eip1193-020a6f13.d.ts} +1 -1
  21. package/dist/index.d.ts +40 -18
  22. package/dist/index.js +374 -374
  23. package/dist/index.mjs +374 -0
  24. package/dist/{parseGwei-14f716fc.d.ts → parseGwei-7c87ff41.d.ts} +40 -118
  25. package/dist/transactionRequest-08d30731.d.ts +132 -0
  26. package/dist/utils/index.d.ts +38 -5
  27. package/dist/utils/index.js +148 -138
  28. package/dist/utils/index.mjs +148 -0
  29. package/dist/{watchAsset-e99e744d.d.ts → watchAsset-bc6373f4.d.ts} +17 -5
  30. package/dist/{webSocket-f4abf66c.d.ts → webSocket-7f88e9e0.d.ts} +1 -1
  31. package/dist/window.d.ts +1 -1
  32. package/dist/window.js +1 -0
  33. package/dist/window.mjs +0 -0
  34. package/package.json +9 -3
  35. package/utils/package.json +1 -1
  36. package/window/package.json +1 -1
  37. package/dist/chunk-4Z43OTO6.js +0 -1349
  38. package/dist/chunk-ALVD6MNU.js +0 -1113
  39. package/dist/transactionRequest-3e463099.d.ts +0 -44
@@ -1,1349 +0,0 @@
1
- import {
2
- AbiConstructorNotFoundError,
3
- AbiConstructorParamsNotFoundError,
4
- AbiDecodingDataSizeInvalidError,
5
- AbiEncodingArrayLengthMismatchError,
6
- AbiEncodingLengthMismatchError,
7
- AbiErrorInputsNotFoundError,
8
- AbiErrorNotFoundError,
9
- AbiErrorSignatureNotFoundError,
10
- AbiEventNotFoundError,
11
- AbiFunctionNotFoundError,
12
- AbiFunctionOutputsNotFoundError,
13
- AbiFunctionSignatureNotFoundError,
14
- DataLengthTooLongError,
15
- DataLengthTooShortError,
16
- FilterTypeNotSupportedError,
17
- InvalidAbiDecodingTypeError,
18
- InvalidAbiEncodingTypeError,
19
- InvalidAddressError,
20
- InvalidArrayError,
21
- InvalidBytesBooleanError,
22
- InvalidDefinitionTypeError,
23
- InvalidHexBooleanError,
24
- InvalidHexValueError,
25
- OffsetOutOfBoundsError,
26
- SizeExceedsPaddingSizeError
27
- } from "./chunk-ALVD6MNU.js";
28
-
29
- // src/utils/data/concat.ts
30
- function concat(values) {
31
- if (typeof values[0] === "string")
32
- return concatHex(values);
33
- return concatBytes(values);
34
- }
35
- function concatBytes(values) {
36
- let length = 0;
37
- for (const arr of values) {
38
- length += arr.length;
39
- }
40
- const result = new Uint8Array(length);
41
- let offset = 0;
42
- for (const arr of values) {
43
- result.set(arr, offset);
44
- offset += arr.length;
45
- }
46
- return result;
47
- }
48
- function concatHex(values) {
49
- return `0x${values.reduce(
50
- (acc, x) => acc + x.replace("0x", ""),
51
- ""
52
- )}`;
53
- }
54
-
55
- // src/utils/data/isBytes.ts
56
- function isBytes(value) {
57
- if (!value)
58
- return false;
59
- if (typeof value !== "object")
60
- return false;
61
- return value.BYTES_PER_ELEMENT === 1 && value.constructor.name === "Uint8Array";
62
- }
63
-
64
- // src/utils/data/isHex.ts
65
- function isHex(value) {
66
- if (!value)
67
- return false;
68
- if (typeof value !== "string")
69
- return false;
70
- return /^0x[0-9a-fA-F]*$/.test(value);
71
- }
72
-
73
- // src/utils/data/pad.ts
74
- function pad(hexOrBytes, { dir, size: size2 = 32 } = {}) {
75
- if (typeof hexOrBytes === "string")
76
- return padHex(hexOrBytes, { dir, size: size2 });
77
- return padBytes(hexOrBytes, { dir, size: size2 });
78
- }
79
- function padHex(hex_, { dir, size: size2 = 32 } = {}) {
80
- let hex = hex_.replace("0x", "");
81
- if (hex.length > size2 * 2)
82
- throw new SizeExceedsPaddingSizeError({
83
- size: Math.ceil(hex.length / 2),
84
- targetSize: size2,
85
- type: "hex"
86
- });
87
- return `0x${hex[dir === "right" ? "padEnd" : "padStart"](
88
- size2 * 2,
89
- "0"
90
- )}`;
91
- }
92
- function padBytes(bytes, { dir, size: size2 = 32 } = {}) {
93
- if (bytes.length > size2)
94
- throw new SizeExceedsPaddingSizeError({
95
- size: bytes.length,
96
- targetSize: size2,
97
- type: "bytes"
98
- });
99
- const paddedBytes = new Uint8Array(size2);
100
- for (let i = 0; i < size2; i++) {
101
- const padEnd = dir === "right";
102
- paddedBytes[padEnd ? i : size2 - i - 1] = bytes[padEnd ? i : bytes.length - i - 1];
103
- }
104
- return paddedBytes;
105
- }
106
-
107
- // src/utils/data/trim.ts
108
- function trim(hexOrBytes, { dir = "left" } = {}) {
109
- let data = typeof hexOrBytes === "string" ? hexOrBytes.replace("0x", "") : hexOrBytes;
110
- let sliceLength = 0;
111
- for (let i = 0; i < data.length - 1; i++) {
112
- if (data[dir === "left" ? i : data.length - i - 1].toString() === "0")
113
- sliceLength++;
114
- else
115
- break;
116
- }
117
- data = dir === "left" ? data.slice(sliceLength) : data.slice(0, data.length - sliceLength);
118
- if (typeof hexOrBytes === "string") {
119
- if (data.length === 1 && dir === "right")
120
- data = `${data}0`;
121
- return `0x${data}`;
122
- }
123
- return data;
124
- }
125
-
126
- // src/utils/data/size.ts
127
- function size(value) {
128
- if (isHex(value))
129
- return Math.ceil((value.length - 2) / 2);
130
- return value.length;
131
- }
132
-
133
- // src/utils/data/slice.ts
134
- function slice(value, start, end) {
135
- if (isHex(value))
136
- return sliceHex(value, start, end);
137
- return sliceBytes(value, start, end);
138
- }
139
- function assertStartOffset(value, start) {
140
- if (typeof start === "number" && start > 0 && start > size(value) - 1)
141
- throw new Error(
142
- `Slice starting at offset "${start}" is out-of-bounds (size: ${size(
143
- value
144
- )}).`
145
- );
146
- }
147
- function sliceBytes(value, start, end) {
148
- assertStartOffset(value, start);
149
- return value.slice(start, end);
150
- }
151
- function sliceHex(value_, start, end) {
152
- assertStartOffset(value_, start);
153
- const value = value_.replace("0x", "").slice((start ?? 0) * 2, (end ?? value_.length) * 2);
154
- return `0x${value}`;
155
- }
156
-
157
- // src/utils/encoding/encodeHex.ts
158
- var hexes = Array.from(
159
- { length: 256 },
160
- (v, i) => i.toString(16).padStart(2, "0")
161
- );
162
- function boolToHex(value) {
163
- return `0x${Number(value)}`;
164
- }
165
- function bytesToHex(value) {
166
- let hex = "";
167
- for (let i = 0; i < value.length; i++) {
168
- hex += hexes[value[i]];
169
- }
170
- return `0x${hex}`;
171
- }
172
- function encodeHex(value) {
173
- if (typeof value === "number" || typeof value === "bigint")
174
- return numberToHex(value);
175
- if (typeof value === "string") {
176
- return stringToHex(value);
177
- }
178
- if (typeof value === "boolean")
179
- return boolToHex(value);
180
- return bytesToHex(value);
181
- }
182
- function numberToHex(value_, opts = {}) {
183
- const { signed, size: size2 } = opts;
184
- let value = BigInt(value_);
185
- let maxValue;
186
- if (size2) {
187
- if (signed)
188
- maxValue = (1n << BigInt(size2) * 8n - 1n) - 1n;
189
- else
190
- maxValue = 2n ** (BigInt(size2) * 8n) - 1n;
191
- } else if (typeof value_ === "number") {
192
- maxValue = BigInt(Number.MAX_SAFE_INTEGER);
193
- }
194
- const minValue = typeof maxValue === "bigint" && signed ? -maxValue - 1n : 0;
195
- if (maxValue && value > maxValue || value < minValue) {
196
- const suffix = typeof value_ === "bigint" ? "n" : "";
197
- throw new Error(
198
- `Number "${value_}${suffix}" is not in safe ${size2 ? `${size2 * 8}-bit ${signed ? "signed" : "unsigned"} ` : ""}integer range ${maxValue ? `(${minValue}${suffix} to ${maxValue}${suffix})` : `(above ${minValue})`}`
199
- );
200
- }
201
- const hex = `0x${(signed && value < 0 ? (1n << BigInt(size2 * 8)) + BigInt(value) : value).toString(16)}`;
202
- if (size2)
203
- return pad(hex, { size: size2 });
204
- return hex;
205
- }
206
- function stringToHex(value) {
207
- let hex = "";
208
- for (let i = 0; i < value.length; i++) {
209
- hex += value.charCodeAt(i).toString(16);
210
- }
211
- return `0x${hex}`;
212
- }
213
-
214
- // src/utils/encoding/encodeBytes.ts
215
- var encoder = new TextEncoder();
216
- function boolToBytes(value) {
217
- const bytes = new Uint8Array(1);
218
- bytes[0] = Number(value);
219
- return bytes;
220
- }
221
- function encodeBytes(value) {
222
- if (typeof value === "number" || typeof value === "bigint")
223
- return numberToBytes(value);
224
- if (typeof value === "boolean")
225
- return boolToBytes(value);
226
- if (value.startsWith("0x"))
227
- return hexToBytes(value);
228
- return stringToBytes(value);
229
- }
230
- function hexToBytes(hex_) {
231
- let hex = hex_.slice(2);
232
- if (hex.length % 2)
233
- hex = `0${hex}`;
234
- const bytes = new Uint8Array(hex.length / 2);
235
- for (let index = 0; index < bytes.length; index++) {
236
- const start = index * 2;
237
- const hexByte = hex.slice(start, start + 2);
238
- const byte = Number.parseInt(hexByte, 16);
239
- if (Number.isNaN(byte) || byte < 0)
240
- throw new Error("Invalid byte sequence");
241
- bytes[index] = byte;
242
- }
243
- return bytes;
244
- }
245
- function numberToBytes(value, opts) {
246
- const hex = numberToHex(value, opts);
247
- return hexToBytes(hex);
248
- }
249
- function stringToBytes(value) {
250
- return encoder.encode(value);
251
- }
252
-
253
- // src/utils/encoding/encodeRlp.ts
254
- function encodeRlp(hexOrBytes, to_) {
255
- const to = to_ || "hex";
256
- return format(bytesToRlp(parse(hexOrBytes)), to);
257
- }
258
- function parse(hexOrBytes) {
259
- if (Array.isArray(hexOrBytes))
260
- return hexOrBytes.map(parse);
261
- return typeof hexOrBytes === "string" ? encodeBytes(hexOrBytes) : hexOrBytes;
262
- }
263
- function format(bytes, type = "bytes") {
264
- return type === "hex" ? bytesToHex(bytes) : bytes;
265
- }
266
- function bytesToRlp(bytes) {
267
- if (Array.isArray(bytes)) {
268
- const encoded = concat(bytes.map(bytesToRlp));
269
- return new Uint8Array([...encodeLength(encoded.length, 192), ...encoded]);
270
- }
271
- if (bytes.length === 1 && bytes[0] < 128)
272
- return bytes;
273
- return new Uint8Array([...encodeLength(bytes.length, 128), ...bytes]);
274
- }
275
- function encodeLength(length, offset) {
276
- if (length < 56)
277
- return [offset + length];
278
- return [encodeBytes(length).length + offset + 55, ...encodeBytes(length)];
279
- }
280
-
281
- // src/utils/encoding/decodeHex.ts
282
- function decodeHex(hex, to) {
283
- if (to === "number")
284
- return hexToNumber(hex);
285
- if (to === "bigint")
286
- return hexToBigInt(hex);
287
- if (to === "string")
288
- return hexToString(hex);
289
- if (to === "boolean")
290
- return hexToBool(hex);
291
- return hexToBytes(hex);
292
- }
293
- function hexToBigInt(hex, opts = {}) {
294
- const { signed } = opts;
295
- const value = BigInt(hex);
296
- if (!signed)
297
- return value;
298
- const size2 = (hex.length - 2) / 2;
299
- const max = (1n << BigInt(size2) * 8n - 1n) - 1n;
300
- if (value <= max)
301
- return value;
302
- return value - BigInt(`0x${"f".padStart(size2 * 2, "f")}`) - 1n;
303
- }
304
- function hexToBool(hex) {
305
- if (trim(hex) === "0x0")
306
- return false;
307
- if (trim(hex) === "0x1")
308
- return true;
309
- throw new InvalidHexBooleanError(hex);
310
- }
311
- function hexToNumber(hex, opts = {}) {
312
- return Number(hexToBigInt(hex, opts));
313
- }
314
- function hexToString(hex) {
315
- const bytes = hexToBytes(hex);
316
- return new TextDecoder().decode(bytes);
317
- }
318
-
319
- // src/utils/encoding/decodeBytes.ts
320
- function decodeBytes(bytes, to) {
321
- if (to === "number")
322
- return bytesToNumber(bytes);
323
- if (to === "bigint")
324
- return bytesToBigint(bytes);
325
- if (to === "boolean")
326
- return bytesToBool(bytes);
327
- if (to === "string")
328
- return bytesToString(bytes);
329
- return bytesToHex(bytes);
330
- }
331
- function bytesToBigint(bytes) {
332
- const hex = bytesToHex(bytes);
333
- return hexToBigInt(hex);
334
- }
335
- function bytesToBool(bytes) {
336
- if (bytes.length > 1 || bytes[0] > 1)
337
- throw new InvalidBytesBooleanError(bytes);
338
- return Boolean(bytes[0]);
339
- }
340
- function bytesToNumber(bytes) {
341
- const hex = bytesToHex(bytes);
342
- return hexToNumber(hex);
343
- }
344
- function bytesToString(bytes) {
345
- return new TextDecoder().decode(bytes);
346
- }
347
-
348
- // src/utils/encoding/decodeRlp.ts
349
- function decodeRlp(value, to) {
350
- const bytes = parse2(value);
351
- const [data, consumed] = rlpToBytes(bytes);
352
- if (consumed < bytes.length)
353
- throw new DataLengthTooLongError({
354
- consumed,
355
- length: bytes.length
356
- });
357
- return format2(data, to);
358
- }
359
- function parse2(value) {
360
- if (typeof value === "string") {
361
- if (value.length > 3 && value.length % 2 !== 0)
362
- throw new InvalidHexValueError(value);
363
- return hexToBytes(value);
364
- }
365
- return value;
366
- }
367
- function format2(bytes, to) {
368
- if (Array.isArray(bytes))
369
- return bytes.map((b) => format2(b, to));
370
- return to === "hex" ? trim(bytesToHex(bytes)) : bytes;
371
- }
372
- function rlpToBytes(bytes, offset = 0) {
373
- if (bytes.length === 0)
374
- return [new Uint8Array([]), 0];
375
- const prefix = bytes[offset];
376
- if (prefix <= 127)
377
- return [new Uint8Array([bytes[offset]]), 1];
378
- if (prefix <= 183) {
379
- const length2 = prefix - 128;
380
- const offset_ = offset + 1;
381
- if (offset_ + length2 > bytes.length)
382
- throw new DataLengthTooShortError({
383
- length: offset_ + length2,
384
- dataLength: bytes.length
385
- });
386
- return [bytes.slice(offset_, offset_ + length2), 1 + length2];
387
- }
388
- if (prefix <= 191) {
389
- const lengthOfLength2 = prefix - 183;
390
- const offset_ = offset + 1;
391
- const length2 = bytesToNumber(bytes.slice(offset_, offset_ + lengthOfLength2));
392
- if (offset_ + lengthOfLength2 + length2 > bytes.length)
393
- throw new DataLengthTooShortError({
394
- length: lengthOfLength2 + length2,
395
- dataLength: bytes.length - lengthOfLength2
396
- });
397
- return [
398
- bytes.slice(offset_ + lengthOfLength2, offset_ + lengthOfLength2 + length2),
399
- 1 + lengthOfLength2 + length2
400
- ];
401
- }
402
- let lengthOfLength = 0;
403
- let length = prefix - 192;
404
- if (prefix > 247) {
405
- lengthOfLength = prefix - 247;
406
- length = bytesToNumber(bytes.slice(offset + 1, offset + 1 + lengthOfLength));
407
- }
408
- let nextOffset = offset + 1 + lengthOfLength;
409
- if (nextOffset > bytes.length)
410
- throw new DataLengthTooShortError({
411
- length: nextOffset,
412
- dataLength: bytes.length
413
- });
414
- let consumed = 1 + lengthOfLength + length;
415
- let result = [];
416
- while (nextOffset < offset + consumed) {
417
- const decoded = rlpToBytes(bytes, nextOffset);
418
- result.push(decoded[0]);
419
- nextOffset += decoded[1];
420
- if (nextOffset > offset + consumed)
421
- throw new OffsetOutOfBoundsError({
422
- nextOffset,
423
- offset: offset + consumed
424
- });
425
- }
426
- return [result, consumed];
427
- }
428
-
429
- // src/utils/solidity.ts
430
- var paramsRegex = /((function|event)\s)?(.*)(\((.*)\))/;
431
- function extractFunctionParts(def) {
432
- const parts = def.match(paramsRegex);
433
- const type = parts?.[2] || void 0;
434
- const name = parts?.[3];
435
- const params = parts?.[5] || void 0;
436
- return { type, name, params };
437
- }
438
- function extractFunctionName(def) {
439
- return extractFunctionParts(def).name;
440
- }
441
- function extractFunctionParams(def) {
442
- const params = extractFunctionParts(def).params;
443
- const splitParams = params?.split(",").map((x) => x.trim().split(" "));
444
- return splitParams?.map((param) => ({
445
- type: param[0],
446
- name: param[1] === "indexed" ? param[2] : param[1],
447
- ...param[1] === "indexed" ? { indexed: true } : {}
448
- }));
449
- }
450
- function extractFunctionType(def) {
451
- return extractFunctionParts(def).type;
452
- }
453
-
454
- // src/utils/hash/keccak256.ts
455
- import { keccak_256 } from "@noble/hashes/sha3";
456
- function keccak256(value, to_) {
457
- const to = to_ || "hex";
458
- const bytes = keccak_256(value);
459
- if (to === "bytes")
460
- return bytes;
461
- return encodeHex(bytes);
462
- }
463
-
464
- // src/utils/hash/hashFunction.ts
465
- var hash = (value) => keccak256(encodeBytes(value));
466
- function hashFunction(def) {
467
- const name = extractFunctionName(def);
468
- const params = extractFunctionParams(def);
469
- if (!params || params.length === 0)
470
- return hash(def.replace(/ /g, ""));
471
- return hash(`${name}(${params.map(({ type }) => type).join(",")})`);
472
- }
473
-
474
- // src/utils/hash/getEventSignature.ts
475
- var getEventSignature = (event) => hashFunction(event);
476
-
477
- // src/utils/hash/getFunctionSignature.ts
478
- var getFunctionSignature = (fn) => slice(hashFunction(fn), 0, 4);
479
-
480
- // src/utils/address/getAddress.ts
481
- var addressRegex = /^(0x)?[a-fA-F0-9]{40}$/;
482
- function checksumAddress(address_) {
483
- const hexAddress = address_.substring(2).toLowerCase();
484
- const hash2 = keccak256(stringToBytes(hexAddress), "bytes");
485
- let address = hexAddress.split("");
486
- for (let i = 0; i < 40; i += 2) {
487
- if (hash2?.[i >> 1] >> 4 >= 8) {
488
- address[i] = address[i].toUpperCase();
489
- }
490
- if ((hash2[i >> 1] & 15) >= 8) {
491
- address[i + 1] = address[i + 1].toUpperCase();
492
- }
493
- }
494
- return `0x${address.join("")}`;
495
- }
496
- function getAddress(address) {
497
- if (!addressRegex.test(address))
498
- throw new InvalidAddressError({ address });
499
- return checksumAddress(address);
500
- }
501
-
502
- // src/utils/address/getContractAddress.ts
503
- function getContractAddress(opts) {
504
- if (opts.opcode === "CREATE2")
505
- return getCreate2Address(opts);
506
- return getCreateAddress(opts);
507
- }
508
- function getCreateAddress(opts) {
509
- const from = encodeBytes(getAddress(opts.from));
510
- let nonce = encodeBytes(opts.nonce);
511
- if (nonce[0] === 0)
512
- nonce = new Uint8Array([]);
513
- return getAddress(
514
- `0x${keccak256(encodeRlp([from, nonce], "bytes")).slice(26)}`
515
- );
516
- }
517
- function getCreate2Address(opts) {
518
- const from = encodeBytes(getAddress(opts.from));
519
- const salt = pad(
520
- isBytes(opts.salt) ? opts.salt : encodeBytes(opts.salt),
521
- { size: 32 }
522
- );
523
- const bytecodeHash = encodeBytes(
524
- keccak256(
525
- isBytes(opts.bytecode) ? opts.bytecode : encodeBytes(opts.bytecode)
526
- )
527
- );
528
- return getAddress(
529
- slice(
530
- keccak256(concat([encodeBytes("0xff"), from, salt, bytecodeHash])),
531
- 12
532
- )
533
- );
534
- }
535
-
536
- // src/utils/address/isAddress.ts
537
- function isAddress(address) {
538
- try {
539
- return Boolean(getAddress(address));
540
- } catch {
541
- return false;
542
- }
543
- }
544
-
545
- // src/utils/address/isAddressEqual.ts
546
- function isAddressEqual(a, b) {
547
- return getAddress(a) === getAddress(b);
548
- }
549
-
550
- // src/utils/abi/encodeAbi.ts
551
- function encodeAbi({
552
- params,
553
- values
554
- }) {
555
- if (params.length !== values.length)
556
- throw new AbiEncodingLengthMismatchError({
557
- expectedLength: params.length,
558
- givenLength: values.length
559
- });
560
- const preparedParams = prepareParams({ params, values });
561
- const data = encodeParams(preparedParams);
562
- if (data.length === 0)
563
- return "0x";
564
- return data;
565
- }
566
- function prepareParams({
567
- params,
568
- values
569
- }) {
570
- let preparedParams = [];
571
- for (let i = 0; i < params.length; i++) {
572
- preparedParams.push(prepareParam({ param: params[i], value: values[i] }));
573
- }
574
- return preparedParams;
575
- }
576
- function prepareParam({
577
- param,
578
- value
579
- }) {
580
- const arrayComponents = getArrayComponents(param.type);
581
- if (arrayComponents) {
582
- const [length, type] = arrayComponents;
583
- return encodeArray(value, { length, param: { ...param, type } });
584
- }
585
- if (param.type === "tuple") {
586
- return encodeTuple(value, {
587
- param
588
- });
589
- }
590
- if (param.type === "address") {
591
- return encodeAddress(value);
592
- }
593
- if (param.type === "bool") {
594
- return encodeBool(value);
595
- }
596
- if (param.type.startsWith("uint") || param.type.startsWith("int")) {
597
- const signed = param.type.startsWith("int");
598
- return encodeNumber(value, { signed });
599
- }
600
- if (param.type.startsWith("bytes")) {
601
- return encodeBytes2(value, { param });
602
- }
603
- if (param.type === "string") {
604
- return encodeString(value);
605
- }
606
- throw new InvalidAbiEncodingTypeError(param.type);
607
- }
608
- function encodeParams(preparedParams) {
609
- let staticSize = 0;
610
- for (let i = 0; i < preparedParams.length; i++) {
611
- const { dynamic, encoded } = preparedParams[i];
612
- if (dynamic)
613
- staticSize += 32;
614
- else
615
- staticSize += size(encoded);
616
- }
617
- let staticParams = [];
618
- let dynamicParams = [];
619
- let dynamicSize = 0;
620
- for (let i = 0; i < preparedParams.length; i++) {
621
- const { dynamic, encoded } = preparedParams[i];
622
- if (dynamic) {
623
- staticParams.push(numberToHex(staticSize + dynamicSize, { size: 32 }));
624
- dynamicParams.push(encoded);
625
- dynamicSize += size(encoded);
626
- } else {
627
- staticParams.push(encoded);
628
- }
629
- }
630
- return concat([...staticParams, ...dynamicParams]);
631
- }
632
- function encodeAddress(value) {
633
- return { dynamic: false, encoded: padHex(value.toLowerCase()) };
634
- }
635
- function encodeArray(value, {
636
- length,
637
- param
638
- }) {
639
- let dynamic = length === null;
640
- if (!Array.isArray(value))
641
- throw new InvalidArrayError(value);
642
- if (!dynamic && value.length !== length)
643
- throw new AbiEncodingArrayLengthMismatchError({
644
- expectedLength: length,
645
- givenLength: value.length,
646
- type: `${param.type}[${length}]`
647
- });
648
- let dynamicChild = false;
649
- let preparedParams = [];
650
- for (let i = 0; i < value.length; i++) {
651
- const preparedParam = prepareParam({ param, value: value[i] });
652
- if (preparedParam.dynamic)
653
- dynamicChild = true;
654
- preparedParams.push(preparedParam);
655
- }
656
- if (dynamic || dynamicChild) {
657
- const data = encodeParams(preparedParams);
658
- if (dynamic) {
659
- const length2 = numberToHex(preparedParams.length, { size: 32 });
660
- return {
661
- dynamic: true,
662
- encoded: preparedParams.length > 0 ? concat([length2, data]) : length2
663
- };
664
- }
665
- if (dynamicChild)
666
- return { dynamic: true, encoded: data };
667
- }
668
- return {
669
- dynamic: false,
670
- encoded: concat(preparedParams.map(({ encoded }) => encoded))
671
- };
672
- }
673
- function encodeBytes2(value, { param }) {
674
- const [_, size_] = param.type.split("bytes");
675
- if (!size_)
676
- return {
677
- dynamic: true,
678
- encoded: concat([
679
- padHex(numberToHex(size(value), { size: 32 })),
680
- padHex(value, { dir: "right" })
681
- ])
682
- };
683
- return { dynamic: false, encoded: padHex(value, { dir: "right" }) };
684
- }
685
- function encodeBool(value) {
686
- return { dynamic: false, encoded: padHex(boolToHex(value)) };
687
- }
688
- function encodeNumber(value, { signed }) {
689
- return {
690
- dynamic: false,
691
- encoded: numberToHex(value, {
692
- size: 32,
693
- signed
694
- })
695
- };
696
- }
697
- function encodeString(value) {
698
- return {
699
- dynamic: true,
700
- encoded: concat([
701
- padHex(numberToHex(value.length, { size: 32 })),
702
- padHex(stringToHex(value), { dir: "right" })
703
- ])
704
- };
705
- }
706
- function encodeTuple(value, { param }) {
707
- let dynamic = false;
708
- let preparedParams = [];
709
- for (let i = 0; i < param.components.length; i++) {
710
- const param_ = param.components[i];
711
- const index = Array.isArray(value) ? i : param_.name;
712
- const preparedParam = prepareParam({
713
- param: param_,
714
- value: value[index]
715
- });
716
- preparedParams.push(preparedParam);
717
- dynamic = preparedParam.dynamic;
718
- }
719
- return {
720
- dynamic,
721
- encoded: dynamic ? encodeParams(preparedParams) : concat(preparedParams.map(({ encoded }) => encoded))
722
- };
723
- }
724
- function getArrayComponents(type) {
725
- const matches = type.match(/^(.*)\[(\d+)?\]$/);
726
- return matches ? [matches[2] ? Number(matches[2]) : null, matches[1]] : void 0;
727
- }
728
-
729
- // src/utils/abi/decodeAbi.ts
730
- function decodeAbi({
731
- data,
732
- params
733
- }) {
734
- if (size(data) % 32 !== 0)
735
- throw new AbiDecodingDataSizeInvalidError(size(data));
736
- return decodeParams({
737
- data,
738
- params
739
- });
740
- }
741
- function decodeParams({
742
- data,
743
- params
744
- }) {
745
- let decodedValues = [];
746
- let position = 0;
747
- for (let i = 0; i < params.length; i++) {
748
- const param = params[i];
749
- const { consumed, value } = decodeParam({ data, param, position });
750
- decodedValues.push(value);
751
- position += consumed;
752
- }
753
- return decodedValues;
754
- }
755
- function decodeParam({
756
- data,
757
- param,
758
- position
759
- }) {
760
- const arrayComponents = getArrayComponents(param.type);
761
- if (arrayComponents) {
762
- const [length, type] = arrayComponents;
763
- return decodeArray(data, {
764
- length,
765
- param: { ...param, type },
766
- position
767
- });
768
- }
769
- if (param.type === "tuple") {
770
- return decodeTuple(data, { param, position });
771
- }
772
- if (param.type === "string") {
773
- return decodeString(data, { position });
774
- }
775
- if (param.type.startsWith("bytes")) {
776
- return decodeBytes2(data, { param, position });
777
- }
778
- let value = slice(data, position, position + 32);
779
- if (param.type.startsWith("uint") || param.type.startsWith("int")) {
780
- return decodeNumber(value, { param });
781
- }
782
- if (param.type === "address") {
783
- return decodeAddress(value);
784
- }
785
- if (param.type === "bool") {
786
- return decodeBool(value);
787
- }
788
- throw new InvalidAbiDecodingTypeError(param.type);
789
- }
790
- function decodeAddress(value) {
791
- return { consumed: 32, value: checksumAddress(trim(value)) };
792
- }
793
- function decodeArray(data, {
794
- param,
795
- length,
796
- position
797
- }) {
798
- if (!length) {
799
- const offset = hexToNumber(slice(data, position, position + 32));
800
- const length2 = hexToNumber(slice(data, offset, offset + 32));
801
- let consumed2 = 0;
802
- let value2 = [];
803
- for (let i = 0; i < length2; ++i) {
804
- const decodedChild = decodeParam({
805
- data: slice(data, offset + 32),
806
- param,
807
- position: consumed2
808
- });
809
- consumed2 += decodedChild.consumed;
810
- value2.push(decodedChild.value);
811
- }
812
- return { value: value2, consumed: 32 };
813
- }
814
- if (hasDynamicChild(param)) {
815
- const arrayComponents = getArrayComponents(param.type);
816
- const dynamicChild = !arrayComponents?.[0];
817
- let consumed2 = 0;
818
- let value2 = [];
819
- for (let i = 0; i < length; ++i) {
820
- const offset = hexToNumber(slice(data, position, position + 32));
821
- const decodedChild = decodeParam({
822
- data: slice(data, offset),
823
- param,
824
- position: dynamicChild ? consumed2 : i * 32
825
- });
826
- consumed2 += decodedChild.consumed;
827
- value2.push(decodedChild.value);
828
- }
829
- return { value: value2, consumed: consumed2 };
830
- }
831
- let consumed = 0;
832
- let value = [];
833
- for (let i = 0; i < length; ++i) {
834
- const decodedChild = decodeParam({
835
- data,
836
- param,
837
- position: position + consumed
838
- });
839
- consumed += decodedChild.consumed;
840
- value.push(decodedChild.value);
841
- }
842
- return { value, consumed };
843
- }
844
- function decodeBool(value) {
845
- return { consumed: 32, value: hexToBool(value) };
846
- }
847
- function decodeBytes2(data, { param, position }) {
848
- const [_, size2] = param.type.split("bytes");
849
- if (!size2) {
850
- const offset = hexToNumber(slice(data, position, position + 32));
851
- const length = hexToNumber(slice(data, offset, offset + 32));
852
- const value2 = slice(data, offset + 32, offset + 32 + length);
853
- return { consumed: 32, value: value2 };
854
- }
855
- const value = slice(data, position, position + parseInt(size2));
856
- return { consumed: 32, value };
857
- }
858
- function decodeNumber(value, { param }) {
859
- const signed = param.type.startsWith("int");
860
- const size2 = parseInt(param.type.split("int")[1] || "256");
861
- return {
862
- consumed: 32,
863
- value: size2 > 48 ? hexToBigInt(value, { signed }) : hexToNumber(value, { signed })
864
- };
865
- }
866
- function decodeString(data, { position }) {
867
- const offset = hexToNumber(slice(data, position, position + 32));
868
- const length = hexToNumber(slice(data, offset, offset + 32));
869
- const value = hexToString(
870
- trim(slice(data, offset + 32, offset + 32 + length))
871
- );
872
- return { consumed: 32, value };
873
- }
874
- function decodeTuple(data, { param, position }) {
875
- const hasUnnamedChild = param.components.length === 0 || param.components.some(({ name }) => !name);
876
- let value = hasUnnamedChild ? [] : {};
877
- let consumed = 0;
878
- if (hasDynamicChild(param)) {
879
- const offset = hexToNumber(slice(data, position, position + 32));
880
- for (let i = 0; i < param.components.length; ++i) {
881
- const component = param.components[i];
882
- const decodedChild = decodeParam({
883
- data: slice(data, offset),
884
- param: component,
885
- position: consumed
886
- });
887
- consumed += decodedChild.consumed;
888
- value[hasUnnamedChild ? i : component?.name] = decodedChild.value;
889
- }
890
- return { consumed: 32, value };
891
- }
892
- for (let i = 0; i < param.components.length; ++i) {
893
- const component = param.components[i];
894
- const decodedChild = decodeParam({
895
- data,
896
- param: component,
897
- position: position + consumed
898
- });
899
- consumed += decodedChild.consumed;
900
- value[hasUnnamedChild ? i : component?.name] = decodedChild.value;
901
- }
902
- return { consumed, value };
903
- }
904
- function hasDynamicChild(param) {
905
- const { type } = param;
906
- if (type === "string")
907
- return true;
908
- if (type === "bytes")
909
- return true;
910
- if (type.endsWith("[]"))
911
- return true;
912
- if (type === "tuple")
913
- return param.components?.some(hasDynamicChild);
914
- const arrayComponents = getArrayComponents(param.type);
915
- if (arrayComponents && hasDynamicChild({ ...param, type: arrayComponents[1] }))
916
- return true;
917
- return false;
918
- }
919
-
920
- // src/utils/abi/getDefinition.ts
921
- function getDefinition(description) {
922
- if (description.type !== "function" && description.type !== "event" && description.type !== "error")
923
- throw new InvalidDefinitionTypeError(description.type);
924
- return `${description.name}(${getParams(description.inputs)})`;
925
- }
926
- function getParams(params) {
927
- if (!params)
928
- return "";
929
- return params.map(getParam).join(",");
930
- }
931
- function getParam(param) {
932
- if (param.type.startsWith("tuple")) {
933
- return `(${getParams(
934
- param.components
935
- )})${param.type.slice("tuple".length)}`;
936
- }
937
- return param.type;
938
- }
939
-
940
- // src/utils/abi/decodeErrorResult.ts
941
- function decodeErrorResult({ abi, data }) {
942
- const signature = slice(data, 0, 4);
943
- const description = abi.find(
944
- (x) => signature === getFunctionSignature(getDefinition(x))
945
- );
946
- if (!description)
947
- throw new AbiErrorSignatureNotFoundError(signature);
948
- return {
949
- errorName: description.name,
950
- args: "inputs" in description && description.inputs && description.inputs.length > 0 ? decodeAbi({ data: slice(data, 4), params: description.inputs }) : void 0
951
- };
952
- }
953
-
954
- // src/utils/abi/decodeFunctionData.ts
955
- function decodeFunctionData({ abi, data }) {
956
- const signature = slice(data, 0, 4);
957
- const description = abi.find(
958
- (x) => signature === getFunctionSignature(getDefinition(x))
959
- );
960
- if (!description)
961
- throw new AbiFunctionSignatureNotFoundError(signature);
962
- return {
963
- functionName: description.name,
964
- args: "inputs" in description && description.inputs && description.inputs.length > 0 ? decodeAbi({ data: slice(data, 4), params: description.inputs }) : void 0
965
- };
966
- }
967
-
968
- // src/utils/abi/decodeFunctionResult.ts
969
- function decodeFunctionResult({
970
- abi,
971
- functionName,
972
- data
973
- }) {
974
- const description = abi.find((x) => "name" in x && x.name === functionName);
975
- if (!description)
976
- throw new AbiFunctionNotFoundError(functionName);
977
- if (!("outputs" in description))
978
- throw new AbiFunctionOutputsNotFoundError(functionName);
979
- const values = decodeAbi({ data, params: description.outputs });
980
- return values.length > 1 ? values : values.length === 1 ? values[0] : void 0;
981
- }
982
-
983
- // src/utils/abi/encodeDeployData.ts
984
- function encodeDeployData({
985
- abi,
986
- args,
987
- bytecode
988
- }) {
989
- if (!args || args.length === 0)
990
- return bytecode;
991
- const description = abi.find((x) => "type" in x && x.type === "constructor");
992
- if (!description)
993
- throw new AbiConstructorNotFoundError();
994
- if (!("inputs" in description))
995
- throw new AbiConstructorParamsNotFoundError();
996
- if (!description.inputs || description.inputs.length === 0)
997
- throw new AbiConstructorParamsNotFoundError();
998
- const data = encodeAbi({
999
- params: description.inputs,
1000
- values: args
1001
- });
1002
- return concatHex([bytecode, data]);
1003
- }
1004
-
1005
- // src/utils/abi/encodeErrorResult.ts
1006
- function encodeErrorResult({
1007
- abi,
1008
- errorName,
1009
- args
1010
- }) {
1011
- const description = abi.find((x) => "name" in x && x.name === errorName);
1012
- if (!description)
1013
- throw new AbiErrorNotFoundError(errorName);
1014
- const definition = getDefinition(description);
1015
- const signature = getFunctionSignature(definition);
1016
- let data = "0x";
1017
- if (args && args.length > 0) {
1018
- if (!("inputs" in description && description.inputs))
1019
- throw new AbiErrorInputsNotFoundError(errorName);
1020
- data = encodeAbi({ params: description.inputs, values: args });
1021
- }
1022
- return concatHex([signature, data]);
1023
- }
1024
-
1025
- // src/utils/abi/encodeEventTopics.ts
1026
- function encodeEventTopics({
1027
- abi,
1028
- eventName,
1029
- args
1030
- }) {
1031
- const description = abi.find((x) => "name" in x && x.name === eventName);
1032
- if (!description)
1033
- throw new AbiEventNotFoundError(eventName);
1034
- const definition = getDefinition(description);
1035
- const signature = getEventSignature(definition);
1036
- let topics = [];
1037
- if (args && "inputs" in description) {
1038
- const args_ = Array.isArray(args) ? args : description.inputs?.map((x) => args[x.name]) ?? [];
1039
- topics = description.inputs?.filter((param) => "indexed" in param && param.indexed).map(
1040
- (param, i) => Array.isArray(args_[i]) ? args_[i].map(
1041
- (_, j) => encodeArg({ param, value: args_[i][j] })
1042
- ) : args_[i] ? encodeArg({ param, value: args_[i] }) : null
1043
- ) ?? [];
1044
- }
1045
- return [signature, ...topics];
1046
- }
1047
- function encodeArg({
1048
- param,
1049
- value
1050
- }) {
1051
- if (param.type === "string" || param.type === "bytes")
1052
- return keccak256(encodeBytes(value));
1053
- if (param.type === "tuple" || param.type.match(/^(.*)\[(\d+)?\]$/))
1054
- throw new FilterTypeNotSupportedError(param.type);
1055
- return encodeAbi({ params: [param], values: [value] });
1056
- }
1057
-
1058
- // src/utils/abi/encodeFunctionData.ts
1059
- function encodeFunctionData({
1060
- abi,
1061
- args,
1062
- functionName
1063
- }) {
1064
- const description = abi.find((x) => "name" in x && x.name === functionName);
1065
- if (!description)
1066
- throw new AbiFunctionNotFoundError(functionName);
1067
- const definition = getDefinition(description);
1068
- const signature = getFunctionSignature(definition);
1069
- const data = "inputs" in description && description.inputs ? encodeAbi({
1070
- params: description.inputs,
1071
- values: args ?? []
1072
- }) : void 0;
1073
- return concatHex([signature, data ?? "0x"]);
1074
- }
1075
-
1076
- // src/utils/abi/encodeFunctionResult.ts
1077
- function encodeFunctionResult({
1078
- abi,
1079
- functionName,
1080
- result
1081
- }) {
1082
- const description = abi.find((x) => "name" in x && x.name === functionName);
1083
- if (!description)
1084
- throw new AbiFunctionNotFoundError(functionName);
1085
- if (!("outputs" in description))
1086
- throw new AbiFunctionOutputsNotFoundError(functionName);
1087
- let values = Array.isArray(result) ? result : [result];
1088
- if (description.outputs.length === 0 && !values[0])
1089
- values = [];
1090
- return encodeAbi({ params: description.outputs, values });
1091
- }
1092
-
1093
- // src/constants.ts
1094
- var etherUnits = {
1095
- gwei: 9,
1096
- wei: 18
1097
- };
1098
- var gweiUnits = {
1099
- ether: -9,
1100
- wei: 9
1101
- };
1102
- var weiUnits = {
1103
- ether: -18,
1104
- gwei: -9
1105
- };
1106
- var transactionType = {
1107
- "0x0": "legacy",
1108
- "0x1": "eip2930",
1109
- "0x2": "eip1559"
1110
- };
1111
-
1112
- // src/utils/formatters/transaction.ts
1113
- function formatTransaction(transaction) {
1114
- const transaction_ = {
1115
- ...transaction,
1116
- blockNumber: transaction.blockNumber ? BigInt(transaction.blockNumber) : null,
1117
- gas: transaction.gas ? BigInt(transaction.gas) : void 0,
1118
- gasPrice: transaction.gasPrice ? BigInt(transaction.gasPrice) : void 0,
1119
- maxFeePerGas: transaction.maxFeePerGas ? BigInt(transaction.maxFeePerGas) : void 0,
1120
- maxPriorityFeePerGas: transaction.maxPriorityFeePerGas ? BigInt(transaction.maxPriorityFeePerGas) : void 0,
1121
- nonce: transaction.nonce ? hexToNumber(transaction.nonce) : void 0,
1122
- transactionIndex: transaction.transactionIndex ? Number(transaction.transactionIndex) : null,
1123
- type: transaction.type ? transactionType[transaction.type] : void 0,
1124
- value: transaction.value ? BigInt(transaction.value) : void 0,
1125
- v: transaction.v ? BigInt(transaction.v) : void 0
1126
- };
1127
- if (transaction_.type === "legacy") {
1128
- delete transaction_["accessList"];
1129
- delete transaction_["maxFeePerGas"];
1130
- delete transaction_["maxPriorityFeePerGas"];
1131
- }
1132
- if (transaction_.type === "eip2930") {
1133
- delete transaction_["maxFeePerGas"];
1134
- delete transaction_["maxPriorityFeePerGas"];
1135
- }
1136
- return transaction_;
1137
- }
1138
-
1139
- // src/utils/formatters/block.ts
1140
- function formatBlock(block) {
1141
- const transactions = block.transactions?.map((transaction) => {
1142
- if (typeof transaction === "string")
1143
- return transaction;
1144
- return formatTransaction(transaction);
1145
- });
1146
- return {
1147
- ...block,
1148
- baseFeePerGas: block.baseFeePerGas ? BigInt(block.baseFeePerGas) : null,
1149
- difficulty: block.difficulty ? BigInt(block.difficulty) : void 0,
1150
- gasLimit: block.gasLimit ? BigInt(block.gasLimit) : void 0,
1151
- gasUsed: block.gasUsed ? BigInt(block.gasUsed) : void 0,
1152
- number: block.number ? BigInt(block.number) : null,
1153
- size: block.size ? BigInt(block.size) : void 0,
1154
- timestamp: block.timestamp ? BigInt(block.timestamp) : void 0,
1155
- transactions,
1156
- totalDifficulty: block.totalDifficulty ? BigInt(block.totalDifficulty) : null
1157
- };
1158
- }
1159
-
1160
- // src/utils/formatters/feeHistory.ts
1161
- function formatFeeHistory(feeHistory) {
1162
- return {
1163
- baseFeePerGas: feeHistory.baseFeePerGas.map((value) => BigInt(value)),
1164
- gasUsedRatio: feeHistory.gasUsedRatio,
1165
- oldestBlock: BigInt(feeHistory.oldestBlock),
1166
- reward: feeHistory.reward?.map(
1167
- (reward) => reward.map((value) => BigInt(value))
1168
- )
1169
- };
1170
- }
1171
-
1172
- // src/utils/formatters/format.ts
1173
- function format3(data, { formatter }) {
1174
- return formatter(data);
1175
- }
1176
-
1177
- // src/utils/formatters/log.ts
1178
- function formatLog(log) {
1179
- return {
1180
- ...log,
1181
- blockNumber: log.blockNumber ? BigInt(log.blockNumber) : null,
1182
- logIndex: log.logIndex ? BigInt(log.logIndex) : null,
1183
- transactionIndex: log.transactionIndex ? BigInt(log.transactionIndex) : null
1184
- };
1185
- }
1186
-
1187
- // src/utils/formatters/transactionReceipt.ts
1188
- var statuses = {
1189
- "0x0": "reverted",
1190
- "0x1": "success"
1191
- };
1192
- function formatTransactionReceipt(transactionReceipt) {
1193
- return {
1194
- ...transactionReceipt,
1195
- blockNumber: transactionReceipt.blockNumber ? BigInt(transactionReceipt.blockNumber) : null,
1196
- cumulativeGasUsed: transactionReceipt.cumulativeGasUsed ? BigInt(transactionReceipt.cumulativeGasUsed) : null,
1197
- effectiveGasPrice: transactionReceipt.effectiveGasPrice ? BigInt(transactionReceipt.effectiveGasPrice) : null,
1198
- gasUsed: transactionReceipt.gasUsed ? BigInt(transactionReceipt.gasUsed) : null,
1199
- logs: transactionReceipt.logs ? transactionReceipt.logs.map(formatLog) : null,
1200
- transactionIndex: transactionReceipt.transactionIndex ? hexToNumber(transactionReceipt.transactionIndex) : null,
1201
- status: transactionReceipt.status ? statuses[transactionReceipt.status] : null,
1202
- type: transactionReceipt.type ? transactionType[transactionReceipt.type] : null
1203
- };
1204
- }
1205
-
1206
- // src/utils/formatters/transactionRequest.ts
1207
- function formatTransactionRequest(transactionRequest) {
1208
- return {
1209
- ...transactionRequest,
1210
- gas: typeof transactionRequest.gas !== "undefined" ? numberToHex(transactionRequest.gas) : void 0,
1211
- gasPrice: typeof transactionRequest.gasPrice !== "undefined" ? numberToHex(transactionRequest.gasPrice) : void 0,
1212
- maxFeePerGas: typeof transactionRequest.maxFeePerGas !== "undefined" ? numberToHex(transactionRequest.maxFeePerGas) : void 0,
1213
- maxPriorityFeePerGas: typeof transactionRequest.maxPriorityFeePerGas !== "undefined" ? numberToHex(transactionRequest.maxPriorityFeePerGas) : void 0,
1214
- nonce: typeof transactionRequest.nonce !== "undefined" ? numberToHex(transactionRequest.nonce) : void 0,
1215
- value: typeof transactionRequest.value !== "undefined" ? numberToHex(transactionRequest.value) : void 0
1216
- };
1217
- }
1218
-
1219
- // src/utils/unit/formatUnit.ts
1220
- function formatUnit(value, decimals) {
1221
- let display = value.toString();
1222
- const negative = display.startsWith("-");
1223
- if (negative)
1224
- display = display.slice(1);
1225
- let [integer, fraction] = [
1226
- display.slice(0, display.length - decimals),
1227
- display.slice(display.length - decimals)
1228
- ];
1229
- fraction = fraction.padStart(decimals, "0");
1230
- fraction = fraction.replace(/(0+)$/, "");
1231
- return `${negative ? "-" : ""}${integer || "0"}${fraction ? `.${fraction}` : ""}`;
1232
- }
1233
-
1234
- // src/utils/unit/formatEther.ts
1235
- function formatEther(wei, unit = "wei") {
1236
- return formatUnit(wei, etherUnits[unit]);
1237
- }
1238
-
1239
- // src/utils/unit/formatGwei.ts
1240
- function formatGwei(wei, unit = "wei") {
1241
- return formatUnit(wei, gweiUnits[unit]);
1242
- }
1243
-
1244
- // src/utils/unit/parseUnit.ts
1245
- function parseUnit(value, decimals) {
1246
- let [integer, fraction = "0"] = value.split(".");
1247
- const negative = integer.startsWith("-");
1248
- if (negative)
1249
- integer = integer.slice(1);
1250
- fraction = fraction.replace(/(0+)$/, "");
1251
- if (decimals === 0) {
1252
- integer = `${Math.round(Number(`${integer}.${fraction}`))}`;
1253
- fraction = "";
1254
- } else if (fraction.length > decimals) {
1255
- const [before, after] = [
1256
- fraction.slice(0, decimals),
1257
- fraction.slice(decimals)
1258
- ];
1259
- fraction = `${/^0+$/.test(before) ? before.slice(0, before.length - 1) : ""}${Math.round(Number(`${before}.${after}`))}`;
1260
- } else {
1261
- fraction = fraction.padEnd(decimals, "0");
1262
- }
1263
- return BigInt(`${negative ? "-" : ""}${integer}${fraction}`);
1264
- }
1265
-
1266
- // src/utils/unit/parseEther.ts
1267
- function parseEther(ether, unit = "wei") {
1268
- return parseUnit(ether, etherUnits[unit]);
1269
- }
1270
-
1271
- // src/utils/unit/parseGwei.ts
1272
- function parseGwei(ether, unit = "wei") {
1273
- return parseUnit(ether, gweiUnits[unit]);
1274
- }
1275
-
1276
- export {
1277
- isBytes,
1278
- isHex,
1279
- pad,
1280
- padHex,
1281
- padBytes,
1282
- trim,
1283
- size,
1284
- slice,
1285
- sliceBytes,
1286
- sliceHex,
1287
- boolToHex,
1288
- bytesToHex,
1289
- encodeHex,
1290
- numberToHex,
1291
- stringToHex,
1292
- boolToBytes,
1293
- encodeBytes,
1294
- hexToBytes,
1295
- numberToBytes,
1296
- stringToBytes,
1297
- encodeRlp,
1298
- decodeHex,
1299
- hexToBigInt,
1300
- hexToBool,
1301
- hexToNumber,
1302
- hexToString,
1303
- decodeBytes,
1304
- bytesToBigint,
1305
- bytesToBool,
1306
- bytesToNumber,
1307
- bytesToString,
1308
- decodeRlp,
1309
- extractFunctionName,
1310
- extractFunctionParams,
1311
- extractFunctionType,
1312
- keccak256,
1313
- getEventSignature,
1314
- getFunctionSignature,
1315
- checksumAddress,
1316
- getAddress,
1317
- getContractAddress,
1318
- getCreateAddress,
1319
- getCreate2Address,
1320
- isAddress,
1321
- isAddressEqual,
1322
- encodeAbi,
1323
- decodeAbi,
1324
- decodeErrorResult,
1325
- decodeFunctionData,
1326
- decodeFunctionResult,
1327
- encodeDeployData,
1328
- encodeErrorResult,
1329
- encodeEventTopics,
1330
- encodeFunctionData,
1331
- encodeFunctionResult,
1332
- etherUnits,
1333
- gweiUnits,
1334
- weiUnits,
1335
- transactionType,
1336
- formatTransaction,
1337
- formatBlock,
1338
- formatFeeHistory,
1339
- format3 as format,
1340
- formatLog,
1341
- formatTransactionReceipt,
1342
- formatTransactionRequest,
1343
- formatUnit,
1344
- formatEther,
1345
- formatGwei,
1346
- parseUnit,
1347
- parseEther,
1348
- parseGwei
1349
- };