viem 2.55.2 → 2.55.4
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 +18 -0
- 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/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/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/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/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/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.map +1 -1
- 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/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 +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,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a `bigint` Value to its string representation (divided by the given exponent).
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts twoslash
|
|
6
|
+
* import { Value } from 'ox'
|
|
7
|
+
*
|
|
8
|
+
* Value.format(420_000_000_000n, 9)
|
|
9
|
+
* // @log: '420'
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @param value - The `bigint` Value to format.
|
|
13
|
+
* @param decimals - The exponent to divide the `bigint` Value by.
|
|
14
|
+
* @returns The string representation of the Value.
|
|
15
|
+
*/
|
|
16
|
+
export declare function format(value: bigint, decimals?: number): string;
|
|
17
|
+
export declare namespace format {
|
|
18
|
+
type ErrorType = Error;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Formats a `bigint` Value (default: wei) to a string representation of Ether.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts twoslash
|
|
25
|
+
* import { Value } from 'ox'
|
|
26
|
+
*
|
|
27
|
+
* Value.formatEther(1_000_000_000_000_000_000n)
|
|
28
|
+
* // @log: '1'
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param wei - The Value to format.
|
|
32
|
+
* @param unit - The unit to format the Value in. @default 'wei'.
|
|
33
|
+
* @returns The Ether string representation of the Value.
|
|
34
|
+
*/
|
|
35
|
+
export declare function formatEther(wei: bigint, unit?: 'wei' | 'gwei' | 'szabo' | 'finney'): string;
|
|
36
|
+
export declare namespace formatEther {
|
|
37
|
+
type ErrorType = format.ErrorType | Error;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Formats a `bigint` Value (default: wei) to a string representation of Gwei.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts twoslash
|
|
44
|
+
* import { Value } from 'ox'
|
|
45
|
+
*
|
|
46
|
+
* Value.formatGwei(1_000_000_000n)
|
|
47
|
+
* // @log: '1'
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @param wei - The Value to format.
|
|
51
|
+
* @param unit - The unit to format the Value in. @default 'wei'.
|
|
52
|
+
* @returns The Gwei string representation of the Value.
|
|
53
|
+
*/
|
|
54
|
+
export declare function formatGwei(wei: bigint, unit?: 'wei'): string;
|
|
55
|
+
export declare namespace formatGwei {
|
|
56
|
+
type ErrorType = format.ErrorType | Error;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Parses a `string` representation of a Value to `bigint` (multiplied by the given exponent).
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts twoslash
|
|
63
|
+
* import { Value } from 'ox'
|
|
64
|
+
*
|
|
65
|
+
* Value.from('420', 9)
|
|
66
|
+
* // @log: 420000000000n
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param value - The string representation of the Value.
|
|
70
|
+
* @param decimals - The exponent to multiply the Value by.
|
|
71
|
+
* @returns The `bigint` representation of the Value.
|
|
72
|
+
*/
|
|
73
|
+
export declare function from(value: string, decimals?: number): bigint;
|
|
74
|
+
export declare namespace from {
|
|
75
|
+
type ErrorType = Error;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Parses a string representation of Ether to a `bigint` Value (default: wei).
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts twoslash
|
|
82
|
+
* import { Value } from 'ox'
|
|
83
|
+
*
|
|
84
|
+
* Value.fromEther('420')
|
|
85
|
+
* // @log: 420000000000000000000n
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param ether - String representation of Ether.
|
|
89
|
+
* @param unit - The unit to parse to. @default 'wei'.
|
|
90
|
+
* @returns A `bigint` Value.
|
|
91
|
+
*/
|
|
92
|
+
export declare function fromEther(ether: string, unit?: 'wei' | 'gwei' | 'szabo' | 'finney'): bigint;
|
|
93
|
+
export declare namespace fromEther {
|
|
94
|
+
type ErrorType = from.ErrorType | Error;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Parses a string representation of Gwei to a `bigint` Value (default: wei).
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts twoslash
|
|
101
|
+
* import { Value } from 'ox'
|
|
102
|
+
*
|
|
103
|
+
* Value.fromGwei('420')
|
|
104
|
+
* // @log: 420000000000n
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @param gwei - String representation of Gwei.
|
|
108
|
+
* @param unit - The unit to parse to. @default 'wei'.
|
|
109
|
+
* @returns A `bigint` Value.
|
|
110
|
+
*/
|
|
111
|
+
export declare function fromGwei(gwei: string, unit?: 'wei'): bigint;
|
|
112
|
+
export declare namespace fromGwei {
|
|
113
|
+
type ErrorType = from.ErrorType | Error;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=Value.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Value.d.ts","sourceRoot":"","sources":["../../../utils/unit/Value.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,SAAI,UAmBjD;AAED,MAAM,CAAC,OAAO,WAAW,MAAM,CAAC;IAC9B,KAAK,SAAS,GAAG,KAAK,CAAA;CACvB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE,MAAM,EACX,IAAI,GAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAgB,UAGlD;AAED,MAAM,CAAC,OAAO,WAAW,WAAW,CAAC;IACnC,KAAK,SAAS,GAAG,MAAM,CAAC,SAAS,GAAG,KAAK,CAAA;CAC1C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,KAAa,UAE1D;AAED,MAAM,CAAC,OAAO,WAAW,UAAU,CAAC;IAClC,KAAK,SAAS,GAAG,MAAM,CAAC,SAAS,GAAG,KAAK,CAAA;CAC1C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,SAAI,UAmD/C;AAwBD,MAAM,CAAC,OAAO,WAAW,IAAI,CAAC;IAC5B,KAAK,SAAS,GAAG,KAAK,CAAA;CACvB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAgB,UAGlD;AAED,MAAM,CAAC,OAAO,WAAW,SAAS,CAAC;IACjC,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;CACxC;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,KAAa,UAEzD;AAED,MAAM,CAAC,OAAO,WAAW,QAAQ,CAAC;IAChC,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;CACxC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatEther.d.ts","sourceRoot":"","sources":["../../../utils/unit/formatEther.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"formatEther.d.ts","sourceRoot":"","sources":["../../../utils/unit/formatEther.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAG5D,MAAM,MAAM,oBAAoB,GAAG,oBAAoB,CAAA;AAEvD;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,KAAK,GAAG,MAAc,UAEpE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatGwei.d.ts","sourceRoot":"","sources":["../../../utils/unit/formatGwei.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"formatGwei.d.ts","sourceRoot":"","sources":["../../../utils/unit/formatGwei.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAG5D,MAAM,MAAM,mBAAmB,GAAG,oBAAoB,CAAA;AAEtD;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,KAAa,UAE1D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatUnits.d.ts","sourceRoot":"","sources":["../../../utils/unit/formatUnits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;
|
|
1
|
+
{"version":3,"file":"formatUnits.d.ts","sourceRoot":"","sources":["../../../utils/unit/formatUnits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAGtD,MAAM,MAAM,oBAAoB,GAAG,SAAS,CAAA;AAE5C;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,UAE1D"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ErrorType } from '../../errors/utils.js';
|
|
2
|
-
import {
|
|
2
|
+
import type { ParseUnitsErrorType } from './parseUnits.js';
|
|
3
3
|
export type ParseEtherErrorType = ParseUnitsErrorType | ErrorType;
|
|
4
4
|
/**
|
|
5
5
|
* Converts a string representation of ether to numerical wei.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseEther.d.ts","sourceRoot":"","sources":["../../../utils/unit/parseEther.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parseEther.d.ts","sourceRoot":"","sources":["../../../utils/unit/parseEther.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAG1D,MAAM,MAAM,mBAAmB,GAAG,mBAAmB,GAAG,SAAS,CAAA;AAEjE;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,KAAK,GAAG,MAAc,UAErE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ErrorType } from '../../errors/utils.js';
|
|
2
|
-
import {
|
|
2
|
+
import type { ParseUnitsErrorType } from './parseUnits.js';
|
|
3
3
|
export type ParseGweiErrorType = ParseUnitsErrorType | ErrorType;
|
|
4
4
|
/**
|
|
5
5
|
* Converts a string representation of gwei to numerical wei.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseGwei.d.ts","sourceRoot":"","sources":["../../../utils/unit/parseGwei.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parseGwei.d.ts","sourceRoot":"","sources":["../../../utils/unit/parseGwei.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAG1D,MAAM,MAAM,kBAAkB,GAAG,mBAAmB,GAAG,SAAS,CAAA;AAEhE;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,KAAa,UAE3D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseUnits.d.ts","sourceRoot":"","sources":["../../../utils/unit/parseUnits.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parseUnits.d.ts","sourceRoot":"","sources":["../../../utils/unit/parseUnits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAGtD,MAAM,MAAM,mBAAmB,GAAG,SAAS,CAAA;AAE3C;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,UAEzD"}
|
package/errors/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '2.55.
|
|
1
|
+
export const version = '2.55.4'
|
package/package.json
CHANGED
package/tempo/chainConfig.ts
CHANGED
|
@@ -22,6 +22,14 @@ import * as Transaction from './Transaction.js'
|
|
|
22
22
|
|
|
23
23
|
const maxExpirySecs = 25
|
|
24
24
|
|
|
25
|
+
/** Returns random past seconds to distinguish otherwise-identical expiring transactions. */
|
|
26
|
+
function randomValidAfter(): number {
|
|
27
|
+
const now = BigInt(Math.floor(Date.now() / 1_000))
|
|
28
|
+
const latest = now - 60n
|
|
29
|
+
if (latest <= 0n) return 0
|
|
30
|
+
return Number(BigInt(Hex.random(8)) % latest)
|
|
31
|
+
}
|
|
32
|
+
|
|
25
33
|
export const chainConfig = {
|
|
26
34
|
blockTime: 1_000,
|
|
27
35
|
extendSchema: extendSchema<{
|
|
@@ -160,6 +168,8 @@ export const chainConfig = {
|
|
|
160
168
|
if (useExpiringNonce) {
|
|
161
169
|
request.nonceKey = maxUint256
|
|
162
170
|
request.nonce = 0
|
|
171
|
+
if (typeof request.validAfter === 'undefined')
|
|
172
|
+
request.validAfter = randomValidAfter()
|
|
163
173
|
if (typeof request.validBefore === 'undefined')
|
|
164
174
|
request.validBefore = Math.floor(Date.now() / 1000) + maxExpirySecs
|
|
165
175
|
} else if (typeof request.nonceKey !== 'undefined') {
|
|
@@ -22,7 +22,6 @@ import {
|
|
|
22
22
|
} from '../cursor.js'
|
|
23
23
|
import { type SizeErrorType, size } from '../data/size.js'
|
|
24
24
|
import { type SliceBytesErrorType, sliceBytes } from '../data/slice.js'
|
|
25
|
-
import { type TrimErrorType, trim } from '../data/trim.js'
|
|
26
25
|
import {
|
|
27
26
|
type BytesToBigIntErrorType,
|
|
28
27
|
type BytesToBoolErrorType,
|
|
@@ -355,7 +354,6 @@ function decodeTuple(
|
|
|
355
354
|
type DecodeStringErrorType =
|
|
356
355
|
| BytesToNumberErrorType
|
|
357
356
|
| BytesToStringErrorType
|
|
358
|
-
| TrimErrorType
|
|
359
357
|
| ErrorType
|
|
360
358
|
|
|
361
359
|
function decodeString(
|
|
@@ -378,7 +376,7 @@ function decodeString(
|
|
|
378
376
|
}
|
|
379
377
|
|
|
380
378
|
const data = cursor.readBytes(length, 32)
|
|
381
|
-
const value = bytesToString(
|
|
379
|
+
const value = bytesToString(data)
|
|
382
380
|
|
|
383
381
|
// As we have gone wondering, restore to the original position + next slot.
|
|
384
382
|
cursor.setPosition(staticPosition + 32)
|
|
@@ -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
|
}
|