Updated on 2026-08-14

This commit is contained in:
Tangem 2025-11-13 19:58:22 +03:00
parent 6018cf0b71
commit f7b10f3ed5
31 changed files with 446 additions and 105 deletions

View file

@ -12,6 +12,8 @@ import com.tangem.datasource.api.tangemTech.YieldSupplyApi
import com.tangem.datasource.api.tangemTech.models.YieldSupplyChangeTokenStatusBody
import com.tangem.datasource.local.yieldsupply.YieldMarketsStore
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.currency.CryptoCurrencyStatus
import com.tangem.domain.models.network.TxInfo
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.walletmanager.WalletManagersFacade
import com.tangem.domain.yield.supply.YieldSupplyRepository
@ -22,6 +24,7 @@ import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.util.concurrent.ConcurrentHashMap
internal class DefaultYieldSupplyRepository(
@ -109,16 +112,21 @@ internal class DefaultYieldSupplyRepository(
override suspend fun saveTokenProtocolStatus(
userWalletId: UserWalletId,
cryptoCurrency: CryptoCurrency,
yieldSupplyEnterStatus: YieldSupplyEnterStatus,
yieldSupplyEnterStatus: YieldSupplyEnterStatus?,
) {
statusMap["${userWalletId}_${cryptoCurrency.id.value}"] = yieldSupplyEnterStatus
val key = getTokenProtocolStatusKey(userWalletId, cryptoCurrency)
if (yieldSupplyEnterStatus != null) {
statusMap[key] = yieldSupplyEnterStatus
} else {
statusMap.remove(key)
}
}
override fun getTokenProtocolStatus(
userWalletId: UserWalletId,
cryptoCurrency: CryptoCurrency,
): YieldSupplyEnterStatus? {
return statusMap["${userWalletId}_${cryptoCurrency.id.value}"]
return statusMap[getTokenProtocolStatusKey(userWalletId, cryptoCurrency)]
}
private fun List<YieldMarketToken>.enrichNetworkIds(): List<YieldMarketToken> {
@ -127,4 +135,44 @@ internal class DefaultYieldSupplyRepository(
token.copy(backendId = chainIdMap[token.chainId])
}
}
override suspend fun getTokenPendingStatus(
userWalletId: UserWalletId,
cryptoCurrencyStatus: CryptoCurrencyStatus,
): YieldSupplyEnterStatus? = try {
val cryptoCurrency = cryptoCurrencyStatus.currency
val walletManager = walletManagersFacade.getOrCreateWalletManager(
userWalletId = userWalletId,
blockchain = cryptoCurrency.network.toBlockchain(),
derivationPath = cryptoCurrency.network.derivationPath.value,
) ?: error("Wallet manager not found")
val pendingTxs = cryptoCurrencyStatus.value.pendingTransactions
val yieldAddress = walletManager.calculateYieldModuleAddress()
val hasRecentYieldEnterTxs = pendingTxs.hasYieldEnterTransactions(yieldAddress)
val hasRecentYieldExitTxs = pendingTxs.hasYieldExitTransactions()
when {
hasRecentYieldEnterTxs -> YieldSupplyEnterStatus.Enter
hasRecentYieldExitTxs -> YieldSupplyEnterStatus.Exit
else -> null
}
} catch (e: Exception) {
Timber.e(e, "Failed to get pending yield supply status")
null
}
private fun Set<TxInfo>.hasYieldEnterTransactions(yieldAddress: String) = any {
it.type == TxInfo.TransactionType.YieldSupply.Enter ||
it.type == TxInfo.TransactionType.Approve &&
(it.interactionAddressType as? TxInfo.InteractionAddressType.Contract)?.address == yieldAddress
}
private fun Set<TxInfo>.hasYieldExitTransactions() = any {
it.type == TxInfo.TransactionType.YieldSupply.Exit
}
private fun getTokenProtocolStatusKey(userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency): String =
"${userWalletId}_${cryptoCurrency.id.value}"
}