diff --git a/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyRepository.kt b/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyRepository.kt index 082d68a3d2..84d2441b9e 100644 --- a/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyRepository.kt +++ b/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyRepository.kt @@ -103,14 +103,18 @@ internal class DefaultYieldSupplyRepository( } override suspend fun saveTokenProtocolStatus( + userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency, yieldSupplyEnterStatus: YieldSupplyEnterStatus, ) { - statusMap[cryptoCurrency.id.value] = yieldSupplyEnterStatus + statusMap["${userWalletId}_${cryptoCurrency.id.value}"] = yieldSupplyEnterStatus } - override fun getTokenProtocolStatus(cryptoCurrency: CryptoCurrency): YieldSupplyEnterStatus? { - return statusMap[cryptoCurrency.id.value] + override fun getTokenProtocolStatus( + userWalletId: UserWalletId, + cryptoCurrency: CryptoCurrency, + ): YieldSupplyEnterStatus? { + return statusMap["${userWalletId}_${cryptoCurrency.id.value}"] } private fun List.enrichNetworkIds(): List { diff --git a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyRepository.kt b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyRepository.kt index deb3c44851..2a62e896fd 100644 --- a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyRepository.kt +++ b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyRepository.kt @@ -59,25 +59,32 @@ interface YieldSupplyRepository { suspend fun deactivateProtocol(cryptoCurrencyToken: CryptoCurrency.Token, address: String): Boolean /** - * Save the last user-initiated yield protocol action for the given currency. + * Save the last user‑initiated yield protocol action for the given wallet and currency. * * The saved value helps the UI render an intermediate "processing" state while waiting * for the definitive protocol status to be fetched from the network. This information - * is transient and not intended to be persisted across app restarts. + * is transient (in‑memory only) and is not persisted across app restarts. * + * @param userWalletId the wallet the action was performed with * @param cryptoCurrency the currency or token the action relates to * @param yieldSupplyEnterStatus the last action intent: [YieldSupplyEnterStatus.Enter] or [YieldSupplyEnterStatus.Exit] */ - suspend fun saveTokenProtocolStatus(cryptoCurrency: CryptoCurrency, yieldSupplyEnterStatus: YieldSupplyEnterStatus) + suspend fun saveTokenProtocolStatus( + userWalletId: UserWalletId, + cryptoCurrency: CryptoCurrency, + yieldSupplyEnterStatus: YieldSupplyEnterStatus, + ) /** - * Get the last saved user-initiated yield protocol action for the given currency, if any. + * Get the last saved user‑initiated yield protocol action for the given wallet and currency, if any. * * Used to determine whether the UI should display an intermediate "processing" state - * until the protocol status retrieved from backend reflects the change. + * until the protocol status retrieved from backend reflects the change. The value is + * transient (in‑memory only) and is not persisted across app restarts. * + * @param userWalletId the wallet to query * @param cryptoCurrency the currency or token to query * @return the last action intent or null if nothing has been recorded */ - fun getTokenProtocolStatus(cryptoCurrency: CryptoCurrency): YieldSupplyEnterStatus? + fun getTokenProtocolStatus(userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency): YieldSupplyEnterStatus? } \ No newline at end of file diff --git a/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/main/model/YieldSupplyModel.kt b/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/main/model/YieldSupplyModel.kt index bf4ac35878..f60bda9cea 100644 --- a/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/main/model/YieldSupplyModel.kt +++ b/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/main/model/YieldSupplyModel.kt @@ -185,7 +185,10 @@ internal class YieldSupplyModel @Inject constructor( @Suppress("MaximumLineLength") private fun onCryptoCurrencyStatusUpdated(cryptoCurrencyStatus: CryptoCurrencyStatus) { val yieldSupplyStatus = cryptoCurrencyStatus.value.yieldSupplyStatus - val tokenProtocolStatus = yieldSupplyRepository.getTokenProtocolStatus(cryptoCurrency) + val tokenProtocolStatus = yieldSupplyRepository.getTokenProtocolStatus( + userWallet.walletId, + cryptoCurrency, + ) val isActive = yieldSupplyStatus?.isActive == true val isCryptoCurrencyStatusFromCache = cryptoCurrencyStatus.value.sources.networkSource != StatusSource.ACTUAL val processing = uiState.value is YieldSupplyUM.Processing diff --git a/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/subcomponents/startearning/model/YieldSupplyStartEarningModel.kt b/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/subcomponents/startearning/model/YieldSupplyStartEarningModel.kt index c205b40b6b..bb8781d903 100644 --- a/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/subcomponents/startearning/model/YieldSupplyStartEarningModel.kt +++ b/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/subcomponents/startearning/model/YieldSupplyStartEarningModel.kt @@ -244,38 +244,48 @@ internal class YieldSupplyStartEarningModel @Inject constructor( ) }, ifRight = { - yieldSupplyRepository.saveTokenProtocolStatus(cryptoCurrency, YieldSupplyEnterStatus.Enter) - val event = AnalyticsParam.TxSentFrom.Earning( - blockchain = cryptoCurrency.network.name, - token = cryptoCurrency.symbol, - feeType = AnalyticsParam.FeeType.Normal, - ) - analytics.send(YieldSupplyAnalytics.FundsEarned( - blockchain = cryptoCurrency.network.name, - token = cryptoCurrency.symbol, - )) - yieldSupplyFeeUM.transactionDataList.forEach { - analytics.send( - Basic.TransactionSent( - sentFrom = event, - memoType = Basic.TransactionSent.MemoType.Null, - ), - ) - } - - val address = cryptoCurrencyStatus.value.networkAddress?.defaultAddress?.value - if (address != null) { - yieldSupplyActivateUseCase(cryptoCurrency, address) - } - - modelScope.launch { - params.callback.onTransactionSent() - } + onStartEarningTransactionSuccess(yieldSupplyFeeUM) }, ) } } + private suspend fun onStartEarningTransactionSuccess(yieldSupplyFeeUM: YieldSupplyFeeUM.Content) { + yieldSupplyRepository.saveTokenProtocolStatus( + userWallet.walletId, + cryptoCurrency, + YieldSupplyEnterStatus.Enter, + ) + val event = AnalyticsParam.TxSentFrom.Earning( + blockchain = cryptoCurrency.network.name, + token = cryptoCurrency.symbol, + feeType = AnalyticsParam.FeeType.Normal, + ) + analytics.send( + YieldSupplyAnalytics.FundsEarned( + blockchain = cryptoCurrency.network.name, + token = cryptoCurrency.symbol, + ), + ) + yieldSupplyFeeUM.transactionDataList.forEach { + analytics.send( + Basic.TransactionSent( + sentFrom = event, + memoType = Basic.TransactionSent.MemoType.Null, + ), + ) + } + + val address = cryptoCurrencyStatus.value.networkAddress?.defaultAddress?.value + if (address != null) { + yieldSupplyActivateUseCase(cryptoCurrency, address) + } + + modelScope.launch { + params.callback.onTransactionSent() + } + } + private fun subscribeOnCurrencyStatusUpdates() { modelScope.launch { getUserWalletUseCase(params.userWalletId).fold( diff --git a/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/subcomponents/stopearning/model/YieldSupplyStopEarningModel.kt b/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/subcomponents/stopearning/model/YieldSupplyStopEarningModel.kt index 658dd165bb..c9dc0ec5fd 100644 --- a/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/subcomponents/stopearning/model/YieldSupplyStopEarningModel.kt +++ b/features/yield-supply/impl/src/main/java/com/tangem/features/yield/supply/impl/subcomponents/stopearning/model/YieldSupplyStopEarningModel.kt @@ -165,37 +165,45 @@ internal class YieldSupplyStopEarningModel @Inject constructor( ) }, ifRight = { - yieldSupplyRepository.saveTokenProtocolStatus(cryptoCurrency, YieldSupplyEnterStatus.Exit) - analytics.send( - YieldSupplyAnalytics.FundsWithdrawn( - token = cryptoCurrency.symbol, - blockchain = cryptoCurrency.network.name, - ), - ) - val event = AnalyticsParam.TxSentFrom.Earning( - blockchain = cryptoCurrency.network.name, - token = cryptoCurrency.symbol, - feeType = AnalyticsParam.FeeType.Normal, - ) - analytics.send( - Basic.TransactionSent( - sentFrom = event, - memoType = Basic.TransactionSent.MemoType.Null, - ), - ) - val address = cryptoCurrencyStatus.value.networkAddress?.defaultAddress?.value - if (address != null) { - yieldSupplyDeactivateUseCase(cryptoCurrency, address) - } - - modelScope.launch { - params.callback.onTransactionSent() - } + onStopEarningTransactionSuccess() }, ) } } + private suspend fun onStopEarningTransactionSuccess() { + yieldSupplyRepository.saveTokenProtocolStatus( + userWallet.walletId, + cryptoCurrency, + YieldSupplyEnterStatus.Exit, + ) + analytics.send( + YieldSupplyAnalytics.FundsWithdrawn( + token = cryptoCurrency.symbol, + blockchain = cryptoCurrency.network.name, + ), + ) + val event = AnalyticsParam.TxSentFrom.Earning( + blockchain = cryptoCurrency.network.name, + token = cryptoCurrency.symbol, + feeType = AnalyticsParam.FeeType.Normal, + ) + analytics.send( + Basic.TransactionSent( + sentFrom = event, + memoType = Basic.TransactionSent.MemoType.Null, + ), + ) + val address = cryptoCurrencyStatus.value.networkAddress?.defaultAddress?.value + if (address != null) { + yieldSupplyDeactivateUseCase(cryptoCurrency, address) + } + + modelScope.launch { + params.callback.onTransactionSent() + } + } + private fun subscribeOnCurrencyStatusUpdates() { modelScope.launch { feeCryptoCurrencyStatusFlow.update {