viem 2.55.2 → 2.55.5
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/CHANGELOG.md +30 -0
- package/_cjs/actions/public/multicall.js +65 -18
- package/_cjs/actions/public/multicall.js.map +1 -1
- package/_cjs/actions/wallet/prepareTransactionRequest.js +8 -1
- package/_cjs/actions/wallet/prepareTransactionRequest.js.map +1 -1
- package/_cjs/chains/definitions/mainnet.js +1 -1
- package/_cjs/chains/definitions/mainnet.js.map +1 -1
- package/_cjs/chains/definitions/robinhood.js +1 -0
- package/_cjs/chains/definitions/robinhood.js.map +1 -1
- package/_cjs/chains/definitions/xLayer.js +1 -0
- package/_cjs/chains/definitions/xLayer.js.map +1 -1
- package/_cjs/errors/version.js +1 -1
- package/_cjs/tempo/Transaction.js +1 -1
- package/_cjs/tempo/Transaction.js.map +1 -1
- package/_cjs/tempo/chainConfig.js +9 -0
- package/_cjs/tempo/chainConfig.js.map +1 -1
- package/_cjs/utils/abi/decodeAbiParameters.js +1 -2
- package/_cjs/utils/abi/decodeAbiParameters.js.map +1 -1
- package/_cjs/utils/unit/Value.js +118 -0
- package/_cjs/utils/unit/Value.js.map +1 -0
- package/_cjs/utils/unit/formatEther.js +2 -3
- package/_cjs/utils/unit/formatEther.js.map +1 -1
- package/_cjs/utils/unit/formatGwei.js +2 -3
- package/_cjs/utils/unit/formatGwei.js.map +1 -1
- package/_cjs/utils/unit/formatUnits.js +2 -11
- package/_cjs/utils/unit/formatUnits.js.map +1 -1
- package/_cjs/utils/unit/parseEther.js +2 -3
- package/_cjs/utils/unit/parseEther.js.map +1 -1
- package/_cjs/utils/unit/parseGwei.js +2 -3
- package/_cjs/utils/unit/parseGwei.js.map +1 -1
- package/_cjs/utils/unit/parseUnits.js +2 -34
- package/_cjs/utils/unit/parseUnits.js.map +1 -1
- package/_esm/actions/public/multicall.js +65 -18
- package/_esm/actions/public/multicall.js.map +1 -1
- package/_esm/actions/wallet/prepareTransactionRequest.js +25 -1
- package/_esm/actions/wallet/prepareTransactionRequest.js.map +1 -1
- package/_esm/chains/definitions/mainnet.js +1 -1
- package/_esm/chains/definitions/mainnet.js.map +1 -1
- package/_esm/chains/definitions/robinhood.js +1 -0
- package/_esm/chains/definitions/robinhood.js.map +1 -1
- package/_esm/chains/definitions/xLayer.js +1 -0
- package/_esm/chains/definitions/xLayer.js.map +1 -1
- package/_esm/errors/version.js +1 -1
- package/_esm/tempo/Transaction.js +1 -1
- package/_esm/tempo/Transaction.js.map +1 -1
- package/_esm/tempo/chainConfig.js +10 -0
- package/_esm/tempo/chainConfig.js.map +1 -1
- package/_esm/utils/abi/decodeAbiParameters.js +1 -2
- package/_esm/utils/abi/decodeAbiParameters.js.map +1 -1
- package/_esm/utils/unit/Value.js +240 -0
- package/_esm/utils/unit/Value.js.map +1 -0
- package/_esm/utils/unit/formatEther.js +2 -3
- package/_esm/utils/unit/formatEther.js.map +1 -1
- package/_esm/utils/unit/formatGwei.js +2 -3
- package/_esm/utils/unit/formatGwei.js.map +1 -1
- package/_esm/utils/unit/formatUnits.js +2 -11
- package/_esm/utils/unit/formatUnits.js.map +1 -1
- package/_esm/utils/unit/parseEther.js +2 -3
- package/_esm/utils/unit/parseEther.js.map +1 -1
- package/_esm/utils/unit/parseGwei.js +2 -3
- package/_esm/utils/unit/parseGwei.js.map +1 -1
- package/_esm/utils/unit/parseUnits.js +2 -36
- package/_esm/utils/unit/parseUnits.js.map +1 -1
- package/_types/actions/public/multicall.d.ts +2 -1
- package/_types/actions/public/multicall.d.ts.map +1 -1
- package/_types/actions/wallet/prepareTransactionRequest.d.ts.map +1 -1
- package/_types/chains/definitions/mainnet.d.ts +1 -1
- package/_types/chains/definitions/robinhood.d.ts +1 -1
- package/_types/chains/definitions/robinhood.d.ts.map +1 -1
- package/_types/chains/definitions/tempo.d.ts +22 -22
- package/_types/chains/definitions/tempoDevnet.d.ts +22 -22
- package/_types/chains/definitions/tempoLocalnet.d.ts +22 -22
- package/_types/chains/definitions/tempoModerato.d.ts +22 -22
- package/_types/chains/definitions/xLayer.d.ts +1 -1
- package/_types/chains/definitions/xLayer.d.ts.map +1 -1
- package/_types/errors/version.d.ts +1 -1
- package/_types/tempo/chainConfig.d.ts +11 -11
- package/_types/tempo/chainConfig.d.ts.map +1 -1
- package/_types/tempo/zones/zone.d.ts +66 -66
- package/_types/utils/abi/decodeAbiParameters.d.ts +1 -2
- package/_types/utils/abi/decodeAbiParameters.d.ts.map +1 -1
- package/_types/utils/unit/Value.d.ts +115 -0
- package/_types/utils/unit/Value.d.ts.map +1 -0
- package/_types/utils/unit/formatEther.d.ts +1 -1
- package/_types/utils/unit/formatEther.d.ts.map +1 -1
- package/_types/utils/unit/formatGwei.d.ts +1 -1
- package/_types/utils/unit/formatGwei.d.ts.map +1 -1
- package/_types/utils/unit/formatUnits.d.ts.map +1 -1
- package/_types/utils/unit/parseEther.d.ts +1 -1
- package/_types/utils/unit/parseEther.d.ts.map +1 -1
- package/_types/utils/unit/parseGwei.d.ts +1 -1
- package/_types/utils/unit/parseGwei.d.ts.map +1 -1
- package/_types/utils/unit/parseUnits.d.ts.map +1 -1
- package/actions/public/multicall.ts +92 -12
- package/actions/wallet/prepareTransactionRequest.ts +28 -1
- package/chains/definitions/mainnet.ts +1 -1
- package/chains/definitions/robinhood.ts +1 -0
- package/chains/definitions/xLayer.ts +1 -0
- package/errors/version.ts +1 -1
- package/package.json +2 -2
- package/tempo/Transaction.ts +1 -1
- package/tempo/chainConfig.ts +10 -0
- package/utils/abi/decodeAbiParameters.ts +1 -3
- package/utils/unit/Value.ts +278 -0
- package/utils/unit/formatEther.ts +3 -4
- package/utils/unit/formatGwei.ts +3 -4
- package/utils/unit/formatUnits.ts +2 -15
- package/utils/unit/parseEther.ts +3 -4
- package/utils/unit/parseGwei.ts +3 -4
- package/utils/unit/parseUnits.ts +2 -40
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// Adapted from the Ox v1 `Value` module.
|
|
2
|
+
|
|
3
|
+
/** @see https://ethereum.github.io/yellowpaper/paper.pdf */
|
|
4
|
+
const exponents = {
|
|
5
|
+
wei: 0,
|
|
6
|
+
gwei: 9,
|
|
7
|
+
szabo: 12,
|
|
8
|
+
finney: 15,
|
|
9
|
+
ether: 18,
|
|
10
|
+
} as const
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Formats a `bigint` Value to its string representation (divided by the given exponent).
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts twoslash
|
|
17
|
+
* import { Value } from 'ox'
|
|
18
|
+
*
|
|
19
|
+
* Value.format(420_000_000_000n, 9)
|
|
20
|
+
* // @log: '420'
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param value - The `bigint` Value to format.
|
|
24
|
+
* @param decimals - The exponent to divide the `bigint` Value by.
|
|
25
|
+
* @returns The string representation of the Value.
|
|
26
|
+
*/
|
|
27
|
+
export function format(value: bigint, decimals = 0) {
|
|
28
|
+
if (!Number.isInteger(decimals) || decimals < 0)
|
|
29
|
+
throw new InvalidDecimalsError({ decimals })
|
|
30
|
+
|
|
31
|
+
let display = value.toString()
|
|
32
|
+
|
|
33
|
+
const negative = display.startsWith('-')
|
|
34
|
+
if (negative) display = display.slice(1)
|
|
35
|
+
|
|
36
|
+
display = display.padStart(decimals, '0')
|
|
37
|
+
|
|
38
|
+
let [integer, fraction] = [
|
|
39
|
+
display.slice(0, display.length - decimals),
|
|
40
|
+
display.slice(display.length - decimals),
|
|
41
|
+
]
|
|
42
|
+
fraction = fraction.replace(/(0+)$/, '')
|
|
43
|
+
return `${negative ? '-' : ''}${integer || '0'}${
|
|
44
|
+
fraction ? `.${fraction}` : ''
|
|
45
|
+
}`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export declare namespace format {
|
|
49
|
+
type ErrorType = Error
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Formats a `bigint` Value (default: wei) to a string representation of Ether.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts twoslash
|
|
57
|
+
* import { Value } from 'ox'
|
|
58
|
+
*
|
|
59
|
+
* Value.formatEther(1_000_000_000_000_000_000n)
|
|
60
|
+
* // @log: '1'
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @param wei - The Value to format.
|
|
64
|
+
* @param unit - The unit to format the Value in. @default 'wei'.
|
|
65
|
+
* @returns The Ether string representation of the Value.
|
|
66
|
+
*/
|
|
67
|
+
export function formatEther(
|
|
68
|
+
wei: bigint,
|
|
69
|
+
unit: 'wei' | 'gwei' | 'szabo' | 'finney' = 'wei',
|
|
70
|
+
) {
|
|
71
|
+
return format(wei, exponents.ether - exponents[unit])
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export declare namespace formatEther {
|
|
75
|
+
type ErrorType = format.ErrorType | Error
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Formats a `bigint` Value (default: wei) to a string representation of Gwei.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts twoslash
|
|
83
|
+
* import { Value } from 'ox'
|
|
84
|
+
*
|
|
85
|
+
* Value.formatGwei(1_000_000_000n)
|
|
86
|
+
* // @log: '1'
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @param wei - The Value to format.
|
|
90
|
+
* @param unit - The unit to format the Value in. @default 'wei'.
|
|
91
|
+
* @returns The Gwei string representation of the Value.
|
|
92
|
+
*/
|
|
93
|
+
export function formatGwei(wei: bigint, unit: 'wei' = 'wei') {
|
|
94
|
+
return format(wei, exponents.gwei - exponents[unit])
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export declare namespace formatGwei {
|
|
98
|
+
type ErrorType = format.ErrorType | Error
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Parses a `string` representation of a Value to `bigint` (multiplied by the given exponent).
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts twoslash
|
|
106
|
+
* import { Value } from 'ox'
|
|
107
|
+
*
|
|
108
|
+
* Value.from('420', 9)
|
|
109
|
+
* // @log: 420000000000n
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @param value - The string representation of the Value.
|
|
113
|
+
* @param decimals - The exponent to multiply the Value by.
|
|
114
|
+
* @returns The `bigint` representation of the Value.
|
|
115
|
+
*/
|
|
116
|
+
export function from(value: string, decimals = 0) {
|
|
117
|
+
if (!Number.isInteger(decimals) || decimals < 0)
|
|
118
|
+
throw new InvalidDecimalsError({ decimals })
|
|
119
|
+
|
|
120
|
+
// Require at least one digit overall. Rejects '', '.', '-', '-.' which
|
|
121
|
+
// the previous regex accepted (and which then either produced garbage or
|
|
122
|
+
// threw a raw `SyntaxError` from `BigInt('')`).
|
|
123
|
+
if (!/^-?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)$/.test(value))
|
|
124
|
+
throw new InvalidDecimalNumberError({ value })
|
|
125
|
+
|
|
126
|
+
let [integer = '', fraction = '0'] = value.split('.')
|
|
127
|
+
|
|
128
|
+
const negative = integer.startsWith('-')
|
|
129
|
+
if (negative) integer = integer.slice(1)
|
|
130
|
+
if (integer === '') integer = '0'
|
|
131
|
+
|
|
132
|
+
// trim trailing zeros.
|
|
133
|
+
fraction = fraction.replace(/(0+)$/, '')
|
|
134
|
+
|
|
135
|
+
// round off if the fraction is larger than the number of decimals.
|
|
136
|
+
if (decimals === 0) {
|
|
137
|
+
// Round half-away-from-zero by inspecting the first fractional digit.
|
|
138
|
+
if (fraction.length > 0 && Number.parseInt(fraction[0]!, 10) >= 5)
|
|
139
|
+
integer = `${BigInt(integer) + 1n}`
|
|
140
|
+
fraction = ''
|
|
141
|
+
} else if (fraction.length > decimals) {
|
|
142
|
+
const left = fraction.slice(0, decimals)
|
|
143
|
+
const roundDigit = Number.parseInt(
|
|
144
|
+
fraction.slice(decimals, decimals + 1)!,
|
|
145
|
+
10,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
if (roundDigit >= 5) {
|
|
149
|
+
// Carry through the truncated fraction digits into the integer part
|
|
150
|
+
// without converting to a JS Number (avoids float precision loss).
|
|
151
|
+
const carried = carry(left)
|
|
152
|
+
if (carried.length > decimals) {
|
|
153
|
+
// Carry overflowed into the integer part.
|
|
154
|
+
fraction = carried.slice(1)
|
|
155
|
+
integer = `${BigInt(integer) + 1n}`
|
|
156
|
+
} else {
|
|
157
|
+
fraction = carried
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
fraction = left
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
fraction = fraction.padEnd(decimals, '0')
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return BigInt(`${negative ? '-' : ''}${integer}${fraction}`)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Adds 1 to a digit string with carry, returning a string of the same length
|
|
171
|
+
* unless the carry overflows past the most-significant digit (in which case
|
|
172
|
+
* the returned string is one digit longer).
|
|
173
|
+
*
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
function carry(digits: string): string {
|
|
177
|
+
const out = digits.split('')
|
|
178
|
+
let i = out.length - 1
|
|
179
|
+
while (i >= 0) {
|
|
180
|
+
const d = Number.parseInt(out[i]!, 10) + 1
|
|
181
|
+
if (d < 10) {
|
|
182
|
+
out[i] = String(d)
|
|
183
|
+
return out.join('')
|
|
184
|
+
}
|
|
185
|
+
out[i] = '0'
|
|
186
|
+
i--
|
|
187
|
+
}
|
|
188
|
+
return `1${out.join('')}`
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export declare namespace from {
|
|
192
|
+
type ErrorType = Error
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Parses a string representation of Ether to a `bigint` Value (default: wei).
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```ts twoslash
|
|
200
|
+
* import { Value } from 'ox'
|
|
201
|
+
*
|
|
202
|
+
* Value.fromEther('420')
|
|
203
|
+
* // @log: 420000000000000000000n
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* @param ether - String representation of Ether.
|
|
207
|
+
* @param unit - The unit to parse to. @default 'wei'.
|
|
208
|
+
* @returns A `bigint` Value.
|
|
209
|
+
*/
|
|
210
|
+
export function fromEther(
|
|
211
|
+
ether: string,
|
|
212
|
+
unit: 'wei' | 'gwei' | 'szabo' | 'finney' = 'wei',
|
|
213
|
+
) {
|
|
214
|
+
return from(ether, exponents.ether - exponents[unit])
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export declare namespace fromEther {
|
|
218
|
+
type ErrorType = from.ErrorType | Error
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Parses a string representation of Gwei to a `bigint` Value (default: wei).
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts twoslash
|
|
226
|
+
* import { Value } from 'ox'
|
|
227
|
+
*
|
|
228
|
+
* Value.fromGwei('420')
|
|
229
|
+
* // @log: 420000000000n
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* @param gwei - String representation of Gwei.
|
|
233
|
+
* @param unit - The unit to parse to. @default 'wei'.
|
|
234
|
+
* @returns A `bigint` Value.
|
|
235
|
+
*/
|
|
236
|
+
export function fromGwei(gwei: string, unit: 'wei' = 'wei') {
|
|
237
|
+
return from(gwei, exponents.gwei - exponents[unit])
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export declare namespace fromGwei {
|
|
241
|
+
type ErrorType = from.ErrorType | Error
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Thrown when a value is not a valid decimal number.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts twoslash
|
|
249
|
+
* import { Value } from 'ox'
|
|
250
|
+
*
|
|
251
|
+
* Value.fromEther('123.456.789')
|
|
252
|
+
* // @error: Value.InvalidDecimalNumberError: Value `123.456.789` is not a valid decimal number.
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
class InvalidDecimalNumberError extends Error {
|
|
256
|
+
override readonly name = 'Value.InvalidDecimalNumberError'
|
|
257
|
+
constructor({ value }: { value: string }) {
|
|
258
|
+
super(`Value \`${value}\` is not a valid decimal number.`)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Thrown when the `decimals` argument is not a non-negative integer.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts twoslash
|
|
267
|
+
* import { Value } from 'ox'
|
|
268
|
+
*
|
|
269
|
+
* Value.from('1', -1)
|
|
270
|
+
* // @error: Value.InvalidDecimalsError: `decimals` must be a non-negative integer. Got `-1`.
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
class InvalidDecimalsError extends Error {
|
|
274
|
+
override readonly name = 'Value.InvalidDecimalsError'
|
|
275
|
+
constructor({ decimals }: { decimals: number }) {
|
|
276
|
+
super(`\`decimals\` must be a non-negative integer. Got \`${decimals}\`.`)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { type FormatUnitsErrorType, formatUnits } from './formatUnits.js'
|
|
1
|
+
import type { FormatUnitsErrorType } from './formatUnits.js'
|
|
2
|
+
import * as Value from './Value.js'
|
|
4
3
|
|
|
5
4
|
export type FormatEtherErrorType = FormatUnitsErrorType
|
|
6
5
|
|
|
@@ -16,5 +15,5 @@ export type FormatEtherErrorType = FormatUnitsErrorType
|
|
|
16
15
|
* // '1'
|
|
17
16
|
*/
|
|
18
17
|
export function formatEther(wei: bigint, unit: 'wei' | 'gwei' = 'wei') {
|
|
19
|
-
return
|
|
18
|
+
return Value.formatEther(wei, unit)
|
|
20
19
|
}
|
package/utils/unit/formatGwei.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { type FormatUnitsErrorType, formatUnits } from './formatUnits.js'
|
|
1
|
+
import type { FormatUnitsErrorType } from './formatUnits.js'
|
|
2
|
+
import * as Value from './Value.js'
|
|
4
3
|
|
|
5
4
|
export type FormatGweiErrorType = FormatUnitsErrorType
|
|
6
5
|
|
|
@@ -16,5 +15,5 @@ export type FormatGweiErrorType = FormatUnitsErrorType
|
|
|
16
15
|
* // '1'
|
|
17
16
|
*/
|
|
18
17
|
export function formatGwei(wei: bigint, unit: 'wei' = 'wei') {
|
|
19
|
-
return
|
|
18
|
+
return Value.formatGwei(wei, unit)
|
|
20
19
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ErrorType } from '../../errors/utils.js'
|
|
2
|
+
import * as Value from './Value.js'
|
|
2
3
|
|
|
3
4
|
export type FormatUnitsErrorType = ErrorType
|
|
4
5
|
|
|
@@ -14,19 +15,5 @@ export type FormatUnitsErrorType = ErrorType
|
|
|
14
15
|
* // '420'
|
|
15
16
|
*/
|
|
16
17
|
export function formatUnits(value: bigint, decimals: number) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const negative = display.startsWith('-')
|
|
20
|
-
if (negative) display = display.slice(1)
|
|
21
|
-
|
|
22
|
-
display = display.padStart(decimals, '0')
|
|
23
|
-
|
|
24
|
-
let [integer, fraction] = [
|
|
25
|
-
display.slice(0, display.length - decimals),
|
|
26
|
-
display.slice(display.length - decimals),
|
|
27
|
-
]
|
|
28
|
-
fraction = fraction.replace(/(0+)$/, '')
|
|
29
|
-
return `${negative ? '-' : ''}${integer || '0'}${
|
|
30
|
-
fraction ? `.${fraction}` : ''
|
|
31
|
-
}`
|
|
18
|
+
return Value.format(value, decimals)
|
|
32
19
|
}
|
package/utils/unit/parseEther.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { etherUnits } from '../../constants/unit.js'
|
|
2
1
|
import type { ErrorType } from '../../errors/utils.js'
|
|
3
|
-
|
|
4
|
-
import
|
|
2
|
+
import type { ParseUnitsErrorType } from './parseUnits.js'
|
|
3
|
+
import * as Value from './Value.js'
|
|
5
4
|
|
|
6
5
|
export type ParseEtherErrorType = ParseUnitsErrorType | ErrorType
|
|
7
6
|
|
|
@@ -17,5 +16,5 @@ export type ParseEtherErrorType = ParseUnitsErrorType | ErrorType
|
|
|
17
16
|
* // 420000000000000000000n
|
|
18
17
|
*/
|
|
19
18
|
export function parseEther(ether: string, unit: 'wei' | 'gwei' = 'wei') {
|
|
20
|
-
return
|
|
19
|
+
return Value.fromEther(ether, unit)
|
|
21
20
|
}
|
package/utils/unit/parseGwei.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { gweiUnits } from '../../constants/unit.js'
|
|
2
1
|
import type { ErrorType } from '../../errors/utils.js'
|
|
3
|
-
|
|
4
|
-
import
|
|
2
|
+
import type { ParseUnitsErrorType } from './parseUnits.js'
|
|
3
|
+
import * as Value from './Value.js'
|
|
5
4
|
|
|
6
5
|
export type ParseGweiErrorType = ParseUnitsErrorType | ErrorType
|
|
7
6
|
|
|
@@ -17,5 +16,5 @@ export type ParseGweiErrorType = ParseUnitsErrorType | ErrorType
|
|
|
17
16
|
* // 420000000000n
|
|
18
17
|
*/
|
|
19
18
|
export function parseGwei(ether: string, unit: 'wei' = 'wei') {
|
|
20
|
-
return
|
|
19
|
+
return Value.fromGwei(ether, unit)
|
|
21
20
|
}
|
package/utils/unit/parseUnits.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { InvalidDecimalNumberError } from '../../errors/unit.js'
|
|
2
1
|
import type { ErrorType } from '../../errors/utils.js'
|
|
2
|
+
import * as Value from './Value.js'
|
|
3
3
|
|
|
4
4
|
export type ParseUnitsErrorType = ErrorType
|
|
5
5
|
|
|
@@ -15,43 +15,5 @@ export type ParseUnitsErrorType = ErrorType
|
|
|
15
15
|
* // 420000000000n
|
|
16
16
|
*/
|
|
17
17
|
export function parseUnits(value: string, decimals: number) {
|
|
18
|
-
|
|
19
|
-
throw new InvalidDecimalNumberError({ value })
|
|
20
|
-
|
|
21
|
-
let [integer, fraction = '0'] = value.split('.')
|
|
22
|
-
|
|
23
|
-
const negative = integer.startsWith('-')
|
|
24
|
-
if (negative) integer = integer.slice(1)
|
|
25
|
-
|
|
26
|
-
// trim trailing zeros.
|
|
27
|
-
fraction = fraction.replace(/(0+)$/, '')
|
|
28
|
-
|
|
29
|
-
// round off if the fraction is larger than the number of decimals.
|
|
30
|
-
if (decimals === 0) {
|
|
31
|
-
if (Math.round(Number(`.${fraction}`)) === 1)
|
|
32
|
-
integer = `${BigInt(integer) + 1n}`
|
|
33
|
-
fraction = ''
|
|
34
|
-
} else if (fraction.length > decimals) {
|
|
35
|
-
const [left, unit, right] = [
|
|
36
|
-
fraction.slice(0, decimals - 1),
|
|
37
|
-
fraction.slice(decimals - 1, decimals),
|
|
38
|
-
fraction.slice(decimals),
|
|
39
|
-
]
|
|
40
|
-
|
|
41
|
-
const rounded = Math.round(Number(`${unit}.${right}`))
|
|
42
|
-
if (rounded > 9)
|
|
43
|
-
fraction = `${BigInt(left) + BigInt(1)}0`.padStart(left.length + 1, '0')
|
|
44
|
-
else fraction = `${left}${rounded}`
|
|
45
|
-
|
|
46
|
-
if (fraction.length > decimals) {
|
|
47
|
-
fraction = fraction.slice(1)
|
|
48
|
-
integer = `${BigInt(integer) + 1n}`
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
fraction = fraction.slice(0, decimals)
|
|
52
|
-
} else {
|
|
53
|
-
fraction = fraction.padEnd(decimals, '0')
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return BigInt(`${negative ? '-' : ''}${integer}${fraction}`)
|
|
18
|
+
return Value.from(value, decimals)
|
|
57
19
|
}
|