@mezo-org/passport 0.5.2 → 0.5.4-dev.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.
Files changed (42) hide show
  1. package/dist/src/api/portal.d.ts +1 -1
  2. package/dist/src/api/portal.d.ts.map +1 -1
  3. package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -1
  4. package/dist/src/hooks/useAssetsConversionRates.js +9 -36
  5. package/dist/src/hooks/useAssetsConversionRates.js.map +1 -1
  6. package/dist/src/hooks/useBorrowData.d.ts +0 -15
  7. package/dist/src/hooks/useBorrowData.d.ts.map +1 -1
  8. package/dist/src/hooks/useBorrowData.js +7 -26
  9. package/dist/src/hooks/useBorrowData.js.map +1 -1
  10. package/dist/src/hooks/useCollateralPrice.d.ts +17 -0
  11. package/dist/src/hooks/useCollateralPrice.d.ts.map +1 -0
  12. package/dist/src/hooks/useCollateralPrice.js +54 -0
  13. package/dist/src/hooks/useCollateralPrice.js.map +1 -0
  14. package/dist/src/hooks/useTokensBalances.d.ts.map +1 -1
  15. package/dist/src/hooks/useTokensBalances.js +3 -0
  16. package/dist/src/hooks/useTokensBalances.js.map +1 -1
  17. package/dist/src/lib/contracts/index.d.ts +1 -1
  18. package/dist/src/lib/contracts/index.d.ts.map +1 -1
  19. package/dist/src/lib/contracts/index.js +0 -1
  20. package/dist/src/lib/contracts/index.js.map +1 -1
  21. package/dist/src/utils/number2.d.ts +106 -0
  22. package/dist/src/utils/number2.d.ts.map +1 -0
  23. package/dist/src/utils/number2.js +289 -0
  24. package/dist/src/utils/number2.js.map +1 -0
  25. package/dist/src/utils/number2.test.d.ts +2 -0
  26. package/dist/src/utils/number2.test.d.ts.map +1 -0
  27. package/dist/src/utils/number2.test.js +223 -0
  28. package/dist/src/utils/number2.test.js.map +1 -0
  29. package/package.json +1 -1
  30. package/src/api/portal.ts +1 -1
  31. package/src/hooks/useAssetsConversionRates.ts +10 -43
  32. package/src/hooks/useBorrowData.ts +9 -27
  33. package/src/hooks/useCollateralPrice.ts +62 -0
  34. package/src/hooks/useTokensBalances.ts +3 -0
  35. package/src/lib/contracts/index.ts +0 -2
  36. package/src/utils/number2.test.ts +309 -0
  37. package/src/utils/number2.ts +419 -0
  38. package/dist/src/lib/contracts/priceOracle.d.ts +0 -43
  39. package/dist/src/lib/contracts/priceOracle.d.ts.map +0 -1
  40. package/dist/src/lib/contracts/priceOracle.js +0 -52
  41. package/dist/src/lib/contracts/priceOracle.js.map +0 -1
  42. package/src/lib/contracts/priceOracle.ts +0 -53
@@ -13,6 +13,7 @@ import { bigIntMax, normalizePrecision } from "../utils/numbers"
13
13
  import useWalletAccount from "./useWalletAccount"
14
14
  import { getAsset } from "../utils/assets"
15
15
  import { convertToUsd } from "../utils/currency"
16
+ import { useCollateralPrice } from "./useCollateralPrice"
16
17
 
17
18
  const DEBT_AND_COLL_PRECISION = 18
18
19
 
@@ -85,22 +86,6 @@ const BORROWER_OPERATIONS_ABI = [
85
86
  },
86
87
  ] as const
87
88
 
