Updated on 2026-08-14

This commit is contained in:
Tangem 2025-02-25 17:17:19 +05:00
parent 6d7ae39ba2
commit a09aae7e41
23 changed files with 337 additions and 87 deletions

View file

@ -36,7 +36,6 @@ import com.tangem.domain.staking.model.StakingApproval
import com.tangem.domain.staking.model.stakekit.*
import com.tangem.domain.staking.model.stakekit.action.StakingAction
import com.tangem.domain.staking.model.stakekit.action.StakingActionCommonType
import com.tangem.domain.staking.model.stakekit.action.StakingActionType
import com.tangem.domain.staking.model.stakekit.transaction.StakingTransaction
import com.tangem.domain.tokens.*
import com.tangem.domain.tokens.model.CryptoCurrency
@ -45,8 +44,6 @@ import com.tangem.domain.transaction.error.GetFeeError
import com.tangem.domain.transaction.usecase.CreateApprovalTransactionUseCase
import com.tangem.domain.transaction.usecase.GetAllowanceUseCase
import com.tangem.domain.transaction.usecase.SendTransactionUseCase
import com.tangem.domain.transaction.usecase.ValidateTransactionUseCase
import com.tangem.domain.utils.convertToSdkAmount
import com.tangem.domain.wallets.models.UserWallet
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.domain.wallets.usecase.GetUserWalletUseCase
@ -72,6 +69,7 @@ import com.tangem.features.staking.impl.presentation.state.transformers.validato
import com.tangem.features.staking.impl.presentation.state.utils.checkAndCalculateSubtractedAmount
import com.tangem.features.staking.impl.presentation.state.utils.isSingleAction
import com.tangem.features.staking.impl.presentation.state.utils.withStubUnstakeAction
import com.tangem.lib.crypto.BlockchainUtils
import com.tangem.utils.Provider
import com.tangem.utils.coroutines.*
import com.tangem.utils.extensions.isSingleItem
@ -108,7 +106,6 @@ internal class StakingModel @Inject constructor(
private val getCardInfoUseCase: GetCardInfoUseCase,
private val saveBlockchainErrorUseCase: SaveBlockchainErrorUseCase,
private val getBalanceNotEnoughForFeeWarningUseCase: GetBalanceNotEnoughForFeeWarningUseCase,
private val validateTransactionUseCase: ValidateTransactionUseCase,
private val getCurrencyCheckUseCase: GetCurrencyCheckUseCase,
private val isAmountSubtractAvailableUseCase: IsAmountSubtractAvailableUseCase,
private val isAnyTokenStakedUseCase: IsAnyTokenStakedUseCase,
@ -123,7 +120,7 @@ internal class StakingModel @Inject constructor(
private val shareManager: ShareManager,
@DelayedWork private val coroutineScope: CoroutineScope,
private val innerRouter: InnerStakingRouter,
private val appRouter: AppRouter,
appRouter: AppRouter,
) : Model(), StakingClickIntents {
val uiState: StateFlow<StakingUiState> = stateController.uiState
@ -239,7 +236,7 @@ internal class StakingModel @Inject constructor(
return
}
isInitialInfoStep && noBalanceState -> {
stateController.update(
val list = buildList {
SetConfirmationStateInitTransformer(
isEnter = true,
isExplicitExit = false,
@ -248,8 +245,27 @@ internal class StakingModel @Inject constructor(
stakingApproval = stakingApproval,
stakingAllowance = stakingAllowance,
yieldArgs = yield.args,
),
)
).let(::add)
if (BlockchainUtils.isSkipAmountEnter(uiState.value.cryptoCurrencyBlockchainId)) {
ValidatorSelectChangeTransformer(
selectedValidator = yield.preferredValidators.firstOrNull(),
yield = yield,
).let(::add)
SetAmountDataTransformer(
clickIntents = this@StakingModel,
cryptoCurrencyStatusProvider = Provider { cryptoCurrencyStatus },
userWalletProvider = Provider { userWallet },
appCurrencyProvider = Provider { appCurrency },
).let(::add)
AmountMaxValueStateTransformer(
cryptoCurrencyStatus = cryptoCurrencyStatus,
minimumTransactionAmount = minimumTransactionAmount,
actionType = uiState.value.actionType,
yield = yield,
).let(::add)
}
}
stateController.updateAll(*list.toTypedArray())
}
}
stakingStateRouter.onNextClick()
@ -633,16 +649,6 @@ internal class StakingModel @Inject constructor(
} else {
null
}
val validation = amount?.let {
validateTransactionUseCase(
userWalletId = userWalletId,
amount = amount.convertToSdkAmount(cryptoCurrencyStatus.currency),
fee = feeState?.fee,
memo = null,
destination = "",
network = cryptoCurrencyStatus.currency.network,
).leftOrNull()
}
val balanceAfterTransaction = calculateBalanceAfterTransaction(
amount = amount.orZero(),
@ -663,7 +669,6 @@ internal class StakingModel @Inject constructor(
appCurrencyProvider = Provider { appCurrency },
feeCryptoCurrencyStatus = feeCryptoCurrencyStatus,
currencyWarning = currencyWarning,
validatorError = validation,
currencyCheck = currencyStatus,
isSubtractAvailable = isAmountSubtractAvailable,
feeError = feeError,
@ -1021,7 +1026,7 @@ internal class StakingModel @Inject constructor(
}
private fun isExplicitExit(balanceType: BalanceType, pendingAction: PendingAction?): Boolean {
return balanceType == BalanceType.STAKED && pendingAction?.type != StakingActionType.RESTAKE
return balanceType == BalanceType.STAKED && pendingAction?.type?.isRestake == false
}
private fun isAssentState(): Boolean {

View file

@ -29,6 +29,14 @@ internal object StakingNotification {
title = resourceReference(R.string.common_error),
subtitle = subtitle,
)
data class CardanoMinimumBalance(
val title: TextReference,
val subtitle: TextReference,
) : StakingNotification.Error(
title = title,
subtitle = subtitle,
)
}
sealed class Warning(

View file

@ -11,6 +11,7 @@ import com.tangem.features.staking.impl.presentation.state.*
import com.tangem.features.staking.impl.presentation.state.utils.isCompositePendingActions
import com.tangem.features.staking.impl.presentation.state.utils.isTronStakedBalance
import com.tangem.lib.crypto.BlockchainUtils
import com.tangem.lib.crypto.BlockchainUtils.isSkipAmountEnter
import com.tangem.utils.extensions.isPositive
import com.tangem.utils.transformer.Transformer
import kotlinx.collections.immutable.ImmutableList
@ -45,12 +46,12 @@ internal class SetConfirmationStateInitTransformer(
override fun transform(prevState: StakingUiState): StakingUiState {
val actionType = when {
isEnter -> StakingActionCommonType.Enter(
skipEnterAmount = isAmountEnterSkipped(prevState),
skipEnterAmount = isSkipAmountEnter(prevState.cryptoCurrencyBlockchainId),
)
isImplicitExit || isExplicitExit -> StakingActionCommonType.Exit(isPartiallyUnstakeDisabled(prevState))
else -> when (pendingAction?.type) {
StakingActionType.STAKE -> StakingActionCommonType.Enter(
skipEnterAmount = isAmountEnterSkipped(prevState),
skipEnterAmount = isSkipAmountEnter(prevState.cryptoCurrencyBlockchainId),
)
StakingActionType.UNSTAKE -> StakingActionCommonType.Exit(
partiallyUnstakeDisabled = isPartiallyUnstakeDisabled(prevState),
@ -100,8 +101,4 @@ internal class SetConfirmationStateInitTransformer(
val max = exitAmount.maximum ?: return false
return !min.isPositive() && !max.isPositive()
}
private fun isAmountEnterSkipped(state: StakingUiState): Boolean {
return BlockchainUtils.isCardano(state.cryptoCurrencyBlockchainId)
}
}

View file

@ -1,30 +1,33 @@
package com.tangem.features.staking.impl.presentation.state.transformers
import com.tangem.blockchain.common.Blockchain
import com.tangem.common.extensions.remove
import com.tangem.common.ui.amountScreen.converters.AmountStateConverter
import com.tangem.common.ui.amountScreen.models.AmountParameters
import com.tangem.common.ui.amountScreen.models.AmountState
import com.tangem.common.ui.amountScreen.models.EnterAmountBoundary
import com.tangem.core.ui.components.containers.pullToRefresh.PullToRefreshConfig
import com.tangem.core.ui.components.currency.icon.converter.CryptoCurrencyToIconStateConverter
import com.tangem.core.ui.components.list.RoundedListWithDividersItemData
import com.tangem.core.ui.extensions.*
import com.tangem.core.ui.format.bigdecimal.crypto
import com.tangem.core.ui.format.bigdecimal.format
import com.tangem.core.ui.format.bigdecimal.percent
import com.tangem.core.ui.components.containers.pullToRefresh.PullToRefreshConfig
import com.tangem.domain.appcurrency.model.AppCurrency
import com.tangem.domain.staking.model.stakekit.BalanceItem
import com.tangem.domain.staking.model.stakekit.Yield
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.domain.wallets.models.UserWallet
import com.tangem.features.staking.impl.R
import com.tangem.features.staking.impl.presentation.state.*
import com.tangem.features.staking.impl.presentation.model.StakingClickIntents
import com.tangem.features.staking.impl.presentation.state.InnerYieldBalanceState
import com.tangem.features.staking.impl.presentation.state.StakingStates
import com.tangem.features.staking.impl.presentation.state.StakingStep
import com.tangem.features.staking.impl.presentation.state.StakingUiState
import com.tangem.features.staking.impl.presentation.state.bottomsheet.InfoType
import com.tangem.features.staking.impl.presentation.state.converters.RewardsValidatorStateConverter
import com.tangem.features.staking.impl.presentation.state.converters.YieldBalancesConverter
import com.tangem.features.staking.impl.presentation.state.utils.getRewardScheduleText
import com.tangem.features.staking.impl.presentation.model.StakingClickIntents
import com.tangem.lib.crypto.BlockchainUtils.isPolkadot
import com.tangem.utils.Provider
import com.tangem.utils.isNullOrZero
import com.tangem.utils.transformer.Transformer
@ -145,7 +148,8 @@ internal class SetInitialDataStateTransformer(
cryptoCurrencyStatus: CryptoCurrencyStatus,
): RoundedListWithDividersItemData? {
val minimumCryptoAmount = yield.args.enter.args[Yield.Args.ArgType.AMOUNT]?.minimum ?: return null
if (!isPolkadot(cryptoCurrencyStatus.currency.network.id.value)) return null
val blockchainId = cryptoCurrencyStatus.currency.network.id.value
if (!showMinimumRequirementInfo(blockchainId)) return null
val formattedAmount = minimumCryptoAmount.format { crypto(cryptoCurrencyStatus.currency) }
@ -239,6 +243,10 @@ internal class SetInitialDataStateTransformer(
return resourceReference(R.string.common_range, wrappedList(formattedMinApr, formattedMaxApr))
}
private fun showMinimumRequirementInfo(blockchainId: String): Boolean {
return blockchainId == Blockchain.Polkadot.id || blockchainId == Blockchain.Cardano.id
}
private companion object {
val EQUALITY_THRESHOLD = BigDecimal(1E-10)

View file

@ -1,6 +1,5 @@
package com.tangem.features.staking.impl.presentation.state.transformers.notifications
import com.tangem.blockchain.common.transaction.Fee
import com.tangem.common.ui.amountScreen.models.AmountState
import com.tangem.common.ui.notifications.NotificationUM
import com.tangem.common.ui.notifications.NotificationsFactory.addDustWarningNotification
@ -11,7 +10,6 @@ import com.tangem.common.ui.notifications.NotificationsFactory.addFeeUnreachable
import com.tangem.common.ui.notifications.NotificationsFactory.addRentExemptionNotification
import com.tangem.common.ui.notifications.NotificationsFactory.addReserveAmountErrorNotification
import com.tangem.common.ui.notifications.NotificationsFactory.addTransactionLimitErrorNotification
import com.tangem.common.ui.notifications.NotificationsFactory.addValidateTransactionNotifications
import com.tangem.core.ui.extensions.networkIconResId
import com.tangem.domain.appcurrency.model.AppCurrency
import com.tangem.domain.staking.model.stakekit.Yield
@ -40,7 +38,6 @@ internal class AddStakingNotificationsTransformer(
private val appCurrencyProvider: Provider<AppCurrency>,
private val feeCryptoCurrencyStatus: CryptoCurrencyStatus?,
private val currencyWarning: CryptoCurrencyWarning?,
private val validatorError: Throwable?,
private val feeError: GetFeeError?,
private val currencyCheck: CryptoCurrencyCheck,
private val isSubtractAvailable: Boolean,
@ -81,7 +78,7 @@ internal class AddStakingNotificationsTransformer(
amountValue = amountValue,
feeValue = feeValue,
reduceAmountBy = reduceAmountBy,
).max(minimumRequirement)
)
} else {
// No amount is taken from account balance on exit or pending actions
BigDecimal.ZERO
@ -212,15 +209,6 @@ internal class AddStakingNotificationsTransformer(
appCurrency = appCurrency,
cryptoCurrencyStatus = cryptoCurrencyStatus,
)
// blockchain specific
addValidateTransactionNotifications(
dustValue = currencyCheck.dustValue.orZero(),
minAdaValue = (feeState?.fee as? Fee.CardanoToken)?.minAdaValue,
validationError = validatorError,
cryptoCurrency = cryptoCurrency,
onReduceClick = prevState.clickIntents::onAmountReduceToClick,
)
}
private fun MutableList<NotificationUM>.addStakeExceedBalanceNotification(

View file

@ -14,6 +14,7 @@ import com.tangem.features.staking.impl.R
import com.tangem.features.staking.impl.presentation.state.StakingNotification
import com.tangem.features.staking.impl.presentation.state.StakingStates
import com.tangem.features.staking.impl.presentation.state.StakingUiState
import com.tangem.lib.crypto.BlockchainUtils.isCardano
import com.tangem.lib.crypto.BlockchainUtils.isCosmos
import com.tangem.lib.crypto.BlockchainUtils.isTron
import com.tangem.utils.Provider
@ -46,7 +47,10 @@ internal class StakingInfoNotificationsFactory(
when (prevState.actionType) {
is StakingActionCommonType.Enter -> addEnterInfoNotifications(sendingAmount, feeValue)
is StakingActionCommonType.Exit -> addExitInfoNotifications()
is StakingActionCommonType.Pending -> addPendingInfoNotifications(prevState)
is StakingActionCommonType.Pending -> {
addCardanoRestakeMinimumAmountNotification(feeValue)
addPendingInfoNotifications(prevState)
}
}
}
@ -70,7 +74,9 @@ internal class StakingInfoNotificationsFactory(
sendingAmount: BigDecimal,
feeValue: BigDecimal,
) {
addCardanoStakeMinimumAmountNotification(feeValue)
addTronRevoteNotification()
addCardanoStakeNotification()
addStakingEntireBalanceNotification(sendingAmount, feeValue)
}
@ -149,6 +155,48 @@ internal class StakingInfoNotificationsFactory(
}
}
private fun MutableList<NotificationUM>.addCardanoStakeNotification() {
val cryptoCurrencyStatus = cryptoCurrencyStatusProvider()
val isCardano = isCardano(cryptoCurrencyStatus.currency.network.id.value)
if (isCardano) {
add(
StakingNotification.Info.Ordinary(
title = resourceReference(R.string.staking_notification_additional_ada_deposit_title),
text = resourceReference(R.string.staking_notification_additional_ada_deposit_text),
),
)
}
}
private fun MutableList<NotificationUM>.addCardanoStakeMinimumAmountNotification(feeValue: BigDecimal) {
val cryptoCurrencyStatus = cryptoCurrencyStatusProvider()
val isCardano = isCardano(cryptoCurrencyStatus.currency.network.id.value)
val balance = cryptoCurrencyStatus.value.amount.orZero()
if (isCardano && balance - feeValue < MINIMUM_STAKE_BALANCE) {
add(
StakingNotification.Error.CardanoMinimumBalance(
title = resourceReference(R.string.staking_notification_minimum_balance_title),
subtitle = resourceReference(R.string.staking_notification_minimum_stake_ada_text),
),
)
}
}
private fun MutableList<NotificationUM>.addCardanoRestakeMinimumAmountNotification(feeValue: BigDecimal) {
val cryptoCurrencyStatus = cryptoCurrencyStatusProvider()
val isCardano = isCardano(cryptoCurrencyStatus.currency.network.id.value)
val balance = cryptoCurrencyStatus.value.amount.orZero()
if (isCardano && balance - feeValue < MINIMUM_RESTAKE_BALANCE) {
add(
StakingNotification.Error.CardanoMinimumBalance(
title = resourceReference(R.string.staking_notification_minimum_restake_ada_title),
subtitle = resourceReference(R.string.staking_notification_minimum_restake_ada_text),
),
)
}
}
private fun MutableList<NotificationUM>.addStakingEntireBalanceNotification(
sendingAmount: BigDecimal,
feeValue: BigDecimal,
@ -158,7 +206,7 @@ internal class StakingInfoNotificationsFactory(
val isEntireBalance = sendingAmount.plus(feeValue) == balance
if (isEntireBalance && isSubtractAvailable) {
if (isEntireBalance && isSubtractAvailable && !isCardano(cryptoCurrencyStatus.currency.network.id.value)) {
add(StakingNotification.Info.StakeEntireBalance)
}
}
@ -179,4 +227,9 @@ internal class StakingInfoNotificationsFactory(
add(StakingNotification.Warning.LowStakedBalance)
}
}
private companion object {
val MINIMUM_STAKE_BALANCE = "5".toBigDecimal()
val MINIMUM_RESTAKE_BALANCE = "3".toBigDecimal()
}
}

View file

@ -7,6 +7,7 @@ import com.tangem.domain.staking.model.stakekit.PendingAction
import com.tangem.domain.staking.model.stakekit.action.StakingActionType
import com.tangem.features.staking.impl.presentation.state.BalanceState
import com.tangem.lib.crypto.BlockchainUtils.isBSC
import com.tangem.lib.crypto.BlockchainUtils.isCardano
import com.tangem.lib.crypto.BlockchainUtils.isSolana
import com.tangem.lib.crypto.BlockchainUtils.isTron
import kotlinx.collections.immutable.ImmutableList
@ -37,14 +38,12 @@ internal fun StakingActionType?.getPendingActionTitle(): TextReference = when (t
internal fun isSingleAction(networkId: String, activeStake: BalanceState): Boolean {
val isSingleAction = activeStake.pendingActions.size <= 1 // Either single or none pending actions
val isCompositePendingActions = isCompositePendingActions(networkId, activeStake.pendingActions)
val isBscRestake = isBSC(networkId) && activeStake.pendingActions.any {
it.type == StakingActionType.RESTAKE
}
val isRestake = activeStake.pendingActions.any { it.type.isRestake }
return isSingleAction && !isBscRestake || isCompositePendingActions
return isSingleAction && !isRestake || isCompositePendingActions
}
internal fun withStubUnstakeAction(networkId: String, activeStake: BalanceState) = if (isBSC(networkId)) {
internal fun withStubUnstakeAction(networkId: String, activeStake: BalanceState) = if (isStubUnstakeAction(networkId)) {
activeStake.pendingActions.plus(
PendingAction(
type = StakingActionType.UNSTAKE,
@ -65,4 +64,8 @@ internal fun isCompositePendingActions(networkId: String, pendingActions: Immuta
isSolana(networkId) -> pendingActions?.any { it.type == StakingActionType.WITHDRAW } == true
else -> false
}
}
private fun isStubUnstakeAction(networkId: String): Boolean {
return isBSC(networkId) || isCardano(networkId)
}

View file

@ -5,6 +5,7 @@ import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
@ -72,6 +73,7 @@ internal fun StakingConfirmationContent(
)
StakingFeeBlock(feeState = state.feeState, isTransactionSent = isTransactionSent)
NotificationsBlock(notifications = state.notifications)
Spacer(Modifier)
}
}