Updated on 2026-08-14
This commit is contained in:
parent
1d06981060
commit
7b5a62d593
10 changed files with 82 additions and 20 deletions
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package com.tangem.domain.yield.supply
|
||||
|
||||
interface YieldSupplyErrorResolver {
|
||||
fun resolve(throwable: Throwable): YieldSupplyError
|
||||
}
|
||||
|
|
@ -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<Throwable, String?> = Either.catch {
|
||||
): Either<YieldSupplyError, String?> = Either.catch {
|
||||
yieldSupplyTransactionRepository.getYieldContractAddress(
|
||||
userWalletId = userWalletId,
|
||||
cryptoCurrency = cryptoCurrency,
|
||||
)
|
||||
}
|
||||
}.mapLeft(yieldSupplyErrorResolver::resolve)
|
||||
}
|
||||
|
|
@ -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<Throwable, BigDecimal?> = Either.catch {
|
||||
): Either<YieldSupplyError, BigDecimal?> = Either.catch {
|
||||
requireNotNull(cryptoCurrency as CryptoCurrency.Token)
|
||||
|
||||
yieldSupplyTransactionRepository.getProtocolBalance(
|
||||
userWalletId = userWalletId,
|
||||
cryptoCurrency = cryptoCurrency,
|
||||
)
|
||||
}
|
||||
}.mapLeft(yieldSupplyErrorResolver::resolve)
|
||||
}
|
||||
|
|
@ -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<Throwable, List<TransactionData.Uncompiled>> = Either.catch {
|
||||
): Either<YieldSupplyError, List<TransactionData.Uncompiled>> = Either.catch {
|
||||
yieldSupplyTransactionRepository.createEnterTransactions(
|
||||
userWalletId = userWalletId,
|
||||
cryptoCurrencyStatus = cryptoCurrencyStatus,
|
||||
maxNetworkFee = maxNetworkFee,
|
||||
)
|
||||
}
|
||||
}.mapLeft(yieldSupplyErrorResolver::resolve)
|
||||
}
|
||||
|
|
@ -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<Throwable, TransactionData.Uncompiled> = Either.catch {
|
||||
): Either<YieldSupplyError, TransactionData.Uncompiled> = Either.catch {
|
||||
yieldSupplyTransactionRepository.createExitTransaction(
|
||||
userWalletId = userWalletId,
|
||||
cryptoCurrencyStatus = cryptoCurrencyStatus,
|
||||
fee = null,
|
||||
)
|
||||
}
|
||||
}.mapLeft(yieldSupplyErrorResolver::resolve)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue