viem 2.55.1 → 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.
Files changed (115) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/_cjs/chains/definitions/mainnet.js +1 -1
  3. package/_cjs/chains/definitions/mainnet.js.map +1 -1
  4. package/_cjs/chains/definitions/robinhood.js +1 -0
  5. package/_cjs/chains/definitions/robinhood.js.map +1 -1
  6. package/_cjs/chains/definitions/xLayer.js +1 -0
  7. package/_cjs/chains/definitions/xLayer.js.map +1 -1
  8. package/_cjs/clients/createClient.js +1 -0
  9. package/_cjs/clients/createClient.js.map +1 -1
  10. package/_cjs/errors/version.js +1 -1
  11. package/_cjs/tempo/Decorator.js.map +1 -1
  12. package/_cjs/tempo/actions/zone.js +78 -14
  13. package/_cjs/tempo/actions/zone.js.map +1 -1
  14. package/_cjs/tempo/chainConfig.js +9 -0
  15. package/_cjs/tempo/chainConfig.js.map +1 -1
  16. package/_cjs/tempo/internal/utils.js +5 -0
  17. package/_cjs/tempo/internal/utils.js.map +1 -1
  18. package/_cjs/utils/abi/decodeAbiParameters.js +1 -2
  19. package/_cjs/utils/abi/decodeAbiParameters.js.map +1 -1
  20. package/_cjs/utils/unit/Value.js +118 -0
  21. package/_cjs/utils/unit/Value.js.map +1 -0
  22. package/_cjs/utils/unit/formatEther.js +2 -3
  23. package/_cjs/utils/unit/formatEther.js.map +1 -1
  24. package/_cjs/utils/unit/formatGwei.js +2 -3
  25. package/_cjs/utils/unit/formatGwei.js.map +1 -1
  26. package/_cjs/utils/unit/formatUnits.js +2 -11
  27. package/_cjs/utils/unit/formatUnits.js.map +1 -1
  28. package/_cjs/utils/unit/parseEther.js +2 -3
  29. package/_cjs/utils/unit/parseEther.js.map +1 -1
  30. package/_cjs/utils/unit/parseGwei.js +2 -3
  31. package/_cjs/utils/unit/parseGwei.js.map +1 -1
  32. package/_cjs/utils/unit/parseUnits.js +2 -34
  33. package/_cjs/utils/unit/parseUnits.js.map +1 -1
  34. package/_esm/chains/definitions/mainnet.js +1 -1
  35. package/_esm/chains/definitions/mainnet.js.map +1 -1
  36. package/_esm/chains/definitions/robinhood.js +1 -0
  37. package/_esm/chains/definitions/robinhood.js.map +1 -1
  38. package/_esm/chains/definitions/xLayer.js +1 -0
  39. package/_esm/chains/definitions/xLayer.js.map +1 -1
  40. package/_esm/clients/createClient.js +4 -3
  41. package/_esm/clients/createClient.js.map +1 -1
  42. package/_esm/errors/version.js +1 -1
  43. package/_esm/tempo/Decorator.js.map +1 -1
  44. package/_esm/tempo/actions/zone.js +141 -17
  45. package/_esm/tempo/actions/zone.js.map +1 -1
  46. package/_esm/tempo/chainConfig.js +10 -0
  47. package/_esm/tempo/chainConfig.js.map +1 -1
  48. package/_esm/tempo/internal/utils.js +5 -0
  49. package/_esm/tempo/internal/utils.js.map +1 -1
  50. package/_esm/utils/abi/decodeAbiParameters.js +1 -2
  51. package/_esm/utils/abi/decodeAbiParameters.js.map +1 -1
  52. package/_esm/utils/unit/Value.js +240 -0
  53. package/_esm/utils/unit/Value.js.map +1 -0
  54. package/_esm/utils/unit/formatEther.js +2 -3
  55. package/_esm/utils/unit/formatEther.js.map +1 -1
  56. package/_esm/utils/unit/formatGwei.js +2 -3
  57. package/_esm/utils/unit/formatGwei.js.map +1 -1
  58. package/_esm/utils/unit/formatUnits.js +2 -11
  59. package/_esm/utils/unit/formatUnits.js.map +1 -1
  60. package/_esm/utils/unit/parseEther.js +2 -3
  61. package/_esm/utils/unit/parseEther.js.map +1 -1
  62. package/_esm/utils/unit/parseGwei.js +2 -3
  63. package/_esm/utils/unit/parseGwei.js.map +1 -1
  64. package/_esm/utils/unit/parseUnits.js +2 -36
  65. package/_esm/utils/unit/parseUnits.js.map +1 -1
  66. package/_types/chains/definitions/mainnet.d.ts +1 -1
  67. package/_types/chains/definitions/robinhood.d.ts +1 -1
  68. package/_types/chains/definitions/robinhood.d.ts.map +1 -1
  69. package/_types/chains/definitions/xLayer.d.ts +1 -1
  70. package/_types/chains/definitions/xLayer.d.ts.map +1 -1
  71. package/_types/clients/createClient.d.ts +3 -3
  72. package/_types/clients/createClient.d.ts.map +1 -1
  73. package/_types/errors/version.d.ts +1 -1
  74. package/_types/tempo/Decorator.d.ts +7 -1
  75. package/_types/tempo/Decorator.d.ts.map +1 -1
  76. package/_types/tempo/actions/zone.d.ts +133 -11
  77. package/_types/tempo/actions/zone.d.ts.map +1 -1
  78. package/_types/tempo/chainConfig.d.ts.map +1 -1
  79. package/_types/tempo/internal/types.d.ts +1 -0
  80. package/_types/tempo/internal/types.d.ts.map +1 -1
  81. package/_types/tempo/internal/utils.d.ts +5 -0
  82. package/_types/tempo/internal/utils.d.ts.map +1 -1
  83. package/_types/utils/abi/decodeAbiParameters.d.ts +1 -2
  84. package/_types/utils/abi/decodeAbiParameters.d.ts.map +1 -1
  85. package/_types/utils/unit/Value.d.ts +115 -0
  86. package/_types/utils/unit/Value.d.ts.map +1 -0
  87. package/_types/utils/unit/formatEther.d.ts +1 -1
  88. package/_types/utils/unit/formatEther.d.ts.map +1 -1
  89. package/_types/utils/unit/formatGwei.d.ts +1 -1
  90. package/_types/utils/unit/formatGwei.d.ts.map +1 -1
  91. package/_types/utils/unit/formatUnits.d.ts.map +1 -1
  92. package/_types/utils/unit/parseEther.d.ts +1 -1
  93. package/_types/utils/unit/parseEther.d.ts.map +1 -1
  94. package/_types/utils/unit/parseGwei.d.ts +1 -1
  95. package/_types/utils/unit/parseGwei.d.ts.map +1 -1
  96. package/_types/utils/unit/parseUnits.d.ts.map +1 -1
  97. package/chains/definitions/mainnet.ts +1 -1
  98. package/chains/definitions/robinhood.ts +1 -0
  99. package/chains/definitions/xLayer.ts +1 -0
  100. package/clients/createClient.ts +4 -3
  101. package/errors/version.ts +1 -1
  102. package/package.json +1 -1
  103. package/tempo/Decorator.ts +21 -2
  104. package/tempo/actions/zone.ts +305 -33
  105. package/tempo/chainConfig.ts +10 -0
  106. package/tempo/internal/types.ts +8 -0
  107. package/tempo/internal/utils.ts +6 -0
  108. package/utils/abi/decodeAbiParameters.ts +1 -3
  109. package/utils/unit/Value.ts +278 -0
  110. package/utils/unit/formatEther.ts +3 -4
  111. package/utils/unit/formatGwei.ts +3 -4
  112. package/utils/unit/formatUnits.ts +2 -15
  113. package/utils/unit/parseEther.ts +3 -4
  114. package/utils/unit/parseGwei.ts +3 -4
  115. 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 { etherUnits } from '../../constants/unit.js'
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 formatUnits(wei, etherUnits[unit])
18
+ return Value.formatEther(wei, unit)
20
19
  }
@@ -1,6 +1,5 @@
1
- import { gweiUnits } from '../../constants/unit.js'
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 formatUnits(wei, gweiUnits[unit])
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
- let display = value.toString()
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
  }
@@ -1,7 +1,6 @@
1
- import { etherUnits } from '../../constants/unit.js'
2
1
  import type { ErrorType } from '../../errors/utils.js'
3
-
4
- import { type ParseUnitsErrorType, parseUnits } from './parseUnits.js'
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 parseUnits(ether, etherUnits[unit])
19
+ return Value.fromEther(ether, unit)
21
20
  }
@@ -1,7 +1,6 @@
1
- import { gweiUnits } from '../../constants/unit.js'
2
1
  import type { ErrorType } from '../../errors/utils.js'
3
-
4
- import { type ParseUnitsErrorType, parseUnits } from './parseUnits.js'
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 parseUnits(ether, gweiUnits[unit])
19
+ return Value.fromGwei(ether, unit)
21
20
  }
@@ -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
- if (!/^(-?)([0-9]*)\.?([0-9]*)$/.test(value))
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
  }