diff --git a/app/src/main/java/com/tangem/tap/di/domain/YieldSupplyDomainModule.kt b/app/src/main/java/com/tangem/tap/di/domain/YieldSupplyDomainModule.kt index f3ed8e4349..8bc8f71489 100644 --- a/app/src/main/java/com/tangem/tap/di/domain/YieldSupplyDomainModule.kt +++ b/app/src/main/java/com/tangem/tap/di/domain/YieldSupplyDomainModule.kt @@ -3,6 +3,7 @@ package com.tangem.tap.di.domain import com.tangem.domain.blockaid.BlockAidGasEstimate import com.tangem.domain.transaction.FeeRepository import com.tangem.domain.transaction.error.FeeErrorResolver +import com.tangem.domain.yield.supply.YieldSupplyErrorResolver import com.tangem.domain.yield.supply.YieldSupplyTransactionRepository import com.tangem.domain.yield.supply.usecase.* import dagger.Module @@ -19,9 +20,11 @@ internal object YieldSupplyDomainModule { @Singleton fun provideYieldSupplyStartEarningUseCase( yieldSupplyTransactionRepository: YieldSupplyTransactionRepository, + yieldSupplyErrorResolver: YieldSupplyErrorResolver, ): YieldSupplyStartEarningUseCase { return YieldSupplyStartEarningUseCase( yieldSupplyTransactionRepository = yieldSupplyTransactionRepository, + yieldSupplyErrorResolver = yieldSupplyErrorResolver, ) } @@ -29,9 +32,11 @@ internal object YieldSupplyDomainModule { @Singleton fun provideYieldSupplyStopEarningUseCase( yieldSupplyTransactionRepository: YieldSupplyTransactionRepository, + yieldSupplyErrorResolver: YieldSupplyErrorResolver, ): YieldSupplyStopEarningUseCase { return YieldSupplyStopEarningUseCase( yieldSupplyTransactionRepository = yieldSupplyTransactionRepository, + yieldSupplyErrorResolver = yieldSupplyErrorResolver, ) } @@ -53,9 +58,11 @@ internal object YieldSupplyDomainModule { @Singleton fun provideYieldSupplyGetContractAddressUseCase( yieldSupplyTransactionRepository: YieldSupplyTransactionRepository, + yieldSupplyErrorResolver: YieldSupplyErrorResolver, ): YieldSupplyGetContractAddressUseCase { return YieldSupplyGetContractAddressUseCase( yieldSupplyTransactionRepository = yieldSupplyTransactionRepository, + yieldSupplyErrorResolver = yieldSupplyErrorResolver, ) } @@ -63,9 +70,11 @@ internal object YieldSupplyDomainModule { @Singleton fun provideYieldSupplyGetProtocolBalanceUseCase( yieldSupplyTransactionRepository: YieldSupplyTransactionRepository, + yieldSupplyErrorResolver: YieldSupplyErrorResolver, ): YieldSupplyGetProtocolBalanceUseCase { return YieldSupplyGetProtocolBalanceUseCase( yieldSupplyTransactionRepository = yieldSupplyTransactionRepository, + yieldSupplyErrorResolver = yieldSupplyErrorResolver, ) } } \ No newline at end of file diff --git a/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyErrorResolver.kt b/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyErrorResolver.kt new file mode 100644 index 0000000000..ddd0c7ab43 --- /dev/null +++ b/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyErrorResolver.kt @@ -0,0 +1,13 @@ +package com.tangem.data.yield.supply + +import com.tangem.domain.yield.supply.YieldSupplyError +import com.tangem.domain.yield.supply.YieldSupplyErrorResolver + +internal data object DefaultYieldSupplyErrorResolver : YieldSupplyErrorResolver { + override fun resolve(throwable: Throwable): YieldSupplyError { + return when (throwable) { + is YieldSupplyError -> throwable + else -> YieldSupplyError.DataError(throwable) + } + } +} \ No newline at end of file diff --git a/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyTransactionRepository.kt b/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyTransactionRepository.kt index f9767b60f8..aecfa8440d 100644 --- a/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyTransactionRepository.kt +++ b/data/yield-supply/src/main/java/com/tangem/data/yield/supply/DefaultYieldSupplyTransactionRepository.kt @@ -17,6 +17,7 @@ import com.tangem.domain.utils.convertToSdkAmount import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.yield.supply.YieldSupplyTransactionRepository import com.tangem.utils.coroutines.CoroutineDispatcherProvider +import com.tangem.utils.extensions.orZero import kotlinx.coroutines.withContext import timber.log.Timber import java.math.BigDecimal @@ -106,7 +107,7 @@ internal class DefaultYieldSupplyTransactionRepository( decimals = cryptoCurrency.decimals, ), ) - }.onFailure(Timber::e).getOrNull() + }.onFailure(Timber::e).getOrThrow() } @Suppress("LongParameterList") @@ -173,14 +174,16 @@ internal class DefaultYieldSupplyTransactionRepository( ) } - enterTransactions.add( - createEnterTransaction( - walletManager = walletManager, - cryptoCurrency = cryptoCurrency, - amount = amount, - yieldContractAddress = calculatedYieldContractAddress, - ), - ) + if (cryptoCurrencyStatus.value.amount.orZero() > BigDecimal.ZERO) { + enterTransactions.add( + createEnterTransaction( + walletManager = walletManager, + cryptoCurrency = cryptoCurrency, + amount = amount, + yieldContractAddress = calculatedYieldContractAddress, + ), + ) + } return enterTransactions } @@ -197,7 +200,7 @@ internal class DefaultYieldSupplyTransactionRepository( derivationPath = cryptoCurrency.network.derivationPath.value, ) ?: error("Wallet manager not found") walletManager.calculateYieldModuleAddress() - }.onFailure(Timber::e).getOrNull() + }.onFailure(Timber::e).getOrThrow() } override suspend fun getYieldContractAddress(userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency): String? = @@ -210,7 +213,7 @@ internal class DefaultYieldSupplyTransactionRepository( derivationPath = cryptoCurrency.network.derivationPath.value, ) ?: error("Wallet manager not found") walletManager.getYieldModuleAddress() - }.onFailure(Timber::e).getOrNull() + }.onFailure(Timber::e).getOrThrow() } private suspend fun getYieldTokenStatus( @@ -232,7 +235,7 @@ internal class DefaultYieldSupplyTransactionRepository( isInitialized = sdkSupplyStatus?.isInitialized == true, isAllowedToSpend = isAllowedToSpend, ) - }.onFailure(Timber::e).getOrNull() + }.onFailure(Timber::e).getOrThrow() } private fun createDeployTransaction( diff --git a/data/yield-supply/src/main/java/com/tangem/data/yield/supply/di/YieldSupplyDataModule.kt b/data/yield-supply/src/main/java/com/tangem/data/yield/supply/di/YieldSupplyDataModule.kt index 4aec69a8ad..8888eba918 100644 --- a/data/yield-supply/src/main/java/com/tangem/data/yield/supply/di/YieldSupplyDataModule.kt +++ b/data/yield-supply/src/main/java/com/tangem/data/yield/supply/di/YieldSupplyDataModule.kt @@ -1,11 +1,13 @@ package com.tangem.data.yield.supply.di import com.tangem.data.yield.supply.DefaultYieldSupplyMarketRepository +import com.tangem.data.yield.supply.DefaultYieldSupplyErrorResolver import com.tangem.data.yield.supply.DefaultYieldSupplyTransactionRepository import com.tangem.datasource.api.tangemTech.YieldSupplyApi import com.tangem.datasource.local.yieldsupply.YieldMarketsStore import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.yield.supply.YieldSupplyMarketRepository +import com.tangem.domain.yield.supply.YieldSupplyErrorResolver import com.tangem.domain.yield.supply.YieldSupplyTransactionRepository import com.tangem.utils.coroutines.CoroutineDispatcherProvider import dagger.Module @@ -43,4 +45,10 @@ internal object YieldSupplyDataModule { dispatchers = dispatchers, ) } + + @Provides + @Singleton + fun provideYieldSupplyErrorResolver(): YieldSupplyErrorResolver { + return DefaultYieldSupplyErrorResolver + } } \ No newline at end of file diff --git a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyError.kt b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyError.kt new file mode 100644 index 0000000000..74e3305a7c --- /dev/null +++ b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyError.kt @@ -0,0 +1,12 @@ +package com.tangem.domain.yield.supply + +sealed class YieldSupplyError : Throwable() { + + abstract val code: Int + + data class DataError( + val throwable: Throwable, + ) : YieldSupplyError() { + override val code: Int = -1 + } +} \ No newline at end of file diff --git a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyErrorResolver.kt b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyErrorResolver.kt new file mode 100644 index 0000000000..c0e1586c15 --- /dev/null +++ b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/YieldSupplyErrorResolver.kt @@ -0,0 +1,5 @@ +package com.tangem.domain.yield.supply + +interface YieldSupplyErrorResolver { + fun resolve(throwable: Throwable): YieldSupplyError +} \ No newline at end of file diff --git a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyGetContractAddressUseCase.kt b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyGetContractAddressUseCase.kt index 4526550e39..2d12deef9e 100644 --- a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyGetContractAddressUseCase.kt +++ b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyGetContractAddressUseCase.kt @@ -3,19 +3,22 @@ package com.tangem.domain.yield.supply.usecase import arrow.core.Either import com.tangem.domain.models.currency.CryptoCurrency import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.domain.yield.supply.YieldSupplyError +import com.tangem.domain.yield.supply.YieldSupplyErrorResolver import com.tangem.domain.yield.supply.YieldSupplyTransactionRepository class YieldSupplyGetContractAddressUseCase( private val yieldSupplyTransactionRepository: YieldSupplyTransactionRepository, + private val yieldSupplyErrorResolver: YieldSupplyErrorResolver, ) { suspend operator fun invoke( userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency.Token, - ): Either = Either.catch { + ): Either = Either.catch { yieldSupplyTransactionRepository.getYieldContractAddress( userWalletId = userWalletId, cryptoCurrency = cryptoCurrency, ) - } + }.mapLeft(yieldSupplyErrorResolver::resolve) } \ No newline at end of file diff --git a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyGetProtocolBalanceUseCase.kt b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyGetProtocolBalanceUseCase.kt index 72ff2ffef7..a36158a7b6 100644 --- a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyGetProtocolBalanceUseCase.kt +++ b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyGetProtocolBalanceUseCase.kt @@ -3,22 +3,25 @@ package com.tangem.domain.yield.supply.usecase import arrow.core.Either import com.tangem.domain.models.currency.CryptoCurrency import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.domain.yield.supply.YieldSupplyError +import com.tangem.domain.yield.supply.YieldSupplyErrorResolver import com.tangem.domain.yield.supply.YieldSupplyTransactionRepository import java.math.BigDecimal class YieldSupplyGetProtocolBalanceUseCase( private val yieldSupplyTransactionRepository: YieldSupplyTransactionRepository, + private val yieldSupplyErrorResolver: YieldSupplyErrorResolver, ) { suspend operator fun invoke( userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency, - ): Either = Either.catch { + ): Either = Either.catch { requireNotNull(cryptoCurrency as CryptoCurrency.Token) yieldSupplyTransactionRepository.getProtocolBalance( userWalletId = userWalletId, cryptoCurrency = cryptoCurrency, ) - } + }.mapLeft(yieldSupplyErrorResolver::resolve) } \ No newline at end of file diff --git a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyStartEarningUseCase.kt b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyStartEarningUseCase.kt index a9ee4d9c8f..9944223759 100644 --- a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyStartEarningUseCase.kt +++ b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyStartEarningUseCase.kt @@ -4,22 +4,25 @@ import arrow.core.Either import com.tangem.blockchain.common.TransactionData import com.tangem.domain.models.currency.CryptoCurrencyStatus import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.domain.yield.supply.YieldSupplyError +import com.tangem.domain.yield.supply.YieldSupplyErrorResolver import com.tangem.domain.yield.supply.YieldSupplyTransactionRepository import java.math.BigDecimal class YieldSupplyStartEarningUseCase( private val yieldSupplyTransactionRepository: YieldSupplyTransactionRepository, + private val yieldSupplyErrorResolver: YieldSupplyErrorResolver, ) { suspend operator fun invoke( userWalletId: UserWalletId, cryptoCurrencyStatus: CryptoCurrencyStatus, maxNetworkFee: BigDecimal, - ): Either> = Either.catch { + ): Either> = Either.catch { yieldSupplyTransactionRepository.createEnterTransactions( userWalletId = userWalletId, cryptoCurrencyStatus = cryptoCurrencyStatus, maxNetworkFee = maxNetworkFee, ) - } + }.mapLeft(yieldSupplyErrorResolver::resolve) } \ No newline at end of file diff --git a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyStopEarningUseCase.kt b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyStopEarningUseCase.kt index d8c3554894..2eb7b8995b 100644 --- a/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyStopEarningUseCase.kt +++ b/domain/yield-supply/src/main/java/com/tangem/domain/yield/supply/usecase/YieldSupplyStopEarningUseCase.kt @@ -5,21 +5,24 @@ import com.tangem.blockchain.common.TransactionData import com.tangem.blockchain.common.transaction.Fee import com.tangem.domain.models.currency.CryptoCurrencyStatus import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.domain.yield.supply.YieldSupplyError +import com.tangem.domain.yield.supply.YieldSupplyErrorResolver import com.tangem.domain.yield.supply.YieldSupplyTransactionRepository class YieldSupplyStopEarningUseCase( private val yieldSupplyTransactionRepository: YieldSupplyTransactionRepository, + private val yieldSupplyErrorResolver: YieldSupplyErrorResolver, ) { suspend operator fun invoke( userWalletId: UserWalletId, cryptoCurrencyStatus: CryptoCurrencyStatus, fee: Fee?, - ): Either = Either.catch { + ): Either = Either.catch { yieldSupplyTransactionRepository.createExitTransaction( userWalletId = userWalletId, cryptoCurrencyStatus = cryptoCurrencyStatus, fee = null, ) - } + }.mapLeft(yieldSupplyErrorResolver::resolve) } \ No newline at end of file