@mezo-org/passport 0.5.2-dev.3 → 0.5.2-dev.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/dist/src/components/Dropdown/Root/AccountMusdListing.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Root/AccountMusdListing.js +0 -2
- package/dist/src/components/Dropdown/Root/AccountMusdListing.js.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.js +51 -26
- package/dist/src/hooks/useAssetsConversionRates.js.map +1 -1
- package/dist/src/hooks/useTokensBalances.d.ts.map +1 -1
- package/dist/src/hooks/useTokensBalances.js +0 -7
- package/dist/src/hooks/useTokensBalances.js.map +1 -1
- package/dist/src/lib/contracts/index.d.ts +1 -1
- package/dist/src/lib/contracts/index.d.ts.map +1 -1
- package/dist/src/lib/contracts/index.js +0 -1
- package/dist/src/lib/contracts/index.js.map +1 -1
- package/dist/src/utils/numbers.test.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Dropdown/Root/AccountMusdListing.tsx +0 -2
- package/src/hooks/useAssetsConversionRates.ts +52 -21
- package/src/hooks/useTokensBalances.ts +0 -9
- package/src/lib/contracts/index.ts +0 -2
- package/src/utils/numbers.test.ts +0 -1
- package/dist/src/hooks/useTokensBalances copy.d.ts +0 -74
- package/dist/src/hooks/useTokensBalances copy.d.ts.map +0 -1
- package/dist/src/hooks/useTokensBalances copy.js +0 -206
- package/dist/src/hooks/useTokensBalances copy.js.map +0 -1
- package/dist/src/lib/contracts/priceOracle.d.ts +0 -43
- package/dist/src/lib/contracts/priceOracle.d.ts.map +0 -1
- package/dist/src/lib/contracts/priceOracle.js +0 -52
- package/dist/src/lib/contracts/priceOracle.js.map +0 -1
- package/dist/src/utils/chain.d.ts +0 -2
- package/dist/src/utils/chain.d.ts.map +0 -1
- package/dist/src/utils/chain.js +0 -5
- package/dist/src/utils/chain.js.map +0 -1
- package/dist/src/utils/number2.d.ts +0 -106
- package/dist/src/utils/number2.d.ts.map +0 -1
- package/dist/src/utils/number2.js +0 -289
- package/dist/src/utils/number2.js.map +0 -1
- package/dist/src/utils/number2.test.d.ts +0 -2
- package/dist/src/utils/number2.test.d.ts.map +0 -1
- package/dist/src/utils/number2.test.js +0 -223
- package/dist/src/utils/number2.test.js.map +0 -1
- package/src/hooks/useTokensBalances copy.ts +0 -289
- package/src/lib/contracts/priceOracle.ts +0 -53
- package/src/utils/chain.ts +0 -5
- package/src/utils/number2.test.ts +0 -309
- package/src/utils/number2.ts +0 -419
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
import { useConfig } from "wagmi"
|
|
2
|
-
import { Abi } from "viem"
|
|
3
|
-
import {
|
|
4
|
-
useQuery,
|
|
5
|
-
useQueryClient,
|
|
6
|
-
UseQueryOptions,
|
|
7
|
-
} from "@tanstack/react-query"
|
|
8
|
-
import { getBalance, readContracts } from "wagmi/actions"
|
|
9
|
-
import {
|
|
10
|
-
mainnetTokenContracts,
|
|
11
|
-
MezoChainToken,
|
|
12
|
-
testnetTokenContracts,
|
|
13
|
-
} from "../lib/contracts"
|
|
14
|
-
import useWalletAccount from "./useWalletAccount"
|
|
15
|
-
import {
|
|
16
|
-
getAsset,
|
|
17
|
-
isBitcoinLikeCryptoAsset,
|
|
18
|
-
isTTokenCryptoAsset,
|
|
19
|
-
} from "../utils/assets"
|
|
20
|
-
import { convertToUsd } from "../utils/currency"
|
|
21
|
-
import { CHAIN_ID } from "../constants"
|
|
22
|
-
import { useAssetsConversionRates } from "./useAssetsConversionRates"
|
|
23
|
-
import { isMezoChain } from "../utils/chain"
|
|
24
|
-
import {
|
|
25
|
-
amountAsUSD,
|
|
26
|
-
bigIntToFloat,
|
|
27
|
-
floatToHumanReadableFormat,
|
|
28
|
-
} from "../utils/number2"
|
|
29
|
-
import { useBorrowData } from "./useBorrowData"
|
|
30
|
-
|
|
31
|
-
// Wagmi handles typesafety with ABI const assertions. TypeScript doesn't
|
|
32
|
-
// support importing JSON as const yet so types cannot be inferred from the
|
|
33
|
-
// imported contract. As a workaround there is minimal ABI definition that can
|
|
34
|
-
// be asserted types from.
|
|
35
|
-
// Ref: https://wagmi.sh/core/typescript#const-assert-abis-typed-data
|
|
36
|
-
|
|
37
|
-
export const BALANCE_OF_ABI = [
|
|
38
|
-
{
|
|
39
|
-
inputs: [
|
|
40
|
-
{
|
|
41
|
-
internalType: "address",
|
|
42
|
-
name: "account",
|
|
43
|
-
type: "address",
|
|
44
|
-
},
|
|
45
|
-
],
|
|
46
|
-
name: "balanceOf",
|
|
47
|
-
outputs: [
|
|
48
|
-
{
|
|
49
|
-
internalType: "uint256",
|
|
50
|
-
name: "",
|
|
51
|
-
type: "uint256",
|
|
52
|
-
},
|
|
53
|
-
],
|
|
54
|
-
stateMutability: "view",
|
|
55
|
-
type: "function",
|
|
56
|
-
},
|
|
57
|
-
] as const satisfies Abi
|
|
58
|
-
|
|
59
|
-
const BALANCE_TOKENS: MezoChainToken[] = [
|
|
60
|
-
"mcbBTC",
|
|
61
|
-
"mDAI",
|
|
62
|
-
"mFBTC",
|
|
63
|
-
"mSolvBTC",
|
|
64
|
-
"mswBTC",
|
|
65
|
-
"mT",
|
|
66
|
-
"mUSDC",
|
|
67
|
-
"mUSDe",
|
|
68
|
-
"mUSDT",
|
|
69
|
-
"mxSolvBTC",
|
|
70
|
-
"MUSD",
|
|
71
|
-
]
|
|
72
|
-
|
|
73
|
-
type UseMezoChainTokensBalancesOptions<T extends MezoChainToken[]> = {
|
|
74
|
-
tokens?: T
|
|
75
|
-
queryOptions?: Omit<
|
|
76
|
-
UseQueryOptions<Record<T[number], TokenBalance>>,
|
|
77
|
-
"queryKey" | "queryFn" | "select" | "enabled"
|
|
78
|
-
>
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export type TokenBalance = {
|
|
82
|
-
decimals: number
|
|
83
|
-
symbol: string
|
|
84
|
-
value: bigint
|
|
85
|
-
usd: {
|
|
86
|
-
value: number
|
|
87
|
-
formatted: string
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const TOKEN_BALANCES_QUERY_KEY = "passport.tokenBalances"
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Hook to get the balance of a list of Mezo tokens for the current account
|
|
95
|
-
* @param options.tokens The list of tokens to get the balance for. It will
|
|
96
|
-
* fallback to all tokens if not provided.
|
|
97
|
-
* @param options.queryOptions The query options to pass to the
|
|
98
|
-
* `useReadContracts`
|
|
99
|
-
* @returns Tanstack's `useQuery` returnings with balance of tokens for the
|
|
100
|
-
* current account in form of typesafe object with token names as keys
|
|
101
|
-
* and balances as values.
|
|
102
|
-
* @example
|
|
103
|
-
* const mezoTokensBalance = useTokensBalances({
|
|
104
|
-
* tokens: ["mT", "mxSolvBTC"],
|
|
105
|
-
* })
|
|
106
|
-
* // Assuming the status is "success"
|
|
107
|
-
* console.log(mezoTokensBalance.data.mT) // Eg. { value: 0n ... }
|
|
108
|
-
* console.log(Object.keys(mezoTokensBalance.data)) // ["mT", "mxSolvBTC"]
|
|
109
|
-
*/
|
|
110
|
-
export function useTokensBalances<T extends MezoChainToken[]>(
|
|
111
|
-
options: UseMezoChainTokensBalancesOptions<T> = {},
|
|
112
|
-
) {
|
|
113
|
-
const { tokens = BALANCE_TOKENS, ...restQueryOptions } = options
|
|
114
|
-
|
|
115
|
-
const walletAccount = useWalletAccount()
|
|
116
|
-
|
|
117
|
-
const config = useConfig()
|
|
118
|
-
|
|
119
|
-
const { data: conversionRatesData } = useAssetsConversionRates()
|
|
120
|
-
|
|
121
|
-
const isMezoChainId = isMezoChain(config.state.chainId)
|
|
122
|
-
|
|
123
|
-
const { data: borrowData } = useBorrowData()
|
|
124
|
-
|
|
125
|
-
return useQuery({
|
|
126
|
-
queryKey: [
|
|
127
|
-
TOKEN_BALANCES_QUERY_KEY,
|
|
128
|
-
walletAccount?.accountAddress,
|
|
129
|
-
options.tokens,
|
|
130
|
-
config.state.chainId,
|
|
131
|
-
borrowData?.collateral.value.toString(),
|
|
132
|
-
],
|
|
133
|
-
enabled:
|
|
134
|
-
!!walletAccount?.accountAddress &&
|
|
135
|
-
!!conversionRatesData &&
|
|
136
|
-
isMezoChainId &&
|
|
137
|
-
!!borrowData?.collateral.value,
|
|
138
|
-
queryFn: async () => {
|
|
139
|
-
const { chainId } = config.state
|
|
140
|
-
const isMainnet = chainId === CHAIN_ID.mainnet
|
|
141
|
-
const contractsMap = isMainnet
|
|
142
|
-
? mainnetTokenContracts
|
|
143
|
-
: testnetTokenContracts
|
|
144
|
-
|
|
145
|
-
const accountAddress = walletAccount?.accountAddress
|
|
146
|
-
|
|
147
|
-
if (!accountAddress) {
|
|
148
|
-
throw new Error("Account address is not available.")
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const tokenContracts = tokens.map((token) => {
|
|
152
|
-
const { address } = contractsMap[token]
|
|
153
|
-
return {
|
|
154
|
-
address,
|
|
155
|
-
abi: BALANCE_OF_ABI,
|
|
156
|
-
functionName: "balanceOf" as const,
|
|
157
|
-
args: [accountAddress],
|
|
158
|
-
chainId,
|
|
159
|
-
}
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
return Promise.all([
|
|
163
|
-
getBalance(config, { address: accountAddress }),
|
|
164
|
-
readContracts(config, {
|
|
165
|
-
contracts: tokenContracts,
|
|
166
|
-
}),
|
|
167
|
-
])
|
|
168
|
-
},
|
|
169
|
-
select: (data) => {
|
|
170
|
-
const [btcBalance, tokensBalancesData] = data
|
|
171
|
-
if (!conversionRatesData) {
|
|
172
|
-
throw new Error("Conversion rates data is not available.")
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const parsedBtcBalance = {
|
|
176
|
-
...btcBalance,
|
|
177
|
-
symbol: getAsset("BTC").symbol,
|
|
178
|
-
usd: convertToUsd(
|
|
179
|
-
btcBalance.value,
|
|
180
|
-
btcBalance.decimals,
|
|
181
|
-
BigInt(conversionRatesData.rates.BTC),
|
|
182
|
-
conversionRatesData.decimals,
|
|
183
|
-
),
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const parsedTokensBalances = tokensBalancesData.map((item, index) => {
|
|
187
|
-
const token = tokens[index]
|
|
188
|
-
const { decimals, symbol } = getAsset(token)
|
|
189
|
-
|
|
190
|
-
if (item.status === "failure") {
|
|
191
|
-
throw new Error(
|
|
192
|
-
`Failed to fetch balance of ${token} for ${walletAccount?.accountAddress}.`,
|
|
193
|
-
)
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const floatAmount = bigIntToFloat({
|
|
197
|
-
amount: item.result,
|
|
198
|
-
decimals,
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
// const formatted =
|
|
202
|
-
// Number(floatAmount) * getTokenUsdPrice(token as SupportedTokenName)
|
|
203
|
-
|
|
204
|
-
const tokenBalance: Omit<TokenBalance, "usd"> = {
|
|
205
|
-
value: item.result,
|
|
206
|
-
decimals,
|
|
207
|
-
symbol,
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
let value = Number(item.result)
|
|
211
|
-
let usd = {
|
|
212
|
-
value,
|
|
213
|
-
formatted: floatToHumanReadableFormat(amountAsUSD({ amount: value })),
|
|
214
|
-
}
|
|
215
|
-
if (isBitcoinLikeCryptoAsset(tokenBalance.symbol)) {
|
|
216
|
-
// TODO: had to add number for rates to avoid red underline
|
|
217
|
-
value = Number(floatAmount) * Number(conversionRatesData.rates.BTC)
|
|
218
|
-
usd = {
|
|
219
|
-
value,
|
|
220
|
-
formatted: floatToHumanReadableFormat(
|
|
221
|
-
amountAsUSD({ amount: value }),
|
|
222
|
-
),
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
if (isTTokenCryptoAsset(tokenBalance.symbol)) {
|
|
226
|
-
// TODO: had to add number for rates to avoid red underline
|
|
227
|
-
value = Number(floatAmount) * Number(conversionRatesData.rates.mT)
|
|
228
|
-
usd = {
|
|
229
|
-
value,
|
|
230
|
-
formatted: floatToHumanReadableFormat(
|
|
231
|
-
amountAsUSD({ amount: value }),
|
|
232
|
-
),
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
return { ...tokenBalance, usd }
|
|
237
|
-
})
|
|
238
|
-
|
|
239
|
-
return [parsedBtcBalance, ...parsedTokensBalances].reduce(
|
|
240
|
-
(acc, token) => ({
|
|
241
|
-
...acc,
|
|
242
|
-
[token.symbol]: token,
|
|
243
|
-
}),
|
|
244
|
-
{} as Record<T[number] | "BTC", TokenBalance>,
|
|
245
|
-
)
|
|
246
|
-
},
|
|
247
|
-
...restQueryOptions,
|
|
248
|
-
})
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Hook for invalidating current user's token balances. Can be used to
|
|
253
|
-
* invalidate the balances manually, which forces the data to be re-fetched.
|
|
254
|
-
* @returns Function `invalidateTokenBalances` that invalidates token balances
|
|
255
|
-
* @example
|
|
256
|
-
* const { invalidateTokenBalances } = useInvalidateTokensBalances()
|
|
257
|
-
* (...)
|
|
258
|
-
* await invalidateTokenBalances()
|
|
259
|
-
*/
|
|
260
|
-
export function useInvalidateTokensBalances() {
|
|
261
|
-
const queryClient = useQueryClient()
|
|
262
|
-
|
|
263
|
-
const invalidateTokensBalancesHandler = () =>
|
|
264
|
-
queryClient.invalidateQueries({ queryKey: [TOKEN_BALANCES_QUERY_KEY] })
|
|
265
|
-
|
|
266
|
-
return {
|
|
267
|
-
invalidateTokensBalances: invalidateTokensBalancesHandler,
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Hook for resetting current user's token balances. Can be used to reset the
|
|
273
|
-
* balances manually, which forces the data to be re-fetched.
|
|
274
|
-
* @returns Function `resetTokenBalances` that invalidates token balances
|
|
275
|
-
* @example
|
|
276
|
-
* const { resetTokenBalances } = useResetTokensBalances()
|
|
277
|
-
* (...)
|
|
278
|
-
* await resetTokenBalances()
|
|
279
|
-
*/
|
|
280
|
-
export function useResetTokensBalances() {
|
|
281
|
-
const queryClient = useQueryClient()
|
|
282
|
-
|
|
283
|
-
const resetTokenBalancesHandler = () =>
|
|
284
|
-
queryClient.resetQueries({ queryKey: [TOKEN_BALANCES_QUERY_KEY] })
|
|
285
|
-
|
|
286
|
-
return {
|
|
287
|
-
resetTokenBalances: resetTokenBalancesHandler,
|
|
288
|
-
}
|
|
289
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { Address } from "viem"
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
address: "0x7b7c000000000000000000000000000000000015" as Address,
|
|
5
|
-
abi: [
|
|
6
|
-
{
|
|
7
|
-
inputs: [],
|
|
8
|
-
name: "decimals",
|
|
9
|
-
outputs: [
|
|
10
|
-
{
|
|
11
|
-
internalType: "uint8",
|
|
12
|
-
name: "",
|
|
13
|
-
type: "uint8",
|
|
14
|
-
},
|
|
15
|
-
],
|
|
16
|
-
stateMutability: "view",
|
|
17
|
-
type: "function",
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
inputs: [],
|
|
21
|
-
name: "latestRoundData",
|
|
22
|
-
outputs: [
|
|
23
|
-
{
|
|
24
|
-
internalType: "uint80",
|
|
25
|
-
name: "roundId",
|
|
26
|
-
type: "uint80",
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
internalType: "int256",
|
|
30
|
-
name: "answer",
|
|
31
|
-
type: "int256",
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
internalType: "uint256",
|
|
35
|
-
name: "startedAt",
|
|
36
|
-
type: "uint256",
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
internalType: "uint256",
|
|
40
|
-
name: "updatedAt",
|
|
41
|
-
type: "uint256",
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
internalType: "uint80",
|
|
45
|
-
name: "answeredInRound",
|
|
46
|
-
type: "uint80",
|
|
47
|
-
},
|
|
48
|
-
],
|
|
49
|
-
stateMutability: "view",
|
|
50
|
-
type: "function",
|
|
51
|
-
},
|
|
52
|
-
] as const,
|
|
53
|
-
}
|
package/src/utils/chain.ts
DELETED
|
@@ -1,309 +0,0 @@
|
|
|
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
|
-
})
|