essential-eth 0.11.2 → 1.0.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.
@@ -0,0 +1,96 @@
1
+ // src/shared/validate-type.ts
2
+ var validateType = (value, allowedTypes) => {
3
+ if (!allowedTypes.includes(typeof value)) {
4
+ throw new Error(
5
+ `${allowedTypes.join(" or ")} required. Received ${typeof value}`
6
+ );
7
+ }
8
+ };
9
+
10
+ // src/utils/fixed-point.ts
11
+ function parseFixed(value, decimals) {
12
+ let negative = false;
13
+ if (value.startsWith("-")) {
14
+ negative = true;
15
+ value = value.substring(1);
16
+ }
17
+ if (value === "") {
18
+ throw new Error("invalid decimal value");
19
+ }
20
+ const parts = value.split(".");
21
+ if (parts.length > 2) {
22
+ throw new Error("too many decimal points");
23
+ }
24
+ const integer = parts[0] || "0";
25
+ let fraction = parts[1] || "";
26
+ if (fraction.length > decimals) {
27
+ const extra = fraction.substring(decimals);
28
+ if (extra.match(/[^0]/)) {
29
+ throw new Error("fractional component exceeds decimals");
30
+ }
31
+ fraction = fraction.substring(0, decimals);
32
+ }
33
+ while (fraction.length < decimals) {
34
+ fraction += "0";
35
+ }
36
+ const result = BigInt(integer + fraction);
37
+ return negative ? -result : result;
38
+ }
39
+ function formatFixed(value, decimals) {
40
+ let negative = "";
41
+ if (value < 0n) {
42
+ negative = "-";
43
+ value = -value;
44
+ }
45
+ let str = value.toString();
46
+ while (str.length <= decimals) {
47
+ str = "0" + str;
48
+ }
49
+ const integerPart = str.substring(0, str.length - decimals);
50
+ const fractionPart = str.substring(str.length - decimals);
51
+ const trimmedFraction = fractionPart.replace(/0+$/, "");
52
+ if (trimmedFraction) {
53
+ return `${negative}${integerPart}.${trimmedFraction}`;
54
+ }
55
+ return `${negative}${integerPart}`;
56
+ }
57
+ function toBigInt(value) {
58
+ if (typeof value === "bigint")
59
+ return value;
60
+ if (typeof value === "string") {
61
+ if (value.startsWith("0x")) {
62
+ return BigInt(value);
63
+ }
64
+ if (value.includes(".")) {
65
+ return BigInt(value.split(".")[0] || "0");
66
+ }
67
+ return BigInt(value);
68
+ }
69
+ return BigInt(value);
70
+ }
71
+
72
+ // src/utils/ether-to-gwei.ts
73
+ function etherToGwei(etherQuantity) {
74
+ validateType(etherQuantity, ["string", "number", "bigint"]);
75
+ return parseFixed(String(etherQuantity), 9);
76
+ }
77
+
78
+ // src/utils/ether-to-wei.ts
79
+ function etherToWei(etherQuantity) {
80
+ validateType(etherQuantity, ["string", "number", "bigint"]);
81
+ return parseFixed(String(etherQuantity), 18);
82
+ }
83
+
84
+ // src/utils/gwei-to-ether.ts
85
+ function gweiToEther(gweiQuantity) {
86
+ validateType(gweiQuantity, ["string", "number", "bigint"]);
87
+ return formatFixed(toBigInt(gweiQuantity), 9);
88
+ }
89
+
90
+ // src/utils/wei-to-ether.ts
91
+ function weiToEther(weiQuantity) {
92
+ validateType(weiQuantity, ["string", "number", "bigint"]);
93
+ return formatFixed(toBigInt(weiQuantity), 18);
94
+ }
95
+
96
+ export { etherToGwei, etherToWei, formatFixed, gweiToEther, parseFixed, toBigInt, validateType, weiToEther };
@@ -0,0 +1,9 @@
1
+ declare function etherToGwei(etherQuantity: string | number | bigint): bigint;
2
+
3
+ declare function etherToWei(etherQuantity: string | number | bigint): bigint;
4
+
5
+ declare function gweiToEther(gweiQuantity: string | number | bigint): string;
6
+
7
+ declare function weiToEther(weiQuantity: string | number | bigint): string;
8
+
9
+ export { etherToGwei, etherToWei, gweiToEther, weiToEther };
@@ -0,0 +1 @@
1
+ export { etherToGwei, etherToWei, gweiToEther, weiToEther } from './chunk-GFWRB7PT.js';
@@ -0,0 +1,63 @@
1
+ import './conversions.js';
2
+
3
+ type Bytes = ArrayLike<number>;
4
+ type BytesLike = Bytes | string;
5
+ type BytesLikeWithNumber = BytesLike | number;
6
+ interface DataOptions {
7
+ allowMissingPrefix?: boolean;
8
+ hexPad?: 'left' | 'right' | null;
9
+ }
10
+ interface Hexable {
11
+ toHexString(): string;
12
+ }
13
+ type SignatureLike = {
14
+ r: string;
15
+ s?: string;
16
+ _vs?: string;
17
+ recoveryParam?: number;
18
+ v?: number;
19
+ } | BytesLike;
20
+ interface Signature {
21
+ r: string;
22
+ s: string;
23
+ _vs: string;
24
+ recoveryParam: number;
25
+ v: number;
26
+ yParityAndS: string;
27
+ compact: string;
28
+ }
29
+ declare function isBytesLike(value: any): value is BytesLike;
30
+ declare function isBytes(value: any): value is Bytes;
31
+ declare function arrayify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): Uint8Array;
32
+ declare function concat(arrayOfBytesLike: ReadonlyArray<BytesLikeWithNumber>): Uint8Array;
33
+ declare function stripZeros(value: BytesLike): Uint8Array;
34
+ declare function zeroPad(value: BytesLike, length: number): Uint8Array;
35
+ declare function isHexString(value: any, length?: number): boolean;
36
+ declare function hexlify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): string;
37
+ declare function hexDataLength(data: BytesLike): number | null;
38
+ declare function hexDataSlice(data: BytesLikeWithNumber, offset: number, endOffset?: number): string;
39
+ declare function hexConcat(items: ReadonlyArray<BytesLike>): string;
40
+ declare function hexValue(value: BytesLike | Hexable | number | bigint): string;
41
+ declare function hexStripZeros(value: BytesLike): string;
42
+ declare function hexZeroPad(value: BytesLikeWithNumber, length: number): string;
43
+
44
+ declare function computeAddress(key: string): string;
45
+
46
+ declare function computePublicKey(privKey: BytesLike): string;
47
+
48
+ declare function hashMessage(message: Bytes | string): string;
49
+
50
+ declare function isAddress(address: string): boolean;
51
+
52
+ declare function keccak256(data: BytesLike): string;
53
+
54
+ declare function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
55
+ declare function solidityKeccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;
56
+
57
+ declare function splitSignature(signature: SignatureLike): Signature;
58
+
59
+ declare function toChecksumAddress(address: string): string;
60
+
61
+ declare function toUtf8Bytes(data: string): Uint8Array;
62
+
63
+ export { toUtf8Bytes as A, BytesLike as B, DataOptions as D, Hexable as H, Signature as S, arrayify as a, Bytes as b, BytesLikeWithNumber as c, concat as d, hexDataLength as e, hexDataSlice as f, hexlify as g, hexConcat as h, hexStripZeros as i, hexValue as j, hexZeroPad as k, isBytes as l, isBytesLike as m, isHexString as n, SignatureLike as o, computeAddress as p, computePublicKey as q, hashMessage as r, stripZeros as s, isAddress as t, keccak256 as u, pack as v, solidityKeccak256 as w, splitSignature as x, toChecksumAddress as y, zeroPad as z };