88
- const PRICE_FEED_ABI = [
89
- {
90
- inputs: [],
91
- name: "fetchPrice",
92
- outputs: [
93
- {
94
- internalType: "uint256",
95
- name: "",
96
- type: "uint256",
97
- },
98
- ],
99
- stateMutability: "view",
100
- type: "function",
101
- },
102
- ] as const
103
-
104
89
  /**
105
90
  * Query hook for getting borrow data. Returns collateral and trove debt for the
106
91
  * connected account, based on it's evm address.
@@ -110,18 +95,20 @@ export function useBorrowData(queryOptions = {}) {
110
95
  const { environment = "mainnet" } = usePassportContext()
111
96
  const walletAccount = useWalletAccount()
112
97
 
98
+ const { data: collateralPriceData } = useCollateralPrice()
99
+
100
+ const collateralPrice = collateralPriceData?.toString()
101
+
113
102
  const contractAddress = useMemo(() => {
114
103
  if (environment === "mainnet") {
115
104
  return {
116
105
  troveManager: mainnetBorrowContracts.TroveManager.address,
117
106
  borrowerOperations: mainnetBorrowContracts.BorrowerOperations.address,
118
- priceFeed: mainnetBorrowContracts.PriceFeed.address,
119
107
  }
120
108
  }
121
109
  return {
122
110
  troveManager: testnetBorrowContracts.TroveManager.address,
123
111
  borrowerOperations: testnetBorrowContracts.BorrowerOperations.address,
124
- priceFeed: testnetBorrowContracts.PriceFeed.address,
125
112
  }
126
113
  }, [environment])
127
114
 
@@ -144,24 +131,19 @@ export function useBorrowData(queryOptions = {}) {
144
131
  functionName: "MUSD_GAS_COMPENSATION",
145
132
  chainId,
146
133
  },
147
- {
148
- abi: PRICE_FEED_ABI,
149
- address: contractAddress.priceFeed,
150
- functionName: "fetchPrice",
151
- chainId,
152
- },
153
134
  ],
154
135
  query: {
155
- enabled: !!walletAccount.accountAddress,
136
+ enabled: !!walletAccount.accountAddress && !!collateralPrice,
156
137
  staleTime: 5 * ONE_MINUTE_MS,
157
138
  retry: 1,
158
139
  select: (data) => {
159
140
  if (!data || data.some((item) => !item.result))
160
141
  throw new Error("No borrow data available")
161
142
 
143
+ if (!collateralPrice) throw new Error("Collateral price not available")
144
+
162
145
  const [rawCollateral, principal, interest] = data[0].result!
163
146
  const gasCompensation = data[1].result!
164
- const collateralPrice = data[2].result!
165
147
 
166
148
  const btcDetails = getAsset("BTC")
167
149
 
@@ -173,7 +155,7 @@ export function useBorrowData(queryOptions = {}) {
173
155
  usd: convertToUsd(
174
156
  rawCollateral,
175
157
  DEBT_AND_COLL_PRECISION,
176
- collateralPrice,
158
+ BigInt(collateralPrice),
177
159
  DEBT_AND_COLL_PRECISION,
178
160
  ),
179
161
  }
@@ -0,0 +1,62 @@
1
+ import { useReadContract } from "wagmi"
2
+ import { useMemo } from "react"
3
+ import {
4
+ mainnetBorrowContracts,
5
+ testnetBorrowContracts,
6
+ } from "../lib/contracts"
7
+ import { usePassportContext } from "./usePassportContext"
8
+ import { CHAIN_ID } from "../constants"
9
+ import useWalletAccount from "./useWalletAccount"
10
+
11
+ // Wagmi handles typesafety with ABI const assertions. TypeScript doesn't
12
+ // support importing JSON as const yet so types cannot be inferred from the
13
+ // imported contract. As a workaround there is minimal ABI definition that can
14
+ // be asserted types from.
15
+ // Ref: https://wagmi.sh/core/typescript#const-assert-abis-typed-data
16
+
17
+ const PRICE_FEED_ABI = [
18
+ {
19
+ inputs: [],
20
+ name: "fetchPrice",
21
+ outputs: [
22
+ {
23
+ internalType: "uint256",
24
+ name: "",
25
+ type: "uint256",
26
+ },
27
+ ],
28
+ stateMutability: "view",
29
+ type: "function",
30
+ },
31
+ ] as const
32
+
33
+ /**
34
+ * Query hook for getting borrow data. Returns collateral and trove debt for the
35
+ * connected account, based on it's evm address.
36
+ * @param queryOptions Query options passed to the underlying `useQuery` hook.
37
+ */
38
+ export function useCollateralPrice(queryOptions = {}) {
39
+ const { environment = "mainnet" } = usePassportContext()
40
+ const walletAccount = useWalletAccount()
41
+
42
+ const priceFeedContractAddress = useMemo(() => {
43
+ if (environment === "mainnet") {
44
+ return mainnetBorrowContracts.PriceFeed.address
45
+ }
46
+ return testnetBorrowContracts.PriceFeed.address
47
+ }, [environment])
48
+
49
+ const chainId = CHAIN_ID[environment]
50
+
51
+ return useReadContract({
52
+ abi: PRICE_FEED_ABI,
53
+ address: priceFeedContractAddress,
54
+ functionName: "fetchPrice",
55
+ chainId,
56
+ query: {
57
+ enabled: !!walletAccount.accountAddress,
58
+ retry: 1,
59
+ ...queryOptions,
60
+ },
61
+ })
62
+ }
@@ -124,6 +124,9 @@ export function useTokensBalances<T extends MezoChainToken[]>(
124
124
  walletAccount?.accountAddress,
125
125
  options.tokens,
126
126
  chainId,
127
+ conversionRatesData?.decimals,
128
+ conversionRatesData?.rates.BTC,
129
+ conversionRatesData?.rates.mT,
127
130
  ],
