@mezo-org/passport 0.5.0 → 0.5.1-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.
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Root/AccountBalance.js +6 -2
- package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -1
- package/dist/src/utils/number2.d.ts +106 -0
- package/dist/src/utils/number2.d.ts.map +1 -0
- package/dist/src/utils/number2.js +289 -0
- package/dist/src/utils/number2.js.map +1 -0
- package/package.json +1 -1
- package/src/components/Dropdown/Root/AccountBalance.tsx +7 -0
- package/src/utils/number2.ts +419 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountBalance.d.ts","sourceRoot":"","sources":["../../../../../src/components/Dropdown/Root/AccountBalance.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,UAAU,EAAgB,MAAM,qBAAqB,CAAA;AAS9D,KAAK,mBAAmB,GAAG,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,CAAA;AAEvD,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,KAAK,EAAE,mBAAmB,
|
|
1
|
+
{"version":3,"file":"AccountBalance.d.ts","sourceRoot":"","sources":["../../../../../src/components/Dropdown/Root/AccountBalance.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,UAAU,EAAgB,MAAM,qBAAqB,CAAA;AAS9D,KAAK,mBAAmB,GAAG,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,CAAA;AAEvD,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,KAAK,EAAE,mBAAmB,qBA8DhE"}
|
|
@@ -11,16 +11,20 @@ export default function AccountBalance(props) {
|
|
|
11
11
|
const { overrides, ...restProps } = props;
|
|
12
12
|
const [, theme] = useStyletron();
|
|
13
13
|
const { nativeBalanceRefetchInterval } = usePassportContext();
|
|
14
|
-
const { data: tokensBalancesData, isError: isTokensBalancesError, isPending: isTokensBalancesDataPending, } = useTokensBalances({
|
|
14
|
+
const { data: tokensBalancesData, isError: isTokensBalancesError, isPending: isTokensBalancesDataPending, error: tokensBalancesError, } = useTokensBalances({
|
|
15
15
|
queryOptions: {
|
|
16
16
|
refetchInterval: nativeBalanceRefetchInterval,
|
|
17
17
|
},
|
|
18
18
|
});
|
|
19
|
-
const { data: borrowData, isError: isBorrowDataError, isPending: isBorrowDataPending, } = useBorrowData();
|
|
19
|
+
const { data: borrowData, isError: isBorrowDataError, isPending: isBorrowDataPending, error: borrowDataError, } = useBorrowData();
|
|
20
20
|
if (isTokensBalancesDataPending || isBorrowDataPending) {
|
|
21
21
|
return React.createElement(ListingItem, { isLoading: true, overrides: overrides });
|
|
22
22
|
}
|
|
23
23
|
if (isTokensBalancesError || isBorrowDataError) {
|
|
24
|
+
console.log("isTokensBalancesError: ", isTokensBalancesError);
|
|
25
|
+
console.log("tokensBalancesError: ", tokensBalancesError);
|
|
26
|
+
console.log("isBorrowDataError: ", isBorrowDataError);
|
|
27
|
+
console.log("borrowDataError: ", borrowDataError);
|
|
24
28
|
return (React.createElement(AccountError, { padding: `${theme.sizing.scale500} ${theme.sizing.scale800}`, topic: "account balance", overrides: overrides }));
|
|
25
29
|
}
|
|
26
30
|
const totalBalanceInUsd = Object.values(tokensBalancesData).reduce((acc, tokenBalance) => acc + tokenBalance.usd.value, 0n);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountBalance.js","sourceRoot":"","sources":["../../../../../src/components/Dropdown/Root/AccountBalance.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAc,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAA;AAClC,OAAO,WAAW,MAAM,gBAAgB,CAAA;AACx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AC,GAAG,GAAG,YAAY,CAAC,GAAG,CAAC,KAAK,EACnD,EAAE,CACH,CAAA;IAED,MAAM,kBAAkB,GAAG,MAAM,CAC/B,WAAW,CAAC,iBAAiB,EAAE,wBAAwB,CAAC,CACzD,CAAA;IAED,MAAM,eAAe,GAAG,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;IAE9D,OAAO,CACL,oBAAC,WAAW,IACV,KAAK,EAAC,cAAc,EACpB,KAAK,EAAE,kBAAkB,EACzB,QAAQ,EAAC,mBAAmB,EAC5B,QAAQ,EAAE,eAAe,EACzB,SAAS,EAAE,SAAS,KAChB,SAAS,GACb,CACH,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export declare const FLOATING_POINT_REGEX: RegExp;
|
|
2
|
+
/** PUBLIC HELPER FUNCTIONS for calculations */
|
|
3
|
+
/**
|
|
4
|
+
* Multiplies a bigint with a decimal number, taking into account the decimal places.
|
|
5
|
+
*
|
|
6
|
+
* @param bigint - The bigint value to multiply.
|
|
7
|
+
* @param number - The decimal number to multiply with.
|
|
8
|
+
* @returns The result of the multiplication as a bigint.
|
|
9
|
+
*/
|
|
10
|
+
export declare const multiplyBigIntWithDecimal: (bigint: bigint, number: number) => bigint;
|
|
11
|
+
/**
|
|
12
|
+
* Converts a fixed-point number to another fixed-point number with a different decimal precision.
|
|
13
|
+
* @param args The arguments for the conversion.
|
|
14
|
+
* - amount: required, the fixed-point number to convert.
|
|
15
|
+
* - decimals: required, the current decimal precision of the number.
|
|
16
|
+
* - targetDecimals: required, the target decimal precision for the conversion.
|
|
17
|
+
* @returns The converted fixed-point number.
|
|
18
|
+
*/
|
|
19
|
+
export declare const convertDecimals: ({ amount, decimals, targetDecimals, }: {
|
|
20
|
+
amount: bigint;
|
|
21
|
+
decimals: number;
|
|
22
|
+
targetDecimals: number;
|
|
23
|
+
}) => bigint;
|
|
24
|
+
/** Helper functions for input components */
|
|
25
|
+
/**
|
|
26
|
+
* Trims decimal places to a specified precision - NO ROUNDING!
|
|
27
|
+
* @param value The value to trim
|
|
28
|
+
* @param precision The number of decimal places to keep
|
|
29
|
+
* @returns The trimmed value
|
|
30
|
+
*/
|
|
31
|
+
export declare const trimDecimals: (value: string, precision: number) => string;
|
|
32
|
+
/**
|
|
33
|
+
* Trims integer part to a specified length - NO ROUNDING!
|
|
34
|
+
* @param value The value to trim
|
|
35
|
+
* @param precision The number of integer places to keep
|
|
36
|
+
* @returns The trimmed value
|
|
37
|
+
* @deprecated just compare the value to the max allowed value in the input component
|
|
38
|
+
*/
|
|
39
|
+
export declare const trimBeforeDecimals: (value: string, precision: number) => string;
|
|
40
|
+
/**
|
|
41
|
+
* Normalizes a decimal number input by removing invalid characters and preventing multiple dots.
|
|
42
|
+
* @param value The value to normalize
|
|
43
|
+
* @returns The normalized value
|
|
44
|
+
*/
|
|
45
|
+
export declare const normalizeDecimalNumber: (value: string) => string;
|
|
46
|
+
/**
|
|
47
|
+
* CONVERSION FUNCTIONS
|
|
48
|
+
* These functions convert between floating-point numbers and big integers as well as
|
|
49
|
+
* provide human-readable formats.
|
|
50
|
+
*
|
|
51
|
+
* They are used to ensure that numbers are correctly formatted for display and calculations
|
|
52
|
+
* and should be used in the UI and business logic to ensure correct number formatting.
|
|
53
|
+
*/
|
|
54
|
+
/** Basic conversion bigint <-> float */
|
|
55
|
+
/**
|
|
56
|
+
* Converts a floating-point number to a big integer representation.
|
|
57
|
+
* @param args The arguments for the conversion.
|
|
58
|
+
* - amount: required, the floating-point number to convert.
|
|
59
|
+
* - decimals: optional, the number of decimal places to consider.
|
|
60
|
+
* @returns The big integer representation of the floating-point number.
|
|
61
|
+
*/
|
|
62
|
+
export declare const floatToBigInt: ({ amount, decimals, }: {
|
|
63
|
+
amount: string | number;
|
|
64
|
+
decimals?: number;
|
|
65
|
+
}) => bigint;
|
|
66
|
+
/**
|
|
67
|
+
* Converts a big integer representation to a floating-point number.
|
|
68
|
+
* @param args The arguments for the conversion.
|
|
69
|
+
* - amount: required, the big integer representation to convert.
|
|
70
|
+
* - decimals: optional, the number of decimal places to consider.
|
|
71
|
+
* - desiredDecimals: optional, the desired number of decimal places for the output.
|
|
72
|
+
* @returns The floating-point representation of the big integer.
|
|
73
|
+
*/
|
|
74
|
+
export declare const bigIntToFloat: ({ amount, decimals, desiredDecimals, }: {
|
|
75
|
+
amount: bigint;
|
|
76
|
+
decimals?: number;
|
|
77
|
+
desiredDecimals?: number;
|
|
78
|
+
}) => string;
|
|
79
|
+
/** Human-readable formats */
|
|
80
|
+
type AmountAsHumanReadableArguments<T extends number | string | bigint> = {
|
|
81
|
+
amount: T;
|
|
82
|
+
desiredDecimals?: number;
|
|
83
|
+
minDecimals?: number;
|
|
84
|
+
decimals?: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Helpers for setting correct arguments and getting unified human-readable
|
|
88
|
+
* formats across the app with _toHumanReadableFormat functions.
|
|
89
|
+
*/
|
|
90
|
+
export declare const amountAsUSD: <T extends number | string | bigint>(args: AmountAsHumanReadableArguments<T>) => AmountAsHumanReadableArguments<T>;
|
|
91
|
+
export declare const amountAsToken: <T extends number | string | bigint>(args: AmountAsHumanReadableArguments<T>) => AmountAsHumanReadableArguments<T>;
|
|
92
|
+
export declare const amountAsMusd: <T extends number | string | bigint>(args: AmountAsHumanReadableArguments<T>) => AmountAsHumanReadableArguments<T>;
|
|
93
|
+
export declare const amountAsMats: <T extends number | string | bigint>(args: AmountAsHumanReadableArguments<T>) => AmountAsHumanReadableArguments<T>;
|
|
94
|
+
/**
|
|
95
|
+
* Converts a floating-point number to a human-readable format - with thousands
|
|
96
|
+
* separators and specified decimal places.
|
|
97
|
+
* @param args The arguments for the conversion.
|
|
98
|
+
* - amount: required, the floating-point number to convert.
|
|
99
|
+
* - desiredDecimals: optional, the desired number of decimal places for the output.
|
|
100
|
+
* - minDecimals: optional, the minimum number of decimal places.
|
|
101
|
+
* @returns The human-readable format of the floating-point number.
|
|
102
|
+
*/
|
|
103
|
+
export declare const floatToHumanReadableFormat: ({ amount, desiredDecimals, minDecimals, }: AmountAsHumanReadableArguments<number | string>) => string;
|
|
104
|
+
export declare const bigIntToHumanReadableFormat: ({ amount, decimals, desiredDecimals, minDecimals, }: AmountAsHumanReadableArguments<bigint>) => string;
|
|
105
|
+
export {};
|
|
106
|
+
//# sourceMappingURL=number2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number2.d.ts","sourceRoot":"","sources":["../../../src/utils/number2.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,oBAAoB,QAAsC,CAAA;AAuFvE,+CAA+C;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,GACpC,QAAQ,MAAM,EACd,QAAQ,MAAM,KACb,MAqBF,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,uCAI7B;IACD,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,cAAc,EAAE,MAAM,CAAA;CACvB,KAAG,MAMH,CAAA;AAED,4CAA4C;AAE5C;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,EAAE,WAAW,MAAM,KAAG,MAK/D,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GAC7B,OAAO,MAAM,EACb,WAAW,MAAM,KAChB,MAMF,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,MAAM,WACM,CAAA;AAE1D;;;;;;;GAOG;AAEH,yCAAyC;AAEzC;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,GAAI,uBAG3B;IACD,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,KAAG,MAeH,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GAAI,wCAI3B;IACD,MAAM,EAAE,MAAM,CAAA;IACd,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const FLOATING_POINT_REGEX = /^[^0-9]*([0-9,]+)(?:\.([0-9]*))?$/;
|
|
3
|
+
const FRIENDLY_DECIMALS_AMOUNT = 6;
|
|
4
|
+
/**
|
|
5
|
+
* HELPER FUNCTIONS
|
|
6
|
+
* These functions are used to convert between floating-point numbers and fixed-point numbers.
|
|
7
|
+
* They handle parsing, conversion, and formatting of numbers with specific decimal places.
|
|
8
|
+
* They shouldn't be used directly in the UI or business logic.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Parses a floating-point string into a fixed-point number representation.
|
|
12
|
+
* @param floatingPointString The floating-point string to parse.
|
|
13
|
+
* @returns The fixed-point number representation or undefined if parsing fails.
|
|
14
|
+
*/
|
|
15
|
+
const parseToFixedPointNumber = (floatingPointString) => {
|
|
16
|
+
if (!floatingPointString.match(FLOATING_POINT_REGEX)) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const [whole, decimals, ...extra] = floatingPointString.split(".");
|
|
20
|
+
// Only one `.` supported.
|
|
21
|
+
if (extra.length > 0) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const noThousandsSeparatorWhole = whole.replace(",", "");
|
|
25
|
+
const setDecimals = decimals ?? "";
|
|
26
|
+
try {
|
|
27
|
+
return {
|
|
28
|
+
amount: BigInt([noThousandsSeparatorWhole, setDecimals].join("")),
|
|
29
|
+
decimals: setDecimals.length,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Separates thousands in a number with commas.
|
|
38
|
+
* @param value The value to format.
|
|
39
|
+
* @param minDecimals The minimum number of decimal places.
|
|
40
|
+
* @param maxDecimals The maximum number of decimal places.
|
|
41
|
+
* @returns The formatted value with thousands separators.
|
|
42
|
+
*/
|
|
43
|
+
const separateThousandsWithComma = (value, minDecimals = 0, maxDecimals = 2) => {
|
|
44
|
+
let adjustedValue = value;
|
|
45
|
+
let adjustedMaxDecimals = maxDecimals;
|
|
46
|
+
// If passed value is not numeric we should return 0
|
|
47
|
+
if (Number.isNaN(+value.toString())) {
|
|
48
|
+
adjustedValue = 0;
|
|
49
|
+
}
|
|
50
|
+
if (typeof adjustedValue === "string") {
|
|
51
|
+
adjustedValue = +adjustedValue;
|
|
52
|
+
}
|
|
53
|
+
if (minDecimals > maxDecimals) {
|
|
54
|
+
adjustedMaxDecimals = minDecimals;
|
|
55
|
+
}
|
|
56
|
+
// Ensure maximumFractionDigits is within allowed range [0, 20]
|
|
57
|
+
const safeMaxDecimals = Math.max(0, Math.min(adjustedMaxDecimals, 20));
|
|
58
|
+
// @ts-expect-error - `maximumFractionDigits` wants to get number less than 21,
|
|
59
|
+
// but as the tokens have 18 decimals have, we can safely pass a parameter
|
|
60
|
+
return adjustedValue.toLocaleString("en-US", {
|
|
61
|
+
minimumFractionDigits: minDecimals,
|
|
62
|
+
maximumFractionDigits: safeMaxDecimals,
|
|
63
|
+
roundingMode: "halfExpand",
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
/** PUBLIC HELPER FUNCTIONS for calculations */
|
|
67
|
+
/**
|
|
68
|
+
* Multiplies a bigint with a decimal number, taking into account the decimal places.
|
|
69
|
+
*
|
|
70
|
+
* @param bigint - The bigint value to multiply.
|
|
71
|
+
* @param number - The decimal number to multiply with.
|
|
72
|
+
* @returns The result of the multiplication as a bigint.
|
|
73
|
+
*/
|
|
74
|
+
export const multiplyBigIntWithDecimal = (bigint, number) => {
|
|
75
|
+
const numberString = number.toString();
|
|
76
|
+
const scientificMatch = /^1e-(\d+)$/.exec(numberString);
|
|
77
|
+
// Ensure we can correctly deal with scientific number expression
|
|
78
|
+
if (scientificMatch) {
|
|
79
|
+
const exponent = parseInt(scientificMatch[1], 10);
|
|
80
|
+
const scale = BigInt(10 ** exponent);
|
|
81
|
+
return (bigint * 1n) / scale;
|
|
82
|
+
}
|
|
83
|
+
const decimalsPart = number.toString().split(".")[1];
|
|
84
|
+
const decimalsCount = decimalsPart ? decimalsPart.length : 0;
|
|
85
|
+
if (!decimalsCount)
|
|
86
|
+
return bigint * BigInt(number);
|
|
87
|
+
const scale = BigInt(10 ** decimalsCount);
|
|
88
|
+
const scaledNumber = BigInt(Math.round(number * Number(scale)));
|
|
89
|
+
return (bigint * scaledNumber) / scale;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Converts a fixed-point number to another fixed-point number with a different decimal precision.
|
|
93
|
+
* @param args The arguments for the conversion.
|
|
94
|
+
* - amount: required, the fixed-point number to convert.
|
|
95
|
+
* - decimals: required, the current decimal precision of the number.
|
|
96
|
+
* - targetDecimals: required, the target decimal precision for the conversion.
|
|
97
|
+
* @returns The converted fixed-point number.
|
|
98
|
+
*/
|
|
99
|
+
export const convertDecimals = ({ amount, decimals, targetDecimals, }) => {
|
|
100
|
+
if (decimals >= targetDecimals) {
|
|
101
|
+
return amount / 10n ** BigInt(decimals - targetDecimals);
|
|
102
|
+
}
|
|
103
|
+
return amount * 10n ** BigInt(targetDecimals - decimals);
|
|
104
|
+
};
|
|
105
|
+
/** Helper functions for input components */
|
|
106
|
+
/**
|
|
107
|
+
* Trims decimal places to a specified precision - NO ROUNDING!
|
|
108
|
+
* @param value The value to trim
|
|
109
|
+
* @param precision The number of decimal places to keep
|
|
110
|
+
* @returns The trimmed value
|
|
111
|
+
*/
|
|
112
|
+
export const trimDecimals = (value, precision) => {
|
|
113
|
+
const [integerPart, decimalPart = ""] = value.split(".");
|
|
114
|
+
return decimalPart.length > precision
|
|
115
|
+
? `${integerPart}.${decimalPart.slice(0, precision)}`
|
|
116
|
+
: value;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Trims integer part to a specified length - NO ROUNDING!
|
|
120
|
+
* @param value The value to trim
|
|
121
|
+
* @param precision The number of integer places to keep
|
|
122
|
+
* @returns The trimmed value
|
|
123
|
+
* @deprecated just compare the value to the max allowed value in the input component
|
|
124
|
+
*/
|
|
125
|
+
export const trimBeforeDecimals = (value, precision) => {
|
|
126
|
+
const [integerPart, decimalPart = ""] = value.split(".");
|
|
127
|
+
return integerPart.length > precision
|
|
128
|
+
? `${integerPart.slice(0, precision)}.${decimalPart}`
|
|
129
|
+
: value;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Normalizes a decimal number input by removing invalid characters and preventing multiple dots.
|
|
133
|
+
* @param value The value to normalize
|
|
134
|
+
* @returns The normalized value
|
|
135
|
+
*/
|
|
136
|
+
export const normalizeDecimalNumber = (value) => value.replace(/[^0-9.]/g, "").replace(/\.(?=.*\.)/g, ""); // Remove all dots except the first one
|
|
137
|
+
/**
|
|
138
|
+
* CONVERSION FUNCTIONS
|
|
139
|
+
* These functions convert between floating-point numbers and big integers as well as
|
|
140
|
+
* provide human-readable formats.
|
|
141
|
+
*
|
|
142
|
+
* They are used to ensure that numbers are correctly formatted for display and calculations
|
|
143
|
+
* and should be used in the UI and business logic to ensure correct number formatting.
|
|
144
|
+
*/
|
|
145
|
+
/** Basic conversion bigint <-> float */
|
|
146
|
+
/**
|
|
147
|
+
* Converts a floating-point number to a big integer representation.
|
|
148
|
+
* @param args The arguments for the conversion.
|
|
149
|
+
* - amount: required, the floating-point number to convert.
|
|
150
|
+
* - decimals: optional, the number of decimal places to consider.
|
|
151
|
+
* @returns The big integer representation of the floating-point number.
|
|
152
|
+
*/
|
|
153
|
+
export const floatToBigInt = ({ amount, decimals = 18, }) => {
|
|
154
|
+
const parsedAmount = amount.toString();
|
|
155
|
+
const fixedPointAmount = parseToFixedPointNumber(parsedAmount);
|
|
156
|
+
if (typeof fixedPointAmount === "undefined") {
|
|
157
|
+
return 0n;
|
|
158
|
+
}
|
|
159
|
+
const bigIntAmount = convertDecimals({
|
|
160
|
+
amount: fixedPointAmount.amount,
|
|
161
|
+
decimals: fixedPointAmount.decimals,
|
|
162
|
+
targetDecimals: decimals,
|
|
163
|
+
});
|
|
164
|
+
return bigIntAmount;
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Converts a big integer representation to a floating-point number.
|
|
168
|
+
* @param args The arguments for the conversion.
|
|
169
|
+
* - amount: required, the big integer representation to convert.
|
|
170
|
+
* - decimals: optional, the number of decimal places to consider.
|
|
171
|
+
* - desiredDecimals: optional, the desired number of decimal places for the output.
|
|
172
|
+
* @returns The floating-point representation of the big integer.
|
|
173
|
+
*/
|
|
174
|
+
export const bigIntToFloat = ({ amount, decimals = 18, desiredDecimals = decimals, }) => {
|
|
175
|
+
let isNegative = false;
|
|
176
|
+
let normalizedAmount = amount;
|
|
177
|
+
if (amount < 0n) {
|
|
178
|
+
normalizedAmount = -amount;
|
|
179
|
+
isNegative = true;
|
|
180
|
+
}
|
|
181
|
+
const desiredDecimalsAmount = normalizedAmount / 10n ** BigInt(Math.max(0, decimals - desiredDecimals));
|
|
182
|
+
// Desired decimals amount with additional decimal to check if number
|
|
183
|
+
// should be rounded up
|
|
184
|
+
const desiredDecimalsAmountWithAdditionalDecimal = normalizedAmount /
|
|
185
|
+
10n ** BigInt(Math.max(0, decimals - desiredDecimals - 1));
|
|
186
|
+
// We are getting the first value that has been cut off
|
|
187
|
+
const firstDecimalAfterCutOff = desiredDecimalsAmountWithAdditionalDecimal
|
|
188
|
+
.toString()
|
|
189
|
+
.at(-1);
|
|
190
|
+
// Padding amount when its length is smaller than desired
|
|
191
|
+
const parsedDesiredAmount = desiredDecimalsAmount
|
|
192
|
+
.toString()
|
|
193
|
+
.padStart(desiredDecimals + 1, "0");
|
|
194
|
+
const integersPart = parsedDesiredAmount.slice(0, -desiredDecimals);
|
|
195
|
+
const decimalsPart = parsedDesiredAmount.slice(-desiredDecimals);
|
|
196
|
+
// If number is above or equal to 5 we should round up the number
|
|
197
|
+
const shouldIncreaseDecimalsPart = Number(firstDecimalAfterCutOff) >= 5 && desiredDecimals !== decimals;
|
|
198
|
+
// If above is true we add 1 to the decimals value and convert it back to string
|
|
199
|
+
const adjustedDecimalsPart = shouldIncreaseDecimalsPart
|
|
200
|
+
? (Number(decimalsPart) + 1).toString().padStart(desiredDecimals, "0")
|
|
201
|
+
: decimalsPart;
|
|
202
|
+
// Determining whether integer part needs to be increased due to rounding up
|
|
203
|
+
const shouldIncreaseIntegerPart = (decimalsPart === "0" && adjustedDecimalsPart !== decimalsPart) ||
|
|
204
|
+
adjustedDecimalsPart.length > decimalsPart.length;
|
|
205
|
+
// If above is true we add 1 to the integers value and convert it back to string
|
|
206
|
+
// and set first value of decimals part to 0
|
|
207
|
+
const adjustedIntegersPart = shouldIncreaseIntegerPart
|
|
208
|
+
? (Number(integersPart) + 1).toString()
|
|
209
|
+
: integersPart;
|
|
210
|
+
const decimalsPartAfterIntegersAdjustments = shouldIncreaseIntegerPart
|
|
211
|
+
? `0${adjustedDecimalsPart.slice(1)}`
|
|
212
|
+
: adjustedDecimalsPart;
|
|
213
|
+
// Inserting "." to separate integers part from decimal part
|
|
214
|
+
const desiredAmount = [
|
|
215
|
+
...adjustedIntegersPart,
|
|
216
|
+
".",
|
|
217
|
+
...decimalsPartAfterIntegersAdjustments,
|
|
218
|
+
].join("");
|
|
219
|
+
// Remove trailing zeros and the trailing decimal point (if exists)
|
|
220
|
+
const trimmedDesiredAmount = desiredAmount.replace(/(?:\.0*|(\.\d+?)0+)$/, "$1");
|
|
221
|
+
return isNegative ? `-${trimmedDesiredAmount}` : trimmedDesiredAmount;
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Helpers for setting correct arguments and getting unified human-readable
|
|
225
|
+
* formats across the app with _toHumanReadableFormat functions.
|
|
226
|
+
*/
|
|
227
|
+
export const amountAsUSD = (args) => ({
|
|
228
|
+
desiredDecimals: 2,
|
|
229
|
+
minDecimals: 2,
|
|
230
|
+
...args,
|
|
231
|
+
});
|
|
232
|
+
export const amountAsToken = (args) => ({
|
|
233
|
+
desiredDecimals: FRIENDLY_DECIMALS_AMOUNT,
|
|
234
|
+
minDecimals: 0,
|
|
235
|
+
...args,
|
|
236
|
+
});
|
|
237
|
+
export const amountAsMusd = (args) => ({
|
|
238
|
+
desiredDecimals: 2,
|
|
239
|
+
minDecimals: 2,
|
|
240
|
+
decimals: 18, // MUSD has 18 decimals
|
|
241
|
+
...args,
|
|
242
|
+
});
|
|
243
|
+
export const amountAsMats = (args) => ({
|
|
244
|
+
desiredDecimals: 0,
|
|
245
|
+
minDecimals: 0,
|
|
246
|
+
...args,
|
|
247
|
+
});
|
|
248
|
+
/**
|
|
249
|
+
* Converts a floating-point number to a human-readable format - with thousands
|
|
250
|
+
* separators and specified decimal places.
|
|
251
|
+
* @param args The arguments for the conversion.
|
|
252
|
+
* - amount: required, the floating-point number to convert.
|
|
253
|
+
* - desiredDecimals: optional, the desired number of decimal places for the output.
|
|
254
|
+
* - minDecimals: optional, the minimum number of decimal places.
|
|
255
|
+
* @returns The human-readable format of the floating-point number.
|
|
256
|
+
*/
|
|
257
|
+
export const floatToHumanReadableFormat = ({ amount, desiredDecimals = 2, // treat is as an USD amount by default
|
|
258
|
+
minDecimals = 0, }) => separateThousandsWithComma(amount, minDecimals, desiredDecimals);
|
|
259
|
+
export const bigIntToHumanReadableFormat = ({ amount, decimals = 18, desiredDecimals = FRIENDLY_DECIMALS_AMOUNT, // treat is as a token by default
|
|
260
|
+
minDecimals = 0, }) => {
|
|
261
|
+
let isNegative = false;
|
|
262
|
+
let normalizedAmount = amount;
|
|
263
|
+
let adjustedDesiredDecimals = desiredDecimals;
|
|
264
|
+
if (minDecimals > desiredDecimals) {
|
|
265
|
+
adjustedDesiredDecimals = minDecimals;
|
|
266
|
+
}
|
|
267
|
+
if (normalizedAmount < 0n) {
|
|
268
|
+
normalizedAmount = -normalizedAmount;
|
|
269
|
+
isNegative = true;
|
|
270
|
+
}
|
|
271
|
+
const trimmedDesiredAmount = bigIntToFloat({
|
|
272
|
+
amount: normalizedAmount,
|
|
273
|
+
decimals,
|
|
274
|
+
desiredDecimals: adjustedDesiredDecimals,
|
|
275
|
+
});
|
|
276
|
+
if (trimmedDesiredAmount === "0" && normalizedAmount !== 0n) {
|
|
277
|
+
const decimalsString = "1".padStart(adjustedDesiredDecimals, "0");
|
|
278
|
+
return `<0.${decimalsString}`;
|
|
279
|
+
}
|
|
280
|
+
const desiredValue = isNegative
|
|
281
|
+
? `-${trimmedDesiredAmount}`
|
|
282
|
+
: trimmedDesiredAmount;
|
|
283
|
+
return floatToHumanReadableFormat({
|
|
284
|
+
amount: desiredValue,
|
|
285
|
+
desiredDecimals: adjustedDesiredDecimals,
|
|
286
|
+
minDecimals,
|
|
287
|
+
});
|
|
288
|
+
};
|
|
289
|
+
//# sourceMappingURL=number2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number2.js","sourceRoot":"","sources":["../../../src/utils/number2.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,MAAM,CAAC,MAAM,oBAAoB,GAAG,mCAAmC,CAAA;AACvE,MAAM,wBAAwB,GAAG,CAAC,CAAA;AAOlC;;;;;GAKG;AAEH;;;;GAIG;AACH,MAAM,uBAAuB,GAAG,CAC9B,mBAA2B,EACG,EAAE;IAChC,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,oBAAoB,CAAC,EAAE,CAAC;QACrD,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC,GAAG,mBAAmB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAElE,0BAA0B;IAC1B,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACrB,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,MAAM,yBAAyB,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;IACxD,MAAM,WAAW,GAAG,QAAQ,IAAI,EAAE,CAAA;IAElC,IAAI,CAAC;QACH,OAAO;YACL,MAAM,EAAE,MAAM,CAAC,CAAC,yBAAyB,EAAE,WAAW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GAAG,EAAE,EAAE,CAAC;QAC1B,gBAAgB,GAAG,CAAC,gBAAgB,CAAA;QACpC,UAAU,GAAG,IAAI,CAAA;IACnB,CAAC;IAED,MAAM,oBAAoB,GAAG,aAAa,CAAC;QACzC,MAAM,EAAE,gBAAgB;QACxB,QAAQ;QACR,eAAe,EAAE,uBAAuB;KACzC,CAAC,CAAA;IAEF,IAAI,oBAAoB,KAAK,GAAG,IAAI,gBAAgB,KAAK,EAAE,EAAE,CAAC;QAC5D,MAAM,cAAc,GAAG,GAAG,CAAC,QAAQ,CAAC,uBAAuB,EAAE,GAAG,CAAC,CAAA;QACjE,OAAO,MAAM,cAAc,EAAE,CAAA;IAC/B,CAAC;IAED,MAAM,YAAY,GAAG,UAAU;QAC7B,CAAC,CAAC,IAAI,oBAAoB,EAAE;QAC5B,CAAC,CAAC,oBAAoB,CAAA;IAExB,OAAO,0BAA0B,CAAC;QAChC,MAAM,EAAE,YAAY;QACpB,eAAe,EAAE,uBAAuB;QACxC,WAAW;KACZ,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -19,6 +19,7 @@ export default function AccountBalance(props: AccountBalanceProps) {
|
|
|
19
19
|
data: tokensBalancesData,
|
|
20
20
|
isError: isTokensBalancesError,
|
|
21
21
|
isPending: isTokensBalancesDataPending,
|
|
22
|
+
error: tokensBalancesError,
|
|
22
23
|
} = useTokensBalances({
|
|
23
24
|
queryOptions: {
|
|
24
25
|
refetchInterval: nativeBalanceRefetchInterval,
|
|
@@ -28,6 +29,7 @@ export default function AccountBalance(props: AccountBalanceProps) {
|
|
|
28
29
|
data: borrowData,
|
|
29
30
|
isError: isBorrowDataError,
|
|
30
31
|
isPending: isBorrowDataPending,
|
|
32
|
+
error: borrowDataError,
|
|
31
33
|
} = useBorrowData()
|
|
32
34
|
|
|
33
35
|
if (isTokensBalancesDataPending || isBorrowDataPending) {
|
|
@@ -35,6 +37,11 @@ export default function AccountBalance(props: AccountBalanceProps) {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
if (isTokensBalancesError || isBorrowDataError) {
|
|
40
|
+
console.log("isTokensBalancesError: ", isTokensBalancesError)
|
|
41
|
+
console.log("tokensBalancesError: ", tokensBalancesError)
|
|
42
|
+
console.log("isBorrowDataError: ", isBorrowDataError)
|
|
43
|
+
console.log("borrowDataError: ", borrowDataError)
|
|
44
|
+
|
|
38
45
|
return (
|
|
39
46
|
<AccountError
|
|
40
47
|
padding={`${theme.sizing.scale500} ${theme.sizing.scale800}`}
|
|
@@ -0,0 +1,419 @@
|
|
|
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
|
+
}
|