Skip to content

Commit

Permalink
Match logic of balance badge to format max input value (#5901)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchung committed Jun 27, 2024
1 parent 8c87e95 commit 7931f74
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand Down
21 changes: 20 additions & 1 deletion src/__swaps__/utils/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7931f74

Please sign in to comment.