Skip to content

Commit

Permalink
Add Ethereum support for PancakeSwap V3
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovch committed Jun 26, 2023
1 parent ce691c0 commit 85b6a85
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.horizontalsystems.ethereumkit.sample.modules.main.Erc20Token

object Configuration {
const val webSocket: Boolean = false
val chain: Chain = Chain.BinanceSmartChain
val chain: Chain = Chain.Ethereum
const val walletId = "walletId"
val watchAddress: String? = null
const val defaultsWords = "apart approve black comfort steel spin real renew tone primary key cherry"
Expand All @@ -25,7 +25,8 @@ object Configuration {
Erc20Token("ArcBlock", "ABT", Address("0xB98d4C97425d9908E66E53A6fDf673ACcA0BE986"), 18),
Erc20Token("DAI", "DAI", Address("0x6b175474e89094c44da98b954eedeac495271d0f"), 18),
Erc20Token("USDT", "USDT", Address("0xdAC17F958D2ee523a2206206994597C13D831ec7"), 6),
Erc20Token("USD Coin", "USDC", Address("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), 6)
Erc20Token("USD Coin", "USDC", Address("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), 6),
Erc20Token("PancakeSwap", "CAKE", Address("0x152649ea73beab28c5b49b26eb48f7ead6d4c898"), 18)
)
Chain.ArbitrumOne -> listOf(
Erc20Token("USDT", "USDT", Address("0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9"), 6),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class UniswapV3ViewModel(
private val signer: Signer
) : ViewModel() {

private var uniswapV3Kit = UniswapV3Kit.getInstance(ethereumKit, DexType.Uniswap)
private var uniswapV3Kit = UniswapV3Kit.getInstance(ethereumKit, DexType.PancakeSwap)
private var gasPrice: GasPrice = GasPrice.Legacy(20_000_000_000)

val fromToken: Erc20Token? = Configuration.erc20Tokens[1]
val toToken: Erc20Token? = Configuration.erc20Tokens[2]
val fromToken: Erc20Token? = Configuration.erc20Tokens[5]
val toToken: Erc20Token? = Configuration.erc20Tokens[3]

private val fromUniswapToken = uniswapToken(fromToken)
private val toUniswapToken = uniswapToken(toToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package io.horizontalsystems.uniswapkit.v3

import io.horizontalsystems.uniswapkit.models.DexType
import java.math.BigInteger

sealed class FeeAmount(val value: BigInteger) {
object LOWEST : FeeAmount(100.toBigInteger())
object LOW : FeeAmount(500.toBigInteger())
object MEDIUM : FeeAmount(3000.toBigInteger())
object MEDIUM_PANCAKESWAP : FeeAmount(2500.toBigInteger())
object MEDIUM_UNISWAP : FeeAmount(3000.toBigInteger())
object HIGH : FeeAmount(10000.toBigInteger())

companion object {
fun sorted() = listOf(LOWEST, LOW, MEDIUM, HIGH)
fun sorted(dexType: DexType) = listOf(
LOWEST,
LOW,
when (dexType) {
DexType.Uniswap -> MEDIUM_UNISWAP
DexType.PancakeSwap -> MEDIUM_PANCAKESWAP
},
HIGH
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class PoolManager(
}

private fun getPancakeSwapFactoryAddress(chain: Chain)= when (chain) {
Chain.BinanceSmartChain -> "0x0BFbCF9fa4f9C56B0F40a671Ad40E0805A091865"
Chain.BinanceSmartChain,
Chain.Ethereum -> "0x0BFbCF9fa4f9C56B0F40a671Ad40E0805A091865"
else -> throw IllegalStateException("Not supported PancakeSwap chain ${ethereumKit.chain}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class QuoterV2(
dexType: DexType
) {

private val feeAmounts = FeeAmount.sorted(dexType)

private val quoterAddress = when (dexType) {
DexType.Uniswap -> getUniswapQuoterAddress(ethereumKit.chain)
DexType.PancakeSwap -> getPancakeSwapQuoterAddress(ethereumKit.chain)
Expand All @@ -38,7 +40,8 @@ class QuoterV2(
}

private fun getPancakeSwapQuoterAddress(chain: Chain) = when (chain) {
Chain.BinanceSmartChain -> "0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997"
Chain.BinanceSmartChain,
Chain.Ethereum -> "0xB048Bbc1Ee6b733FFfCFb9e9CeF7375518e25997"
else -> throw IllegalStateException("Not supported PancakeSwap chain ${ethereumKit.chain}")
}

Expand All @@ -63,7 +66,7 @@ class QuoterV2(
): BestTrade? {
val sqrtPriceLimitX96 = BigInteger.ZERO

val amounts = FeeAmount.sorted().mapNotNull { fee ->
val amounts = feeAmounts.mapNotNull { fee ->
coroutineContext.ensureActive()
try {
val callResponse = ethCall(
Expand Down Expand Up @@ -159,7 +162,7 @@ class QuoterV2(
): BestTrade? {
val sqrtPriceLimitX96 = BigInteger.ZERO

val amounts = FeeAmount.sorted().mapNotNull { fee ->
val amounts = feeAmounts.mapNotNull { fee ->
coroutineContext.ensureActive()
try {
val callResponse = ethCall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class SwapRouter(private val ethereumKit: EthereumKit, dexType: DexType) {
}

private fun getPancakeSwapRouterAddress(chain: Chain)= when (chain) {
Chain.BinanceSmartChain -> Address("0x13f4EA83D0bd40E75C8222255bc855a974568Dd4")
Chain.BinanceSmartChain,
Chain.Ethereum -> Address("0x13f4EA83D0bd40E75C8222255bc855a974568Dd4")
else -> throw IllegalStateException("Not supported PancakeSwap chain ${ethereumKit.chain}")
}

Expand Down

0 comments on commit 85b6a85

Please sign in to comment.