Skip to content

Commit

Permalink
refactor: create InsufficientCoinJoinMoneyException for dry run excep…
Browse files Browse the repository at this point in the history
…tion handling
  • Loading branch information
HashEngineering committed Jan 22, 2024
1 parent 1952198 commit 6d5b983
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
20 changes: 9 additions & 11 deletions wallet/src/de/schildbach/wallet/ui/send/SendCoinsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -174,17 +175,8 @@ class SendCoinsFragment: Fragment(R.layout.send_coins_fragment) {
} else if (dryRunException != null) {
errorMessage = when (dryRunException) {
is Wallet.DustySendRequested -> getString(R.string.send_coins_error_dusty_send)
is InsufficientMoneyException -> if (!requirePinForBalance || userAuthorizedDuring) {
getString(
if (viewModel.isCoinJoinInsufficentMoneyException) {
R.string.send_coins_error_insufficient_mixed_money
} else {
R.string.send_coins_error_insufficient_money
}
)
} else {
""
}
is InsufficientCoinJoinMoneyException -> getErrorMessage(R.string.send_coins_error_insufficient_mixed_money)
is InsufficientMoneyException -> getErrorMessage(R.string.send_coins_error_insufficient_money)
is Wallet.CouldNotAdjustDownwards -> getString(R.string.send_coins_error_dusty_send)
else -> dryRunException.toString()
}
Expand All @@ -207,6 +199,12 @@ class SendCoinsFragment: Fragment(R.layout.send_coins_fragment) {
)
}

private fun getErrorMessage(@StringRes errorMessage: Int) = if (!requirePinForBalance || userAuthorizedDuring) {
getString(errorMessage)
} else {
""
}

private suspend fun authenticateOrConfirm() {
if (!viewModel.everythingPlausible()) {
return
Expand Down
12 changes: 6 additions & 6 deletions wallet/src/de/schildbach/wallet/ui/send/SendCoinsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import javax.inject.Inject
import kotlin.math.min

class SendException(message: String) : Exception(message)
class InsufficientCoinJoinMoneyException(ex: InsufficientMoneyException) : InsufficientMoneyException(ex.missing, "${ex.message} [coinjoin]")

@HiltViewModel
class SendCoinsViewModel @Inject constructor(
Expand Down Expand Up @@ -124,11 +125,6 @@ class SendCoinsViewModel @Inject constructor(

private var coinJoinMode: CoinJoinMode = CoinJoinMode.NONE

val isCoinJoinInsufficentMoneyException: Boolean
get() {
return coinJoinMode != CoinJoinMode.NONE && !currentAmount.isGreaterThan(wallet.getBalance(MaxOutputAmountCoinSelector()))
}

init {
blockchainStateDao.observeState()
.filterNotNull()
Expand Down Expand Up @@ -303,7 +299,11 @@ class SendCoinsViewModel @Inject constructor(
dryrunSendRequest = sendRequest
_dryRunSuccessful.value = true
} catch (ex: Exception) {
dryRunException = ex
dryRunException = if (ex is InsufficientMoneyException && coinJoinMode != CoinJoinMode.NONE && !currentAmount.isGreaterThan(wallet.getBalance(MaxOutputAmountCoinSelector()))) {
InsufficientCoinJoinMoneyException(ex)
} else {
ex
}
_dryRunSuccessful.value = false
}
}
Expand Down

0 comments on commit 6d5b983

Please sign in to comment.