Skip to content

Commit

Permalink
Merge pull request #35 from horizontalsystems/release
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
omurovch committed Mar 1, 2019
2 parents f5b74cb + b1c5154 commit 751fcb2
Show file tree
Hide file tree
Showing 102 changed files with 7,683 additions and 569 deletions.
16 changes: 12 additions & 4 deletions app/build.gradle
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "io.horizontalsystems.ethereum.kit.android"
applicationId "io.horizontalsystems.ethereumkit"
minSdkVersion 23
targetSdkVersion 27
versionCode 1
Expand All @@ -22,18 +22,26 @@ android {
packagingOptions {
exclude 'META-INF/rxjava.properties'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:27.1.1'

implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'

// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation 'android.arch.lifecycle:extensions:1.1.1'
kapt "android.arch.lifecycle:compiler:1.1.1"

testImplementation 'junit:junit:4.12'
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="io.horizontalsystems.ethereumkit"
<manifest package="io.horizontalsystems.ethereumkit.sample"
xmlns:android="http://schemas.android.com/apk/res/android">


<uses-permission android:name="android.permission.INTERNET"/>

<application
android:name=".sample.App"
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -31,4 +31,4 @@

</application>

</manifest>
</manifest>
@@ -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
}

}
Expand Up @@ -10,15 +10,16 @@ import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import io.horizontalsystems.ethereumkit.EthereumKit
import io.horizontalsystems.ethereumkit.R

class BalanceFragment : Fragment() {

lateinit var viewModel: MainViewModel
lateinit var balanceValue: TextView
lateinit var tokenBalanceValue: TextView
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,18 +32,31 @@ class BalanceFragment : Fragment() {
balanceValue.text = (balance ?: 0).toString()
})

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 @@ -57,10 +71,12 @@ class BalanceFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)

balanceValue = view.findViewById(R.id.balanceValue)
tokenBalanceValue = view.findViewById(R.id.tokenBalanceValue)
refreshButton = view.findViewById(R.id.buttonRefresh)
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
Expand Up @@ -5,7 +5,6 @@ import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import io.horizontalsystems.ethereumkit.R

class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {

Expand Down
Expand Up @@ -2,62 +2,196 @@ 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(), EthereumKit.Listener {
class MainViewModel : ViewModel() {

val transactions = MutableLiveData<List<Transaction>>()
val balance = MutableLiveData<Double>()
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 fee = MutableLiveData<Double>()
val kitState = MutableLiveData<KitState>()
val etherState = MutableLiveData<SyncState>()
val erc20State = MutableLiveData<SyncState>()

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

private var ethereumKit: EthereumKit

init {
val words = listOf("subway", "plate", "brick", "pattern", "inform", "used", "oblige", "identify", "cherry", "drop", "flush", "balance")
ethereumKit = EthereumKit(words, NetworkType.Kovan)
// 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.ethereumKit(App.instance, words, "unique-wallet-id", testMode, infuraKey = infuraKey, etherscanKey = etherscanKey)
ethereumAdapter = EthereumAdapter(ethereumKit)
erc20Adapter = Erc20Adapter(ethereumKit, contractAddress, contractDecimal)

ethereumKit.start()

ethereumKit.listener = this

transactions.value = ethereumKit.transactions
balance.value = ethereumKit.balance
fee.value = ethereumKit.fee()
updateBalance()
updateErc20Balance()
updateState()
updateErc20State()
updateLastBlockHeight()

//
// Ethereum
//
ethereumAdapter.transactionSubject.subscribe {
updateTransactions()
}.let {
disposables.add(it)
}

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

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

ethereumAdapter.syncStateUpdateSubject.subscribe {
updateState()
}.let {
disposables.add(it)
}

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

//
// ERC20
//

erc20Adapter.balanceSubject.subscribe {
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())
}

fun receiveAddress(): String {
return ethereumKit.receiveAddress()
return ethereumKit.receiveAddress
}

fun send(address: String, amount: Double) {
ethereumKit.send(address, amount) { error ->
sendStatus.value = error
}
}
fun send(address: String, amount: BigDecimal) {
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) }

override fun transactionsUpdated(inserted: List<Transaction>, updated: List<Transaction>, deleted: List<Int>) {
transactions.postValue(ethereumKit.transactions)
}

override fun balanceUpdated(balance: Double) {
this.balance.postValue(balance)
//
// ERC20
//

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) }
}

override fun lastBlockHeightUpdated(height: Int) {
this.lastBlockHeight.postValue(height)
fun filterTransactions(ethTx: Boolean) {
val txMethod = if (ethTx) ethereumAdapter.transactionsSingle() else erc20Adapter.transactionsSingle()

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

override fun onKitStateUpdate(state: KitState) {
this.kitState.postValue(state)
//
// Private
//

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

}

0 comments on commit 751fcb2

Please sign in to comment.