From 2d98489526ffad19f3aefd2de26f07e03a42b627 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 20 Apr 2022 13:02:30 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/tap/domain/TapWalletManager.kt | 39 ---------------- .../tap/features/wallet/redux/WalletAction.kt | 2 + .../tap/features/wallet/redux/WalletState.kt | 9 ++-- .../redux/middlewares/WalletMiddleware.kt | 46 +++++++++++++++++++ .../wallet/redux/reducers/WalletReducer.kt | 9 ++++ 5 files changed, 63 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt index 7605a39ee5..611ecbfb50 100644 --- a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt @@ -1,6 +1,5 @@ package com.tangem.tap.domain -import com.tangem.blockchain.blockchains.solana.RentProvider import com.tangem.blockchain.common.* import com.tangem.common.extensions.guard import com.tangem.common.services.Result @@ -15,7 +14,6 @@ import com.tangem.tap.common.ThrottlerWithValues import com.tangem.tap.common.extensions.dispatchDebugErrorNotification import com.tangem.tap.common.extensions.dispatchOnMain import com.tangem.tap.common.extensions.safeUpdate -import com.tangem.tap.common.extensions.stripZeroPlainString import com.tangem.tap.common.redux.global.FiatCurrencyName import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.currenciesRepository @@ -25,8 +23,6 @@ import com.tangem.tap.domain.extensions.makePrimaryWalletManager import com.tangem.tap.domain.extensions.makeWalletManagersForApp import com.tangem.tap.domain.tokens.BlockchainNetwork import com.tangem.tap.features.demo.isDemoCard -import com.tangem.tap.features.wallet.models.PendingTransactionType -import com.tangem.tap.features.wallet.models.getPendingTransactions import com.tangem.tap.features.wallet.redux.Currency import com.tangem.tap.features.wallet.redux.WalletAction import com.tangem.tap.network.NetworkConnectivity @@ -60,7 +56,6 @@ class TapWalletManager { } when (result) { is Result.Success -> { - checkForRentWarning(walletManager) dispatchOnMain(WalletAction.LoadWallet.Success(result.data, blockchainNetwork)) } is Result.Failure -> { @@ -329,40 +324,6 @@ class TapWalletManager { else -> null } } - - private suspend fun checkForRentWarning(walletManager: WalletManager) { - val rentProvider = walletManager as? RentProvider ?: return - - when (val result = rentProvider.minimalBalanceForRentExemption()) { - is com.tangem.blockchain.extensions.Result.Success -> { - fun isNeedToShowWarning(balance: BigDecimal, rentExempt: BigDecimal): Boolean = - balance < rentExempt - - val balance = walletManager.wallet.fundsAvailable(AmountType.Coin) - val outgoingTxs = - walletManager.wallet.getPendingTransactions(PendingTransactionType.Outgoing) - val rentExempt = result.data - val show = if (outgoingTxs.isEmpty()) { - isNeedToShowWarning(balance, rentExempt) - } else { - val outgoingAmount = outgoingTxs.sumOf { it.amount ?: BigDecimal.ZERO } - val rest = balance.minus(outgoingAmount) - isNeedToShowWarning(rest, rentExempt) - } - if (!show) return - - val currency = walletManager.wallet.blockchain.currency - dispatchOnMain( - WalletAction.SetWalletRent( - blockchain = BlockchainNetwork.fromWalletManager(walletManager), - minRent = ("${rentProvider.rentAmount().stripZeroPlainString()} $currency"), - rentExempt = ("${rentExempt.stripZeroPlainString()} $currency") - ) - ) - } - is com.tangem.blockchain.extensions.Result.Failure -> {} - } - } } fun Wallet.getFirstToken(): Token? { diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt index 03c58d7f42..77549829d7 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt @@ -159,4 +159,6 @@ sealed class WalletAction : Action { val minRent: String, val rentExempt: String ) : WalletAction() + + data class RemoveWalletRent(val blockchain: BlockchainNetwork) : WalletAction() } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt index 8ad7344077..de5d1ee593 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt @@ -395,6 +395,9 @@ sealed interface Currency { return derivationPath != blockchain.derivationPath(derivationStyle)?.rawPath } + fun isBlockchain(): Boolean = this is Blockchain + + fun isToken(): Boolean = this is Token companion object { fun fromBlockchainNetwork( @@ -402,13 +405,13 @@ sealed interface Currency { token: com.tangem.blockchain.common.Token? = null ): Currency { return if (token != null) { - Currency.Token( + Token( token = token, blockchain = blockchainNetwork.blockchain, derivationPath = blockchainNetwork.derivationPath ) } else { - Currency.Blockchain( + Blockchain( blockchain = blockchainNetwork.blockchain, derivationPath = blockchainNetwork.derivationPath ) @@ -430,7 +433,7 @@ sealed interface Currency { } fun fromTokenWithBlockchain(tokenWithBlockchain: TokenWithBlockchain): Token { - return Currency.Token( + return Token( token = tokenWithBlockchain.token, blockchain = tokenWithBlockchain.blockchain, derivationPath = null diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt index c7528ad95f..428ef7b4b5 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt @@ -3,8 +3,10 @@ package com.tangem.tap.features.wallet.redux.middlewares import android.content.Intent import android.net.Uri import androidx.core.content.ContextCompat +import com.tangem.blockchain.blockchains.solana.RentProvider import com.tangem.blockchain.common.Amount import com.tangem.blockchain.common.AmountType +import com.tangem.blockchain.common.WalletManager import com.tangem.common.CompletionResult import com.tangem.common.core.TangemSdkError import com.tangem.common.extensions.isZero @@ -20,9 +22,12 @@ import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.common.redux.navigation.AppScreen import com.tangem.tap.common.redux.navigation.NavigationAction import com.tangem.tap.domain.extensions.toSendableAmounts +import com.tangem.tap.domain.tokens.BlockchainNetwork import com.tangem.tap.features.demo.DemoHelper import com.tangem.tap.features.home.redux.HomeAction import com.tangem.tap.features.send.redux.PrepareSendScreen +import com.tangem.tap.features.wallet.models.PendingTransactionType +import com.tangem.tap.features.wallet.models.getPendingTransactions import com.tangem.tap.features.wallet.redux.* import com.tangem.tap.network.NetworkStateChanged import com.tangem.tap.scope @@ -34,6 +39,7 @@ import kotlinx.coroutines.launch import org.rekotlin.Action import org.rekotlin.DispatchFunction import org.rekotlin.Middleware +import java.math.BigDecimal class WalletMiddleware { private val tradeCryptoMiddleware = TradeCryptoMiddleware() @@ -77,6 +83,7 @@ class WalletMiddleware { } } is WalletAction.LoadWallet.Success -> { + checkForRentWarning(walletState.getWalletManager(action.blockchain)) val coinAmount = action.wallet.amounts[AmountType.Coin]?.value if (coinAmount != null && !coinAmount.isZero()) { if (walletState.getWalletData(action.blockchain) == null) { @@ -289,4 +296,43 @@ class WalletMiddleware { tokenRate = tokenRate ) } + + private fun checkForRentWarning(walletManager: WalletManager?) { + val rentProvider = walletManager as? RentProvider ?: return + + scope.launch { + when (val result = rentProvider.minimalBalanceForRentExemption()) { + is com.tangem.blockchain.extensions.Result.Success -> { + fun isNeedToShowWarning(balance: BigDecimal, rentExempt: BigDecimal): Boolean { + return balance < rentExempt + } + + val balance = walletManager.wallet.fundsAvailable(AmountType.Coin) + val outgoingTxs = walletManager.wallet.getPendingTransactions(PendingTransactionType.Outgoing) + val rentExempt = result.data + val show = if (outgoingTxs.isEmpty()) { + isNeedToShowWarning(balance, rentExempt) + } else { + val outgoingAmount = outgoingTxs.sumOf { it.amount ?: BigDecimal.ZERO } + val rest = balance.minus(outgoingAmount) + isNeedToShowWarning(rest, rentExempt) + } + + val currency = walletManager.wallet.blockchain.currency + if (show) { + dispatchOnMain(WalletAction.SetWalletRent( + blockchain = BlockchainNetwork.fromWalletManager(walletManager), + minRent = ("${rentProvider.rentAmount().stripZeroPlainString()} $currency"), + rentExempt = ("${rentExempt.stripZeroPlainString()} $currency") + )) + } else { + dispatchOnMain(WalletAction.RemoveWalletRent( + blockchain = BlockchainNetwork.fromWalletManager(walletManager), + )) + } + } + is com.tangem.blockchain.extensions.Result.Failure -> {} + } + } + } } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt index 35fab475ae..97e6e778ec 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt @@ -314,6 +314,15 @@ private fun internalReduce(action: Action, state: AppState): WalletState { newState = newState.updateWalletsData(listOf(walletData)) } } + is WalletAction.RemoveWalletRent -> { + var walletData = newState.getWalletData(action.blockchain) + if (walletData == null) { + newState + } else { + walletData = walletData.copy(warningRent = null) + newState = newState.updateWalletsData(listOf(walletData)) + } + } } return newState }