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

Core refactoring #22

Merged
merged 12 commits into from
Feb 25, 2019
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package io.horizontalsystems.ethereumkit.sample

import android.app.Application
import io.horizontalsystems.ethereumkit.EthereumKit

class App : Application() {

override fun onCreate() {
super.onCreate()
instance = this
}

EthereumKit.init(this)
companion object {
lateinit var instance: App
private set
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BalanceFragment : Fragment() {
lateinit var feeValue: TextView
lateinit var lbhValue: TextView
lateinit var kitStateValue: TextView
lateinit var erc20StateValue: TextView
lateinit var refreshButton: Button

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -31,22 +32,31 @@ class BalanceFragment : Fragment() {
balanceValue.text = (balance ?: 0).toString()
})

viewModel.tokenBalance.observe(this, Observer { balance ->
viewModel.erc20TokenBalance.observe(this, Observer { balance ->
tokenBalanceValue.text = (balance ?: 0).toString()
})

viewModel.fee.observe(this, Observer { fee ->
feeValue.text = String.format("%f", fee)
feeValue.text = fee?.toPlainString()
})

viewModel.lastBlockHeight.observe(this, Observer { lbh ->
lbhValue.text = (lbh ?: 0).toString()
})
viewModel.kitState.observe(this, Observer { kitState ->
viewModel.etherState.observe(this, Observer { kitState ->
kitStateValue.text = when (kitState) {
is EthereumKit.KitState.Synced -> "Synced"
is EthereumKit.KitState.Syncing -> "Syncing"
is EthereumKit.KitState.NotSynced -> "NotSynced"
is EthereumKit.SyncState.Synced -> "Synced"
is EthereumKit.SyncState.Syncing -> "Syncing"
is EthereumKit.SyncState.NotSynced -> "NotSynced"
else -> "null"
}
})

viewModel.erc20State.observe(this, Observer { kitState ->
erc20StateValue.text = when (kitState) {
is EthereumKit.SyncState.Synced -> "Synced"
is EthereumKit.SyncState.Syncing -> "Syncing"
is EthereumKit.SyncState.NotSynced -> "NotSynced"
else -> "null"
}
})
Expand All @@ -66,6 +76,7 @@ class BalanceFragment : Fragment() {
feeValue = view.findViewById(R.id.feeValue)
lbhValue = view.findViewById(R.id.lbhValue)
kitStateValue = view.findViewById(R.id.kitStateValue)
erc20StateValue = view.findViewById(R.id.erc20StateValue)

refreshButton.setOnClickListener {
viewModel.refresh()
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,61 @@ package io.horizontalsystems.ethereumkit.sample

import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import android.util.Log
import android.widget.Toast
import io.horizontalsystems.ethereumkit.EthereumKit
import io.horizontalsystems.ethereumkit.EthereumKit.KitState
import io.horizontalsystems.ethereumkit.EthereumKit.NetworkType
import io.horizontalsystems.ethereumkit.models.Transaction
import io.horizontalsystems.ethereumkit.EthereumKit.SyncState
import io.horizontalsystems.ethereumkit.sample.core.Erc20Adapter
import io.horizontalsystems.ethereumkit.sample.core.EthereumAdapter
import io.horizontalsystems.ethereumkit.sample.core.TransactionRecord
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import java.math.BigDecimal

class MainViewModel : ViewModel() {

val transactions = MutableLiveData<List<Transaction>>()
private val infuraKey = "2a1306f1d12f4c109a4d4fb9be46b02e"
private val etherscanKey = "GKNHXT22ED7PRVCKZATFZQD1YI7FK9AAYE"
private val contractAddress = "0xF559862f9265756619d5523bBC4bd8422898e97d"
private val contractDecimal = 28
private val testMode = true

private val disposables = CompositeDisposable()

private var ethereumKit: EthereumKit
private val erc20Adapter: Erc20Adapter
private val ethereumAdapter: EthereumAdapter



val transactions = MutableLiveData<List<TransactionRecord>>()
val balance = MutableLiveData<BigDecimal>()
val fee = MutableLiveData<BigDecimal>()
val lastBlockHeight = MutableLiveData<Int>()
val kitState = MutableLiveData<KitState>()

val tokenTransactions = MutableLiveData<List<Transaction>>()
val tokenBalance = MutableLiveData<BigDecimal>()
val etherState = MutableLiveData<SyncState>()
val erc20State = MutableLiveData<SyncState>()

val erc20TokenBalance = MutableLiveData<BigDecimal>()
val sendStatus = SingleLiveEvent<Throwable?>()

private val disposables = CompositeDisposable()

private var ethereumKit: EthereumKit
private val contractAddress = "0xF559862f9265756619d5523bBC4bd8422898e97d"

private val erc20Adapter = ERC20Adapter(contractAddress, 28)
private val ethereumAdapter = EthereumAdapter()

init {
// val words = "subway plate brick pattern inform used oblige identify cherry drop flush balance".split(" ")
val words = "mom year father track attend frown loyal goddess crisp abandon juice roof".split(" ")

ethereumKit = EthereumKit(words, NetworkType.Ropsten, "unique-wallet-id")
ethereumKit.listener = ethereumAdapter
ethereumKit.register(erc20Adapter)
ethereumKit = EthereumKit.ethereumKit(App.instance, words, "unique-wallet-id", testMode, infuraKey = infuraKey, etherscanKey = etherscanKey)
ethereumAdapter = EthereumAdapter(ethereumKit)
erc20Adapter = Erc20Adapter(ethereumKit, contractAddress, contractDecimal)

ethereumKit.start()


// Previous or default values
balance.value = ethereumKit.balance
tokenBalance.value = ethereumKit.balanceERC20(contractAddress)
fee.value = ethereumKit.fee()
updateBalance()
updateErc20Balance()
updateState()
updateErc20State()
updateLastBlockHeight()

//
// Ethereum
Expand All @@ -53,41 +68,68 @@ class MainViewModel : ViewModel() {
}

ethereumAdapter.balanceSubject.subscribe {
this.balance.postValue(it)
updateBalance()
}.let {
disposables.add(it)
}

ethereumAdapter.lastBlockHeightSubject.subscribe {
this.lastBlockHeight.postValue(it)
updateLastBlockHeight()
}.let {
disposables.add(it)
}

ethereumAdapter.kitStateUpdateSubject.subscribe {
this.kitState.postValue(it)
ethereumAdapter.syncStateUpdateSubject.subscribe {
updateState()
}.let {
disposables.add(it)
}

erc20Adapter.syncStateUpdateSubject.subscribe {
updateErc20State()
}.let {
disposables.add(it)
}

//
// ERC20
//

erc20Adapter.balanceSubject.subscribe {
this.tokenBalance.postValue(it)
updateErc20Balance()
}.let {
disposables.add(it)
}

ethereumKit.start()
}

private fun updateLastBlockHeight() {
lastBlockHeight.postValue(ethereumKit.lastBlockHeight)
}

private fun updateState() {
etherState.postValue(ethereumAdapter.syncState)
}

private fun updateErc20State() {
erc20State.postValue(erc20Adapter.syncState)
}

private fun updateBalance() {
balance.postValue(ethereumAdapter.balance)
}

private fun updateErc20Balance() {
erc20TokenBalance.postValue(erc20Adapter.balance)
}

//
// Ethereum
//

fun refresh() {
ethereumKit.refresh()
ethereumKit.start()
fee.postValue(ethereumKit.fee())
}

Expand All @@ -96,43 +138,60 @@ class MainViewModel : ViewModel() {
}

fun send(address: String, amount: BigDecimal) {
ethereumKit.send(address, amount) { error ->
sendStatus.value = error
}
ethereumAdapter.sendSingle(address, amount)
.subscribeOn(io.reactivex.schedulers.Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
//success
Toast.makeText(App.instance, "Success", Toast.LENGTH_SHORT).show()
}, {
Log.e("MainViewModel", "send failed ${it.message}")
sendStatus.value = it
})?.let { disposables.add(it) }

}

//
// ERC20
//

fun sendERC20(address: String, amount: Double) {
ethereumKit.sendERC20(address, contractAddress, amount) { error ->
sendStatus.value = error
}
fun sendERC20(address: String, amount: BigDecimal) {
erc20Adapter.sendSingle(address, amount)
.subscribeOn(io.reactivex.schedulers.Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
//success
Toast.makeText(App.instance, "Success", Toast.LENGTH_SHORT).show()
}, {
Log.e("MainViewModel", "send failed ${it.message}")
sendStatus.value = it
})?.let { disposables.add(it) }
}

fun filterTransactions(ethTx: Boolean) {
val txMethod = if (ethTx)
ethereumKit.transactions() else
ethereumKit.transactionsERC20(contractAddress)

txMethod.subscribe { txList: List<Transaction> ->
transactions.value = txList
}.let {
disposables.add(it)
}
val txMethod = if (ethTx) ethereumAdapter.transactionsSingle() else erc20Adapter.transactionsSingle()

txMethod
.observeOn(AndroidSchedulers.mainThread())
.subscribe { txList: List<TransactionRecord> ->
transactions.value = txList
}.let {
disposables.add(it)
}
}

//
// Private
//

private fun updateTransactions() {
ethereumKit.transactions().subscribe { list: List<Transaction> ->
transactions.value = list
}.let {
disposables.add(it)
}
ethereumAdapter.transactionsSingle()
.observeOn(AndroidSchedulers.mainThread())
.subscribe { list: List<TransactionRecord> ->
transactions.value = list
}.let {
disposables.add(it)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SendReceiveFragment : Fragment() {
when {
sendAddress.text.isEmpty() -> sendAddress.error = "Send address cannot be blank"
sendAmount.text.isEmpty() -> sendAmount.error = "Send amount cannot be blank"
else -> viewModel.sendERC20(sendAddress.text.toString(), sendAmount.text.toString().toDouble())
else -> viewModel.sendERC20(sendAddress.text.toString(), sendAmount.text.toString().toBigDecimal())
}
}

Expand Down
Loading