Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate 1inch API to V5.2 #297

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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<Any>,
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}"
}
}

Expand All @@ -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<Any>,
@SerializedName("tx") val transaction: SwapTransaction
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,18 +18,27 @@ 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 {
val loggingInterceptor = HttpLoggingInterceptor { message ->
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()
Expand Down Expand Up @@ -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,
Expand All @@ -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<Quote>

@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,
Expand All @@ -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<Swap>
}

Expand Down
Loading