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

Refactor UniswapKit, OneInchKit to make usable without EthereumKit #300

Merged
merged 3 commits into from
Jan 15, 2024
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 @@ -9,7 +9,11 @@ import io.horizontalsystems.ethereumkit.core.EthereumKit.SyncState
import io.horizontalsystems.ethereumkit.core.eip1559.Eip1559GasPriceProvider
import io.horizontalsystems.ethereumkit.core.signer.Signer
import io.horizontalsystems.ethereumkit.core.toHexString
import io.horizontalsystems.ethereumkit.models.*
import io.horizontalsystems.ethereumkit.models.Address
import io.horizontalsystems.ethereumkit.models.Chain
import io.horizontalsystems.ethereumkit.models.GasPrice
import io.horizontalsystems.ethereumkit.models.RpcSource
import io.horizontalsystems.ethereumkit.models.TransactionSource
import io.horizontalsystems.ethereumkit.sample.App
import io.horizontalsystems.ethereumkit.sample.Configuration
import io.horizontalsystems.ethereumkit.sample.SingleLiveEvent
Expand Down Expand Up @@ -38,6 +42,8 @@ class MainViewModel : ViewModel() {
lateinit var ethereumKit: EthereumKit
lateinit var ethereumAdapter: EthereumAdapter
lateinit var signer: Signer
lateinit var rpcSource: RpcSource
private lateinit var transactionSource: TransactionSource

lateinit var erc20Adapter: Erc20Adapter

Expand Down Expand Up @@ -72,15 +78,20 @@ class MainViewModel : ViewModel() {
val toToken: Erc20Token = Configuration.erc20Tokens[1]
lateinit var gasPriceHelper: GasPriceHelper

private val chain: Chain
get() = ethereumKit.chain

fun init() {
val words = Configuration.defaultsWords.split(" ")
val seed = Mnemonic().toSeed(words)
signer = Signer.getInstance(seed, Configuration.chain)
ethereumKit = createKit()
ethereumAdapter = EthereumAdapter(ethereumKit, signer)
erc20Adapter = Erc20Adapter(App.instance, fromToken ?: toToken
?: Configuration.erc20Tokens.first(), ethereumKit, signer)
uniswapKit = UniswapKit.getInstance(ethereumKit)
erc20Adapter = Erc20Adapter(
App.instance, fromToken ?: toToken
?: Configuration.erc20Tokens.first(), ethereumKit, signer
)
uniswapKit = UniswapKit.getInstance()

Erc20Kit.addTransactionSyncer(ethereumKit)
Erc20Kit.addDecorators(ethereumKit)
Expand Down Expand Up @@ -179,46 +190,38 @@ class MainViewModel : ViewModel() {
}

private fun createKit(): EthereumKit {
val rpcSource: RpcSource?
val transactionSource: TransactionSource?

when (Configuration.chain) {
Chain.BinanceSmartChain -> {
transactionSource = TransactionSource.bscscan(Configuration.bscScanKey)
rpcSource = RpcSource.binanceSmartChainHttp()
}

Chain.Ethereum -> {
transactionSource = TransactionSource.ethereumEtherscan(Configuration.etherscanKey)
rpcSource = if (Configuration.webSocket)
RpcSource.ethereumInfuraWebSocket(Configuration.infuraProjectId, Configuration.infuraSecret)
else
RpcSource.ethereumInfuraHttp(Configuration.infuraProjectId, Configuration.infuraSecret)
}

Chain.ArbitrumOne -> {
transactionSource = TransactionSource.arbiscan(Configuration.arbiscanApiKey)
rpcSource = RpcSource.arbitrumOneRpcHttp()
}

Chain.EthereumGoerli -> {
transactionSource = TransactionSource.goerliEtherscan(Configuration.etherscanKey)
rpcSource = if (Configuration.webSocket)
RpcSource.goerliInfuraWebSocket(Configuration.infuraProjectId, Configuration.infuraSecret)
else
RpcSource.goerliInfuraHttp(Configuration.infuraProjectId, Configuration.infuraSecret)
}

else -> {
rpcSource = null
transactionSource = null
throw Exception("Could not get rpcSource & transactionSource!")
}
}

checkNotNull(rpcSource) {
throw Exception("Could not get rpcSource!")
}

checkNotNull(transactionSource) {
throw Exception("Could not get transactionSource!")
}

return if (Configuration.watchAddress != null) {
EthereumKit.getInstance(
App.instance, Address(Configuration.watchAddress),
Expand Down Expand Up @@ -402,7 +405,7 @@ class MainViewModel : ViewModel() {
val tokenIn = uniswapToken(fromToken)
val tokenOut = uniswapToken(toToken)

uniswapKit.swapData(tokenIn, tokenOut)
uniswapKit.swapData(rpcSource, chain, tokenIn, tokenOut)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
Expand All @@ -415,7 +418,7 @@ class MainViewModel : ViewModel() {
}

fun syncAllowance() {
erc20Adapter.allowance(uniswapKit.routerAddress)
erc20Adapter.allowance(uniswapKit.routerAddress(chain))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
Expand All @@ -428,7 +431,7 @@ class MainViewModel : ViewModel() {
}

fun approve(decimalAmount: BigDecimal) {
val spenderAddress = uniswapKit.routerAddress
val spenderAddress = uniswapKit.routerAddress(chain)

val token = fromToken ?: return
val amount = decimalAmount.movePointRight(token.decimals).toBigInteger()
Expand Down Expand Up @@ -457,7 +460,7 @@ class MainViewModel : ViewModel() {

private fun uniswapToken(token: Erc20Token?): Token {
if (token == null)
return uniswapKit.etherToken()
return uniswapKit.etherToken(chain)

return uniswapKit.token(token.contractAddress, token.decimals)
}
Expand Down Expand Up @@ -489,12 +492,12 @@ class MainViewModel : ViewModel() {
fun swap() {
tradeData.value?.let { tradeData ->

val transactionData = uniswapKit.transactionData(tradeData)
val transactionData = uniswapKit.transactionData(ethereumKit.receiveAddress, chain, tradeData)
ethereumKit.estimateGas(transactionData, gasPrice)
.flatMap { gasLimit ->
logger.info("gas limit: $gasLimit")

val transactionData = uniswapKit.transactionData(tradeData)
val transactionData = uniswapKit.transactionData(ethereumKit.receiveAddress, chain, tradeData)
ethereumKit.rawTransaction(transactionData, gasPrice, gasLimit)
}
.flatMap { rawTransaction ->
Expand Down Expand Up @@ -524,4 +527,5 @@ data class Erc20Token(
val name: String,
val code: String,
val contractAddress: Address,
val decimals: Int)
val decimals: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import io.horizontalsystems.ethereumkit.core.EthereumKit
import io.horizontalsystems.ethereumkit.core.signer.Signer
import io.horizontalsystems.ethereumkit.models.RpcSource
import io.horizontalsystems.ethereumkit.sample.core.Erc20Adapter
import io.horizontalsystems.ethereumkit.sample.core.EthereumAdapter
import io.horizontalsystems.ethereumkit.sample.modules.main.GasPriceHelper
Expand All @@ -49,6 +50,7 @@ class UniswapV3Fragment : Fragment() {
mainViewModel.ethereumAdapter,
mainViewModel.gasPriceHelper,
mainViewModel.signer,
mainViewModel.rpcSource
)
}
}
Expand All @@ -61,14 +63,16 @@ fun UniswapV3Screen(
erc20Adapter: Erc20Adapter,
ethereumAdapter: EthereumAdapter,
gasPriceHelper: GasPriceHelper,
signer: Signer
signer: Signer,
rpcSource: RpcSource
) {
val factory = UniswapV3ViewModel.Factory(
ethereumKit,
erc20Adapter,
ethereumAdapter,
gasPriceHelper,
signer
signer,
rpcSource
)
val viewModel = viewModel<UniswapV3ViewModel>(factory = factory)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.lifecycle.viewModelScope
import io.horizontalsystems.ethereumkit.core.EthereumKit
import io.horizontalsystems.ethereumkit.core.signer.Signer
import io.horizontalsystems.ethereumkit.models.GasPrice
import io.horizontalsystems.ethereumkit.models.RpcSource
import io.horizontalsystems.ethereumkit.sample.Configuration
import io.horizontalsystems.ethereumkit.sample.core.Erc20Adapter
import io.horizontalsystems.ethereumkit.sample.core.EthereumAdapter
Expand All @@ -31,10 +32,12 @@ class UniswapV3ViewModel(
private val erc20Adapter: Erc20Adapter,
private val ethereumAdapter: EthereumAdapter,
private val gasPriceHelper: GasPriceHelper,
private val signer: Signer
private val signer: Signer,
private val rpcSource: RpcSource
) : ViewModel() {

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

val fromToken: Erc20Token? = Configuration.erc20Tokens[5]
Expand Down Expand Up @@ -83,7 +86,7 @@ class UniswapV3ViewModel(
allowance = BigDecimal(Int.MAX_VALUE)
} else {
allowance = try {
erc20Adapter.allowance(uniswapV3Kit.routerAddress).await().stripTrailingZeros()
erc20Adapter.allowance(uniswapV3Kit.routerAddress(chain)).await().stripTrailingZeros()
} catch (it: Throwable) {
Log.e("AAA", "allowance error", it)
BigDecimal.ZERO
Expand All @@ -93,7 +96,7 @@ class UniswapV3ViewModel(

fun approve() {
val tmpAmountIn = amountIn ?: return
val spenderAddress = uniswapV3Kit.routerAddress
val spenderAddress = uniswapV3Kit.routerAddress(chain)

val token = fromUniswapToken
val amount = tmpAmountIn.movePointRight(token.decimals).toBigInteger()
Expand Down Expand Up @@ -138,6 +141,8 @@ class UniswapV3ViewModel(
job = viewModelScope.launch(Dispatchers.IO) {
try {
val bestTradeExactIn = uniswapV3Kit.bestTradeExactIn(
rpcSource = rpcSource,
chain = chain,
tokenIn = fromUniswapToken,
tokenOut = toUniswapToken,
amountIn = amountIn,
Expand All @@ -156,7 +161,7 @@ class UniswapV3ViewModel(
}

private fun uniswapToken(token: Erc20Token?) = when (token) {
null -> uniswapV3Kit.etherToken()
null -> uniswapV3Kit.etherToken(chain)
else -> uniswapV3Kit.token(token.contractAddress, token.decimals)
}

Expand All @@ -177,6 +182,8 @@ class UniswapV3ViewModel(
job = viewModelScope.launch(Dispatchers.IO) {
try {
val bestTradeExactOut = uniswapV3Kit.bestTradeExactOut(
rpcSource = rpcSource,
chain = chain,
tokenIn = fromUniswapToken,
tokenOut = toUniswapToken,
amountOut = amountOut,
Expand Down Expand Up @@ -222,7 +229,7 @@ class UniswapV3ViewModel(
val tradeData = tradeData ?: return

viewModelScope.launch(Dispatchers.IO) {
val transactionData = uniswapV3Kit.transactionData(tradeData)
val transactionData = uniswapV3Kit.transactionData(ethereumKit.receiveAddress, chain, tradeData)

loading = true
emitState()
Expand All @@ -249,11 +256,12 @@ class UniswapV3ViewModel(
private val erc20Adapter: Erc20Adapter,
private val ethereumAdapter: EthereumAdapter,
private val gasPriceHelper: GasPriceHelper,
private val signer: Signer
private val signer: Signer,
private val rpcSource: RpcSource
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return UniswapV3ViewModel(ethereumKit, erc20Adapter, ethereumAdapter, gasPriceHelper, signer) as T
return UniswapV3ViewModel(ethereumKit, erc20Adapter, ethereumAdapter, gasPriceHelper, signer, rpcSource) as T
}
}
}
Expand Down
Loading
Loading