Skip to content

Commit 0244613

Browse files
committed
refactor: improve balance sufficiency calculations and update currency handling
- Refactored the calculateBalanceSufficiency function to consistently use BigNumber for USD amounts, enhancing precision in financial calculations. - Updated the MessagesRefillModal to display insufficient funds in USDT, improving user clarity on payment requirements. - Introduced UsdCurrencyAmount for better currency formatting and handling, ensuring accurate representation of amounts in the UI. These changes collectively enhance the accuracy and usability of financial interactions within the application.
1 parent 1656340 commit 0244613

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

src/features/app-messages/ui/MessagesRefillModal.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FC, useEffect, useState } from 'react';
2+
import BigNumber from 'bignumber.js';
23
import {
34
Box,
45
Button,
@@ -18,7 +19,7 @@ import {
1819
UseRadioProps,
1920
VStack
2021
} from '@chakra-ui/react';
21-
import { FilledWarnIcon16, formatWithSuffix, H4 } from 'src/shared';
22+
import { FilledWarnIcon16, formatWithSuffix, H4, UsdCurrencyAmount } from 'src/shared';
2223
import { RadioCard } from 'src/shared/ui/checkbox';
2324
import { AppMessagesPackage } from '../model';
2425
import { RefillModalContent } from 'src/entities';
@@ -39,12 +40,9 @@ const MessagesRefillModal: FC<{
3940
onChange: name => setSelectedPlan(options.find(pkg => pkg.name === name) || null)
4041
});
4142
const group = getRootProps();
42-
const sufficiencyCheck = useBalanceSufficiencyCheck(selectedPlan?.price.amount || null);
43+
const sufficiencyCheck = useBalanceSufficiencyCheck(selectedPlan?.price.amount ?? null);
4344
const notEnoughAmount = sufficiencyCheck ? getPaymentDeficit(sufficiencyCheck) : BigInt(0);
44-
45-
useEffect(() => {
46-
setSelectedPlan(options[0]);
47-
}, [options]);
45+
const notEnoughAmountUsdt = new UsdCurrencyAmount(new BigNumber(notEnoughAmount).div(1e6));
4846

4947
const tonRefillModal = useDisclosure();
5048
const confirmPaymentModal = useDisclosure();
@@ -104,8 +102,8 @@ const MessagesRefillModal: FC<{
104102
{!!notEnoughAmount && (
105103
<Text textStyle="body2" mt="3" color="text.secondary">
106104
<FilledWarnIcon16 />
107-
&nbsp;Not enough ${notEnoughAmount.toString()} to buy the plan, fund your
108-
account
105+
&nbsp;Not enough {notEnoughAmountUsdt.stringCurrencyAmount} USDT to buy the
106+
plan, fund your account
109107
</Text>
110108
)}
111109
</ModalBody>

src/features/balance/model/queries.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,20 @@ function mapDTOBalanceToBalance(
5656
*/
5757
export function calculateBalanceSufficiency(
5858
balance: Balance,
59-
amountInUsd: BigNumber | number,
59+
amountInUsd: BigNumber,
6060
tonRate: number,
6161
includePromo = true
6262
): SufficiencyCheckResult {
63-
const amountBn = amountInUsd instanceof BigNumber ? amountInUsd : new BigNumber(amountInUsd);
64-
6563
// Convert USD amount to cents (since USDT is in 6 decimals, but we work in cents for simplicity)
66-
const amountInCents = amountBn.multipliedBy(100);
64+
const amountInMicroUsdt = amountInUsd.multipliedBy(1e6);
6765

6866
// Check USDT sufficiency
6967
const usdtBalance = includePromo
7068
? balance.usdt.amount + balance.usdt.promo_amount
7169
: balance.usdt.amount;
7270

73-
const usdtSufficient = usdtBalance >= BigInt(amountInCents.toFixed(0));
74-
const usdtDeficit = usdtSufficient ? BigInt(0) : BigInt(amountInCents.toFixed(0)) - usdtBalance;
71+
const usdtSufficient = usdtBalance >= BigInt(amountInMicroUsdt.toFixed(0));
72+
const usdtDeficit = usdtSufficient ? BigInt(0) : BigInt(amountInMicroUsdt.toFixed(0)) - usdtBalance;
7573

7674
// Check TON sufficiency (if TON balance exists)
7775
let tonSufficient = false;
@@ -85,10 +83,10 @@ export function calculateBalanceSufficiency(
8583
// Convert TON to USD equivalent
8684
// tonBalance is in nanoTON (10^9), so convert to TON first
8785
const tonInUsd = Number(tonBalance) / Math.pow(10, TON_DECIMALS) * tonRate;
88-
const tonInUsdCents = new BigNumber(tonInUsd).multipliedBy(100);
86+
const tonInUsdMicroUsd = new BigNumber(tonInUsd).multipliedBy(1e6);
8987

90-
tonSufficient = tonInUsdCents.gte(amountInCents);
91-
tonDeficit = tonSufficient ? BigInt(0) : BigInt(amountInCents.minus(tonInUsdCents).toFixed(0));
88+
tonSufficient = tonInUsdMicroUsd.gte(amountInMicroUsdt);
89+
tonDeficit = tonSufficient ? BigInt(0) : BigInt(amountInMicroUsdt.minus(tonInUsdMicroUsd).toFixed(0));
9290
}
9391

9492
return {
@@ -211,7 +209,7 @@ export function useTonRateQuery() {
211209
* @returns SufficiencyCheckResult or undefined if loading/error
212210
*/
213211
export function useBalanceSufficiencyCheck(
214-
amountInUsd: BigNumber | number | null | undefined,
212+
amountInUsd: BigNumber | null,
215213
options?: { includePromo?: boolean }
216214
): SufficiencyCheckResult | undefined {
217215
const { data: balance } = useBalanceQuery();

0 commit comments

Comments
 (0)