Skip to content

Commit

Permalink
AND-8825 Staking / Added notification about unstake fee required
Browse files Browse the repository at this point in the history
  • Loading branch information
iMaks99 committed Oct 29, 2024
1 parent c931c3e commit 0eb4dc1
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 114 deletions.
3 changes: 3 additions & 0 deletions core/res/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@
<string name="onboarding_wallet_info_title_fourth">Восстановление кода доступа</string>
<string name="onboarding_wallet_info_title_second">Идентичные карты</string>
<string name="onboarding_wallet_info_title_third">Код доступа</string>
<string name="onramp_pay_with">Оплата с</string>
<string name="onramp_via">Через</string>
<string name="organize_tokens_group">Группы</string>
<string name="organize_tokens_sort_by_balance">По балансу</string>
<string name="organize_tokens_title">Упорядочить токены</string>
Expand Down Expand Up @@ -758,6 +760,7 @@
<string name="staking_notification_new_validator_funds_transfer">Стейкинг в сети %1$s с новым валидатором автоматически переведет ваши текущие застейканные средства на него.</string>
<string name="staking_notification_restake_rewards_text">Реинвестируйте свои заработанные награды в вашу застейканную сумму, увеличивая потенциальный доход</string>
<string name="staking_notification_restake_text">Рестейк позволяет вам переместить средства из одного валидатора в другого без необходимости выхода из стейкинга.</string>
<string name="staking_notification_stake_entire_balance_text">Вы собираетесь застейкать весь баланс, рекомендуем оставить небольшую сумму для оплаты комиссии сети при выходе из стейкинга или получении награды.</string>
<string name="staking_notification_unlock_text">Разблокируйте свои средства, чтобы вывести их из стейкинга. Разблокировка займёт %s.</string>
<string name="staking_notification_unstake_cosmos_text">Ваши средства будут доступны для использования после 21-дневного периода отзыва. Награда будет выведена вместе с вашими выводими средствами.</string>
<string name="staking_notification_unstake_text">Ваши средства будут доступны после %s периода отзыва.</string>
Expand Down
3 changes: 3 additions & 0 deletions core/res/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,11 @@
<string name="onboarding_wallet_info_title_fourth">Access code restore</string>
<string name="onboarding_wallet_info_title_second">Identical cards</string>
<string name="onboarding_wallet_info_title_third">Access code</string>
<string name="onramp_pay_with">Pay with</string>
<string name="onramp_residency_bottomsheet_country_not_supported">Our services are not available in this country</string>
<string name="onramp_residency_bottomsheet_country_subtitle">Change or confirm it</string>
<string name="onramp_residency_bottomsheet_title">Your residence has been identified as</string>
<string name="onramp_via">Via</string>
<string name="organize_tokens_group">Group</string>
<string name="organize_tokens_sort_by_balance">By balance</string>
<string name="organize_tokens_title">Organize tokens</string>
Expand Down Expand Up @@ -742,6 +744,7 @@
<string name="staking_notification_new_validator_funds_transfer">Staking in the %1$s network with a new validator will automatically transfer all previously staked funds to this validator</string>
<string name="staking_notification_restake_rewards_text">Reinvests your earned rewards in your staked amount, increasing potential earnings.</string>
<string name="staking_notification_restake_text">Restake lets you move your funds from one validator to another without the need to unstake</string>
<string name="staking_notification_stake_entire_balance_text">You’re about to stake your entire balance. We recommend leaving a small amount to cover network fees for unstaking or claiming rewards.</string>
<string name="staking_notification_unlock_text">Unlock your money to withdraw it from staking process. Unlocking takes %s.</string>
<string name="staking_notification_unstake_cosmos_text">Your funds will be available for use after the 21-day unbonding period. Reward will be withdrawn along with your unstaking funds.</string>
<string name="staking_notification_unstake_text">Your funds will be available for use after the %s unbonding period.</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ internal object StakingNotification {
subtitle = subtitle,
buttonsState = buttonsState,
onCloseClick = onCloseClick,

) {
data class EarnRewards(
val subtitleText: TextReference,
Expand All @@ -68,6 +67,11 @@ internal object StakingNotification {
subtitle = subtitleText,
)

data object StakeEntireBalance : StakingNotification.Info(
title = resourceReference(R.string.common_network_fee_title),
subtitle = resourceReference(R.string.staking_notification_stake_entire_balance_text),
)

data class Unstake(
val cooldownPeriodDays: Int,
@StringRes val subtitleRes: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tangem.features.staking.impl.presentation.state.transformers
package com.tangem.features.staking.impl.presentation.state.transformers.notifications

import com.tangem.common.ui.amountScreen.models.AmountState
import com.tangem.common.ui.notifications.NotificationUM
Expand All @@ -11,30 +11,21 @@ import com.tangem.common.ui.notifications.NotificationsFactory.addReserveAmountE
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.core.ui.extensions.pluralReference
import com.tangem.core.ui.extensions.resourceReference
import com.tangem.core.ui.extensions.wrappedList
import com.tangem.domain.appcurrency.model.AppCurrency
import com.tangem.domain.staking.model.stakekit.BalanceType
import com.tangem.domain.staking.model.stakekit.Yield
import com.tangem.domain.staking.model.stakekit.YieldBalance
import com.tangem.domain.staking.model.stakekit.action.StakingActionCommonType
import com.tangem.domain.staking.model.stakekit.action.StakingActionType
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.domain.tokens.model.warnings.CryptoCurrencyCheck
import com.tangem.domain.tokens.model.warnings.CryptoCurrencyWarning
import com.tangem.domain.transaction.error.GetFeeError
import com.tangem.features.staking.impl.R
import com.tangem.features.staking.impl.presentation.state.FeeState
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.features.staking.impl.presentation.state.utils.checkAndCalculateSubtractedAmount
import com.tangem.features.staking.impl.presentation.state.utils.checkFeeCoverage
import com.tangem.lib.crypto.BlockchainUtils
import com.tangem.lib.crypto.BlockchainUtils.isCosmos
import com.tangem.lib.crypto.BlockchainUtils.isTron
import com.tangem.utils.Provider
import com.tangem.utils.extensions.orZero
import com.tangem.utils.transformer.Transformer
Expand All @@ -53,6 +44,13 @@ internal class AddStakingNotificationsTransformer(
private val isSubtractAvailable: Boolean,
private val yield: Yield,
) : Transformer<StakingUiState> {

private val stakingInfoNotificationsFactory = StakingInfoNotificationsFactory(
cryptoCurrencyStatusProvider = cryptoCurrencyStatusProvider,
yield = yield,
isSubtractAvailable = isSubtractAvailable,
)

override fun transform(prevState: StakingUiState): StakingUiState {
val cryptoCurrencyStatus = cryptoCurrencyStatusProvider()
val balance = cryptoCurrencyStatus.value.amount.orZero()
Expand Down Expand Up @@ -105,7 +103,12 @@ internal class AddStakingNotificationsTransformer(
isFeeCoverage = isFeeCoverage && isEnterAction && !sendingAmount.equals(minimumRequirement),
)

addInfoNotifications(prevState)
stakingInfoNotificationsFactory.addInfoNotifications(
notifications = this,
prevState = prevState,
sendingAmount = sendingAmount,
feeValue = feeValue,
)
}.toImmutableList()

return prevState.copy(
Expand Down Expand Up @@ -241,105 +244,4 @@ internal class AddStakingNotificationsTransformer(
add(notification)
}
}

private fun MutableList<NotificationUM>.addInfoNotifications(prevState: StakingUiState) {
when (prevState.actionType) {
StakingActionCommonType.Enter -> addEnterInfoNotifications()
StakingActionCommonType.Exit -> addExitInfoNotifications()
is StakingActionCommonType.Pending -> addPendingInfoNotifications(prevState)
}
}

private fun MutableList<NotificationUM>.addExitInfoNotifications() {
val cooldownPeriodDays = yield.metadata.cooldownPeriod?.days
if (cooldownPeriodDays != null) {
add(
StakingNotification.Info.Unstake(
cooldownPeriodDays = cooldownPeriodDays,
subtitleRes = if (isCosmos(cryptoCurrencyStatusProvider().currency.network.id.value)) {
R.string.staking_notification_unstake_cosmos_text
} else {
R.string.staking_notification_unstake_text
},
),
)
}
}

private fun MutableList<NotificationUM>.addEnterInfoNotifications() {
addTronRevoteNotification()
}

private fun MutableList<NotificationUM>.addPendingInfoNotifications(prevState: StakingUiState) {
val confirmationState = prevState.confirmationState as? StakingStates.ConfirmationState.Data
val pendingActionType = confirmationState?.pendingAction?.type
val (titleReference, textReference) = when (pendingActionType) {
StakingActionType.CLAIM_REWARDS -> {
resourceReference(R.string.common_claim) to
resourceReference(R.string.staking_notification_claim_rewards_text)
}
StakingActionType.RESTAKE_REWARDS -> {
resourceReference(R.string.staking_restake) to
resourceReference(R.string.staking_notification_restake_rewards_text)
}
StakingActionType.WITHDRAW -> {
resourceReference(R.string.staking_withdraw) to
resourceReference(R.string.staking_notification_withdraw_text)
}
StakingActionType.UNLOCK_LOCKED -> {
val cooldownPeriodDays = yield.metadata.cooldownPeriod?.days
if (cooldownPeriodDays != null) {
resourceReference(R.string.staking_unlocked_locked) to resourceReference(
R.string.staking_notification_unlock_text,
wrappedList(
pluralReference(
id = R.plurals.common_days,
count = cooldownPeriodDays,
formatArgs = wrappedList(cooldownPeriodDays),
),
),
)
} else {
null to null
}
}
StakingActionType.VOTE_LOCKED -> {
resourceReference(R.string.staking_revote) to
resourceReference(R.string.staking_notifications_revote_tron_text)
}
StakingActionType.RESTAKE -> {
resourceReference(R.string.staking_restake) to
resourceReference(R.string.staking_notification_restake_text)
}
else -> null to null
}

if (titleReference != null && textReference != null) {
add(
StakingNotification.Info.Ordinary(
title = titleReference,
text = textReference,
),
)
}
}

private fun MutableList<NotificationUM>.addTronRevoteNotification() {
val cryptoCurrencyStatus = cryptoCurrencyStatusProvider()
val isTron = isTron(cryptoCurrencyStatus.currency.network.id.value)
val hasStakedBalance = (cryptoCurrencyStatus.value.yieldBalance as? YieldBalance.Data)?.balance
?.items?.any {
it.type == BalanceType.PREPARING ||
it.type == BalanceType.STAKED ||
it.type == BalanceType.LOCKED
} == true
if (isTron && hasStakedBalance) {
add(
StakingNotification.Info.Ordinary(
title = resourceReference(R.string.staking_revote),
text = resourceReference(R.string.staking_notifications_revote_tron_text),
),
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tangem.features.staking.impl.presentation.state.transformers
package com.tangem.features.staking.impl.presentation.state.transformers.notifications

import com.tangem.common.ui.notifications.NotificationUM
import com.tangem.features.staking.impl.presentation.state.StakingStates
Expand Down
Loading

0 comments on commit 0eb4dc1

Please sign in to comment.