@mezo-org/passport 0.5.2-dev.3 → 0.5.2-dev.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 (55) hide show
  1. package/dist/src/components/Dropdown/Root/AccountMusdListing.d.ts.map +1 -1
  2. package/dist/src/components/Dropdown/Root/AccountMusdListing.js +0 -2
  3. package/dist/src/components/Dropdown/Root/AccountMusdListing.js.map +1 -1
  4. package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -1
  5. package/dist/src/hooks/useAssetsConversionRates.js +9 -36
  6. package/dist/src/hooks/useAssetsConversionRates.js.map +1 -1
  7. package/dist/src/hooks/useBorrowData.d.ts +0 -15
  8. package/dist/src/hooks/useBorrowData.d.ts.map +1 -1
  9. package/dist/src/hooks/useBorrowData.js +3 -25
  10. package/dist/src/hooks/useBorrowData.js.map +1 -1
  11. package/dist/src/hooks/useCollateralPrice.d.ts +17 -0
  12. package/dist/src/hooks/useCollateralPrice.d.ts.map +1 -0
  13. package/dist/src/hooks/useCollateralPrice.js +56 -0
  14. package/dist/src/hooks/useCollateralPrice.js.map +1 -0
  15. package/dist/src/hooks/useTokensBalances.d.ts.map +1 -1
  16. package/dist/src/hooks/useTokensBalances.js +0 -7
  17. package/dist/src/hooks/useTokensBalances.js.map +1 -1
  18. package/dist/src/lib/contracts/index.d.ts +1 -1
  19. package/dist/src/lib/contracts/index.d.ts.map +1 -1
  20. package/dist/src/lib/contracts/index.js +0 -1
  21. package/dist/src/lib/contracts/index.js.map +1 -1
  22. package/dist/src/utils/numbers.test.js.map +1 -1
  23. package/package.json +1 -1
  24. package/src/components/Dropdown/Root/AccountMusdListing.tsx +0 -2
  25. package/src/hooks/useAssetsConversionRates.ts +10 -43
  26. package/src/hooks/useBorrowData.ts +5 -27
  27. package/src/hooks/useCollateralPrice.ts +64 -0
  28. package/src/hooks/useTokensBalances.ts +0 -9
  29. package/src/lib/contracts/index.ts +0 -2
  30. package/src/utils/numbers.test.ts +0 -1
  31. package/dist/src/hooks/useTokensBalances copy.d.ts +0 -74
  32. package/dist/src/hooks/useTokensBalances copy.d.ts.map +0 -1
  33. package/dist/src/hooks/useTokensBalances copy.js +0 -206
  34. package/dist/src/hooks/useTokensBalances copy.js.map +0 -1
  35. package/dist/src/lib/contracts/priceOracle.d.ts +0 -43
  36. package/dist/src/lib/contracts/priceOracle.d.ts.map +0 -1
  37. package/dist/src/lib/contracts/priceOracle.js +0 -52
  38. package/dist/src/lib/contracts/priceOracle.js.map +0 -1
  39. package/dist/src/utils/chain.d.ts +0 -2
  40. package/dist/src/utils/chain.d.ts.map +0 -1
  41. package/dist/src/utils/chain.js +0 -5
  42. package/dist/src/utils/chain.js.map +0 -1
  43. package/dist/src/utils/number2.d.ts +0 -106
  44. package/dist/src/utils/number2.d.ts.map +0 -1
  45. package/dist/src/utils/number2.js +0 -289
  46. package/dist/src/utils/number2.js.map +0 -1
  47. package/dist/src/utils/number2.test.d.ts +0 -2
  48. package/dist/src/utils/number2.test.d.ts.map +0 -1
  49. package/dist/src/utils/number2.test.js +0 -223
  50. package/dist/src/utils/number2.test.js.map +0 -1
  51. package/src/hooks/useTokensBalances copy.ts +0 -289
  52. package/src/lib/contracts/priceOracle.ts +0 -53
  53. package/src/utils/chain.ts +0 -5
  54. package/src/utils/number2.test.ts +0 -309
  55. package/src/utils/number2.ts +0 -419
