essential-eth 0.13.0 → 1.1.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.
@@ -1,210 +0,0 @@
1
- import Big from 'big.js';
2
-
3
- // src/shared/tiny-big/tiny-big.ts
4
-
5
- // src/classes/utils/hex-to-decimal.ts
6
- function hexToDecimal(hex) {
7
- return BigInt(hex).toString();
8
- }
9
-
10
- // src/shared/tiny-big/helpers.ts
11
- function stripTrailingZeroes(numberString) {
12
- const isNegative = numberString.startsWith("-");
13
- numberString = numberString.replace("-", "");
14
- numberString = numberString.replace(
15
- /\.0*$/g,
16
- ""
17
- );
18
- numberString = numberString.replace(/^0+/, "");
19
- if (numberString.includes(".")) {
20
- numberString = numberString.replace(/0+$/, "");
21
- }
22
- if (numberString.startsWith(".")) {
23
- numberString = `0${numberString}`;
24
- }
25
- return `${isNegative ? "-" : ""}${numberString}`;
26
- }
27
- function scientificStrToDecimalStr(scientificString) {
28
- if (!scientificString.match(
29
- /e/i
30
- /* lowercase and uppercase E */
31
- )) {
32
- return stripTrailingZeroes(scientificString);
33
- }
34
- let [base, power] = scientificString.split(
35
- /e/i
36
- );
37
- const isNegative = Number(base) < 0;
38
- base = base.replace("-", "");
39
- base = stripTrailingZeroes(base);
40
- const [wholeNumber, fraction = ""] = base.split(".");
41
- if (Number(power) === 0) {
42
- return `${isNegative ? "-" : ""}${stripTrailingZeroes(base)}`;
43
- } else {
44
- const includesDecimal = base.includes(".");
45
- if (!includesDecimal) {
46
- base = `${base}.`;
47
- }
48
- base = base.replace(".", "");
49
- const baseLength = base.length;
50
- let splitPaddedNumber;
51
- if (Number(power) < 0) {
52
- if (wholeNumber.length < Math.abs(Number(power))) {
53
- base = base.padStart(
54
- baseLength + Math.abs(Number(power)) - wholeNumber.length,
55
- "0"
56
- );
57
- }
58
- splitPaddedNumber = base.split("");
59
- if (wholeNumber.length < Math.abs(Number(power))) {
60
- splitPaddedNumber = [".", ...splitPaddedNumber];
61
- } else {
62
- splitPaddedNumber.splice(
63
- splitPaddedNumber.length - Math.abs(Number(power)),
64
- 0,
65
- "."
66
- );
67
- }
68
- } else {
69
- if (fraction.length < Math.abs(Number(power))) {
70
- base = base.padEnd(
71
- baseLength + Math.abs(Number(power)) - fraction.length,
72
- "0"
73
- );
74
- }
75
- splitPaddedNumber = base.split("");
76
- if (fraction.length > Math.abs(Number(power))) {
77
- splitPaddedNumber.splice(
78
- splitPaddedNumber.length - Math.abs(Number(power)),
79
- 0,
80
- "."
81
- );
82
- }
83
- }
84
- const toReturn = stripTrailingZeroes(splitPaddedNumber.join(""));
85
- return `${isNegative ? "-" : ""}${toReturn}`;
86
- }
87
- }
88
-
89
- // src/shared/tiny-big/tiny-big.ts
90
- var TinyBig = class extends Big {
91
- constructor(value) {
92
- if (typeof value === "string" && value.startsWith("0x")) {
93
- value = hexToDecimal(value);
94
- }
95
- super(value);
96
- }
97
- /**
98
- * Used anytime you're passing in "value" to ethers or web3
99
- * For now, TypeScript will complain that `TinyBig` is not a `BigNumberish`. You can // @ts-ignore or call this
100
- *
101
- * @returns the TinyBig represented as a hex string
102
- * @example
103
- * ```javascript
104
- * tinyBig(293).toHexString();
105
- * // '0x125'
106
- * ```
107
- * @example
108
- * ```javascript
109
- * tinyBig(681365874).toHexString();
110
- * // '0x289cd172'
111
- */
112
- toHexString() {
113
- return `0x${BigInt(this.toString()).toString(16)}`;
114
- }
115
- toNumber() {
116
- return Number(scientificStrToDecimalStr(super.toString()));
117
- }
118
- toString() {
119
- if (this.toNumber() === 0) {
120
- return "0";
121
- }
122
- return scientificStrToDecimalStr(super.toString());
123
- }
124
- /**
125
- * Eithers pads or shortens a string to a specified length
126
- *
127
- * @param str the string to pad or chop
128
- * @param padChar the character to pad the string with
129
- * @param length the desired length of the given string
130
- * @returns a string of the desired length, either padded with the specified padChar or with the beginning of the string chopped off
131
- * @example
132
- * ```javascript
133
- * padAndChop('essential-eth', 'a', 8);
134
- * // 'tial-eth'
135
- * ```
136
- * @example
137
- * ```javascript
138
- * padAndChop('essential-eth', 'A', 20);
139
- * // 'AAAAAAAessential-eth'
140
- * ```
141
- */
142
- padAndChop = (str, padChar, length) => {
143
- return (Array(length).fill(padChar).join("") + str).slice(length * -1);
144
- };
145
- toTwos(bitCount) {
146
- let binaryStr;
147
- if (this.gte(0)) {
148
- const twosComp = this.toNumber().toString(2);
149
- binaryStr = this.padAndChop(twosComp, "0", bitCount || twosComp.length);
150
- } else {
151
- binaryStr = this.plus(Math.pow(2, bitCount)).toNumber().toString(2);
152
- if (Number(binaryStr) < 0) {
153
- throw new Error("Cannot calculate twos complement");
154
- }
155
- }
156
- const binary = `0b${binaryStr}`;
157
- const decimal = Number(binary);
158
- return tinyBig(decimal);
159
- }
160
- };
161
- function tinyBig(value) {
162
- return new TinyBig(value);
163
- }
164
-
165
- // src/shared/validate-type.ts
166
- var validateType = (value, allowedTypes) => {
167
- if (!allowedTypes.includes(typeof value)) {
168
- throw new Error(
169
- `${allowedTypes.join(" or ")} required. Received ${typeof value}`
170
- );
171
- }
172
- };
173
-
174
- // src/utils/ether-to-gwei.ts
175
- function etherToGwei(etherQuantity) {
176
- validateType(etherQuantity, ["string", "number", "object"]);
177
- const result = tinyBig(etherQuantity).times("1000000000");
178
- return tinyBig(result);
179
- }
180
-
181
- // src/utils/ether-to-wei.ts
182
- function etherToWei(etherQuantity) {
183
- validateType(etherQuantity, ["string", "number", "object"]);
184
- const result = tinyBig(etherQuantity).times("1000000000000000000");
185
- return tinyBig(result);
186
- }
187
-
188
- // src/utils/gwei-to-ether.ts
189
- function gweiToEther(gweiQuantity) {
190
- validateType(gweiQuantity, ["string", "number", "object"]);
191
- const result = tinyBig(gweiQuantity).div("1000000000");
192
- return tinyBig(result);
193
- }
194
-
195
- // src/utils/wei-to-ether.ts
196
- function weiToEther(weiQuantity) {
197
- validateType(weiQuantity, ["string", "number", "object"]);
198
- try {
199
- let _weiQuantity = weiQuantity;
200
- if (typeof weiQuantity === "string" && weiQuantity.slice(0, 2) === "0x") {
201
- _weiQuantity = BigInt(weiQuantity).toString();
202
- }
203
- const result = tinyBig(_weiQuantity).div("1000000000000000000");
204
- return tinyBig(result);
205
- } catch (error) {
206
- throw error;
207
- }
208
- }
209
-
210
- export { TinyBig, etherToGwei, etherToWei, gweiToEther, hexToDecimal, tinyBig, validateType, weiToEther };