diff --git a/apps/wallet/data/battery/src/main/java/com/tonapps/wallet/data/battery/entity/RechargeMethodEntity.kt b/apps/wallet/data/battery/src/main/java/com/tonapps/wallet/data/battery/entity/RechargeMethodEntity.kt index 6ceb30dcb..514de4089 100644 --- a/apps/wallet/data/battery/src/main/java/com/tonapps/wallet/data/battery/entity/RechargeMethodEntity.kt +++ b/apps/wallet/data/battery/src/main/java/com/tonapps/wallet/data/battery/entity/RechargeMethodEntity.kt @@ -44,7 +44,7 @@ data class RechargeMethodEntity( fun fromTon(amount: BigDecimal): Coins { - return Coins.of(amount.divide(rate.toBigDecimal(), decimals, RoundingMode.HALF_UP)) + return Coins.of(amount.divide(rate.toBigDecimal(), decimals, RoundingMode.HALF_UP), decimals) } fun fromTon(amount: String) = fromTon(amount.toBigDecimal()) diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/component/coin/CoinEditText.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/component/coin/CoinEditText.kt index c2f3b2486..110a4b403 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/component/coin/CoinEditText.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/component/coin/CoinEditText.kt @@ -25,6 +25,8 @@ class CoinEditText @JvmOverloads constructor( private val suffixDrawable = SuffixDrawable(context) + private lateinit var formattingConfig: CoinFormattingConfig + var doOnValueChange: ((Double) -> Unit)? = null var suffix: String? @@ -38,15 +40,22 @@ class CoinEditText @JvmOverloads constructor( setMaxLength(24) setRightDrawable(suffixDrawable) compoundDrawablePadding = 38.dp - val formattingConfig = CoinFormattingConfig(decimals = 9) - setFormattingTextWatcher(CoinFormattingTextWatcher(formattingConfig)) - setFormattingInputFilter(CoinFormattingFilter(formattingConfig)) + setDecimals(9) doAfterTextChanged { val value = getValue() doOnValueChange?.invoke(value) } } + val decimals: Int + get() = formattingConfig.decimals + + fun setDecimals(decimals: Int) { + formattingConfig = CoinFormattingConfig(decimals = decimals) + setFormattingTextWatcher(CoinFormattingTextWatcher(formattingConfig)) + setFormattingInputFilter(CoinFormattingFilter(formattingConfig)) + } + fun getValue(): Double { val text = text.toString() if (text.isEmpty()) { diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeScreen.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeScreen.kt index 99c038408..e6b5e856e 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeScreen.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeScreen.kt @@ -154,8 +154,8 @@ class BatteryRechargeScreen(wallet: WalletEntity): BaseListWalletScreen sign(event.request) + is BatteryRechargeEvent.Sign -> sign(event.request, event.forceRelayer) is BatteryRechargeEvent.Error -> showError() is BatteryRechargeEvent.MaxAmountError -> { val message = requireContext().getString( diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeViewModel.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeViewModel.kt index e0073ea15..1a2f3b8dd 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeViewModel.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/BatteryRechargeViewModel.kt @@ -79,7 +79,7 @@ class BatteryRechargeViewModel( private val promoStateFlow = MutableStateFlow(PromoState.Default) private val _amountFlow = MutableStateFlow(0.0) - private val amountFlow = _amountFlow.map { Coins.of(it) } + private val amountFlow = combine(_amountFlow, tokenFlow) { amount, token -> Coins.of(amount, token.decimals) } private val _addressFlow = MutableStateFlow("") @@ -198,11 +198,13 @@ class BatteryRechargeViewModel( uiItems.add( Item.Amount( symbol = token.symbol, + decimals = token.decimals, formattedRemaining = CurrencyFormatter.format( currency = token.symbol, value = remainingBalance ), formattedMinAmount = CurrencyFormatter.format( - currency = token.symbol, value = minAmount + currency = token.symbol, value = minAmount, + customScale = token.decimals, ), isInsufficientBalance = remainingBalance.isNegative, isLessThanMin = isLessThanMin, @@ -285,6 +287,7 @@ class BatteryRechargeViewModel( destinationFlow ) { (wallet, token), destination -> val rechargeMethod = getRechargeMethod(wallet, token) + val batteryBalance = getBatteryBalance(wallet) val config = getBatteryConfig(wallet) val batteryMaxInputAmount = rechargeMethod.fromTon(api.config.batteryMaxInputAmount) @@ -321,6 +324,15 @@ class BatteryRechargeViewModel( false -> TonNetwork.MAINNET } + val forceRelayer = when { + token.isTon -> false + batteryBalance.balance.value > BigDecimal.ZERO -> true + rechargeMethod.minBootstrapValue != null -> { + amount.value > rechargeMethod.minBootstrapValue!!.toBigDecimal() + } + else -> false + } + if (token.isTon) { val request = SignRequestEntity( fromValue = wallet.contract.address.toAccountId(), @@ -335,7 +347,7 @@ class BatteryRechargeViewModel( ), network = network, ) - _eventFlow.tryEmit(BatteryRechargeEvent.Sign(request)) + _eventFlow.tryEmit(BatteryRechargeEvent.Sign(request, forceRelayer)) } else { val queryId = TransferEntity.newWalletQueryId() val customPayload = if (token.isCompressed) { @@ -365,7 +377,7 @@ class BatteryRechargeViewModel( ), network = network, ) - _eventFlow.tryEmit(BatteryRechargeEvent.Sign(request)) + _eventFlow.tryEmit(BatteryRechargeEvent.Sign(request, forceRelayer)) } }.catch { _eventFlow.tryEmit(BatteryRechargeEvent.Error) @@ -505,8 +517,8 @@ class BatteryRechargeViewModel( } } - fun sign(request: SignRequestEntity) = flow { - val boc = SendTransactionScreen.run(context, wallet, request, forceRelayer = true) + fun sign(request: SignRequestEntity, forceRelayer: Boolean) = flow { + val boc = SendTransactionScreen.run(context, wallet, request, forceRelayer = forceRelayer) emit(boc) }.flowOn(Dispatchers.IO) } \ No newline at end of file diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/entity/BatteryRechargeEvent.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/entity/BatteryRechargeEvent.kt index 9866c9a20..1a7a178b1 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/entity/BatteryRechargeEvent.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/entity/BatteryRechargeEvent.kt @@ -5,7 +5,7 @@ import com.tonapps.icu.Coins import com.tonapps.wallet.data.core.entity.SignRequestEntity sealed class BatteryRechargeEvent { - data class Sign(val request: SignRequestEntity) : BatteryRechargeEvent() + data class Sign(val request: SignRequestEntity, val forceRelayer: Boolean) : BatteryRechargeEvent() data object Error : BatteryRechargeEvent() data class MaxAmountError(val maxAmount: Coins, val currency: String) : BatteryRechargeEvent() } diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/list/Item.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/list/Item.kt index 21c4047b9..fc26c0a6c 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/list/Item.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/list/Item.kt @@ -37,6 +37,7 @@ sealed class Item(type: Int) : BaseListItem(type) { data class Amount( val symbol: String, + val decimals: Int, val formattedRemaining: CharSequence, val formattedMinAmount: CharSequence, val isInsufficientBalance: Boolean, diff --git a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/list/holder/AmountHolder.kt b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/list/holder/AmountHolder.kt index d9ba761cd..83d005eb8 100644 --- a/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/list/holder/AmountHolder.kt +++ b/apps/wallet/instance/app/src/main/java/com/tonapps/tonkeeper/ui/screen/battery/recharge/list/holder/AmountHolder.kt @@ -21,6 +21,9 @@ class AmountHolder( override fun onBind(item: Item.Amount) { amountView.doOnValueChange = onValueChange amountView.suffix = item.symbol + if (amountView.decimals != item.decimals) { + amountView.setDecimals(item.decimals) + } currencyView.text = item.formattedCharges applyAvailable(item.formattedRemaining, item.formattedMinAmount, item.isInsufficientBalance, item.isLessThanMin) amountView.focus()