@@ -1,419 +0,0 @@
1
- // Matches floating point numbers with optional thousands separators
2
- export const FLOATING_POINT_REGEX = /^[^0-9]*([0-9,]+)(?:\.([0-9]*))?$/
3
- const FRIENDLY_DECIMALS_AMOUNT = 6
4
-
5
- type FixedPointNumber = {
6
- amount: bigint
7
- decimals: number
8
- }
9
-
10
- /**
11
- * HELPER FUNCTIONS
12
- * These functions are used to convert between floating-point numbers and fixed-point numbers.
13
- * They handle parsing, conversion, and formatting of numbers with specific decimal places.
14
- * They shouldn't be used directly in the UI or business logic.
15
- */
16
-
17
- /**
18
- * Parses a floating-point string into a fixed-point number representation.
19
- * @param floatingPointString The floating-point string to parse.
20
- * @returns The fixed-point number representation or undefined if parsing fails.
21
- */
22
- const parseToFixedPointNumber = (
23
- floatingPointString: string,
24
- ): FixedPointNumber | undefined => {
25
- if (!floatingPointString.match(FLOATING_POINT_REGEX)) {
26
- return undefined
27
- }
28
-
29
- const [whole, decimals, ...extra] = floatingPointString.split(".")
30
-
31
- // Only one `.` supported.
32
- if (extra.length > 0) {
33
- return undefined
34
- }
35
-
36
- const noThousandsSeparatorWhole = whole.replace(",", "")
37
- const setDecimals = decimals ?? ""
38
-
39
- try {
40
- return {
41
- amount: BigInt([noThousandsSeparatorWhole, setDecimals].join("")),
42
- decimals: setDecimals.length,
43
- }
44
- } catch (error) {
45
- return undefined
46
- }
47
- }
48
-
49
- /**
50
- * Separates thousands in a number with commas.
51
- * @param value The value to format.
52
- * @param minDecimals The minimum number of decimal places.
53
- * @param maxDecimals The maximum number of decimal places.
54
- * @returns The formatted value with thousands separators.
55
- */
56
- const separateThousandsWithComma = (
57
- value: number | bigint | string,
58
- minDecimals = 0,
59
- maxDecimals = 2,
60
- ): string => {
61
- let adjustedValue = value
62
- let adjustedMaxDecimals = maxDecimals
63
-
64
- // If passed value is not numeric we should return 0
65
- if (Number.isNaN(+value.toString())) {
66
- adjustedValue = 0
67
- }
68
-
69
- if (typeof adjustedValue === "string") {
70
- adjustedValue = +adjustedValue
71
- }
72
-
73
- if (minDecimals > maxDecimals) {
74
- adjustedMaxDecimals = minDecimals
75
- }
76
-
77
- // Ensure maximumFractionDigits is within allowed range [0, 20]
78
- const safeMaxDecimals = Math.max(0, Math.min(adjustedMaxDecimals, 20))
79
-
80
- // @ts-expect-error - `maximumFractionDigits` wants to get number less than 21,
81
- // but as the tokens have 18 decimals have, we can safely pass a parameter
82
- return adjustedValue.toLocaleString("en-US", {
83
- minimumFractionDigits: minDecimals,
84
- maximumFractionDigits: safeMaxDecimals,
85
- roundingMode: "halfExpand",
86
- })
87
- }
88
-
89
- /** PUBLIC HELPER FUNCTIONS for calculations */
90
-
91
- /**
92
- * Multiplies a bigint with a decimal number, taking into account the decimal places.
93
- *
94
- * @param bigint - The bigint value to multiply.
95
- * @param number - The decimal number to multiply with.
96
- * @returns The result of the multiplication as a bigint.
97
- */
98
- export const multiplyBigIntWithDecimal = (
99
- bigint: bigint,
100
- number: number,
101
- ): bigint => {
102
- const numberString = number.toString()
103
- const scientificMatch = /^1e-(\d+)$/.exec(numberString)
104
-
105
- // Ensure we can correctly deal with scientific number expression
106
- if (scientificMatch) {
107
- const exponent = parseInt(scientificMatch[1], 10)
108
- const scale = BigInt(10 ** exponent)
109
-
110
- return (bigint * 1n) / scale
111
- }
112
-
113
- const decimalsPart = number.toString().split(".")[1]
114
- const decimalsCount = decimalsPart ? decimalsPart.length : 0
115
-
116
- if (!decimalsCount) return bigint * BigInt(number)
117
-
118
- const scale = BigInt(10 ** decimalsCount)
119
- const scaledNumber = BigInt(Math.round(number * Number(scale)))
120
-
121
- return (bigint * scaledNumber) / scale
122
- }
123
-
124
- /**
125
- * Converts a fixed-point number to another fixed-point number with a different decimal precision.
126
- * @param args The arguments for the conversion.
127
- * - amount: required, the fixed-point number to convert.
128
- * - decimals: required, the current decimal precision of the number.
129
- * - targetDecimals: required, the target decimal precision for the conversion.
130
- * @returns The converted fixed-point number.
131
- */
132
- export const convertDecimals = ({
133
- amount,
134
- decimals,
135
- targetDecimals,
136
- }: {
137
- amount: bigint
138
- decimals: number
139
- targetDecimals: number
140
- }): bigint => {
141
- if (decimals >= targetDecimals) {
142
- return amount / 10n ** BigInt(decimals - targetDecimals)
143
- }
144
-
145
- return amount * 10n ** BigInt(targetDecimals - decimals)
146
- }
147
-
148
- /** Helper functions for input components */
149
-
150
- /**
151
- * Trims decimal places to a specified precision - NO ROUNDING!
152
- * @param value The value to trim
153
- * @param precision The number of decimal places to keep
154
- * @returns The trimmed value
155
- */
156
- export const trimDecimals = (value: string, precision: number): string => {
157
- const [integerPart, decimalPart = ""] = value.split(".")
158
- return decimalPart.length > precision
159
- ? `${integerPart}.${decimalPart.slice(0, precision)}`
160
- : value
161
- }
162
-
163
- /**
164
- * Trims integer part to a specified length - NO ROUNDING!
165
- * @param value The value to trim
166
- * @param precision The number of integer places to keep
167
- * @returns The trimmed value
168
- * @deprecated just compare the value to the max allowed value in the input component
169
- */
170
- export const trimBeforeDecimals = (
171
- value: string,
172
- precision: number,
173
- ): string => {
174
- const [integerPart, decimalPart = ""] = value.split(".")
175
-
176
- return integerPart.length > precision
177
- ? `${integerPart.slice(0, precision)}.${decimalPart}`
178
- : value
179
- }
180
-
181
- /**
182
- * Normalizes a decimal number input by removing invalid characters and preventing multiple dots.
183
- * @param value The value to normalize
184
- * @returns The normalized value
185
- */
186
- export const normalizeDecimalNumber = (value: string) =>
187
- value.replace(/[^0-9.]/g, "").replace(/\.(?=.*\.)/g, "") // Remove all dots except the first one
188
-
189
- /**
190
- * CONVERSION FUNCTIONS
191
- * These functions convert between floating-point numbers and big integers as well as
192
- * provide human-readable formats.
193
- *
194
- * They are used to ensure that numbers are correctly formatted for display and calculations
195
- * and should be used in the UI and business logic to ensure correct number formatting.
196
- */
197
-
198
- /** Basic conversion bigint <-> float */
199
-
200
- /**
201
- * Converts a floating-point number to a big integer representation.
202
- * @param args The arguments for the conversion.
203
- * - amount: required, the floating-point number to convert.
204
- * - decimals: optional, the number of decimal places to consider.
205
- * @returns The big integer representation of the floating-point number.
206
- */
207
- export const floatToBigInt = ({
208
- amount,
209
- decimals = 18,
210
- }: {
211
- amount: string | number
212
- decimals?: number
213
- }): bigint => {
214
- const parsedAmount = amount.toString()
215
- const fixedPointAmount = parseToFixedPointNumber(parsedAmount)
216
-
217
- if (typeof fixedPointAmount === "undefined") {
218
- return 0n
219
- }
220
-
221
- const bigIntAmount = convertDecimals({
222
- amount: fixedPointAmount.amount,
223
- decimals: fixedPointAmount.decimals,
224
- targetDecimals: decimals,
225
- })
226
-
227
- return bigIntAmount
228
- }
229
-
230
- /**
231
- * Converts a big integer representation to a floating-point number.
232
- * @param args The arguments for the conversion.
233
- * - amount: required, the big integer representation to convert.
234
- * - decimals: optional, the number of decimal places to consider.
235
- * - desiredDecimals: optional, the desired number of decimal places for the output.
236
- * @returns The floating-point representation of the big integer.
237
- */
238
- export const bigIntToFloat = ({
239
- amount,
240
- decimals = 18,
241
- desiredDecimals = decimals,
242
- }: {
243
- amount: bigint
244
- decimals?: number
245
- desiredDecimals?: number
246
- }): string => {
247
- let isNegative = false
248
- let normalizedAmount = amount
249
-
250
- if (amount < 0n) {
251
- normalizedAmount = -amount
252
- isNegative = true
253
- }
254
-
255
- const desiredDecimalsAmount =
256
- normalizedAmount / 10n ** BigInt(Math.max(0, decimals - desiredDecimals))
257
-
258
- // Desired decimals amount with additional decimal to check if number
259
- // should be rounded up
260
- const desiredDecimalsAmountWithAdditionalDecimal =
261
- normalizedAmount /
262
- 10n ** BigInt(Math.max(0, decimals - desiredDecimals - 1))
263
-
264
- // We are getting the first value that has been cut off
265
- const firstDecimalAfterCutOff = desiredDecimalsAmountWithAdditionalDecimal
266
- .toString()
267
- .at(-1)
268
-
269
- // Padding amount when its length is smaller than desired
270
- const parsedDesiredAmount = desiredDecimalsAmount
271
- .toString()
272
- .padStart(desiredDecimals + 1, "0")
273
-
274
- const integersPart = parsedDesiredAmount.slice(0, -desiredDecimals)
275
- const decimalsPart = parsedDesiredAmount.slice(-desiredDecimals)
276
-
277
- // If number is above or equal to 5 we should round up the number
278
- const shouldIncreaseDecimalsPart =
279
- Number(firstDecimalAfterCutOff) >= 5 && desiredDecimals !== decimals
280
-
281
- // If above is true we add 1 to the decimals value and convert it back to string
282
- const adjustedDecimalsPart = shouldIncreaseDecimalsPart
283
- ? (Number(decimalsPart) + 1).toString().padStart(desiredDecimals, "0")
284
- : decimalsPart
285
-
286
- // Determining whether integer part needs to be increased due to rounding up
287
- const shouldIncreaseIntegerPart =
288
- (decimalsPart === "0" && adjustedDecimalsPart !== decimalsPart) ||
289
- adjustedDecimalsPart.length > decimalsPart.length
290
-
291
- // If above is true we add 1 to the integers value and convert it back to string
292
- // and set first value of decimals part to 0
293
- const adjustedIntegersPart = shouldIncreaseIntegerPart
294
- ? (Number(integersPart) + 1).toString()
295
- : integersPart
296
-
297
- const decimalsPartAfterIntegersAdjustments = shouldIncreaseIntegerPart
298
- ? `0${adjustedDecimalsPart.slice(1)}`
299
- : adjustedDecimalsPart
300
-
301
- // Inserting "." to separate integers part from decimal part
302
- const desiredAmount = [
303
- ...adjustedIntegersPart,
304
- ".",
305
- ...decimalsPartAfterIntegersAdjustments,
306
- ].join("")
307
-
308
- // Remove trailing zeros and the trailing decimal point (if exists)
309
- const trimmedDesiredAmount = desiredAmount.replace(
310
- /(?:\.0*|(\.\d+?)0+)$/,
311
- "$1",
312
- )
313
-
314
- return isNegative ? `-${trimmedDesiredAmount}` : trimmedDesiredAmount
315
- }
316
-
317
- /** Human-readable formats */
318
-
319
- type AmountAsHumanReadableArguments<T extends number | string | bigint> = {
320
- amount: T
321
- desiredDecimals?: number
322
- minDecimals?: number
323
- decimals?: number
324
- }
325
-
326
- /**
327
- * Helpers for setting correct arguments and getting unified human-readable
328
- * formats across the app with _toHumanReadableFormat functions.
329
- */
330
-
331
- export const amountAsUSD = <T extends number | string | bigint>(
332
- args: AmountAsHumanReadableArguments<T>,
333
- ): AmountAsHumanReadableArguments<T> => ({
334
- desiredDecimals: 2,
335
- minDecimals: 2,
336
- ...args,
337
- })
338
-
339
- export const amountAsToken = <T extends number | string | bigint>(
340
- args: AmountAsHumanReadableArguments<T>,
341
- ): AmountAsHumanReadableArguments<T> => ({
342
- desiredDecimals: FRIENDLY_DECIMALS_AMOUNT,
343
- minDecimals: 0,
344
- ...args,
345
- })
346
-
347
- export const amountAsMusd = <T extends number | string | bigint>(
348
- args: AmountAsHumanReadableArguments<T>,
349
- ): AmountAsHumanReadableArguments<T> => ({
350
- desiredDecimals: 2,
351
- minDecimals: 2,
352
- decimals: 18, // MUSD has 18 decimals
353
- ...args,
354
- })
355
-
356
- export const amountAsMats = <T extends number | string | bigint>(
357
- args: AmountAsHumanReadableArguments<T>,
358
- ): AmountAsHumanReadableArguments<T> => ({
359
- desiredDecimals: 0,
360
- minDecimals: 0,
361
- ...args,
362
- })
363
-
364
- /**
365
- * Converts a floating-point number to a human-readable format - with thousands
366
- * separators and specified decimal places.
367
- * @param args The arguments for the conversion.
368
- * - amount: required, the floating-point number to convert.
369
- * - desiredDecimals: optional, the desired number of decimal places for the output.
370
- * - minDecimals: optional, the minimum number of decimal places.
371
- * @returns The human-readable format of the floating-point number.
372
- */
373
- export const floatToHumanReadableFormat = ({
374
- amount,
375
- desiredDecimals = 2, // treat is as an USD amount by default
376
- minDecimals = 0,
377
- }: AmountAsHumanReadableArguments<number | string>): string =>
378
- separateThousandsWithComma(amount, minDecimals, desiredDecimals)
379
-
380
- export const bigIntToHumanReadableFormat = ({
381
- amount,
382
- decimals = 18,
383
- desiredDecimals = FRIENDLY_DECIMALS_AMOUNT, // treat is as a token by default
384
- minDecimals = 0,
385
- }: AmountAsHumanReadableArguments<bigint>): string => {
386
- let isNegative = false
387
- let normalizedAmount = amount
388
- let adjustedDesiredDecimals = desiredDecimals
389
-
390
- if (minDecimals > desiredDecimals) {
391
- adjustedDesiredDecimals = minDecimals
392
- }
393
-
394
- if (normalizedAmount < 0n) {
395
- normalizedAmount = -normalizedAmount
396
- isNegative = true
397
- }
398
-
399
- const trimmedDesiredAmount = bigIntToFloat({
400
- amount: normalizedAmount,
401
- decimals,
402
- desiredDecimals: adjustedDesiredDecimals,
403
- })
404
-
405
- if (trimmedDesiredAmount === "0" && normalizedAmount !== 0n) {
406
- const decimalsString = "1".padStart(adjustedDesiredDecimals, "0")
407
- return `<0.${decimalsString}`
408
- }
409
-
410
- const desiredValue = isNegative
411
- ? `-${trimmedDesiredAmount}`
412
- : trimmedDesiredAmount
413
-
414
- return floatToHumanReadableFormat({
415
- amount: desiredValue,
416
- desiredDecimals: adjustedDesiredDecimals,
417
- minDecimals,
418
- })
419
- }