Updated on 2026-08-14

This commit is contained in:
Tangem 2023-11-25 02:09:28 +02:00
parent fd0a07af67
commit 1fdc1d07b2
7 changed files with 63 additions and 7 deletions

View file

@ -32,6 +32,7 @@ interface TangemExpressApi {
@Query("toContractAddress") toContractAddress: String,
@Query("toNetwork") toNetwork: String,
@Query("fromAmount") fromAmount: String,
@Query("fromDecimals") fromDecimals: Int,
@Query("providerId") providerId: String,
@Query("rateType") rateType: String,
): ApiResponse<ExchangeQuoteResponse>
@ -43,6 +44,7 @@ interface TangemExpressApi {
@Query("toContractAddress") toContractAddress: String,
@Query("toNetwork") toNetwork: String,
@Query("fromAmount") fromAmount: String,
@Query("fromDecimals") fromDecimals: Int,
@Query("providerId") providerId: String,
@Query("rateType") rateType: String,
@Query("toAddress") toAddress: String,

View file

@ -145,6 +145,7 @@ internal class SwapRepositoryImpl @Inject constructor(
toContractAddress: String,
toNetwork: String,
fromAmount: String,
fromDecimals: Int,
providerId: String,
rateType: RateType,
): AggregatedSwapDataModel<QuoteModel> {
@ -156,6 +157,7 @@ internal class SwapRepositoryImpl @Inject constructor(
toContractAddress = toContractAddress,
toNetwork = toNetwork,
fromAmount = fromAmount,
fromDecimals = fromDecimals,
providerId = providerId,
rateType = rateType.name.lowercase(),
).getOrThrow()
@ -182,6 +184,7 @@ internal class SwapRepositoryImpl @Inject constructor(
toContractAddress: String,
toNetwork: String,
fromAmount: String,
fromDecimals: Int,
providerId: String,
rateType: RateType,
toAddress: String,
@ -195,6 +198,7 @@ internal class SwapRepositoryImpl @Inject constructor(
toContractAddress = toContractAddress,
toNetwork = toNetwork,
fromAmount = fromAmount,
fromDecimals = fromDecimals,
providerId = providerId,
rateType = rateType.name.lowercase(),
toAddress = toAddress,

View file

@ -69,6 +69,7 @@ interface SwapInteractor {
@Suppress("LongParameterList")
@Throws(IllegalStateException::class)
suspend fun onSwap(
exchangeProviderType: ExchangeProviderType,
networkId: String,
swapStateData: SwapStateData,
currencyToSend: CryptoCurrency,

View file

@ -305,6 +305,7 @@ internal class SwapInteractorImpl @Inject constructor(
)
} else {
provider to loadQuoteData(
exchangeProviderType = ExchangeProviderType.DEX,
networkId = networkId,
amount = amount,
fromTokenStatus = fromToken,
@ -326,6 +327,7 @@ internal class SwapInteractorImpl @Inject constructor(
isBalanceWithoutFeeEnough: Boolean,
): Pair<SwapProvider, SwapState> {
return provider to loadQuoteData(
exchangeProviderType = ExchangeProviderType.CEX,
networkId = networkId,
amount = amount,
fromTokenStatus = fromToken,
@ -338,6 +340,32 @@ internal class SwapInteractorImpl @Inject constructor(
@Deprecated("used in old swap mechanism")
override suspend fun onSwap(
exchangeProviderType: ExchangeProviderType,
networkId: String,
swapStateData: SwapStateData,
currencyToSend: CryptoCurrency,
currencyToGet: CryptoCurrency,
amountToSwap: String,
fee: TxFee,
): TxState {
return when (exchangeProviderType) {
ExchangeProviderType.CEX -> {
onSwapCex()
}
ExchangeProviderType.DEX -> {
onSwapDex(
networkId = networkId,
swapStateData = swapStateData,
currencyToSend = currencyToSend,
currencyToGet = currencyToGet,
amountToSwap = amountToSwap,
fee = fee,
)
}
}
}
private suspend fun onSwapDex(
networkId: String,
swapStateData: SwapStateData,
currencyToSend: CryptoCurrency,
@ -391,6 +419,10 @@ internal class SwapInteractorImpl @Inject constructor(
}
}
private fun onSwapCex(): TxState {
TODO()
}
@Deprecated("used in old swap mechanism")
override fun getTokenBalance(networkId: String, token: CryptoCurrency): SwapAmount {
return cache.getBalanceForToken(
@ -486,6 +518,7 @@ internal class SwapInteractorImpl @Inject constructor(
*/
@Suppress("LongParameterList")
private suspend fun loadQuoteData(
exchangeProviderType: ExchangeProviderType,
networkId: String,
amount: SwapAmount,
fromTokenStatus: CryptoCurrencyStatus,
@ -503,12 +536,13 @@ internal class SwapInteractorImpl @Inject constructor(
toContractAddress = toToken.getContractAddress(),
toNetwork = toToken.network.backendId,
fromAmount = amount.toStringWithRightOffset(),
fromDecimals = amount.decimals,
providerId = provider.providerId,
rateType = RateType.FLOAT,
)
getState(
exchangeProviderType = exchangeProviderType,
quoteDataModel = quotes,
amount = amount,
fromToken = fromTokenStatus,
@ -521,6 +555,7 @@ internal class SwapInteractorImpl @Inject constructor(
}
private suspend fun getState(
exchangeProviderType: ExchangeProviderType,
quoteDataModel: AggregatedSwapDataModel<QuoteModel>,
amount: SwapAmount,
fromToken: CryptoCurrencyStatus,
@ -539,12 +574,21 @@ internal class SwapInteractorImpl @Inject constructor(
toTokenAmount = quoteModel.toTokenAmount,
swapStateData = null,
)
val quotesState = updatePermissionState(
networkId = networkId,
fromToken = fromToken.currency,
swapAmount = amount,
quotesLoadedState = swapState,
)
val quotesState = when (exchangeProviderType) {
ExchangeProviderType.DEX -> {
updatePermissionState(
networkId = networkId,
fromToken = fromToken.currency,
swapAmount = amount,
quotesLoadedState = swapState
)
}
ExchangeProviderType.CEX -> {
swapState.copy(permissionState = PermissionDataState.Empty)
}
}
return quotesState.copy(
preparedSwapConfigState = quotesState.preparedSwapConfigState.copy(
isAllowedToSpend = isAllowedToSpend,
@ -587,6 +631,7 @@ internal class SwapInteractorImpl @Inject constructor(
toContractAddress = toToken.currency.getContractAddress(),
toNetwork = toToken.currency.network.backendId,
fromAmount = amount.toStringWithRightOffset(),
fromDecimals = amount.decimals,
providerId = provider.providerId,
rateType = RateType.FLOAT,
toAddress = toToken.value.networkAddress?.defaultAddress ?: "",

View file

@ -21,6 +21,7 @@ interface SwapRepository {
toContractAddress: String,
toNetwork: String,
fromAmount: String,
fromDecimals: Int,
providerId: String,
rateType: RateType,
): AggregatedSwapDataModel<QuoteModel>
@ -63,6 +64,7 @@ interface SwapRepository {
toContractAddress: String,
toNetwork: String,
fromAmount: String,
fromDecimals: Int,
providerId: String,
rateType: RateType,
toAddress: String,

View file

@ -324,6 +324,7 @@ internal class SwapViewModel @Inject constructor(
viewModelScope.launch(dispatchers.main) {
runCatching(dispatchers.io) {
swapInteractor.onSwap(
exchangeProviderType = requireNotNull(dataState.selectedProvider?.type),
networkId = dataState.networkId,
swapStateData = requireNotNull(dataState.swapDataModel),
currencyToSend = requireNotNull(dataState.fromCryptoCurrency?.currency),

View file

@ -13,6 +13,7 @@ import java.math.BigDecimal
* @property amountToSend amount of tx
* @property currencyToSend currency for tx
*/
// TODO split to cex,dex
data class SwapTxData(
val networkId: String,
val feeAmount: BigDecimal,