Skip to content

Commit 751fcb2

Browse files
authored
Merge pull request #35 from horizontalsystems/release
Release 0.2.0
2 parents f5b74cb + b1c5154 commit 751fcb2

File tree

102 files changed

+7683
-569
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+7683
-569
lines changed

app/build.gradle

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'kotlin-android-extensions'
55
android {
66
compileSdkVersion 27
77
defaultConfig {
8-
applicationId "io.horizontalsystems.ethereum.kit.android"
8+
applicationId "io.horizontalsystems.ethereumkit"
99
minSdkVersion 23
1010
targetSdkVersion 27
1111
versionCode 1
@@ -22,18 +22,26 @@ android {
2222
packagingOptions {
2323
exclude 'META-INF/rxjava.properties'
2424
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
2529
}
2630

2731
dependencies {
28-
implementation fileTree(dir: 'libs', include: ['*.jar'])
32+
implementation fileTree(include: ['*.jar'], dir: 'libs')
33+
34+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
2935

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

40+
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
41+
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
42+
3543
// ViewModel and LiveData
36-
implementation "android.arch.lifecycle:extensions:1.1.1"
44+
implementation 'android.arch.lifecycle:extensions:1.1.1'
3745
kapt "android.arch.lifecycle:compiler:1.1.1"
3846

3947
testImplementation 'junit:junit:4.12'

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest package="io.horizontalsystems.ethereumkit"
2+
<manifest package="io.horizontalsystems.ethereumkit.sample"
33
xmlns:android="http://schemas.android.com/apk/res/android">
44

55

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

88
<application
9-
android:name=".sample.App"
9+
android:name=".App"
1010
android:allowBackup="true"
1111
android:icon="@mipmap/ic_launcher"
1212
android:label="@string/app_name"
@@ -31,4 +31,4 @@
3131

3232
</application>
3333

34-
</manifest>
34+
</manifest>
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package io.horizontalsystems.ethereumkit.sample
22

33
import android.app.Application
4-
import io.horizontalsystems.ethereumkit.EthereumKit
54

65
class App : Application() {
76

87
override fun onCreate() {
98
super.onCreate()
9+
instance = this
10+
}
1011

11-
EthereumKit.init(this)
12+
companion object {
13+
lateinit var instance: App
14+
private set
1215
}
1316

1417
}

app/src/main/java/io/horizontalsystems/ethereumkit/sample/BalanceFragment.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import android.view.ViewGroup
1010
import android.widget.Button
1111
import android.widget.TextView
1212
import io.horizontalsystems.ethereumkit.EthereumKit
13-
import io.horizontalsystems.ethereumkit.R
1413

1514
class BalanceFragment : Fragment() {
1615

1716
lateinit var viewModel: MainViewModel
1817
lateinit var balanceValue: TextView
18+
lateinit var tokenBalanceValue: TextView
1919
lateinit var feeValue: TextView
2020
lateinit var lbhValue: TextView
2121
lateinit var kitStateValue: TextView
22+
lateinit var erc20StateValue: TextView
2223
lateinit var refreshButton: Button
2324

2425
override fun onCreate(savedInstanceState: Bundle?) {
@@ -31,18 +32,31 @@ class BalanceFragment : Fragment() {
3132
balanceValue.text = (balance ?: 0).toString()
3233
})
3334

35+
viewModel.erc20TokenBalance.observe(this, Observer { balance ->
36+
tokenBalanceValue.text = (balance ?: 0).toString()
37+
})
38+
3439
viewModel.fee.observe(this, Observer { fee ->
35-
feeValue.text = String.format("%f", fee)
40+
feeValue.text = fee?.toPlainString()
3641
})
3742

3843
viewModel.lastBlockHeight.observe(this, Observer { lbh ->
3944
lbhValue.text = (lbh ?: 0).toString()
4045
})
41-
viewModel.kitState.observe(this, Observer { kitState ->
46+
viewModel.etherState.observe(this, Observer { kitState ->
4247
kitStateValue.text = when (kitState) {
43-
is EthereumKit.KitState.Synced -> "Synced"
44-
is EthereumKit.KitState.Syncing -> "Syncing"
45-
is EthereumKit.KitState.NotSynced -> "NotSynced"
48+
is EthereumKit.SyncState.Synced -> "Synced"
49+
is EthereumKit.SyncState.Syncing -> "Syncing"
50+
is EthereumKit.SyncState.NotSynced -> "NotSynced"
51+
else -> "null"
52+
}
53+
})
54+
55+
viewModel.erc20State.observe(this, Observer { kitState ->
56+
erc20StateValue.text = when (kitState) {
57+
is EthereumKit.SyncState.Synced -> "Synced"
58+
is EthereumKit.SyncState.Syncing -> "Syncing"
59+
is EthereumKit.SyncState.NotSynced -> "NotSynced"
4660
else -> "null"
4761
}
4862
})
@@ -57,10 +71,12 @@ class BalanceFragment : Fragment() {
5771
super.onViewCreated(view, savedInstanceState)
5872

5973
balanceValue = view.findViewById(R.id.balanceValue)
74+
tokenBalanceValue = view.findViewById(R.id.tokenBalanceValue)
6075
refreshButton = view.findViewById(R.id.buttonRefresh)
6176
feeValue = view.findViewById(R.id.feeValue)
6277
lbhValue = view.findViewById(R.id.lbhValue)
6378
kitStateValue = view.findViewById(R.id.kitStateValue)
79+
erc20StateValue = view.findViewById(R.id.erc20StateValue)
6480

6581
refreshButton.setOnClickListener {
6682
viewModel.refresh()

app/src/main/java/io/horizontalsystems/ethereumkit/sample/MainActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.support.design.widget.BottomNavigationView
55
import android.support.v4.app.Fragment
66
import android.support.v7.app.AppCompatActivity
77
import android.view.MenuItem
8-
import io.horizontalsystems.ethereumkit.R
98

109
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
1110

app/src/main/java/io/horizontalsystems/ethereumkit/sample/MainViewModel.kt

Lines changed: 163 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,196 @@ package io.horizontalsystems.ethereumkit.sample
22

33
import android.arch.lifecycle.MutableLiveData
44
import android.arch.lifecycle.ViewModel
5+
import android.util.Log
6+
import android.widget.Toast
57
import io.horizontalsystems.ethereumkit.EthereumKit
6-
import io.horizontalsystems.ethereumkit.EthereumKit.KitState
7-
import io.horizontalsystems.ethereumkit.EthereumKit.NetworkType
8-
import io.horizontalsystems.ethereumkit.models.Transaction
8+
import io.horizontalsystems.ethereumkit.EthereumKit.SyncState
9+
import io.horizontalsystems.ethereumkit.sample.core.Erc20Adapter
10+
import io.horizontalsystems.ethereumkit.sample.core.EthereumAdapter
11+
import io.horizontalsystems.ethereumkit.sample.core.TransactionRecord
12+
import io.reactivex.android.schedulers.AndroidSchedulers
13+
import io.reactivex.disposables.CompositeDisposable
14+
import java.math.BigDecimal
915

10-
class MainViewModel : ViewModel(), EthereumKit.Listener {
16+
class MainViewModel : ViewModel() {
1117

12-
val transactions = MutableLiveData<List<Transaction>>()
13-
val balance = MutableLiveData<Double>()
18+
private val infuraKey = "2a1306f1d12f4c109a4d4fb9be46b02e"
19+
private val etherscanKey = "GKNHXT22ED7PRVCKZATFZQD1YI7FK9AAYE"
20+
private val contractAddress = "0xF559862f9265756619d5523bBC4bd8422898e97d"
21+
private val contractDecimal = 28
22+
private val testMode = true
23+
24+
private val disposables = CompositeDisposable()
25+
26+
private var ethereumKit: EthereumKit
27+
private val erc20Adapter: Erc20Adapter
28+
private val ethereumAdapter: EthereumAdapter
29+
30+
31+
32+
val transactions = MutableLiveData<List<TransactionRecord>>()
33+
val balance = MutableLiveData<BigDecimal>()
34+
val fee = MutableLiveData<BigDecimal>()
1435
val lastBlockHeight = MutableLiveData<Int>()
15-
val fee = MutableLiveData<Double>()
16-
val kitState = MutableLiveData<KitState>()
36+
val etherState = MutableLiveData<SyncState>()
37+
val erc20State = MutableLiveData<SyncState>()
38+
39+
val erc20TokenBalance = MutableLiveData<BigDecimal>()
1740
val sendStatus = SingleLiveEvent<Throwable?>()
1841

19-
private var ethereumKit: EthereumKit
2042

2143
init {
22-
val words = listOf("subway", "plate", "brick", "pattern", "inform", "used", "oblige", "identify", "cherry", "drop", "flush", "balance")
23-
ethereumKit = EthereumKit(words, NetworkType.Kovan)
44+
// val words = "subway plate brick pattern inform used oblige identify cherry drop flush balance".split(" ")
45+
val words = "mom year father track attend frown loyal goddess crisp abandon juice roof".split(" ")
46+
47+
ethereumKit = EthereumKit.ethereumKit(App.instance, words, "unique-wallet-id", testMode, infuraKey = infuraKey, etherscanKey = etherscanKey)
48+
ethereumAdapter = EthereumAdapter(ethereumKit)
49+
erc20Adapter = Erc20Adapter(ethereumKit, contractAddress, contractDecimal)
50+
51+
ethereumKit.start()
2452

25-
ethereumKit.listener = this
2653

27-
transactions.value = ethereumKit.transactions
28-
balance.value = ethereumKit.balance
2954
fee.value = ethereumKit.fee()
55+
updateBalance()
56+
updateErc20Balance()
57+
updateState()
58+
updateErc20State()
59+
updateLastBlockHeight()
60+
61+
//
62+
// Ethereum
63+
//
64+
ethereumAdapter.transactionSubject.subscribe {
65+
updateTransactions()
66+
}.let {
67+
disposables.add(it)
68+
}
69+
70+
ethereumAdapter.balanceSubject.subscribe {
71+
updateBalance()
72+
}.let {
73+
disposables.add(it)
74+
}
75+
76+
ethereumAdapter.lastBlockHeightSubject.subscribe {
77+
updateLastBlockHeight()
78+
}.let {
79+
disposables.add(it)
80+
}
81+
82+
ethereumAdapter.syncStateUpdateSubject.subscribe {
83+
updateState()
84+
}.let {
85+
disposables.add(it)
86+
}
87+
88+
erc20Adapter.syncStateUpdateSubject.subscribe {
89+
updateErc20State()
90+
}.let {
91+
disposables.add(it)
92+
}
93+
94+
//
95+
// ERC20
96+
//
97+
98+
erc20Adapter.balanceSubject.subscribe {
99+
updateErc20Balance()
100+
}.let {
101+
disposables.add(it)
102+
}
103+
30104
ethereumKit.start()
31105
}
32106

107+
private fun updateLastBlockHeight() {
108+
lastBlockHeight.postValue(ethereumKit.lastBlockHeight)
109+
}
110+
111+
private fun updateState() {
112+
etherState.postValue(ethereumAdapter.syncState)
113+
}
114+
115+
private fun updateErc20State() {
116+
erc20State.postValue(erc20Adapter.syncState)
117+
}
118+
119+
private fun updateBalance() {
120+
balance.postValue(ethereumAdapter.balance)
121+
}
122+
123+
private fun updateErc20Balance() {
124+
erc20TokenBalance.postValue(erc20Adapter.balance)
125+
}
126+
127+
//
128+
// Ethereum
129+
//
130+
33131
fun refresh() {
34-
ethereumKit.refresh()
132+
ethereumKit.start()
35133
fee.postValue(ethereumKit.fee())
36134
}
37135

38136
fun receiveAddress(): String {
39-
return ethereumKit.receiveAddress()
137+
return ethereumKit.receiveAddress
40138
}
41139

42-
fun send(address: String, amount: Double) {
43-
ethereumKit.send(address, amount) { error ->
44-
sendStatus.value = error
45-
}
46-
}
140+
fun send(address: String, amount: BigDecimal) {
141+
ethereumAdapter.sendSingle(address, amount)
142+
.subscribeOn(io.reactivex.schedulers.Schedulers.io())
143+
.observeOn(AndroidSchedulers.mainThread())
144+
.subscribe({
145+
//success
146+
Toast.makeText(App.instance, "Success", Toast.LENGTH_SHORT).show()
147+
}, {
148+
Log.e("MainViewModel", "send failed ${it.message}")
149+
sendStatus.value = it
150+
})?.let { disposables.add(it) }
47151

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

52-
override fun balanceUpdated(balance: Double) {
53-
this.balance.postValue(balance)
154+
//
155+
// ERC20
156+
//
157+
158+
fun sendERC20(address: String, amount: BigDecimal) {
159+
erc20Adapter.sendSingle(address, amount)
160+
.subscribeOn(io.reactivex.schedulers.Schedulers.io())
161+
.observeOn(AndroidSchedulers.mainThread())
162+
.subscribe({
163+
//success
164+
Toast.makeText(App.instance, "Success", Toast.LENGTH_SHORT).show()
165+
}, {
166+
Log.e("MainViewModel", "send failed ${it.message}")
167+
sendStatus.value = it
168+
})?.let { disposables.add(it) }
54169
}
55170

56-
override fun lastBlockHeightUpdated(height: Int) {
57-
this.lastBlockHeight.postValue(height)
171+
fun filterTransactions(ethTx: Boolean) {
172+
val txMethod = if (ethTx) ethereumAdapter.transactionsSingle() else erc20Adapter.transactionsSingle()
173+
174+
txMethod
175+
.observeOn(AndroidSchedulers.mainThread())
176+
.subscribe { txList: List<TransactionRecord> ->
177+
transactions.value = txList
178+
}.let {
179+
disposables.add(it)
180+
}
58181
}
59182

60-
override fun onKitStateUpdate(state: KitState) {
61-
this.kitState.postValue(state)
183+
//
184+
// Private
185+
//
186+
187+
private fun updateTransactions() {
188+
ethereumAdapter.transactionsSingle()
189+
.observeOn(AndroidSchedulers.mainThread())
190+
.subscribe { list: List<TransactionRecord> ->
191+
transactions.value = list
192+
}.let {
193+
disposables.add(it)
194+
}
62195
}
196+
63197
}

0 commit comments

Comments
 (0)