Updated on 2026-08-14

This commit is contained in:
Tangem 2024-10-10 15:56:02 +04:00
commit 369ed25ac6
515 changed files with 6100 additions and 12756 deletions

View file

@ -20,14 +20,13 @@ import com.tangem.datasource.api.express.models.response.SwapPair
import com.tangem.datasource.api.express.models.response.SwapPairsWithProviders
import com.tangem.datasource.api.express.models.response.TxDetails
import com.tangem.datasource.crypto.DataSignatureVerifier
import com.tangem.domain.common.util.derivationStyleProvider
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.walletmanager.WalletManagersFacade
import com.tangem.domain.wallets.legacy.UserWalletsListManager
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.feature.swap.converters.*
import com.tangem.feature.swap.domain.api.SwapRepository
import com.tangem.feature.swap.domain.models.DataError
import com.tangem.feature.swap.domain.models.ExpressDataError
import com.tangem.feature.swap.domain.models.ExpressException
import com.tangem.feature.swap.domain.models.createFromAmountWithOffset
import com.tangem.feature.swap.domain.models.domain.*
@ -196,7 +195,7 @@ internal class DefaultSwapRepository(
toDecimals: Int,
providerId: String,
rateType: RateType,
): Either<DataError, QuoteModel> {
): Either<ExpressDataError, QuoteModel> {
return withContext(coroutineDispatcher.io) {
try {
val response = tangemExpressApi.getExchangeQuote(
@ -234,7 +233,7 @@ internal class DefaultSwapRepository(
toAddress: String,
refundAddress: String?, // for cex only
refundExtraId: String?, // for cex only
): Either<DataError, SwapDataModel> {
): Either<ExpressDataError, SwapDataModel> {
return withContext(coroutineDispatcher.io) {
try {
val requestId = UUID.randomUUID().toString()
@ -256,12 +255,12 @@ internal class DefaultSwapRepository(
).getOrThrow()
if (dataSignatureVerifier.verifySignature(response.signature, response.txDetailsJson)) {
val txDetails = parseTxDetails(response.txDetailsJson)
?: return@withContext DataError.UnknownError.left()
?: return@withContext ExpressDataError.UnknownError.left()
if (txDetails.requestId != requestId) {
return@withContext DataError.InvalidRequestIdError().left()
return@withContext ExpressDataError.InvalidRequestIdError().left()
}
if (!toAddress.equals(txDetails.payoutAddress, ignoreCase = true)) {
return@withContext DataError.InvalidPayoutAddressError().left()
return@withContext ExpressDataError.InvalidPayoutAddressError().left()
}
expressDataConverter.convert(
ExchangeDataResponseWithTxDetails(
@ -270,7 +269,7 @@ internal class DefaultSwapRepository(
),
).right()
} else {
DataError.InvalidSignatureError().left()
ExpressDataError.InvalidSignatureError().left()
}
} catch (ex: Exception) {
getDataError(ex).left()
@ -285,7 +284,7 @@ internal class DefaultSwapRepository(
payInAddress: String,
txHash: String,
payInExtraId: String?,
): Either<DataError, Unit> = withContext(coroutineDispatcher.io) {
): Either<ExpressDataError, Unit> = withContext(coroutineDispatcher.io) {
try {
tangemExpressApi.exchangeSent(
ExchangeSentRequestBody(
@ -390,21 +389,20 @@ internal class DefaultSwapRepository(
cryptoCurrencyFactory.createCoin(
blockchain = blockchain,
extraDerivationPath = null,
derivationStyleProvider = requireNotNull(
scanResponse = requireNotNull(
userWalletsListManager
.selectedUserWalletSync
?.scanResponse
?.derivationStyleProvider,
?.scanResponse,
),
),
)
}
private fun getDataError(ex: Exception): DataError {
private fun getDataError(ex: Exception): ExpressDataError {
return if (ex is ApiResponseError.HttpException) {
errorsDataConverter.convert(ex.errorBody ?: "")
} else {
DataError.UnknownError
ExpressDataError.UnknownError
}
}
}

View file

@ -3,76 +3,77 @@ package com.tangem.feature.swap.converters
import com.squareup.moshi.JsonAdapter
import com.tangem.datasource.api.express.models.response.ExpressError
import com.tangem.datasource.api.express.models.response.ExpressErrorResponse
import com.tangem.feature.swap.domain.models.DataError
import com.tangem.feature.swap.domain.models.ExpressDataError
import com.tangem.feature.swap.domain.models.createFromAmountWithOffset
import com.tangem.utils.converter.Converter
internal class ErrorsDataConverter(
private val jsonAdapter: JsonAdapter<ExpressErrorResponse>,
) : Converter<String, DataError> {
) : Converter<String, ExpressDataError> {
@Suppress("MagicNumber", "CyclomaticComplexMethod")
override fun convert(value: String): DataError {
override fun convert(value: String): ExpressDataError {
try {
val error = jsonAdapter.fromJson(value)?.error ?: return DataError.UnknownError
val error = jsonAdapter.fromJson(value)?.error ?: return ExpressDataError.UnknownError
return when (error.code) {
2010 -> DataError.BadRequest(code = error.code)
2200 -> DataError.SwapsAreUnavailableNowError(code = error.code)
2210 -> DataError.ExchangeProviderNotFoundError(code = error.code)
2220 -> DataError.ExchangeProviderNotActiveError(code = error.code)
2230 -> DataError.ExchangeProviderNotAvailableError(code = error.code)
2231 -> DataError.ExchangeProviderProviderInternalError(code = error.code)
2240 -> DataError.ExchangeNotPossibleError(code = error.code)
2010 -> ExpressDataError.BadRequest(code = error.code)
2200 -> ExpressDataError.SwapsAreUnavailableNowError(code = error.code)
2210 -> ExpressDataError.ExchangeProviderNotFoundError(code = error.code)
2220 -> ExpressDataError.ExchangeProviderNotActiveError(code = error.code)
2230 -> ExpressDataError.ExchangeProviderNotAvailableError(code = error.code)
2231 -> ExpressDataError.ExchangeProviderProviderInternalError(code = error.code)
2240 -> ExpressDataError.ExchangeNotPossibleError(code = error.code)
2250 -> tryParseExchangeTooSmallAmountError(error = error)
2251 -> tryParseExchangeTooBigAmountError(error = error)
2260 -> tryParseExchangeNotEnoughAllowanceError(error = error)
2270 -> DataError.ExchangeNotEnoughBalanceError(code = error.code)
2280 -> DataError.ExchangeInvalidAddressError(code = error.code)
2270 -> ExpressDataError.ExchangeNotEnoughBalanceError(code = error.code)
2280 -> ExpressDataError.ExchangeInvalidAddressError(code = error.code)
2290 -> tryParseExchangeInvalidFromDecimalsError(error = error)
else -> DataError.UnknownErrorWithCode(error.code)
else -> ExpressDataError.UnknownErrorWithCode(error.code)
}
} catch (e: Exception) {
return DataError.UnknownError
return ExpressDataError.UnknownError
}
}
private fun tryParseExchangeTooSmallAmountError(error: ExpressError): DataError {
val minAmount = error.value?.minAmount ?: return DataError.UnknownErrorWithCode(error.code)
val decimals = error.value?.decimals ?: return DataError.UnknownErrorWithCode(error.code)
private fun tryParseExchangeTooSmallAmountError(error: ExpressError): ExpressDataError {
val minAmount = error.value?.minAmount ?: return ExpressDataError.UnknownErrorWithCode(error.code)
val decimals = error.value?.decimals ?: return ExpressDataError.UnknownErrorWithCode(error.code)
return DataError.ExchangeTooSmallAmountError(
return ExpressDataError.ExchangeTooSmallAmountError(
code = error.code,
amount = createFromAmountWithOffset(minAmount, decimals),
)
}
private fun tryParseExchangeTooBigAmountError(error: ExpressError): DataError {
val minAmount = error.value?.maxAmount ?: return DataError.UnknownErrorWithCode(error.code)
val decimals = error.value?.decimals ?: return DataError.UnknownErrorWithCode(error.code)
private fun tryParseExchangeTooBigAmountError(error: ExpressError): ExpressDataError {
val minAmount = error.value?.maxAmount ?: return ExpressDataError.UnknownErrorWithCode(error.code)
val decimals = error.value?.decimals ?: return ExpressDataError.UnknownErrorWithCode(error.code)
return DataError.ExchangeTooBigAmountError(
return ExpressDataError.ExchangeTooBigAmountError(
code = error.code,
amount = createFromAmountWithOffset(minAmount, decimals),
)
}
private fun tryParseExchangeNotEnoughAllowanceError(error: ExpressError): DataError {
val currentAllowance = error.value?.currentAllowance ?: return DataError.UnknownErrorWithCode(error.code)
private fun tryParseExchangeNotEnoughAllowanceError(error: ExpressError): ExpressDataError {
val currentAllowance = error.value?.currentAllowance ?: return ExpressDataError.UnknownErrorWithCode(error.code)
return DataError.ExchangeNotEnoughAllowanceError(
return ExpressDataError.ExchangeNotEnoughAllowanceError(
code = error.code,
currentAllowance = currentAllowance,
)
}
private fun tryParseExchangeInvalidFromDecimalsError(error: ExpressError): DataError {
val receivedFromDecimals = error.value?.receivedFromDecimals ?: return DataError.UnknownErrorWithCode(
private fun tryParseExchangeInvalidFromDecimalsError(error: ExpressError): ExpressDataError {
val receivedFromDecimals = error.value?.receivedFromDecimals ?: return ExpressDataError.UnknownErrorWithCode(
code = error.code,
)
val expressFromDecimals = error.value?.expressFromDecimals ?: return DataError.UnknownErrorWithCode(error.code)
val expressFromDecimals =
error.value?.expressFromDecimals ?: return ExpressDataError.UnknownErrorWithCode(error.code)
return DataError.ExchangeInvalidFromDecimalsError(
return ExpressDataError.ExchangeInvalidFromDecimalsError(
code = error.code,
receivedFromDecimals = receivedFromDecimals,
expressFromDecimals = expressFromDecimals,