From 7931f74b5edb50e0fda1635ad8b813039bf28309 Mon Sep 17 00:00:00 2001 From: Jin Date: Fri, 28 Jun 2024 06:07:46 +0900 Subject: [PATCH] Match logic of balance badge to format max input value (#5901) --- .../Swap/hooks/useSwapInputsController.ts | 15 ++++++------- src/__swaps__/utils/numbers.ts | 21 ++++++++++++++++++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts index d2a327f2b86..258ab8fa4dd 100644 --- a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts +++ b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts @@ -18,7 +18,11 @@ import { CrosschainQuote, Quote, QuoteError, SwapType, getCrosschainQuote, getQu import { useAnimatedInterval } from '@/hooks/reanimated/useAnimatedInterval'; import store from '@/redux/store'; import { swapsStore } from '@/state/swaps/swapsStore'; -import { convertAmountToNativeDisplayWorklet, convertRawAmountToDecimalFormat } from '@/__swaps__/utils/numbers'; +import { + convertAmountToNativeDisplayWorklet, + convertRawAmountToDecimalFormat, + handleSignificantDecimalsWorklet, +} from '@/__swaps__/utils/numbers'; import { NavigationSteps } from './useSwapNavigation'; import { RainbowError, logger } from '@/logger'; import { @@ -134,14 +138,7 @@ export function useSwapInputsController({ } if (equalWorklet(inputValues.value.inputAmount, internalSelectedInputAsset.value.maxSwappableAmount)) { - const formattedAmount = valueBasedDecimalFormatter({ - amount: inputValues.value.inputAmount, - isStablecoin: internalSelectedInputAsset.value?.type === 'stablecoin' ?? false, - nativePrice: inputNativePrice.value, - roundingMode: 'none', - stripSeparators: false, - }); - + const formattedAmount = handleSignificantDecimalsWorklet(inputValues.value.inputAmount, internalSelectedInputAsset.value.decimals); return formattedAmount; } else { return addCommasToNumber(inputValues.value.inputAmount, '0'); diff --git a/src/__swaps__/utils/numbers.ts b/src/__swaps__/utils/numbers.ts index a8656bd0a02..a20c54b6664 100644 --- a/src/__swaps__/utils/numbers.ts +++ b/src/__swaps__/utils/numbers.ts @@ -5,6 +5,7 @@ import { isNil } from 'lodash'; import { supportedNativeCurrencies } from '@/references'; import { BigNumberish } from '@/__swaps__/utils/hex'; +import { lessThanWorklet, orderOfMagnitudeWorklet } from '../safe-math/SafeMath'; type nativeCurrencyType = typeof supportedNativeCurrencies; @@ -146,11 +147,29 @@ export const handleSignificantDecimalsWithThreshold = (value: BigNumberish, deci return lessThan(result, threshold) ? `< ${threshold}` : result; }; +export const handleSignificantDecimalsWorklet = (value: number | string, decimals: number, buffer = 3): string => { + 'worklet'; + let dec; + + if (lessThanWorklet(value, 1)) { + const orderOfMagnitude = orderOfMagnitudeWorklet(value); + const sigDigitsWithBuffer = -orderOfMagnitude - 1 + buffer; + dec = Math.min(sigDigitsWithBuffer, 8); + } else { + dec = Math.min(decimals, buffer); + } + return Number(value).toLocaleString('en-US', { + useGrouping: true, + minimumFractionDigits: 2, + maximumFractionDigits: dec, + }); +}; + export const handleSignificantDecimals = (value: BigNumberish, decimals: number, buffer = 3, skipDecimals = false): string => { let dec; if (lessThan(new BigNumber(value).abs(), 1)) { dec = new BigNumber(value).toFixed()?.slice?.(2).search(/[^0]/g) + buffer; - dec = Math.min(decimals, 8); + dec = Math.min(dec, 8); } else { dec = Math.min(decimals, buffer); }