128
131
  enabled: !!walletAccount?.accountAddress && !!conversionRatesData,
129
132
  queryFn: async () => {
@@ -95,5 +95,3 @@ export const testnetBorrowContracts = {
95
95
  BorrowerOperations: borrowerOperationsTestnet,
96
96
  PriceFeed: priceFeedTestnet,
97
97
  } as unknown as ContractsMap<MezoBorrowContract>
98
-
99
- export { default as priceOracleContract } from "./priceOracle"
@@ -0,0 +1,309 @@
1
+ import {
2
+ bigIntToFloat,
3
+ bigIntToHumanReadableFormat,
4
+ floatToBigInt,
5
+ floatToHumanReadableFormat,
6
+ multiplyBigIntWithDecimal,
7
+ normalizeDecimalNumber,
8
+ trimBeforeDecimals,
9
+ trimDecimals,
10
+ } from "./number2"
11
+
12
+ describe("floatToBigInt", () => {
13
+ it("should convert integer string to bigint with default decimals", () => {
14
+ expect(floatToBigInt({ amount: "1" })).toBe(1_000_000_000_000_000_000n)
15
+ })
16
+
17
+ it("should convert float string to bigint with default decimals", () => {
18
+ expect(floatToBigInt({ amount: "1.23" })).toBe(1_230_000_000_000_000_000n)
19
+ })
20
+
21
+ it("should convert float string to bigint with custom decimals", () => {
22
+ expect(floatToBigInt({ amount: "1.23", decimals: 2 })).toBe(123n)
23
+ expect(floatToBigInt({ amount: "1.2345", decimals: 2 })).toBe(123n)
24
+ expect(floatToBigInt({ amount: "1.2345", decimals: 6 })).toBe(1234500n)
25
+ })
26
+
27
+ it("should handle numbers as input", () => {
28
+ expect(floatToBigInt({ amount: 2.5 })).toBe(2_500_000_000_000_000_000n)
29
+ })
30
+
31
+ it("should handle zero", () => {
32
+ expect(floatToBigInt({ amount: "0" })).toBe(0n)
33
+ expect(floatToBigInt({ amount: 0 })).toBe(0n)
34
+ })
35
+
36
+ it("should handle negative numbers", () => {
37
+ expect(floatToBigInt({ amount: "-1.5" })).toBe(-1_500_000_000_000_000_000n)
38
+ })
39
+
40
+ it("should handle numbers with thousands separator", () => {
41
+ expect(floatToBigInt({ amount: "1,234.56" })).toBe(
42
+ 1_234_560_000_000_000_000_000n,
43
+ )
44
+ })
45
+
46
+ it("should return 0n for invalid input", () => {
47
+ expect(floatToBigInt({ amount: "abc" })).toBe(0n)
48
+ expect(floatToBigInt({ amount: "1.2.3" })).toBe(0n)
49
+ expect(floatToBigInt({ amount: "" })).toBe(0n)
50
+ })
51
+ })
52
+
53
+ describe("bigIntToFloat", () => {
54
+ it("should convert bigint to float string with default decimals", () => {
55
+ expect(bigIntToFloat({ amount: 1_000_000_000_000_000_000n })).toBe("1")
56
+ expect(bigIntToFloat({ amount: 1_230_000_000_000_000_000n })).toBe("1.23")
57
+ })
58
+
59
+ it("should convert bigint to float string with custom decimals", () => {
60
+ expect(bigIntToFloat({ amount: 123n, decimals: 2 })).toBe("1.23")
61
+ expect(bigIntToFloat({ amount: 1234500n, decimals: 6 })).toBe("1.2345")
62
+ })
63
+
64
+ it("should handle desiredDecimals less than decimals", () => {
65
+ expect(
66
+ bigIntToFloat({
67
+ amount: 1_234_567_890_000_000_000n,
68
+ decimals: 18,
69
+ desiredDecimals: 2,
70
+ }),
71
+ ).toBe("1.23")
72
+ expect(
73
+ bigIntToFloat({
74
+ amount: 1_234_567_890_000_000_000n,
75
+ decimals: 18,
76
+ desiredDecimals: 6,
77
+ }),
78
+ ).toBe("1.234568")
79
+ })
80
+
81
+ it("should handle zero", () => {
82
+ expect(bigIntToFloat({ amount: 0n })).toBe("0")
83
+ })
84
+
85
+ it("should handle negative numbers", () => {
86
+ expect(bigIntToFloat({ amount: -1_500_000_000_000_000_000n })).toBe("-1.5")
87
+ })
88
+
89
+ it("should handle rounding up", () => {
90
+ expect(
91
+ bigIntToFloat({
92
+ amount: 1_249_999_999_999_999_999n,
93
+ decimals: 18,
94
+ desiredDecimals: 2,
95
+ }),
96
+ ).toBe("1.25")
97
+ expect(
98
+ bigIntToFloat({
99
+ amount: 1_234_567_890_000_000_000n,
100
+ decimals: 18,
101
+ desiredDecimals: 4,
102
+ }),
103
+ ).toBe("1.2346")
104
+ })
105
+
106
+ it("should handle large numbers", () => {
107
+ expect(
108
+ bigIntToFloat({
109
+ amount: 123_456_789_012_345_678_901_234_567_890n,
110
+ decimals: 18,
111
+ desiredDecimals: 2,
112
+ }),
113
+ ).toBe("123456789012.35")
114
+ })
115
+ })
116
+
117
+ describe("floatToHumanReadableFormat", () => {
118
+ it("should format number with thousands separator and default decimals", () => {
119
+ expect(floatToHumanReadableFormat({ amount: 1234567.891 })).toBe(
120
+ "1,234,567.89",
121
+ )
122
+ expect(floatToHumanReadableFormat({ amount: "1234567.891" })).toBe(
123
+ "1,234,567.89",
124
+ )
125
+ })
126
+
127
+ it("should format with custom decimals", () => {
128
+ expect(
129
+ floatToHumanReadableFormat({ amount: 1234567.891, desiredDecimals: 3 }),
130
+ ).toBe("1,234,567.891")
131
+ expect(
132
+ floatToHumanReadableFormat({ amount: 1234567.8, desiredDecimals: 0 }),
133
+ ).toBe("1,234,568")
134
+ })
135
+
136
+ it("should format with minDecimals", () => {
137
+ expect(
138
+ floatToHumanReadableFormat({
139
+ amount: 1234.5,
140
+ desiredDecimals: 2,
141
+ minDecimals: 1,
142
+ }),
143
+ ).toBe("1,234.5")
144
+ expect(
145
+ floatToHumanReadableFormat({
146
+ amount: 1234.5,
147
+ desiredDecimals: 2,
148
+ minDecimals: 2,
149
+ }),
150
+ ).toBe("1,234.50")
151
+ expect(
152
+ floatToHumanReadableFormat({
153
+ amount: 1234.5,
154
+ desiredDecimals: 2,
155
+ minDecimals: 4,
156
+ }),
157
+ ).toBe("1,234.5000")
158
+ })
159
+
160
+ it("should return '0' for invalid input", () => {
161
+ expect(floatToHumanReadableFormat({ amount: "abc" })).toBe("0")
162
+ })
163
+ })
164
+
165
+ describe("bigIntToHumanReadableFormat", () => {
166
+ it("should format bigint to human readable string", () => {
167
+ expect(
168
+ bigIntToHumanReadableFormat({
169
+ amount: 1_234_567_890_000_000_000n,
170
+ decimals: 18,
171
+ desiredDecimals: 2,
172
+ }),
173
+ ).toBe("1.23")
174
+ expect(
175
+ bigIntToHumanReadableFormat({
176
+ amount: 1_234_567_890_000_000_000n,
177
+ decimals: 18,
178
+ desiredDecimals: 6,
179
+ }),
180
+ ).toBe("1.234568")
181
+ })
182
+
183
+ it("should handle negative values", () => {
184
+ expect(
185
+ bigIntToHumanReadableFormat({
186
+ amount: -1_234_567_890_000_000_000n,
187
+ decimals: 18,
188
+ desiredDecimals: 2,
189
+ }),
190
+ ).toBe("-1.23")
191
+ })
192
+
193
+ it("should show <0.01 for very small nonzero values", () => {
194
+ expect(
195
+ bigIntToHumanReadableFormat({
196
+ amount: 1n,
197
+ decimals: 18,
198
+ desiredDecimals: 2,
199
+ }),
200
+ ).toBe("<0.01")
201
+ expect(
202
+ bigIntToHumanReadableFormat({
203
+ amount: -1n,
204
+ decimals: 18,
205
+ desiredDecimals: 2,
206
+ }),
207
+ ).toBe("<0.01")
208
+ })
209
+
210
+ it("should format with minDecimals", () => {
211
+ expect(
212
+ bigIntToHumanReadableFormat({
213
+ amount: 1_234_000_000_000_000_000n,
214
+ decimals: 18,
215
+ desiredDecimals: 2,
216
+ minDecimals: 3,
217
+ }),
218
+ ).toBe("1.234")
219
+ expect(
220
+ bigIntToHumanReadableFormat({
221
+ amount: 1_234_000_000_000_000_000n,
222
+ decimals: 18,
223
+ desiredDecimals: 3,
224
+ minDecimals: 2,
225
+ }),
226
+ ).toBe("1.234")
227
+ expect(
228
+ bigIntToHumanReadableFormat({
229
+ amount: 1_234_000_000_000_000_000n,
230
+ decimals: 18,
231
+ desiredDecimals: 2,
232
+ minDecimals: 4,
233
+ }),
234
+ ).toBe("1.2340")
235
+ })
236
+ })
237
+
238
+ describe("multiplyBigIntWithDecimal", () => {
239
+ it("should multiply bigint by integer", () => {
240
+ expect(multiplyBigIntWithDecimal(10n, 2)).toBe(20n)
241
+ })
242
+
243
+ it("should multiply bigint by decimal", () => {
244
+ expect(multiplyBigIntWithDecimal(100n, 0.5)).toBe(50n)
245
+ expect(multiplyBigIntWithDecimal(100n, 1.25)).toBe(125n)
246
+ })
247
+
248
+ it("should handle scientific notation", () => {
249
+ expect(multiplyBigIntWithDecimal(100n, 1e-2)).toBe(1n)
250
+ })
251
+
252
+ it("should handle zero", () => {
253
+ expect(multiplyBigIntWithDecimal(0n, 1.5)).toBe(0n)
254
+ })
255
+ })
256
+
257
+ describe("trimDecimals", () => {
258
+ it("should trim decimals to specified precision", () => {
259
+ expect(trimDecimals("1.23456", 2)).toBe("1.23")
260
+ expect(trimDecimals("1.2", 4)).toBe("1.2")
261
+ expect(trimDecimals("1", 2)).toBe("1")
262
+ })
263
+ })
264
+
265
+ describe("trimBeforeDecimals", () => {
266
+ it("should trim integer part to specified length", () => {
267
+ expect(trimBeforeDecimals("12345.67", 3)).toBe("123.67")
268
+ expect(trimBeforeDecimals("12.34", 5)).toBe("12.34")
269
+ expect(trimBeforeDecimals("123456", 2)).toBe("12.")
270
+ })
271
+ })
272
+
273
+ describe("normalizeDecimalNumber", () => {
274
+ it("should remove invalid characters", () => {
275
+ expect(normalizeDecimalNumber("12a3.4b5")).toBe("123.45")
276
+ })
277
+
278
+ it("should prevent multiple dots", () => {
279
+ expect(normalizeDecimalNumber("1.2.3")).toBe("12.3")
280
+ expect(normalizeDecimalNumber("1..1.2")).toBe("11.2")
281
+ })
282
+
283
+ it("should allow only digits and one dot", () => {
284
+ expect(normalizeDecimalNumber("abc")).toBe("")
285
+ expect(normalizeDecimalNumber("123")).toBe("123")
286
+ })
287
+
288
+ it("should handle leading and trailing spaces", () => {
289
+ expect(normalizeDecimalNumber(" 1.23 ")).toBe("1.23")
290
+ expect(normalizeDecimalNumber(" 1.2.3 ")).toBe("12.3")
291
+ })
292
+
293
+ it("should handle empty input", () => {
294
+ expect(normalizeDecimalNumber("")).toBe("")
295
+ expect(normalizeDecimalNumber(" ")).toBe("")
296
+ })
297
+
298
+ it("should handle numbers with thousands separator", () => {
299
+ expect(normalizeDecimalNumber("1,234.56")).toBe("1234.56")
300
+ expect(normalizeDecimalNumber("1,234,567.89")).toBe("1234567.89")
301
+ })
302
+
303
+ it("should handle scientific notation", () => {
304
+ // This function does not support scientific notation as our input is expected
305
+ // to accept only decimal numbers
306
+ expect(normalizeDecimalNumber("1e3")).toBe("13")
307
+ expect(normalizeDecimalNumber("1.23e2")).toBe("1.232")
308
+ })
309
+ })