diff --git a/data/staking/src/main/java/com/tangem/data/staking/DefaultStakingRepository.kt b/data/staking/src/main/java/com/tangem/data/staking/DefaultStakingRepository.kt index fd08381f40..d6ce77cad7 100644 --- a/data/staking/src/main/java/com/tangem/data/staking/DefaultStakingRepository.kt +++ b/data/staking/src/main/java/com/tangem/data/staking/DefaultStakingRepository.kt @@ -246,6 +246,47 @@ internal class DefaultStakingRepository( } } + override suspend fun getStakingAvailabilitySync( + userWalletId: UserWalletId, + cryptoCurrency: CryptoCurrency, + ): StakingAvailability { + if (!checkFeatureToggleEnabled(cryptoCurrency.network.id)) { + return StakingAvailability.Unavailable + } + + if (checkForInvalidCardBatch(userWalletId, cryptoCurrency)) { + return StakingAvailability.Unavailable + } + + val rawCurrencyId = cryptoCurrency.id.rawCurrencyId + if (rawCurrencyId == null) { + return StakingAvailability.Unavailable + } + + val isSupportedInMobileApp = getSupportedIntegrationId(cryptoCurrency.id).isNullOrEmpty().not() + + val yields = getEnabledYieldsSync() + if (yields.isEmpty()) { + return StakingAvailability.TemporaryUnavailable + } + + val prefetchedYield = findPrefetchedYield( + yields = yields, + currencyId = rawCurrencyId, + symbol = cryptoCurrency.symbol, + ) + + return when { + prefetchedYield != null && isSupportedInMobileApp -> { + StakingAvailability.Available(prefetchedYield.id) + } + prefetchedYield == null && isSupportedInMobileApp -> { + StakingAvailability.TemporaryUnavailable + } + else -> StakingAvailability.Unavailable + } + } + private fun checkFeatureToggleEnabled(networkId: Network.ID): Boolean { return when (networkId.toBlockchain()) { Blockchain.TON -> stakingFeatureToggles.isTonStakingEnabled diff --git a/domain/staking/src/main/java/com/tangem/domain/staking/GetStakingAvailabilityUseCase.kt b/domain/staking/src/main/java/com/tangem/domain/staking/GetStakingAvailabilityUseCase.kt index 5f704c96b5..b65f257b05 100644 --- a/domain/staking/src/main/java/com/tangem/domain/staking/GetStakingAvailabilityUseCase.kt +++ b/domain/staking/src/main/java/com/tangem/domain/staking/GetStakingAvailabilityUseCase.kt @@ -32,4 +32,14 @@ class GetStakingAvailabilityUseCase( it.right() }.catch { emit(stakingErrorResolver.resolve(it).left()) } } + + suspend fun invokeSync( + userWalletId: UserWalletId, + cryptoCurrency: CryptoCurrency, + ): Either = Either.catch { + stakingRepository.getStakingAvailabilitySync( + userWalletId = userWalletId, + cryptoCurrency = cryptoCurrency, + ) + }.mapLeft(stakingErrorResolver::resolve) } \ No newline at end of file diff --git a/domain/staking/src/main/java/com/tangem/domain/staking/repositories/StakingRepository.kt b/domain/staking/src/main/java/com/tangem/domain/staking/repositories/StakingRepository.kt index 7ed0ee1686..e399fcd26c 100644 --- a/domain/staking/src/main/java/com/tangem/domain/staking/repositories/StakingRepository.kt +++ b/domain/staking/src/main/java/com/tangem/domain/staking/repositories/StakingRepository.kt @@ -37,6 +37,11 @@ interface StakingRepository { fun getStakingAvailability(userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency): Flow + suspend fun getStakingAvailabilitySync( + userWalletId: UserWalletId, + cryptoCurrency: CryptoCurrency, + ): StakingAvailability + suspend fun getActions( userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency, diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/deeplink/DefaultStakingDeepLinkHandler.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/deeplink/DefaultStakingDeepLinkHandler.kt index cc791844e8..4b5a2a090c 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/deeplink/DefaultStakingDeepLinkHandler.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/deeplink/DefaultStakingDeepLinkHandler.kt @@ -3,7 +3,12 @@ package com.tangem.features.staking.impl.deeplink import arrow.core.getOrElse import com.tangem.common.routing.AppRoute import com.tangem.common.routing.AppRouter +import com.tangem.core.deeplink.DeeplinkConst.NETWORK_ID_KEY +import com.tangem.core.deeplink.DeeplinkConst.TOKEN_ID_KEY +import com.tangem.core.deeplink.DeeplinkConst.WALLET_ID_KEY +import com.tangem.domain.staking.GetStakingAvailabilityUseCase import com.tangem.domain.staking.GetYieldUseCase +import com.tangem.domain.staking.model.StakingAvailability import com.tangem.domain.tokens.GetCryptoCurrenciesUseCase import com.tangem.domain.wallets.models.UserWalletId import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase @@ -15,6 +20,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import timber.log.Timber +@Suppress("LongParameterList") internal class DefaultStakingDeepLinkHandler @AssistedInject constructor( @Assisted private val scope: CoroutineScope, @Assisted private val queryParams: Map, @@ -22,6 +28,7 @@ internal class DefaultStakingDeepLinkHandler @AssistedInject constructor( private val getSelectedWalletSyncUseCase: GetSelectedWalletSyncUseCase, private val getCryptoCurrenciesUseCase: GetCryptoCurrenciesUseCase, private val getYieldUseCase: GetYieldUseCase, + private val getStakingAvailabilityUseCase: GetStakingAvailabilityUseCase, ) : StakingDeepLinkHandler { init { @@ -29,24 +36,28 @@ internal class DefaultStakingDeepLinkHandler @AssistedInject constructor( } private fun handleDeepLink() { - // It is okay here, we are navigating from outside, and there is no other way to getting UserWallet - val userWalletId = queryParams[WALLET_ID_KEY]?.let(::UserWalletId) - ?: getSelectedWalletSyncUseCase().getOrNull()?.walletId + val networkId = queryParams[NETWORK_ID_KEY] + val tokenId = queryParams[TOKEN_ID_KEY] - if (userWalletId == null) { + // It is okay here, we are navigating from outside, and there is no other way to getting UserWallet + val selectedUserWalletId = getSelectedWalletSyncUseCase().getOrNull()?.walletId + val walletId = queryParams[WALLET_ID_KEY]?.let(::UserWalletId) ?: selectedUserWalletId + + // If selected user wallet is different than from deeplink - ignore deeplink + // If selected user wallet is null - ignore deeplink + if (walletId != selectedUserWalletId || selectedUserWalletId == null) { Timber.e("Error on getting user wallet") return } - val networkId = queryParams[NETWORK_ID_KEY] - val tokenId = queryParams[TOKEN_ID_KEY] - scope.launch { - val cryptoCurrency = getCryptoCurrenciesUseCase(userWalletId = userWalletId).getOrElse { + val cryptoCurrency = getCryptoCurrenciesUseCase(userWalletId = selectedUserWalletId).getOrElse { Timber.e("Error on getting crypto currency list") return@launch }.firstOrNull { - it.network.backendId == networkId && it.id.rawCurrencyId?.value == tokenId + val isNetwork = it.network.backendId.equals(networkId, ignoreCase = true) + val isCurrency = it.id.rawCurrencyId?.value?.equals(tokenId, ignoreCase = true) == true + isNetwork && isCurrency } if (cryptoCurrency == null) { @@ -60,6 +71,15 @@ internal class DefaultStakingDeepLinkHandler @AssistedInject constructor( return@launch } + val isStakingEnabled = getStakingAvailabilityUseCase.invokeSync( + userWalletId = selectedUserWalletId, + cryptoCurrency = cryptoCurrency, + ).getOrNull() + + if (isStakingEnabled !is StakingAvailability.Available) { + return@launch + } + val yield = getYieldUseCase.invoke( cryptoCurrencyId = cryptoCurrency.id, symbol = cryptoCurrency.symbol, @@ -70,7 +90,7 @@ internal class DefaultStakingDeepLinkHandler @AssistedInject constructor( appRouter.push( AppRoute.Staking( - userWalletId = userWalletId, + userWalletId = selectedUserWalletId, cryptoCurrencyId = cryptoCurrency.id, yieldId = yield.id, ), @@ -85,10 +105,4 @@ internal class DefaultStakingDeepLinkHandler @AssistedInject constructor( queryParams: Map, ): DefaultStakingDeepLinkHandler } - - private companion object { - const val WALLET_ID_KEY = "walletId" - const val NETWORK_ID_KEY = "network_id" - const val TOKEN_ID_KEY = "token_id" - } } \ No newline at end of file