From 97ff092d70ae8e3db79cbde5068131c2cfc59035 Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 18 Jul 2024 19:05:50 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../middlewares/TradeCryptoMiddleware.kt | 18 ++++++++++++++ .../domain/tokens/legacy/TradeCryptoAction.kt | 8 +++++++ .../viewmodels/TokenDetailsViewModel.kt | 24 +++++++++++++------ .../WalletCurrencyActionsClickIntents.kt | 24 ++++++++++++++++--- 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/TradeCryptoMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/TradeCryptoMiddleware.kt index a4233d6ea8..0f96c671b9 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/TradeCryptoMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/TradeCryptoMiddleware.kt @@ -7,6 +7,7 @@ import com.tangem.blockchain.common.Blockchain import com.tangem.common.routing.AppRoute import com.tangem.common.routing.AppRouter import com.tangem.core.analytics.Analytics +import com.tangem.domain.staking.model.Yield import com.tangem.domain.tokens.legacy.TradeCryptoAction import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.NetworkAddress @@ -56,6 +57,11 @@ object TradeCryptoMiddleware { is TradeCryptoAction.Buy -> proceedBuyAction(state, action) is TradeCryptoAction.Sell -> proceedSellAction(action) is TradeCryptoAction.Swap -> openSwap(currency = action.cryptoCurrency) + is TradeCryptoAction.Stake -> openStaking( + userWalletId = action.userWalletId, + cryptoCurrencyId = action.cryptoCurrencyId, + yield = action.yield, + ) is TradeCryptoAction.SendToken -> { if (isSendRedesignedEnabled) { handleNewSendToken(action = action) @@ -159,6 +165,18 @@ object TradeCryptoMiddleware { store.dispatchNavigationAction { push(AppRoute.Swap(currency = currency)) } } + private fun openStaking(userWalletId: UserWalletId, cryptoCurrencyId: CryptoCurrency.ID, yield: Yield) { + store.dispatchNavigationAction { + push( + AppRoute.Staking( + userWalletId = userWalletId, + cryptoCurrencyId = cryptoCurrencyId, + yield = yield, + ), + ) + } + } + private fun handleSendToken(action: TradeCryptoAction.SendToken) { val currency = action.tokenCurrency val blockchain = Blockchain.fromId(currency.network.id.value) diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/legacy/TradeCryptoAction.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/legacy/TradeCryptoAction.kt index 092ad6d3ba..92a29e7a39 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/legacy/TradeCryptoAction.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/legacy/TradeCryptoAction.kt @@ -1,8 +1,10 @@ package com.tangem.domain.tokens.legacy +import com.tangem.domain.staking.model.Yield import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.wallets.models.UserWallet +import com.tangem.domain.wallets.models.UserWalletId import org.rekotlin.Action import java.math.BigDecimal @@ -40,6 +42,12 @@ sealed class TradeCryptoAction : Action { data class Swap(val cryptoCurrency: CryptoCurrency) : TradeCryptoAction() + data class Stake( + val userWalletId: UserWalletId, + val cryptoCurrencyId: CryptoCurrency.ID, + val yield: Yield, + ) : TradeCryptoAction() + data class TransactionInfo( val transactionId: String, val destinationAddress: String, diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt index 4c32037a24..6bdc2a3375 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt @@ -453,12 +453,7 @@ internal class TokenDetailsViewModel @Inject constructor( } override fun onStakeBannerClick() { - viewModelScope.launch { - val yield = getYieldUseCase.invoke(cryptoCurrency.id, cryptoCurrency.symbol).getOrNull() - yield ?: error("Staking is unavailable") - - router.openStaking(userWalletId, cryptoCurrency, yield) - } + openStaking() } override fun onReloadClick() { @@ -559,7 +554,9 @@ internal class TokenDetailsViewModel @Inject constructor( } override fun onStakeClick(unavailabilityReason: ScenarioUnavailabilityReason) { - Timber.e("Not implemented yet") + if (handleUnavailabilityReason(unavailabilityReason)) return + + openStaking() } override fun onGenerateExtendedKey() { @@ -835,6 +832,19 @@ internal class TokenDetailsViewModel @Inject constructor( return true } + private fun openStaking() { + viewModelScope.launch { + val yield = getYieldUseCase.invoke( + cryptoCurrencyId = cryptoCurrency.id, + symbol = cryptoCurrency.symbol, + ).getOrElse { + error("Staking is unavailable for ${cryptoCurrency.name}") + } + + router.openStaking(userWalletId, cryptoCurrency, yield) + } + } + private companion object { const val EXCHANGE_STATUS_UPDATE_DELAY = 10_000L } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/intents/WalletCurrencyActionsClickIntents.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/intents/WalletCurrencyActionsClickIntents.kt index 55f1538573..c6b5eb1cff 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/intents/WalletCurrencyActionsClickIntents.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/intents/WalletCurrencyActionsClickIntents.kt @@ -1,5 +1,6 @@ package com.tangem.feature.wallet.presentation.wallet.viewmodels.intents +import arrow.core.getOrElse import com.tangem.blockchain.common.address.AddressType import com.tangem.core.analytics.api.AnalyticsEventHandler import com.tangem.core.ui.clipboard.ClipboardManager @@ -18,6 +19,7 @@ import com.tangem.domain.appcurrency.extenstions.unwrap import com.tangem.domain.common.util.cardTypesResolver import com.tangem.domain.demo.IsDemoCardUseCase import com.tangem.domain.redux.ReduxStateHolder +import com.tangem.domain.staking.GetYieldUseCase import com.tangem.domain.tokens.* import com.tangem.domain.tokens.legacy.TradeCryptoAction import com.tangem.domain.tokens.model.CryptoCurrency @@ -44,7 +46,6 @@ import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.take import kotlinx.coroutines.launch -import timber.log.Timber import javax.inject.Inject interface WalletCurrencyActionsClickIntents { @@ -92,6 +93,7 @@ internal class WalletCurrencyActionsClickIntentsImplementor @Inject constructor( private val reduxStateHolder: ReduxStateHolder, private val hapticManager: HapticManager, private val clipboardManager: ClipboardManager, + private val getYieldUseCase: GetYieldUseCase, ) : BaseWalletClickIntents(), WalletCurrencyActionsClickIntents { override fun onSendClick( @@ -368,8 +370,24 @@ internal class WalletCurrencyActionsClickIntentsImplementor @Inject constructor( } override fun onStakeClick(cryptoCurrencyStatus: CryptoCurrencyStatus) { - // TODO staking - Timber.e("Not implemented yet") + viewModelScope.launch { + val userWalletId = stateHolder.getSelectedWalletId() + val cryptoCurrency = cryptoCurrencyStatus.currency + val yield = getYieldUseCase.invoke( + cryptoCurrencyId = cryptoCurrency.id, + symbol = cryptoCurrency.symbol, + ).getOrElse { + error("Staking is unavailable for ${cryptoCurrency.name}") + } + + reduxStateHolder.dispatch( + TradeCryptoAction.Stake( + userWalletId = userWalletId, + cryptoCurrencyId = cryptoCurrency.id, + yield = yield, + ), + ) + } } private fun openExplorer() {