Updated on 2026-08-14

This commit is contained in:
Tangem 2026-06-22 16:00:05 +02:00
parent 0869c78f17
commit 0ab5d41ba0

View file

@ -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)
}