Skip to content

Commit

Permalink
Add Polkadot gas fees & existenial deposit reaping warning (#1404)
Browse files Browse the repository at this point in the history
  • Loading branch information
yvebe authored Nov 26, 2024
1 parent 18c2c8e commit 8b1329a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import androidx.compose.runtime.Immutable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vultisig.wallet.R
import com.vultisig.wallet.data.chains.helpers.PolkadotHelper
import com.vultisig.wallet.data.models.Account
import com.vultisig.wallet.data.models.Address
import com.vultisig.wallet.data.models.AddressBookEntry
import com.vultisig.wallet.data.models.Chain
import com.vultisig.wallet.data.models.ChainId
import com.vultisig.wallet.data.models.Coin
import com.vultisig.wallet.data.models.Coins
import com.vultisig.wallet.data.models.FiatValue
import com.vultisig.wallet.data.models.GasFeeParams
import com.vultisig.wallet.data.models.ImageModel
Expand Down Expand Up @@ -93,6 +95,7 @@ internal data class SendFormUiModel(
val showGasFee: Boolean = true,
val dstAddressError: UiText? = null,
val tokenAmountError: UiText? = null,
val reapingError: UiText? = null,
val hasMemo: Boolean = false,
val showGasSettings: Boolean = false,
val specific: BlockChainSpecificAndUtxo? = null,
Expand Down Expand Up @@ -193,6 +196,7 @@ internal class SendFormViewModel @Inject constructor(
calculateGasFees()
calculateSpecific()
collectAdvanceGasUi()
collectAmountChecks()
}

fun loadData(
Expand Down Expand Up @@ -786,7 +790,6 @@ internal class SendFormViewModel @Inject constructor(
accounts,
) { token, accounts ->
val address = token.address
val showGasFee = !token.allowZeroGas()
val hasMemo = token.isNativeToken

val uiModel = accountToTokenBalanceUiModelMapper.map(SendSrc(
Expand All @@ -807,7 +810,6 @@ internal class SendFormViewModel @Inject constructor(
it.copy(
from = address,
selectedCoin = uiModel,
showGasFee = showGasFee,
hasMemo = hasMemo,
)
}
Expand Down Expand Up @@ -846,6 +848,45 @@ internal class SendFormViewModel @Inject constructor(
}
}

private fun collectAmountChecks() {
viewModelScope.launch {
combine(
selectedToken.filterNotNull(),
tokenAmountFieldState.textAsFlow(),
gasFee.filterNotNull(),
) { selectedToken, tokenAmount, gasFee ->
val selectedAccount = selectedAccount
if (selectedAccount != null &&
selectedToken.chain == Chain.Polkadot &&
selectedToken.ticker == Coins.polkadot.ticker
) {
val balance = selectedAccount.tokenValue
?.value
?: BigInteger.ZERO
val tokenAmountInt = tokenAmount.toString()
.toBigDecimalOrNull()
?.movePointRight(selectedToken.decimal)
?.toBigInteger()
?: BigInteger.ZERO

if (balance - (gasFee.value + tokenAmountInt) <
PolkadotHelper.DEFAULT_EXISTENTIAL_DEPOSIT.toBigInteger()
) {
uiState.update {
it.copy(
reapingError = UiText.StringResource(R.string.send_form_polka_reaping_warning)
)
}
} else {
uiState.update {
it.copy(reapingError = null)
}
}
}
}.collect()
}
}

private suspend fun convertValue(
value: String,
transform: (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.vultisig.wallet.ui.screens.send

import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -282,6 +283,23 @@ internal fun SendFormScreen(
}
)
}

AnimatedContent(
targetState = state.reapingError,
label = "error message"
) { errorMessage ->
if (errorMessage != null) {
Column {
UiSpacer(size = 8.dp)
Text(
text = errorMessage.asString(),
color = Theme.colors.error,
style = Theme.menlo.body1
)
}
}
}

UiSpacer(size = 80.dp)

}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,5 @@
<string name="backup_password_screen_skip" translatable="false">Skip Password</string>
<string name="backup_password_screen_empty_password" translatable="false">Please use \"Skip Password\" to save without password</string>
<string name="network_connection_lost" translatable="false">Network connection lost. Please check your internet connection</string>
<string name="send_form_polka_reaping_warning" translatable="false">Ensure you leave enough funds to cover the fees. If an account balance falls below 1 DOT (Existential Deposit), the account will be deactivated (reaped) and any remaining funds will be lost. You can reactivate the address with a new deposit larger than the existential deposit at any time. However, this will not recover the lost funds.</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,9 @@ class PolkadotHelper(
)
)
}

companion object {
const val DEFAULT_FEE_PLANCKS = 250_000_000L
const val DEFAULT_EXISTENTIAL_DEPOSIT = 10_000_000_000L // 1 DOT
}
}
24 changes: 13 additions & 11 deletions data/src/main/kotlin/com/vultisig/wallet/data/models/Coin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ object Coins {
contractAddress = "",
isNativeToken = true,
)
val polkadot = Coin(
chain = Chain.Polkadot,
ticker = "DOT",
logo = "dot",
address = "",
decimal = 10,
hexPublicKey = "",
priceProviderID = "polkadot",
contractAddress = "",
isNativeToken = true,
)

val SupportedCoins = listOf(
wewe,
Coin(
Expand Down Expand Up @@ -1451,17 +1463,7 @@ object Coins {
contractAddress = "",
isNativeToken = true,
),
Coin(
chain = Chain.Polkadot,
ticker = "DOT",
logo = "dot",
address = "",
decimal = 10,
hexPublicKey = "",
priceProviderID = "polkadot",
contractAddress = "",
isNativeToken = true,
),
polkadot,
Coin(
chain = Chain.ZkSync,
ticker = "ETH",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.vultisig.wallet.data.api.EvmApiFactory
import com.vultisig.wallet.data.api.SolanaApi
import com.vultisig.wallet.data.api.ThorChainApi
import com.vultisig.wallet.data.api.chains.SuiApi
import com.vultisig.wallet.data.chains.helpers.PolkadotHelper
import com.vultisig.wallet.data.chains.helpers.SolanaHelper.Companion.DefaultFeeInLamports
import com.vultisig.wallet.data.crypto.ThorChainHelper
import com.vultisig.wallet.data.models.Chain
Expand Down Expand Up @@ -113,7 +114,7 @@ internal class GasFeeRepositoryImpl @Inject constructor(
Chain.Polkadot -> {
val nativeToken = tokenRepository.getNativeToken(chain.id)
TokenValue(
value = 0.toBigInteger(),
value = PolkadotHelper.DEFAULT_FEE_PLANCKS.toBigInteger(),
unit = chain.feeUnit,
decimals = nativeToken.decimal,
)
Expand Down

0 comments on commit 8b1329a

Please sign in to comment.