Updated on 2026-08-14
This commit is contained in:
commit
07a67713b9
7 changed files with 63 additions and 12 deletions
|
|
@ -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,10 +44,10 @@ 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,
|
||||
@Query("refundAddress") refundAddress: String,
|
||||
): ApiResponse<ExchangeDataResponse>
|
||||
|
||||
@GET("exchange-result")
|
||||
|
|
|
|||
|
|
@ -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,10 +184,10 @@ internal class SwapRepositoryImpl @Inject constructor(
|
|||
toContractAddress: String,
|
||||
toNetwork: String,
|
||||
fromAmount: String,
|
||||
fromDecimals: Int,
|
||||
providerId: String,
|
||||
rateType: RateType,
|
||||
toAddress: String,
|
||||
refundAddress: String
|
||||
): AggregatedSwapDataModel<SwapDataModel> {
|
||||
return withContext(coroutineDispatcher.io) {
|
||||
try {
|
||||
|
|
@ -195,10 +197,10 @@ internal class SwapRepositoryImpl @Inject constructor(
|
|||
toContractAddress = toContractAddress,
|
||||
toNetwork = toNetwork,
|
||||
fromAmount = fromAmount,
|
||||
fromDecimals = fromDecimals,
|
||||
providerId = providerId,
|
||||
rateType = rateType.name.lowercase(),
|
||||
toAddress = toAddress,
|
||||
refundAddress = refundAddress
|
||||
).getOrThrow()
|
||||
AggregatedSwapDataModel(
|
||||
dataModel = expressDataConverter.convert(response)
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ interface SwapInteractor {
|
|||
@Suppress("LongParameterList")
|
||||
@Throws(IllegalStateException::class)
|
||||
suspend fun onSwap(
|
||||
exchangeProviderType: ExchangeProviderType,
|
||||
networkId: String,
|
||||
swapStateData: SwapStateData,
|
||||
currencyToSend: CryptoCurrency,
|
||||
|
|
|
|||
|
|
@ -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,10 +631,10 @@ 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 ?: "",
|
||||
refundAddress = fromToken.value.networkAddress?.defaultAddress ?: "",
|
||||
).let {
|
||||
val swapData = it.dataModel
|
||||
if (swapData != null) {
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ interface SwapRepository {
|
|||
toContractAddress: String,
|
||||
toNetwork: String,
|
||||
fromAmount: String,
|
||||
fromDecimals: Int,
|
||||
providerId: String,
|
||||
rateType: RateType,
|
||||
): AggregatedSwapDataModel<QuoteModel>
|
||||
|
|
@ -63,10 +64,10 @@ interface SwapRepository {
|
|||
toContractAddress: String,
|
||||
toNetwork: String,
|
||||
fromAmount: String,
|
||||
fromDecimals: Int,
|
||||
providerId: String,
|
||||
rateType: RateType,
|
||||
toAddress: String,
|
||||
refundAddress: String,
|
||||
): AggregatedSwapDataModel<SwapDataModel>
|
||||
|
||||
fun getNativeTokenForNetwork(networkId: String): CryptoCurrency
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue