From 6ea62cc4c6ab1156ed7b5084d4983c7d0a7fb36b Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 12 Dec 2023 18:30:33 +0300 Subject: [PATCH] Updated on 2026-08-14 --- app/build.gradle.kts | 1 + features/swap/data/build.gradle.kts | 2 + .../tangem/feature/swap/SwapRepositoryImpl.kt | 29 ++-- .../tangem/feature/swap/di/SwapDataModule.kt | 2 +- features/swap/domain/api/.gitignore | 1 + features/swap/domain/api/build.gradle.kts | 18 +++ .../swap/domain/api}/SwapRepository.kt | 8 +- features/swap/domain/build.gradle.kts | 3 +- features/swap/domain/models/.gitignore | 1 + features/swap/domain/models/build.gradle.kts | 23 +++ .../feature/swap/domain/models/DataError.kt | 0 .../swap/domain/models/ExpressException.kt | 0 .../feature/swap/domain/models/SwapAmount.kt | 0 .../swap/domain/models/domain/Currency.kt | 0 .../domain/models/domain/ExchangeQuote.kt | 0 .../domain/models/domain/ExchangeStatus.kt | 0 .../models/domain/ExpressTransactionModel.kt | 0 .../domain/models/domain/LeastTokenInfo.kt | 0 .../swap/domain/models/domain/NetworkInfo.kt | 0 .../models/domain/PairsWithProviders.kt | 0 .../domain/models/domain/PermissionOptions.kt | 0 .../models/domain/PreparedSwapConfigState.kt | 0 .../swap/domain/models/domain/QuoteModel.kt | 0 .../domain/SavedLastSwappedCryptoCurrency.kt | 0 .../domain/SavedSwapTransactionListModel.kt | 0 .../domain/models/domain/SwapApproveType.kt | 0 .../domain/models/domain/SwapDataModel.kt | 0 .../domain/models/domain/SwapPairLeast.kt | 0 .../domain/models/domain/TransactionModel.kt | 0 .../swap/domain/models/ui/AmountFormatter.kt | 0 .../swap/domain/models/ui/SwapState.kt | 0 .../swap/domain/models/ui/TokensDataState.kt | 0 .../models/ui/TokensDataStateExpress.kt | 0 .../feature/swap/domain/models/ui/TxState.kt | 0 .../feature/swap/domain/SwapInteractor.kt | 5 +- .../feature/swap/domain/SwapInteractorImpl.kt | 153 +++++++++--------- .../swap/domain/di/SwapDomainModule.kt | 1 + .../models/data/AggregatedSwapDataModel.kt | 15 -- features/swap/presentation/build.gradle.kts | 9 +- .../tangem/feature/swap/ui/StateBuilder.kt | 4 +- .../feature/swap/viewmodels/SwapViewModel.kt | 2 +- features/tokendetails/impl/build.gradle.kts | 3 +- .../viewmodels/ExchangeStatusFactory.kt | 2 +- .../viewmodels/TokenDetailsViewModel.kt | 2 +- settings.gradle.kts | 4 + 45 files changed, 165 insertions(+), 123 deletions(-) create mode 100644 features/swap/domain/api/.gitignore create mode 100644 features/swap/domain/api/build.gradle.kts rename features/swap/domain/{src/main/java/com/tangem/feature/swap/domain => api/src/main/java/com/tangem/feature/swap/domain/api}/SwapRepository.kt (91%) create mode 100644 features/swap/domain/models/.gitignore create mode 100644 features/swap/domain/models/build.gradle.kts rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/DataError.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/Currency.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeQuote.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeStatus.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/ExpressTransactionModel.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/LeastTokenInfo.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/NetworkInfo.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/PairsWithProviders.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/PermissionOptions.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/PreparedSwapConfigState.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/QuoteModel.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedLastSwappedCryptoCurrency.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedSwapTransactionListModel.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapApproveType.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapDataModel.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapPairLeast.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/domain/TransactionModel.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataState.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt (100%) rename features/swap/domain/{ => models}/src/main/java/com/tangem/feature/swap/domain/models/ui/TxState.kt (100%) delete mode 100644 features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/data/AggregatedSwapDataModel.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index de5864d8b1..2791ba88f4 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -87,6 +87,7 @@ dependencies { implementation(projects.features.swap.api) implementation(projects.features.swap.presentation) implementation(projects.features.swap.domain) + implementation(projects.features.swap.domain.api) implementation(projects.features.swap.data) implementation(projects.features.tester.api) implementation(projects.features.tester.impl) diff --git a/features/swap/data/build.gradle.kts b/features/swap/data/build.gradle.kts index 53dddfc5a9..a2b37ee319 100644 --- a/features/swap/data/build.gradle.kts +++ b/features/swap/data/build.gradle.kts @@ -15,6 +15,8 @@ dependencies { implementation(projects.core.datasource) implementation(projects.core.utils) implementation(projects.features.swap.domain) + implementation(projects.features.swap.domain.models) + implementation(projects.features.swap.domain.api) /** Network */ implementation(deps.retrofit) diff --git a/features/swap/data/src/main/java/com/tangem/feature/swap/SwapRepositoryImpl.kt b/features/swap/data/src/main/java/com/tangem/feature/swap/SwapRepositoryImpl.kt index 26ac346f90..ffd12827ba 100644 --- a/features/swap/data/src/main/java/com/tangem/feature/swap/SwapRepositoryImpl.kt +++ b/features/swap/data/src/main/java/com/tangem/feature/swap/SwapRepositoryImpl.kt @@ -1,8 +1,10 @@ package com.tangem.feature.swap import arrow.core.Either +import arrow.core.left import arrow.core.raise.catch import arrow.core.raise.either +import arrow.core.right import com.tangem.blockchain.common.Amount import com.tangem.blockchain.common.Approver import com.tangem.blockchain.common.Blockchain @@ -27,12 +29,11 @@ import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.wallets.legacy.WalletsStateHolder import com.tangem.domain.wallets.models.UserWalletId import com.tangem.feature.swap.converters.* -import com.tangem.feature.swap.domain.SwapRepository +import com.tangem.feature.swap.domain.api.SwapRepository +import com.tangem.feature.swap.domain.models.domain.* import com.tangem.feature.swap.domain.models.DataError import com.tangem.feature.swap.domain.models.ExpressException import com.tangem.feature.swap.domain.models.createFromAmountWithOffset -import com.tangem.feature.swap.domain.models.data.AggregatedSwapDataModel -import com.tangem.feature.swap.domain.models.domain.* import com.tangem.utils.coroutines.CoroutineDispatcherProvider import kotlinx.coroutines.async import kotlinx.coroutines.withContext @@ -184,7 +185,7 @@ internal class SwapRepositoryImpl @Inject constructor( toDecimals: Int, providerId: String, rateType: RateType, - ): AggregatedSwapDataModel { + ): Either { return withContext(coroutineDispatcher.io) { try { val response = tangemExpressApi.getExchangeQuote( @@ -198,14 +199,12 @@ internal class SwapRepositoryImpl @Inject constructor( providerId = providerId, rateType = rateType.name.lowercase(), ).getOrThrow() - AggregatedSwapDataModel( - dataModel = QuoteModel( - toTokenAmount = createFromAmountWithOffset(response.toAmount, response.toDecimals), - allowanceContract = response.allowanceContract, - ), - ) + QuoteModel( + toTokenAmount = createFromAmountWithOffset(response.toAmount, response.toDecimals), + allowanceContract = response.allowanceContract, + ).right() } catch (ex: Exception) { - AggregatedSwapDataModel(null, getDataError(ex)) + getDataError(ex).left() } } } @@ -227,7 +226,7 @@ internal class SwapRepositoryImpl @Inject constructor( providerId: String, rateType: RateType, toAddress: String, - ): AggregatedSwapDataModel { + ): Either { return withContext(coroutineDispatcher.io) { try { val response = tangemExpressApi.getExchangeData( @@ -242,11 +241,9 @@ internal class SwapRepositoryImpl @Inject constructor( rateType = rateType.name.lowercase(), toAddress = toAddress, ).getOrThrow() - AggregatedSwapDataModel( - dataModel = expressDataConverter.convert(response), - ) + expressDataConverter.convert(response).right() } catch (ex: Exception) { - AggregatedSwapDataModel(null, getDataError(ex)) + getDataError(ex).left() } } } diff --git a/features/swap/data/src/main/java/com/tangem/feature/swap/di/SwapDataModule.kt b/features/swap/data/src/main/java/com/tangem/feature/swap/di/SwapDataModule.kt index b30b20f817..83ddab6b62 100644 --- a/features/swap/data/src/main/java/com/tangem/feature/swap/di/SwapDataModule.kt +++ b/features/swap/data/src/main/java/com/tangem/feature/swap/di/SwapDataModule.kt @@ -13,7 +13,7 @@ import com.tangem.domain.wallets.legacy.WalletsStateHolder import com.tangem.feature.swap.SwapRepositoryImpl import com.tangem.feature.swap.converters.ErrorsDataConverter import com.tangem.feature.swap.DefaultSwapTransactionRepository -import com.tangem.feature.swap.domain.SwapRepository +import com.tangem.feature.swap.domain.api.SwapRepository import com.tangem.feature.swap.domain.SwapTransactionRepository import com.tangem.utils.coroutines.CoroutineDispatcherProvider import dagger.Module diff --git a/features/swap/domain/api/.gitignore b/features/swap/domain/api/.gitignore new file mode 100644 index 0000000000..42afabfd2a --- /dev/null +++ b/features/swap/domain/api/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/features/swap/domain/api/build.gradle.kts b/features/swap/domain/api/build.gradle.kts new file mode 100644 index 0000000000..38ecfe752e --- /dev/null +++ b/features/swap/domain/api/build.gradle.kts @@ -0,0 +1,18 @@ +plugins { + alias(deps.plugins.android.library) + alias(deps.plugins.kotlin.android) + id("configuration") +} + +android { + namespace = "com.tangem.feature.swap.domain.api" +} + +dependencies { + implementation(projects.features.swap.domain.models) + implementation(projects.domain.tokens.models) + implementation(projects.domain.wallets.models) + + implementation(deps.arrow.core) + +} \ No newline at end of file diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapRepository.kt b/features/swap/domain/api/src/main/java/com/tangem/feature/swap/domain/api/SwapRepository.kt similarity index 91% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapRepository.kt rename to features/swap/domain/api/src/main/java/com/tangem/feature/swap/domain/api/SwapRepository.kt index f68c41e255..ea82fe6495 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapRepository.kt +++ b/features/swap/domain/api/src/main/java/com/tangem/feature/swap/domain/api/SwapRepository.kt @@ -1,10 +1,10 @@ -package com.tangem.feature.swap.domain +package com.tangem.feature.swap.domain.api import arrow.core.Either import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.wallets.models.UserWalletId -import com.tangem.feature.swap.domain.models.data.AggregatedSwapDataModel import com.tangem.feature.swap.domain.models.domain.* +import com.tangem.feature.swap.domain.models.DataError import java.math.BigDecimal interface SwapRepository { @@ -28,7 +28,7 @@ interface SwapRepository { toDecimals: Int, providerId: String, rateType: RateType, - ): AggregatedSwapDataModel + ): Either /** * Returns address of 1inch router that must be trusted @@ -77,7 +77,7 @@ interface SwapRepository { providerId: String, rateType: RateType, toAddress: String, - ): AggregatedSwapDataModel + ): Either fun getNativeTokenForNetwork(networkId: String): CryptoCurrency } \ No newline at end of file diff --git a/features/swap/domain/build.gradle.kts b/features/swap/domain/build.gradle.kts index b1a50d2773..5fa5dfec86 100644 --- a/features/swap/domain/build.gradle.kts +++ b/features/swap/domain/build.gradle.kts @@ -29,6 +29,8 @@ dependencies { implementation(projects.domain.card) implementation(projects.domain.appCurrency.models) implementation(projects.domain.txhistory.models) + implementation(projects.features.swap.domain.api) + implementation(projects.features.swap.domain.models) /** Core modules */ implementation(projects.core.utils) @@ -38,7 +40,6 @@ dependencies { implementation(projects.features.wallet.api) /** Other Libraries **/ - implementation(deps.kotlin.serialization) implementation(deps.kotlin.coroutines) implementation(deps.arrow.core) implementation(deps.timber) diff --git a/features/swap/domain/models/.gitignore b/features/swap/domain/models/.gitignore new file mode 100644 index 0000000000..42afabfd2a --- /dev/null +++ b/features/swap/domain/models/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/features/swap/domain/models/build.gradle.kts b/features/swap/domain/models/build.gradle.kts new file mode 100644 index 0000000000..96958add9b --- /dev/null +++ b/features/swap/domain/models/build.gradle.kts @@ -0,0 +1,23 @@ +plugins { + alias(deps.plugins.android.library) + alias(deps.plugins.kotlin.android) + alias(deps.plugins.kotlin.kapt) + id("configuration") +} + +android { + namespace = "com.tangem.feature.swap.domain.models" +} + +dependencies { + /** Domain */ + implementation(projects.domain.tokens.models) + + /** Core modules */ + implementation(projects.core.utils) + + /** Other Libraries **/ + implementation(deps.kotlin.serialization) + implementation(deps.arrow.core) + +} \ No newline at end of file diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/DataError.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/DataError.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/DataError.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/DataError.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/Currency.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/Currency.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/Currency.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/Currency.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeQuote.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeQuote.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeQuote.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeQuote.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeStatus.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeStatus.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeStatus.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/ExchangeStatus.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/ExpressTransactionModel.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/ExpressTransactionModel.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/ExpressTransactionModel.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/ExpressTransactionModel.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/LeastTokenInfo.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/LeastTokenInfo.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/LeastTokenInfo.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/LeastTokenInfo.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/NetworkInfo.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/NetworkInfo.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/NetworkInfo.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/NetworkInfo.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/PairsWithProviders.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/PairsWithProviders.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/PairsWithProviders.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/PairsWithProviders.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/PermissionOptions.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/PermissionOptions.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/PermissionOptions.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/PermissionOptions.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/PreparedSwapConfigState.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/PreparedSwapConfigState.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/PreparedSwapConfigState.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/PreparedSwapConfigState.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/QuoteModel.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/QuoteModel.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/QuoteModel.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/QuoteModel.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedLastSwappedCryptoCurrency.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedLastSwappedCryptoCurrency.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedLastSwappedCryptoCurrency.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedLastSwappedCryptoCurrency.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedSwapTransactionListModel.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedSwapTransactionListModel.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedSwapTransactionListModel.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SavedSwapTransactionListModel.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapApproveType.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapApproveType.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapApproveType.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapApproveType.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapDataModel.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapDataModel.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapDataModel.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapDataModel.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapPairLeast.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapPairLeast.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapPairLeast.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/SwapPairLeast.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/TransactionModel.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/TransactionModel.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/domain/TransactionModel.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/domain/TransactionModel.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataState.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataState.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataState.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataState.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TxState.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/TxState.kt similarity index 100% rename from features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TxState.kt rename to features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/TxState.kt diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractor.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractor.kt index d36610f673..765647a01e 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractor.kt +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractor.kt @@ -5,7 +5,10 @@ import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.Network import com.tangem.domain.wallets.models.UserWallet import com.tangem.feature.swap.domain.models.SwapAmount -import com.tangem.feature.swap.domain.models.domain.* +import com.tangem.feature.swap.domain.models.domain.IncludeFeeInAmount +import com.tangem.feature.swap.domain.models.domain.PermissionOptions +import com.tangem.feature.swap.domain.models.domain.SwapDataModel +import com.tangem.feature.swap.domain.models.domain.SwapProvider import com.tangem.feature.swap.domain.models.ui.* interface SwapInteractor { diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt index 3b32d69aec..3754e0c835 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt @@ -1,5 +1,6 @@ package com.tangem.feature.swap.domain +import arrow.core.Either import arrow.core.getOrElse import com.tangem.blockchain.common.Amount import com.tangem.blockchain.common.AmountType @@ -19,9 +20,10 @@ import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.models.UserWalletId import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase +import com.tangem.feature.swap.domain.api.SwapRepository import com.tangem.feature.swap.domain.converters.SwapCurrencyConverter +import com.tangem.feature.swap.domain.models.DataError import com.tangem.feature.swap.domain.models.SwapAmount -import com.tangem.feature.swap.domain.models.data.AggregatedSwapDataModel import com.tangem.feature.swap.domain.models.domain.* import com.tangem.feature.swap.domain.models.toStringWithRightOffset import com.tangem.feature.swap.domain.models.ui.* @@ -297,13 +299,14 @@ internal class SwapInteractorImpl @Inject constructor( ) val fromTokenAddress = getTokenAddress(fromToken.currency) - val isAllowedToSpend = if (quotes.dataModel != null) { - quotes.dataModel.allowanceContract?.let { - isAllowedToSpend(networkId, fromToken.currency, amount, it) - } ?: true - } else { - false - } + val isAllowedToSpend = quotes.fold( + ifRight = { + it.allowanceContract?.let { + isAllowedToSpend(networkId, fromToken.currency, amount, it) + } ?: true + }, + ifLeft = { false }, + ) if (isAllowedToSpend && allowPermissionsHandler.isAddressAllowanceInProgress(fromTokenAddress)) { allowPermissionsHandler.removeAddressFromProgress(fromTokenAddress) @@ -497,13 +500,13 @@ internal class SwapInteractorImpl @Inject constructor( providerId = swapProvider.providerId, rateType = RateType.FLOAT, toAddress = currencyToGet.value.networkAddress?.defaultAddress?.value ?: "", - ) + ).getOrNull() val txData = walletManagersFacade.createTransaction( amount = amount.value.convertToAmount(currencyToSend.currency), fee = getFeeForTransaction(txFee), memo = null, - destination = (exchangeData.dataModel?.transaction as ExpressTransactionModel.CEX).txTo, + destination = (exchangeData?.transaction as ExpressTransactionModel.CEX).txTo, userWalletId = userWalletId, network = currencyToSend.currency.network, ) @@ -514,7 +517,7 @@ internal class SwapInteractorImpl @Inject constructor( network = currencyToSend.currency.network, ) - val externalUrl = (exchangeData.dataModel.transaction as? ExpressTransactionModel.CEX)?.externalTxUrl + val externalUrl = (exchangeData.transaction as? ExpressTransactionModel.CEX)?.externalTxUrl return result.fold( ifLeft = { @@ -533,7 +536,7 @@ internal class SwapInteractorImpl @Inject constructor( currencyToGet = currencyToGet, amount = amount, swapProvider = swapProvider, - swapDataModel = exchangeData.dataModel, + swapDataModel = exchangeData, timestamp = timestamp, ) storeLastCryptoCurrencyId(currencyToGet.currency) @@ -544,10 +547,10 @@ internal class SwapInteractorImpl @Inject constructor( ), fromAmountValue = amount.value, toAmount = amountFormatter.formatSwapAmountToUI( - exchangeData.dataModel.toTokenAmount, + exchangeData.toTokenAmount, currencyToGet.currency.symbol, ), - toAmountValue = exchangeData.dataModel.toTokenAmount.value, + toAmountValue = exchangeData.toTokenAmount.value, txAddress = userWalletManager.getLastTransactionHash( currencyToSend.currency.network.backendId, derivationPath, @@ -751,7 +754,7 @@ internal class SwapInteractorImpl @Inject constructor( private suspend fun getQuotesState( exchangeProviderType: ExchangeProviderType, - quoteDataModel: AggregatedSwapDataModel, + quoteDataModel: Either, amount: SwapAmount, fromToken: CryptoCurrencyStatus, toToken: CryptoCurrencyStatus, @@ -761,58 +764,60 @@ internal class SwapInteractorImpl @Inject constructor( txFee: TxFeeState, includeFeeInAmount: IncludeFeeInAmount, ): SwapState { - val quoteModel = quoteDataModel.dataModel - if (quoteModel != null) { - val swapState = updateBalances( - networkId = networkId, - fromTokenStatus = fromToken, - toTokenStatus = toToken, - fromTokenAmount = amount, - toTokenAmount = quoteModel.toTokenAmount, - swapData = null, - txFeeState = txFee, - ) + return quoteDataModel.fold( + ifRight = { quoteModel -> + val swapState = updateBalances( + networkId = networkId, + fromTokenStatus = fromToken, + toTokenStatus = toToken, + fromTokenAmount = amount, + toTokenAmount = quoteModel.toTokenAmount, + swapData = null, + txFeeState = txFee, + ) - return when (exchangeProviderType) { - ExchangeProviderType.DEX -> { - val state = updatePermissionState( - networkId = networkId, - fromTokenStatus = fromToken, - swapAmount = amount, - quotesLoadedState = swapState, - isAllowedToSpend = isAllowedToSpend, - spenderAddress = quoteModel.allowanceContract, - ) - state.copy( - preparedSwapConfigState = state.preparedSwapConfigState.copy( + when (exchangeProviderType) { + ExchangeProviderType.DEX -> { + val state = updatePermissionState( + networkId = networkId, + fromTokenStatus = fromToken, + swapAmount = amount, + quotesLoadedState = swapState, isAllowedToSpend = isAllowedToSpend, - isBalanceEnough = isBalanceWithoutFeeEnough, - ), - ) + spenderAddress = quoteModel.allowanceContract, + ) + state.copy( + preparedSwapConfigState = state.preparedSwapConfigState.copy( + isAllowedToSpend = isAllowedToSpend, + isBalanceEnough = isBalanceWithoutFeeEnough, + ), + ) + } + ExchangeProviderType.CEX -> { + swapState.copy( + permissionState = PermissionDataState.Empty, + preparedSwapConfigState = PreparedSwapConfigState( + isFeeEnough = includeFeeInAmount !is IncludeFeeInAmount.BalanceNotEnough, + isAllowedToSpend = isAllowedToSpend, + isBalanceEnough = isBalanceWithoutFeeEnough, + hasOutgoingTransaction = hasOutgoingTransaction(fromToken), + includeFeeInAmount = includeFeeInAmount, + ), + ) + } } - ExchangeProviderType.CEX -> { - swapState.copy( - permissionState = PermissionDataState.Empty, - preparedSwapConfigState = PreparedSwapConfigState( - isFeeEnough = includeFeeInAmount !is IncludeFeeInAmount.BalanceNotEnough, - isAllowedToSpend = isAllowedToSpend, - isBalanceEnough = isBalanceWithoutFeeEnough, - hasOutgoingTransaction = hasOutgoingTransaction(fromToken), - includeFeeInAmount = includeFeeInAmount, - ), - ) - } - } - } else { - val rates = getQuotes(fromToken.currency.id) - val fromTokenSwapInfo = TokenSwapInfo( - tokenAmount = amount, - amountFiat = rates[fromToken.currency.id]?.fiatRate?.multiply(amount.value) - ?: BigDecimal.ZERO, - cryptoCurrencyStatus = fromToken, - ) - return SwapState.SwapError(fromTokenSwapInfo, quoteDataModel.error) - } + }, + ifLeft = { error -> + val rates = getQuotes(fromToken.currency.id) + val fromTokenSwapInfo = TokenSwapInfo( + tokenAmount = amount, + amountFiat = rates[fromToken.currency.id]?.fiatRate?.multiply(amount.value) + ?: BigDecimal.ZERO, + cryptoCurrencyStatus = fromToken, + ) + return SwapState.SwapError(fromTokenSwapInfo, error) + }, + ) } private suspend fun getIncludeFeeInAmount( @@ -882,7 +887,7 @@ internal class SwapInteractorImpl @Inject constructor( amount: SwapAmount, selectedFee: FeeType, ): SwapState { - repository.getExchangeData( + return repository.getExchangeData( fromContractAddress = fromToken.currency.getContractAddress(), fromNetwork = fromToken.currency.network.backendId, toContractAddress = toToken.currency.getContractAddress(), @@ -893,9 +898,8 @@ internal class SwapInteractorImpl @Inject constructor( providerId = provider.providerId, rateType = RateType.FLOAT, toAddress = toToken.value.networkAddress?.defaultAddress?.value ?: "", - ).let { - val swapData = it.dataModel - if (swapData != null) { + ).fold( + ifRight = { swapData -> val feeData = transactionManager.getFee( networkId = networkId, amountToSend = amount.value, @@ -926,7 +930,7 @@ internal class SwapInteractorImpl @Inject constructor( swapData = swapData, txFeeState = txFeeState, ) - return swapState.copy( + swapState.copy( permissionState = PermissionDataState.Empty, preparedSwapConfigState = PreparedSwapConfigState( isAllowedToSpend = true, @@ -936,7 +940,8 @@ internal class SwapInteractorImpl @Inject constructor( includeFeeInAmount = IncludeFeeInAmount.Excluded, // exclude for dex ), ) - } else { + }, + ifLeft = { error -> val rates = getQuotes(fromToken.currency.id) val fromTokenSwapInfo = TokenSwapInfo( tokenAmount = amount, @@ -944,12 +949,12 @@ internal class SwapInteractorImpl @Inject constructor( ?: BigDecimal.ZERO, cryptoCurrencyStatus = fromToken, ) - return SwapState.SwapError( + SwapState.SwapError( fromTokenSwapInfo, - it.error, + error, ) - } - } + }, + ) } @Suppress("LongParameterList") diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/di/SwapDomainModule.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/di/SwapDomainModule.kt index f6bf9093c9..d299f7824e 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/di/SwapDomainModule.kt +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/di/SwapDomainModule.kt @@ -13,6 +13,7 @@ import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.wallets.legacy.WalletsStateHolder import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase import com.tangem.feature.swap.domain.* +import com.tangem.feature.swap.domain.api.SwapRepository import com.tangem.lib.crypto.TransactionManager import com.tangem.lib.crypto.UserWalletManager import com.tangem.utils.coroutines.CoroutineDispatcherProvider diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/data/AggregatedSwapDataModel.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/data/AggregatedSwapDataModel.kt deleted file mode 100644 index 45a895181e..0000000000 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/data/AggregatedSwapDataModel.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.tangem.feature.swap.domain.models.data - -import com.tangem.feature.swap.domain.models.DataError - -/** - * Model that aggregate data model from repository return with error [DataError] if it exists - * - * @param T model type - * @property dataModel - * @property error possible from repository [DataError] - */ -data class AggregatedSwapDataModel( - val dataModel: T?, - val error: DataError = DataError.UnknownError, -) \ No newline at end of file diff --git a/features/swap/presentation/build.gradle.kts b/features/swap/presentation/build.gradle.kts index b2ea5cda48..db0e737edf 100644 --- a/features/swap/presentation/build.gradle.kts +++ b/features/swap/presentation/build.gradle.kts @@ -26,6 +26,10 @@ dependencies { implementation(projects.domain.tokens.models) implementation(projects.domain.wallets) implementation(projects.domain.wallets.models) + implementation(projects.domain.settings) + implementation(projects.features.swap.domain) + implementation(projects.features.swap.domain.api) + implementation(projects.features.swap.domain.models) /** AndroidX */ implementation(deps.androidx.activity.compose) @@ -48,11 +52,6 @@ dependencies { implementation(projects.features.swap.api) implementation(projects.features.tokendetails.api) - /** Domain */ - implementation(projects.features.swap.domain) - implementation(projects.domain.tokens.models) - implementation(projects.domain.settings) - /** Other libraries */ implementation(deps.compose.shimmer) implementation(deps.kotlin.serialization) diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt index 2837f48f2a..6f674f14e9 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt @@ -12,11 +12,11 @@ import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.feature.swap.converters.TokensDataConverter +import com.tangem.feature.swap.domain.models.domain.* +import com.tangem.feature.swap.domain.models.ui.* import com.tangem.feature.swap.domain.models.DataError import com.tangem.feature.swap.domain.models.SwapAmount -import com.tangem.feature.swap.domain.models.domain.* import com.tangem.feature.swap.domain.models.formatToUIRepresentation -import com.tangem.feature.swap.domain.models.ui.* import com.tangem.feature.swap.models.* import com.tangem.feature.swap.models.states.* import com.tangem.feature.swap.presentation.R diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt index b17c398d2e..944e9ff75d 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt @@ -22,8 +22,8 @@ import com.tangem.feature.swap.domain.models.ExpressException import com.tangem.feature.swap.domain.models.domain.PermissionOptions import com.tangem.feature.swap.domain.models.domain.SwapDataModel import com.tangem.feature.swap.domain.models.domain.SwapProvider -import com.tangem.feature.swap.domain.models.formatToUIRepresentation import com.tangem.feature.swap.domain.models.ui.* +import com.tangem.feature.swap.domain.models.formatToUIRepresentation import com.tangem.feature.swap.models.* import com.tangem.feature.swap.presentation.SwapFragment import com.tangem.feature.swap.router.SwapNavScreen diff --git a/features/tokendetails/impl/build.gradle.kts b/features/tokendetails/impl/build.gradle.kts index 8c4c106fa0..860b64eec4 100644 --- a/features/tokendetails/impl/build.gradle.kts +++ b/features/tokendetails/impl/build.gradle.kts @@ -70,9 +70,10 @@ dependencies { /** Temp dependency to swap domain */ implementation(projects.features.swap.domain) + implementation(projects.features.swap.domain.api) + implementation(projects.features.swap.domain.models) /** Feature Apis */ implementation(projects.features.tokendetails.api) implementation(projects.features.send.api) - implementation(projects.features.swap.domain) } \ No newline at end of file diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/ExchangeStatusFactory.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/ExchangeStatusFactory.kt index 13445dc76d..e1f2ca8766 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/ExchangeStatusFactory.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/ExchangeStatusFactory.kt @@ -12,7 +12,7 @@ import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.models.analytics.TokenExchangeAnalyticsEvent import com.tangem.domain.wallets.models.UserWalletId import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase -import com.tangem.feature.swap.domain.SwapRepository +import com.tangem.feature.swap.domain.api.SwapRepository import com.tangem.feature.swap.domain.SwapTransactionRepository import com.tangem.feature.swap.domain.models.domain.ExchangeStatus import com.tangem.feature.swap.domain.models.domain.ExchangeStatusModel diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt index 1a718c9846..8b10c5c379 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt @@ -35,7 +35,7 @@ import com.tangem.domain.wallets.models.UserWalletId import com.tangem.domain.wallets.usecase.GetExploreUrlUseCase import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase import com.tangem.domain.wallets.usecase.GetUserWalletUseCase -import com.tangem.feature.swap.domain.SwapRepository +import com.tangem.feature.swap.domain.api.SwapRepository import com.tangem.feature.swap.domain.SwapTransactionRepository import com.tangem.feature.tokendetails.presentation.router.InnerTokenDetailsRouter import com.tangem.feature.tokendetails.presentation.tokendetails.state.SwapTransactionsState diff --git a/settings.gradle.kts b/settings.gradle.kts index 3ab3775f15..2d7d4f7efc 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -79,12 +79,16 @@ include(":libs:auth") // region Feature modules include(":features:onboarding") + include(":features:referral:data") include(":features:referral:domain") include(":features:referral:presentation") + include(":features:swap:api") include(":features:swap:data") include(":features:swap:domain") +include(":features:swap:domain:models") +include(":features:swap:domain:api") include(":features:swap:presentation") include(":features:tester:api")