Skip to content

Commit

Permalink
Refactor OneInchKit to make usable without EthereumKit
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovch committed Jan 15, 2024
1 parent d6676bf commit 7adf26e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,14 @@ import java.math.BigInteger
import java.util.*

class OneInchKit(
private val evmKit: EthereumKit,
private val service: OneInchService
) {

val routerAddress: Address = when (evmKit.chain) {
Chain.Ethereum,
Chain.BinanceSmartChain,
Chain.Polygon,
Chain.Optimism,
Chain.ArbitrumOne,
Chain.Gnosis,
Chain.Fantom,
Chain.Avalanche -> Address("0x1111111254eeb25477b68fb85ed929f73a960582")
else -> throw IllegalArgumentException("Invalid Chain: ${evmKit.chain.id}")
}

fun getApproveCallDataAsync(tokenAddress: Address, amount: BigInteger) =
service.getApproveCallDataAsync(tokenAddress, amount)
fun getApproveCallDataAsync(chain: Chain, tokenAddress: Address, amount: BigInteger) =
service.getApproveCallDataAsync(chain, tokenAddress, amount)

fun getQuoteAsync(
chain: Chain,
fromToken: Address,
toToken: Address,
amount: BigInteger,
Expand All @@ -44,6 +32,7 @@ class OneInchKit(
mainRouteParts: Int? = null,
parts: Int? = null
) = service.getQuoteAsync(
chain,
fromToken,
toToken,
amount,
Expand All @@ -57,6 +46,8 @@ class OneInchKit(
)

fun getSwapAsync(
chain: Chain,
receiveAddress: Address,
fromToken: Address,
toToken: Address,
amount: BigInteger,
Expand All @@ -72,10 +63,11 @@ class OneInchKit(
parts: Int? = null,
mainRouteParts: Int? = null
) = service.getSwapAsync(
chain,
fromToken,
toToken,
amount,
evmKit.receiveAddress,
receiveAddress,
slippagePercentage,
protocols,
recipient,
Expand All @@ -90,15 +82,27 @@ class OneInchKit(
)

companion object {
fun getInstance(evmKit: EthereumKit, apiKey: String): OneInchKit {
val service = OneInchService(evmKit.chain, apiKey)
return OneInchKit(evmKit, service)
fun getInstance(apiKey: String): OneInchKit {
return OneInchKit(OneInchService(apiKey))
}

fun addDecorators(evmKit: EthereumKit) {
evmKit.addMethodDecorator(OneInchMethodDecorator(OneInchContractMethodFactories))
evmKit.addTransactionDecorator(OneInchTransactionDecorator(evmKit.receiveAddress))
}

fun routerAddress(chain: Chain): Address = when (chain) {
Chain.Ethereum,
Chain.BinanceSmartChain,
Chain.Polygon,
Chain.Optimism,
Chain.ArbitrumOne,
Chain.Gnosis,
Chain.Fantom,
Chain.Avalanche -> Address("0x1111111254eeb25477b68fb85ed929f73a960582")

else -> throw IllegalArgumentException("Invalid Chain: ${chain.id}")
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
import java.math.BigInteger
import java.util.logging.Logger

class OneInchService(
chain: Chain,
apiKey: String
) {
private val logger = Logger.getLogger("OneInchService")
private val url = "https://api.1inch.dev/swap/v5.2/${chain.id}/"
private val url = "https://api.1inch.dev/swap/v5.2/"
private val service: OneInchServiceApi

init {
Expand Down Expand Up @@ -60,10 +60,11 @@ class OneInchService(
service = retrofit.create(OneInchServiceApi::class.java)
}

fun getApproveCallDataAsync(tokenAddress: Address, amount: BigInteger) =
service.getApproveCallData(tokenAddress.hex, amount)
fun getApproveCallDataAsync(chain: Chain, tokenAddress: Address, amount: BigInteger) =
service.getApproveCallData(tokenAddress = tokenAddress.hex, amount = amount, chainId = chain.id)

fun getQuoteAsync(
chain: Chain,
fromToken: Address,
toToken: Address,
amount: BigInteger,
Expand All @@ -86,7 +87,8 @@ class OneInchService(
connectorTokens = connectorTokens?.joinToString(","),
gasLimit = gasLimit,
parts = parts,
mainRouteParts = mainRouteParts
mainRouteParts = mainRouteParts,
chainId = chain.id
)
} else {
service.getQuote(
Expand All @@ -99,11 +101,13 @@ class OneInchService(
connectorTokens = connectorTokens?.joinToString(","),
gasLimit = gasLimit,
parts = parts,
mainRouteParts = mainRouteParts
mainRouteParts = mainRouteParts,
chainId = chain.id
)
}

fun getSwapAsync(
chain: Chain,
fromTokenAddress: Address,
toTokenAddress: Address,
amount: BigInteger,
Expand Down Expand Up @@ -136,7 +140,8 @@ class OneInchService(
allowPartialFill = allowPartialFill,
gasLimit = gasLimit,
parts = parts,
mainRouteParts = mainRouteParts
mainRouteParts = mainRouteParts,
chainId = chain.id
)
} else {
service.getSwap(
Expand All @@ -154,19 +159,21 @@ class OneInchService(
allowPartialFill = allowPartialFill,
gasLimit = gasLimit,
parts = parts,
mainRouteParts = mainRouteParts
mainRouteParts = mainRouteParts,
chainId = chain.id
)
}

private interface OneInchServiceApi {
@GET("approve/calldata")
@GET("{chain_id}/approve/calldata")
fun getApproveCallData(
@Query("tokenAddress") tokenAddress: String,
@Query("amount") amount: BigInteger? = null,
@Query("infinity") infinity: Boolean? = null
@Query("infinity") infinity: Boolean? = null,
@Path("chain_id") chainId: Int
): Single<ApproveCallData>

@GET("quote")
@GET("{chain_id}/quote")
fun getQuote(
@Query("src") fromTokenAddress: String,
@Query("dst") toTokenAddress: String,
Expand All @@ -183,9 +190,10 @@ class OneInchService(
@Query("includeTokensInfo") includeTokensInfo: Boolean = true,
@Query("includeProtocols") includeProtocols: Boolean = true,
@Query("includeGas") includeGas: Boolean = true,
@Path("chain_id") chainId: Int
): Single<Quote>

@GET("swap")
@GET("{chain_id}/swap")
fun getSwap(
@Query("src") fromTokenAddress: String,
@Query("dst") toTokenAddress: String,
Expand All @@ -208,6 +216,7 @@ class OneInchService(
@Query("includeTokensInfo") includeTokensInfo: Boolean = true,
@Query("includeProtocols") includeProtocols: Boolean = true,
@Query("includeGas") includeGas: Boolean = true,
@Path("chain_id") chainId: Int
): Single<Swap>
}

Expand Down

0 comments on commit 7adf26e

Please sign in to comment.