diff --git a/features/swap/impl/src/main/java/com/tangem/feature/swap/model/SwapModel.kt b/features/swap/impl/src/main/java/com/tangem/feature/swap/model/SwapModel.kt index a54036ab54..c1b113f3c2 100644 --- a/features/swap/impl/src/main/java/com/tangem/feature/swap/model/SwapModel.kt +++ b/features/swap/impl/src/main/java/com/tangem/feature/swap/model/SwapModel.kt @@ -101,7 +101,6 @@ import com.tangem.feature.swap.models.states.SwapNotificationUM import com.tangem.feature.swap.router.SwapRoute import com.tangem.feature.swap.ui.StateBuilder import com.tangem.feature.swap.ui.transfer.SwapTransferStateBuilder -import com.tangem.feature.swap.utils.formatToUIRepresentation import com.tangem.feature.swap.utils.getContractAddress import com.tangem.features.approval.api.GiveApprovalComponent import com.tangem.features.approval.api.GiveApprovalEntryComponent @@ -1709,17 +1708,19 @@ internal class SwapModel @Inject constructor( * value is converted to the active input currency for display, while quotes still use crypto. */ private fun applyCryptoAmount( - cryptoValue: String, + cryptoAmount: SwapAmount, forceQuotesUpdate: Boolean = false, reduceBalanceBy: BigDecimal = BigDecimal.ZERO, ) { + val cryptoValue = cryptoAmount.value.parseBigDecimal(cryptoAmount.decimals) val fromSwapCurrencyStatus = dataState.fromSwapCurrencyStatus val fiatRate = fromSwapCurrencyStatus?.status?.value?.fiatRate val fieldValue = if (fromSwapCurrencyStatus != null && isFiatInput.value && fiatRate != null) { - cryptoValue.toFiatFromCrypto(fiatRate) + cryptoAmount.value.toFiatFromCrypto(fiatRate) } else { cryptoValue } + updateAmount( cryptoValue = cryptoValue, fieldValue = fieldValue, @@ -1818,7 +1819,7 @@ internal class SwapModel @Inject constructor( private fun onMaxAmountClicked() { dataState.fromSwapCurrencyStatus?.let { fromCurrency -> val balance = swapInteractor.getTokenBalance(fromCurrency.status) - applyCryptoAmount(balance.formatToUIRepresentation()) + applyCryptoAmount(balance) } } @@ -1835,12 +1836,12 @@ internal class SwapModel @Inject constructor( percent = percent, ) - applyCryptoAmount(newValue.toPlainString()) + applyCryptoAmount(SwapAmount(newValue, fromCurrency.status.currency.decimals)) } private fun onReduceAmountClicked(newAmount: SwapAmount, reduceBalanceBy: BigDecimal = BigDecimal.ZERO) { applyCryptoAmount( - cryptoValue = newAmount.formatToUIRepresentation(), + cryptoAmount = newAmount, forceQuotesUpdate = true, reduceBalanceBy = reduceBalanceBy, ) @@ -1858,8 +1859,13 @@ internal class SwapModel @Inject constructor( .parseBigDecimal(cryptoDecimals) } + private fun BigDecimal.toFiatFromCrypto(fiatRate: BigDecimal): String { + return multiply(fiatRate) + .parseBigDecimal(FIAT_DECIMALS) + } + private fun String.toFiatFromCrypto(fiatRate: BigDecimal): String { - return parseToBigDecimal(FIAT_DECIMALS) + return (parseBigDecimalOrNull() ?: BigDecimal.ZERO) .multiply(fiatRate) .parseBigDecimal(FIAT_DECIMALS) }