From fe96dfa18317e54d24bbbcb215f7d15364f02cd5 Mon Sep 17 00:00:00 2001 From: chyngyz Date: Thu, 19 Oct 2023 13:27:45 +0600 Subject: [PATCH] Migrate 1inch API to V5.2 --- .../oneinchkit/OneInchKit.kt | 13 +++---- .../oneinchkit/OneInchService.kt | 37 ++++++++++++++----- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchKit.kt b/oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchKit.kt index d779c563..a2cdd384 100644 --- a/oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchKit.kt +++ b/oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchKit.kt @@ -90,8 +90,8 @@ class OneInchKit( ) companion object { - fun getInstance(evmKit: EthereumKit): OneInchKit { - val service = OneInchService(evmKit.chain) + fun getInstance(evmKit: EthereumKit, apiKey: String): OneInchKit { + val service = OneInchService(evmKit.chain, apiKey) return OneInchKit(evmKit, service) } @@ -114,13 +114,12 @@ data class Token( data class Quote( val fromToken: Token, val toToken: Token, - val fromTokenAmount: BigInteger, - val toTokenAmount: BigInteger, + @SerializedName("toAmount") val toTokenAmount: BigInteger, @SerializedName("protocols") val route: List, - val estimatedGas: Long + @SerializedName("gas") val estimatedGas: Long ) { override fun toString(): String { - return "Quote {fromToken: ${fromToken.name}, toToken: ${toToken.name}, fromTokenAmount: $fromTokenAmount, toTokenAmount: $toTokenAmount}" + return "Quote {fromToken: ${fromToken.name}, toToken: ${toToken.name}, toTokenAmount: $toTokenAmount}" } } @@ -143,7 +142,7 @@ data class Swap( val fromToken: Token, val toToken: Token, val fromTokenAmount: BigInteger, - val toTokenAmount: BigInteger, + @SerializedName("toAmount") val toTokenAmount: BigInteger, @SerializedName("protocols") val route: List, @SerializedName("tx") val transaction: SwapTransaction ) { diff --git a/oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchService.kt b/oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchService.kt index a7c353f4..5135a4cb 100644 --- a/oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchService.kt +++ b/oneinchkit/src/main/java/io/horizontalsystems/oneinchkit/OneInchService.kt @@ -6,6 +6,7 @@ import io.horizontalsystems.ethereumkit.models.Chain import io.horizontalsystems.ethereumkit.models.GasPrice import io.horizontalsystems.ethereumkit.network.* import io.reactivex.Single +import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit @@ -17,10 +18,11 @@ import java.math.BigInteger import java.util.logging.Logger class OneInchService( - chain: Chain + chain: Chain, + apiKey: String ) { private val logger = Logger.getLogger("OneInchService") - private val url = "https://api-unstoppable.1inch.io/v5.0/${chain.id}/" + private val url = "https://api-unstoppable.1inch.io/v5.2/${chain.id}/" private val service: OneInchServiceApi init { @@ -28,7 +30,15 @@ class OneInchService( logger.info(message) }.setLevel(HttpLoggingInterceptor.Level.BASIC) + val headersInterceptor = Interceptor { interceptorChain -> + val requestBuilder = interceptorChain.request().newBuilder() + requestBuilder.header("Accept", "application/json") + requestBuilder.header("Authorization", "Bearer $apiKey") + interceptorChain.proceed(requestBuilder.build()) + } + val httpClient = OkHttpClient.Builder() + .addInterceptor(headersInterceptor) .addInterceptor(loggingInterceptor) val gson = GsonBuilder() @@ -158,8 +168,8 @@ class OneInchService( @GET("quote") fun getQuote( - @Query("fromTokenAddress") fromTokenAddress: String, - @Query("toTokenAddress") toTokenAddress: String, + @Query("src") fromTokenAddress: String, + @Query("dst") toTokenAddress: String, @Query("amount") amount: BigInteger, @Query("protocols") protocols: String? = null, @Query("maxFeePerGas") maxFeePerGas: Long? = null, @@ -169,18 +179,22 @@ class OneInchService( @Query("connectorTokens") connectorTokens: String? = null, @Query("gasLimit") gasLimit: Long? = null, @Query("parts") parts: Int? = null, - @Query("mainRouteParts") mainRouteParts: Int? = null + @Query("mainRouteParts") mainRouteParts: Int? = null, + @Query("includeTokensInfo") includeTokensInfo: Boolean = true, + @Query("includeProtocols") includeProtocols: Boolean = true, + @Query("includeGas") includeGas: Boolean = true, ): Single @GET("swap") fun getSwap( - @Query("fromTokenAddress") fromTokenAddress: String, - @Query("toTokenAddress") toTokenAddress: String, + @Query("src") fromTokenAddress: String, + @Query("dst") toTokenAddress: String, @Query("amount") amount: BigInteger, - @Query("fromAddress") fromAddress: String, + @Query("from") fromAddress: String, @Query("slippage") slippagePercentage: Float, + @Query("referrer") referrer: String? = null, @Query("protocols") protocols: String? = null, - @Query("destReceiver") recipient: String? = null, + @Query("receiver") recipient: String? = null, @Query("maxFeePerGas") maxFeePerGas: Long? = null, @Query("maxPriorityFeePerGas") maxPriorityFeePerGas: Long? = null, @Query("gasPrice") gasPrice: Long? = null, @@ -190,7 +204,10 @@ class OneInchService( @Query("allowPartialFill") allowPartialFill: Boolean? = null, @Query("gasLimit") gasLimit: Long? = null, @Query("parts") parts: Int? = null, - @Query("mainRouteParts") mainRouteParts: Int? = null + @Query("mainRouteParts") mainRouteParts: Int? = null, + @Query("includeTokensInfo") includeTokensInfo: Boolean = true, + @Query("includeProtocols") includeProtocols: Boolean = true, + @Query("includeGas") includeGas: Boolean = true, ): Single }