Updated on 2026-08-14

This commit is contained in:
Tangem 2024-06-14 07:30:12 +03:00
commit 7a306916aa
36 changed files with 765 additions and 181 deletions

View file

@ -16,7 +16,6 @@ dependencies {
implementation(projects.core.utils)
implementation(projects.domain.tokens.models)
implementation(projects.domain.staking)
implementation(projects.features.staking.api)
// region DI

View file

@ -11,28 +11,62 @@ import com.tangem.domain.staking.model.StakingEntryInfo
import com.tangem.domain.staking.model.Yield
import com.tangem.domain.staking.repositories.StakingRepository
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.features.staking.api.featuretoggles.StakingFeatureToggles
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import kotlinx.coroutines.withContext
internal class DefaultStakingRepository(
private val stakeKitApi: StakeKitApi,
private val stakingFeatureToggles: StakingFeatureToggles,
private val stakingYieldsStore: StakingYieldsStore,
private val dispatchers: CoroutineDispatcherProvider,
) : StakingRepository {
private val yieldConverter = YieldConverter()
override fun isStakingSupported(currencyId: String): Boolean {
return integrationIds.contains(currencyId)
}
override suspend fun fetchEnabledYields() {
withContext(dispatchers.io) {
val stakingTokensWithYields = stakeKitApi.getMultipleYields().getOrThrow()
stakingYieldsStore.store(stakingTokensWithYields.data)
}
}
override suspend fun getYield(cryptoCurrencyId: CryptoCurrency.ID, symbol: String): Yield {
return withContext(dispatchers.io) {
val yields = getEnabledYields() ?: error("No yields found")
val rawCurrencyId = cryptoCurrencyId.rawCurrencyId ?: error("Staking custom tokens is not available")
val prefetchedYield = findPrefetchedYield(
yields = yields,
currencyId = rawCurrencyId,
symbol = symbol,
)
prefetchedYield ?: error("Staking is unavailable")
}
}
override suspend fun getEntryInfo(integrationId: String): StakingEntryInfo {
return withContext(dispatchers.io) {
val yield = stakeKitApi.getSingleYield(integrationId).getOrThrow()
StakingEntryInfo(
interestRate = yield.apy,
periodInDays = yield.metadata.cooldownPeriod.days,
tokenSymbol = yield.token.symbol,
)
}
}
override suspend fun getStakingAvailabilityForActions(
cryptoCurrencyId: CryptoCurrency.ID,
symbol: String,
): StakingAvailability {
val rawCurrencyId = cryptoCurrencyId.rawCurrencyId ?: return StakingAvailability.Unavailable
if (!stakingFeatureToggles.isStakingEnabled) {
return StakingAvailability.Unavailable
}
return withContext(dispatchers.io) {
val yields = getEnabledYields() ?: return@withContext StakingAvailability.Unavailable
@ -56,34 +90,6 @@ internal class DefaultStakingRepository(
.find { it.token.coinGeckoId == currencyId && it.token.symbol == symbol }
}
override fun isStakingSupported(currencyId: String): Boolean {
return integrationIds.contains(currencyId)
}
override suspend fun getEntryInfo(integrationId: String): StakingEntryInfo {
return withContext(dispatchers.io) {
val yield = stakeKitApi.getSingleYield(integrationId).getOrThrow()
StakingEntryInfo(
interestRate = yield.apy,
periodInDays = yield.metadata.cooldownPeriod.days,
tokenSymbol = yield.token.symbol,
)
}
}
override suspend fun fetchEnabledYields() {
if (!stakingFeatureToggles.isStakingEnabled) {
return
}
withContext(dispatchers.io) {
val stakingTokensWithYields = stakeKitApi.getMultipleYields().getOrThrow()
stakingYieldsStore.store(stakingTokensWithYields.data)
}
}
private suspend fun getEnabledYields(): List<Yield>? {
val yields = stakingYieldsStore.getSyncOrNull() ?: return null
return yields.map { yieldConverter.convert(it) }

View file

@ -4,7 +4,6 @@ import com.tangem.data.staking.DefaultStakingRepository
import com.tangem.datasource.api.stakekit.StakeKitApi
import com.tangem.datasource.local.token.StakingYieldsStore
import com.tangem.domain.staking.repositories.StakingRepository
import com.tangem.features.staking.api.featuretoggles.StakingFeatureToggles
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import dagger.Module
import dagger.Provides
@ -20,13 +19,11 @@ internal object StakingDataModule {
@Singleton
fun provideStakingRepository(
stakeKitApi: StakeKitApi,
stakingFeatureToggles: StakingFeatureToggles,
stakingTokenStore: StakingYieldsStore,
dispatchers: CoroutineDispatcherProvider,
): StakingRepository {
return DefaultStakingRepository(
stakeKitApi = stakeKitApi,
stakingFeatureToggles = stakingFeatureToggles,
stakingYieldsStore = stakingTokenStore,
dispatchers = dispatchers,
)