Skip to content

Commit 47bdad3

Browse files
committed
chore(swap): show specific error when sell amount is too low for fee
When a swap is denied because the amount can't cover the 1% sell fee, show a targeted "Amount Too Low" alert instead of the generic error. Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 0aa9e3e commit 47bdad3

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

apps/flipcash/core/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@
423423
<string name="error_title_buySellFailed">Something Went Wrong</string>
424424
<string name="error_description_buySellFailed">Please try again</string>
425425

426+
<string name="error_title_sellFailedDueToBeingTooLow">Amount Too Low</string>
427+
<string name="error_description_sellFailedDueToBeingTooLow">The amount you entered is too small to cover the required transaction fee. Please enter a larger amount</string>
428+
426429
<string name="label_fromCurrencyAppreciation">from currency appreciation</string>
427430
<string name="label_fromCurrencyDepreciation">from currency depreciation</string>
428431

apps/flipcash/shared/tokens/src/main/kotlin/com/flipcash/app/tokens/ui/BuySellSwapTokenViewModel.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.getcode.manager.BottomBarManager
1616
import com.getcode.opencode.controllers.TransactionOperations
1717
import com.getcode.opencode.exchange.Exchange
1818
import com.getcode.opencode.internal.solana.model.SwapId
19+
import com.getcode.opencode.model.core.errors.SwapError
1920
import com.getcode.opencode.model.financial.Currency
2021
import com.getcode.opencode.model.financial.CurrencyCode
2122
import com.getcode.opencode.model.financial.Fiat
@@ -584,9 +585,18 @@ class BuySellSwapTokenViewModel @Inject constructor(
584585
dispatchEvent(Event.UpdateSellState(loading = false, success = true))
585586
// sell submitted, drop from balance
586587
tokenCoordinator.subtract(token, amount)
587-
}.onFailure {
588-
trackTransaction(token, error = it)
588+
}.onFailure { cause ->
589+
trackTransaction(token, error = cause)
589590
dispatchEvent(Event.UpdateSellState(loading = false, success = false))
591+
if (cause is SwapError.Denied) {
592+
if (cause.amountTooLowForFee) {
593+
BottomBarManager.showAlert(
594+
title = resources.getString(R.string.error_title_sellFailedDueToBeingTooLow),
595+
message = resources.getString(R.string.error_description_sellFailedDueToBeingTooLow),
596+
)
597+
return@onFailure
598+
}
599+
}
590600
BottomBarManager.showError(
591601
title = resources.getString(R.string.error_title_buySellFailed),
592602
message = resources.getString(R.string.error_description_buySellFailed),

services/opencode/src/main/kotlin/com/getcode/opencode/model/core/errors/Errors.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ sealed class SwapError(
215215
override val message: String? = null,
216216
override val cause: Throwable? = null
217217
) : CodeServerError(message, cause) {
218-
data class Denied(private val reasons: List<String>) : SwapError(message = reasons.joinToString())
218+
data class Denied(private val reasons: List<String>) : SwapError(message = reasons.joinToString()) {
219+
val amountTooLowForFee = reasons.contains("swap would not generate a sell fee")
220+
}
219221
class Signature : SwapError(), NotifiableError
220222
class Unrecognized : SwapError("Unrecognized"), NotifiableError
221223
class InvalidSwap(reasons: List<String>): SwapError(message = reasons.joinToString()), NotifiableError

0 commit comments

Comments
 (0)