diff --git a/src/__swaps__/screens/Swap/components/ExchangeRateBubble.tsx b/src/__swaps__/screens/Swap/components/ExchangeRateBubble.tsx index 939a27237f1..ba0b6c54c6d 100644 --- a/src/__swaps__/screens/Swap/components/ExchangeRateBubble.tsx +++ b/src/__swaps__/screens/Swap/components/ExchangeRateBubble.tsx @@ -85,27 +85,27 @@ export const ExchangeRateBubble = () => { switch (rotatingIndex.value) { case 0: { - const formattedRate = valueBasedDecimalFormatter( - inputAssetPrice / outputAssetPrice, - outputAssetPrice, - 'up', - -1, - isOutputAssetStablecoin, - false - ); + const formattedRate = valueBasedDecimalFormatter({ + amount: inputAssetPrice / outputAssetPrice, + usdTokenPrice: outputAssetPrice, + roundingMode: 'up', + precisionAdjustment: -1, + isStablecoin: isOutputAssetStablecoin, + stripSeparators: false, + }); fromAssetText.value = `1 ${inputAssetSymbol}`; toAssetText.value = `${formattedRate} ${outputAssetSymbol}`; break; } case 1: { - const formattedRate = valueBasedDecimalFormatter( - outputAssetPrice / inputAssetPrice, - inputAssetPrice, - 'up', - -1, - isInputAssetStablecoin, - false - ); + const formattedRate = valueBasedDecimalFormatter({ + amount: outputAssetPrice / inputAssetPrice, + usdTokenPrice: inputAssetPrice, + roundingMode: 'up', + precisionAdjustment: -1, + isStablecoin: isInputAssetStablecoin, + stripSeparators: false, + }); fromAssetText.value = `1 ${outputAssetSymbol}`; toAssetText.value = `${formattedRate} ${inputAssetSymbol}`; break; diff --git a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts index 9b51700046d..df5d5cdfdae 100644 --- a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts +++ b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts @@ -85,26 +85,26 @@ export function useSwapInputsController({ } if (inputMethod.value === 'outputAmount') { - return valueBasedDecimalFormatter( - inputValues.value.inputAmount, - internalSelectedInputAsset.value.nativePrice, - 'up', - -1, - internalSelectedInputAsset.value?.type === 'stablecoin' ?? false, - false - ); + return valueBasedDecimalFormatter({ + amount: inputValues.value.inputAmount, + usdTokenPrice: internalSelectedInputAsset.value.nativePrice, + roundingMode: 'up', + precisionAdjustment: -1, + isStablecoin: internalSelectedInputAsset.value?.type === 'stablecoin' ?? false, + stripSeparators: false, + }); } const balance = Number(internalSelectedInputAsset.value?.balance.amount || 0); - return niceIncrementFormatter( - incrementDecimalPlaces.value, - balance, - internalSelectedInputAsset.value.nativePrice, - niceIncrement.value, - percentageToSwap.value, - sliderXPosition.value - ); + return niceIncrementFormatter({ + incrementDecimalPlaces: incrementDecimalPlaces.value, + inputAssetBalance: balance, + inputAssetUsdPrice: internalSelectedInputAsset.value.nativePrice, + niceIncrement: niceIncrement.value, + percentageToSwap: percentageToSwap.value, + sliderXPosition: sliderXPosition.value, + }); }); const formattedInputNativeValue = useDerivedValue(() => { @@ -132,14 +132,14 @@ export function useSwapInputsController({ return addCommasToNumber(inputValues.value.outputAmount); } - return valueBasedDecimalFormatter( - inputValues.value.outputAmount, - internalSelectedOutputAsset.value.nativePrice, - 'down', - -1, - internalSelectedOutputAsset.value?.type === 'stablecoin' ?? false, - false - ); + return valueBasedDecimalFormatter({ + amount: inputValues.value.outputAmount, + usdTokenPrice: internalSelectedOutputAsset.value.nativePrice, + roundingMode: 'down', + precisionAdjustment: -1, + isStablecoin: internalSelectedOutputAsset.value?.type === 'stablecoin' ?? false, + stripSeparators: false, + }); }); const formattedOutputNativeValue = useDerivedValue(() => { @@ -572,15 +572,15 @@ export function useSwapInputsController({ } // If the change set the slider position to > 0 - const inputAmount = niceIncrementFormatter( - incrementDecimalPlaces.value, - balance, - internalSelectedInputAsset.value?.nativePrice, - niceIncrement.value, - percentageToSwap.value, - sliderXPosition.value, - true - ); + const inputAmount = niceIncrementFormatter({ + incrementDecimalPlaces: incrementDecimalPlaces.value, + inputAssetBalance: balance, + inputAssetUsdPrice: internalSelectedInputAsset.value?.nativePrice, + niceIncrement: niceIncrement.value, + percentageToSwap: percentageToSwap.value, + sliderXPosition: sliderXPosition.value, + stripSeparators: true, + }); const inputNativeValue = Number(inputAmount) * internalSelectedInputAsset.value?.nativePrice; inputValues.modify(values => { return { @@ -603,15 +603,15 @@ export function useSwapInputsController({ if (!current.assetToSell?.nativePrice || !current.assetToBuy?.nativePrice) return; const balance = Number(current.assetToSell.balance.amount); - const inputAmount = niceIncrementFormatter( - incrementDecimalPlaces.value, - balance, - current.assetToSell.nativePrice, - niceIncrement.value, - percentageToSwap.value, - sliderXPosition.value, - true - ); + const inputAmount = niceIncrementFormatter({ + incrementDecimalPlaces: incrementDecimalPlaces.value, + inputAssetBalance: balance, + inputAssetUsdPrice: current.assetToSell.nativePrice, + niceIncrement: niceIncrement.value, + percentageToSwap: percentageToSwap.value, + sliderXPosition: sliderXPosition.value, + stripSeparators: true, + }); const inputNativeValue = Number(inputAmount) * current.assetToSell.nativePrice; const outputAmount = (inputNativeValue / current.assetToBuy.nativePrice) * (1 - SWAP_FEE); // TODO: Implement swap fee @@ -662,15 +662,15 @@ export function useSwapInputsController({ } // If the change set the slider position to > 0 - const inputAmount = niceIncrementFormatter( - incrementDecimalPlaces.value, - balance, - current.assetToSell.nativePrice, - niceIncrement.value, - percentageToSwap.value, - sliderXPosition.value, - true - ); + const inputAmount = niceIncrementFormatter({ + incrementDecimalPlaces: incrementDecimalPlaces.value, + inputAssetBalance: balance, + inputAssetUsdPrice: current.assetToSell.nativePrice, + niceIncrement: niceIncrement.value, + percentageToSwap: percentageToSwap.value, + sliderXPosition: sliderXPosition.value, + stripSeparators: true, + }); const inputNativeValue = Number(inputAmount) * current.assetToSell.nativePrice; inputValues.modify(values => { return { diff --git a/src/__swaps__/utils/swaps.ts b/src/__swaps__/utils/swaps.ts index 78925562c5e..1d2338becbe 100644 --- a/src/__swaps__/utils/swaps.ts +++ b/src/__swaps__/utils/swaps.ts @@ -190,14 +190,21 @@ export function trimTrailingZeros(value: string) { return withTrimmedZeros.endsWith('.') ? withTrimmedZeros.slice(0, -1) : withTrimmedZeros; } -export function valueBasedDecimalFormatter( - amount: number, - usdTokenPrice: number, - roundingMode?: 'up' | 'down', - precisionAdjustment?: number, - isStablecoin?: boolean, - stripSeparators = true -): string { +export function valueBasedDecimalFormatter({ + amount, + usdTokenPrice, + roundingMode, + precisionAdjustment, + isStablecoin, + stripSeparators = true, +}: { + amount: number; + usdTokenPrice: number; + roundingMode?: 'up' | 'down'; + precisionAdjustment?: number; + isStablecoin?: boolean; + stripSeparators?: boolean; +}): string { 'worklet'; function calculateDecimalPlaces(usdTokenPrice: number, precisionAdjustment?: number): number { @@ -239,21 +246,52 @@ export function valueBasedDecimalFormatter( return numberFormatter.format(roundedAmount); } -export function niceIncrementFormatter( - incrementDecimalPlaces: number, - inputAssetBalance: number, - inputAssetUsdPrice: number, - niceIncrement: number, - percentageToSwap: number, - sliderXPosition: number, - stripSeparators?: boolean -) { +export function niceIncrementFormatter({ + incrementDecimalPlaces, + inputAssetBalance, + inputAssetUsdPrice, + niceIncrement, + percentageToSwap, + sliderXPosition, + stripSeparators, +}: { + incrementDecimalPlaces: number; + inputAssetBalance: number; + inputAssetUsdPrice: number; + niceIncrement: number; + percentageToSwap: number; + sliderXPosition: number; + stripSeparators?: boolean; +}) { 'worklet'; if (percentageToSwap === 0) return '0'; - if (percentageToSwap === 0.25) return valueBasedDecimalFormatter(inputAssetBalance * 0.25, inputAssetUsdPrice, 'up', -3); - if (percentageToSwap === 0.5) return valueBasedDecimalFormatter(inputAssetBalance * 0.5, inputAssetUsdPrice, 'up', -3); - if (percentageToSwap === 0.75) return valueBasedDecimalFormatter(inputAssetBalance * 0.75, inputAssetUsdPrice, 'up', -3); - if (percentageToSwap === 1) return valueBasedDecimalFormatter(inputAssetBalance, inputAssetUsdPrice, 'up'); + if (percentageToSwap === 0.25) + return valueBasedDecimalFormatter({ + amount: inputAssetBalance * 0.25, + usdTokenPrice: inputAssetUsdPrice, + roundingMode: 'up', + precisionAdjustment: -3, + }); + if (percentageToSwap === 0.5) + return valueBasedDecimalFormatter({ + amount: inputAssetBalance * 0.5, + usdTokenPrice: inputAssetUsdPrice, + roundingMode: 'up', + precisionAdjustment: -3, + }); + if (percentageToSwap === 0.75) + return valueBasedDecimalFormatter({ + amount: inputAssetBalance * 0.75, + usdTokenPrice: inputAssetUsdPrice, + roundingMode: 'up', + precisionAdjustment: -3, + }); + if (percentageToSwap === 1) + return valueBasedDecimalFormatter({ + amount: inputAssetBalance, + usdTokenPrice: inputAssetUsdPrice, + roundingMode: 'up', + }); const exactIncrement = inputAssetBalance / 100; const isIncrementExact = niceIncrement === exactIncrement;