Skip to content

Commit 40d2954

Browse files
committed
fix: properly support new rounding handling with balance checks
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 0a0662b commit 40d2954

3 files changed

Lines changed: 31 additions & 17 deletions

File tree

apps/flipcash/features/cash/src/main/kotlin/com/flipcash/app/cash/internal/CashScreenViewModel.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.flipcash.app.core.ui.CurrencyHolder
77
import com.flipcash.app.onramp.ConfirmationEvent
88
import com.flipcash.app.onramp.OnRampAmount
99
import com.flipcash.app.onramp.OnRampAmountController
10-
import com.flipcash.features.cash.BuildConfig
1110
import com.flipcash.features.cash.R
1211
import com.flipcash.services.analytics.AnalyticsEvent
1312
import com.flipcash.services.analytics.FlipcashAnalyticsService
@@ -27,7 +26,6 @@ import com.getcode.opencode.model.financial.Rate
2726
import com.getcode.opencode.model.financial.SendLimit
2827
import com.getcode.opencode.model.financial.TokenWithLocalizedBalance
2928
import com.getcode.opencode.model.financial.minus
30-
import com.getcode.opencode.utils.roundTo
3129
import com.getcode.solana.keys.Mint
3230
import com.getcode.ui.components.text.AmountAnimatedInputUiModel
3331
import com.getcode.ui.components.text.NumberInputHelper
@@ -81,12 +79,17 @@ internal class CashScreenViewModel @Inject constructor(
8179
val maxAvailableForGive: String
8280
get() = maxForGive?.let { Fiat(it.first, it.second).formatted() }.orEmpty()
8381

82+
8483
val isError: Boolean
8584
get() {
8685
if (amountAnimatedModel.amountData.amount.isEmpty()) return false
87-
val enteredAmount = Fiat(amountAnimatedModel.amountData.amount.toDoubleOrNull() ?: 0.0)
8886
if (maxForGive != null) {
89-
if (enteredAmount.doubleValue <= maxForGive.first.roundTo(2)) {
87+
val enteredAmount = Fiat(
88+
fiat = amountAnimatedModel.amountData.amount.toDoubleOrNull() ?: 0.0,
89+
currencyCode = maxForGive.second
90+
)
91+
val limit = Fiat(maxForGive.first, maxForGive.second)
92+
if (enteredAmount.valueLessThanOrEqualTo(limit)) {
9093
return false
9194
}
9295
}
@@ -120,18 +123,17 @@ internal class CashScreenViewModel @Inject constructor(
120123
}
121124

122125
val checkBalanceLimit: () -> Boolean = {
126+
// this balance check differs from withdrawal due to the fact this is a localized check
127+
// whereas withdrawal is USD locked
123128
val amount = stateFlow.value.amountAnimatedModel.amountData.amount.toDoubleOrNull() ?: 0.0
124-
val conversionRate =
125-
exchange.rateToUsd(stateFlow.value.currencyModel.code ?: CurrencyCode.USD)
126-
?: Rate.ignore
127-
val enteredInUsdc = Fiat(
129+
val enteredAmount = Fiat(
128130
fiat = amount,
129131
currencyCode = stateFlow.value.currencyModel.code ?: CurrencyCode.USD
130-
).convertingTo(conversionRate)
131-
val tokenBalance = stateFlow.value.token?.balance?.underlyingTokenAmount ?: Fiat.Zero
132+
)
133+
val tokenBalance = stateFlow.value.token?.balance?.nativeAmount ?: Fiat.Zero
132134

133-
val isOverBalance = enteredInUsdc > tokenBalance.rounded()
134-
if (isOverBalance || conversionRate == Rate.ignore) {
135+
val isOverBalance = enteredAmount.valueGreaterThan(tokenBalance)
136+
if (isOverBalance) {
135137
BottomBarManager.showMessage(
136138
resources.getString(R.string.error_title_youNeedMoreCash),
137139
resources.getString(R.string.error_description_youNeedMoreCash),

apps/flipcash/features/withdrawal/src/main/kotlin/com/flipcash/app/withdrawal/WithdrawalViewModel.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ internal class WithdrawalViewModel @Inject constructor(
102102
val isError: Boolean
103103
get() {
104104
if (amountEntryState.amountAnimatedModel.amountData.amount.isEmpty()) return false
105-
val enteredAmount = Fiat(amountEntryState.amountAnimatedModel.amountData.amount.toDoubleOrNull() ?: 0.0)
106-
if (enteredAmount.doubleValue <= tokenBalance.rounded().doubleValue) {
107-
return false
108-
}
105+
val enteredAmount = Fiat(
106+
fiat = amountEntryState.amountAnimatedModel.amountData.amount.toDoubleOrNull() ?: 0.0,
107+
currencyCode = tokenBalance.currencyCode
108+
)
109109

110-
return true
110+
return !enteredAmount.valueLessThanOrEqualTo(tokenBalance)
111111
}
112112
}
113113

services/opencode/src/main/kotlin/com/getcode/opencode/model/financial/Fiat.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ data class Fiat(
132132
// Comparable implementation
133133
override fun compareTo(other: Fiat): Int = this.quarks.compareTo(other.quarks)
134134

135+
fun valueLessThan(other: Fiat): Boolean =
136+
this.formatted(showPrefix = false).toDouble() < other.formatted(showPrefix = false).toDouble()
137+
138+
fun valueGreaterThan(other: Fiat): Boolean =
139+
this.formatted(showPrefix = false).toDouble() > other.formatted(showPrefix = false).toDouble()
140+
141+
fun valueGreaterThanOrEqualTo(other: Fiat): Boolean =
142+
this.formatted(showPrefix = false).toDouble() >= other.formatted(showPrefix = false).toDouble()
143+
144+
fun valueLessThanOrEqualTo(other: Fiat): Boolean =
145+
this.formatted(showPrefix = false).toDouble() <= other.formatted(showPrefix = false).toDouble()
146+
135147
fun estimatedTokenAmountIn(token: Token?, fractionDigits: Int? = token?.decimals): String {
136148
if (token?.address == Mint.usdc) {
137149
return formatted(showPrefix = false)

0 commit comments

Comments
 (0)