Skip to content

Commit

Permalink
fix battery decimals & forcing (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokin0andrey authored Oct 3, 2024
1 parent 4ccd647 commit a7c2d28
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ class BatteryRechargeScreen(wallet: WalletEntity): BaseListWalletScreen<ScreenCo
finish()
}

private fun sign(request: SignRequestEntity) {
viewModel.sign(request).catch {
private fun sign(request: SignRequestEntity, forceRelayer: Boolean) {
viewModel.sign(request, forceRelayer).catch {
showError(it.bestMessage)
}.onEach {
postDelayed(1000) {
Expand All @@ -166,7 +166,7 @@ class BatteryRechargeScreen(wallet: WalletEntity): BaseListWalletScreen<ScreenCo

private fun onEvent(event: BatteryRechargeEvent) {
when (event) {
is BatteryRechargeEvent.Sign -> sign(event.request)
is BatteryRechargeEvent.Sign -> sign(event.request, event.forceRelayer)
is BatteryRechargeEvent.Error -> showError()
is BatteryRechargeEvent.MaxAmountError -> {
val message = requireContext().getString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class BatteryRechargeViewModel(
private val promoStateFlow = MutableStateFlow<PromoState>(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("")

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

Expand Down Expand Up @@ -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(),
Expand All @@ -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) {
Expand Down Expand Up @@ -365,7 +377,7 @@ class BatteryRechargeViewModel(
),
network = network,
)
_eventFlow.tryEmit(BatteryRechargeEvent.Sign(request))
_eventFlow.tryEmit(BatteryRechargeEvent.Sign(request, forceRelayer))
}
}.catch {
_eventFlow.tryEmit(BatteryRechargeEvent.Error)
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a7c2d28

Please sign in to comment.