@@ -3,12 +3,15 @@ package io.horizontalsystems.ethereumkit.sample
33import android.arch.lifecycle.MutableLiveData
44import android.arch.lifecycle.ViewModel
55import android.util.Log
6- import android.widget.Toast
7- import io.horizontalsystems.ethereumkit.EthereumKit
8- import io.horizontalsystems.ethereumkit.EthereumKit.SyncState
6+ import io.horizontalsystems.erc20kit.core.Erc20Kit
7+ import io.horizontalsystems.ethereumkit.core.EthereumKit
8+ import io.horizontalsystems.ethereumkit.core.EthereumKit.NetworkType
9+ import io.horizontalsystems.ethereumkit.core.EthereumKit.SyncState
910import io.horizontalsystems.ethereumkit.sample.core.Erc20Adapter
1011import io.horizontalsystems.ethereumkit.sample.core.EthereumAdapter
1112import io.horizontalsystems.ethereumkit.sample.core.TransactionRecord
13+ import io.horizontalsystems.hdwalletkit.HDWallet
14+ import io.horizontalsystems.hdwalletkit.Mnemonic
1215import io.reactivex.android.schedulers.AndroidSchedulers
1316import io.reactivex.disposables.CompositeDisposable
1417import java.math.BigDecimal
@@ -23,36 +26,43 @@ class MainViewModel : ViewModel() {
2326
2427 private val disposables = CompositeDisposable ()
2528
26- private var ethereumKit: EthereumKit
27- private val erc20Adapter: Erc20Adapter
28- private val ethereumAdapter: EthereumAdapter
29-
29+ private lateinit var ethereumKit: EthereumKit
30+ private lateinit var ethereumAdapter: EthereumAdapter
3031
32+ private lateinit var erc20Adapter: Erc20Adapter
3133
3234 val transactions = MutableLiveData <List <TransactionRecord >>()
3335 val balance = MutableLiveData <BigDecimal >()
3436 val fee = MutableLiveData <BigDecimal >()
35- val lastBlockHeight = MutableLiveData <Int >()
37+ val lastBlockHeight = MutableLiveData <Long >()
3638 val etherState = MutableLiveData <SyncState >()
3739 val erc20State = MutableLiveData <SyncState >()
3840
3941 val erc20TokenBalance = MutableLiveData <BigDecimal >()
4042 val sendStatus = SingleLiveEvent <Throwable ?>()
41- val gasPriceInWei = 3000000000L
4243
4344
45+ val gasPrice: Long = 5_000_000_000
46+
4447 init {
48+ init ()
49+ }
50+
51+ fun init () {
4552 // val words = "subway plate brick pattern inform used oblige identify cherry drop flush balance".split(" ")
4653 val words = " mom year father track attend frown loyal goddess crisp abandon juice roof" .split(" " )
4754
48- ethereumKit = EthereumKit .ethereumKit(App .instance, words, " unique-wallet-id" , testMode, infuraKey = infuraKey, etherscanKey = etherscanKey)
49- ethereumAdapter = EthereumAdapter (ethereumKit)
50- erc20Adapter = Erc20Adapter (ethereumKit, contractAddress, contractDecimal)
55+ val seed = Mnemonic ().toSeed(words)
56+ val hdWallet = HDWallet (seed, if (testMode) 1 else 60 )
57+ val privateKey = hdWallet.privateKey(0 , 0 , true ).privKey
58+ val nodePrivateKey = hdWallet.privateKey(102 , 102 , true ).privKey
5159
52- ethereumKit.start()
60+ ethereumKit = EthereumKit .getInstance(App .instance, privateKey, EthereumKit .SyncMode .ApiSyncMode (infuraKey, etherscanKey), NetworkType .Ropsten , " unique-wallet-id" )
61+ ethereumAdapter = EthereumAdapter (ethereumKit)
5362
63+ erc20Adapter = Erc20Adapter (App .instance, ethereumKit, " Max Token" , " MXT" , contractAddress, contractDecimal)
5464
55- fee.value = ethereumKit.fee(gasPriceInWei )
65+ fee.value = ethereumKit.fee(gasPrice )
5666 updateBalance()
5767 updateErc20Balance()
5868 updateState()
@@ -62,46 +72,54 @@ class MainViewModel : ViewModel() {
6272 //
6373 // Ethereum
6474 //
65- ethereumAdapter.transactionSubject.subscribe {
66- updateTransactions()
75+
76+ ethereumAdapter.lastBlockHeightFlowable.subscribe {
77+ updateLastBlockHeight()
6778 }.let {
6879 disposables.add(it)
6980 }
7081
71- ethereumAdapter.balanceSubject .subscribe {
72- updateBalance ()
82+ ethereumAdapter.transactionsFlowable .subscribe {
83+ updateTransactions ()
7384 }.let {
7485 disposables.add(it)
7586 }
7687
77- ethereumAdapter.lastBlockHeightSubject .subscribe {
78- updateLastBlockHeight ()
88+ ethereumAdapter.balanceFlowable .subscribe {
89+ updateBalance ()
7990 }.let {
8091 disposables.add(it)
8192 }
8293
83- ethereumAdapter.syncStateUpdateSubject .subscribe {
94+ ethereumAdapter.syncStateFlowable .subscribe {
8495 updateState()
8596 }.let {
8697 disposables.add(it)
8798 }
8899
89- erc20Adapter.syncStateUpdateSubject.subscribe {
90- updateErc20State()
91- }.let {
92- disposables.add(it)
93- }
94100
95101 //
96102 // ERC20
97103 //
98104
99- erc20Adapter.balanceSubject.subscribe {
105+ erc20Adapter.transactionsFlowable.subscribe {
106+ updateErc20Transactions()
107+ }.let {
108+ disposables.add(it)
109+ }
110+
111+ erc20Adapter.balanceFlowable.subscribe {
100112 updateErc20Balance()
101113 }.let {
102114 disposables.add(it)
103115 }
104116
117+ erc20Adapter.syncStateFlowable.subscribe {
118+ updateErc20State()
119+ }.let {
120+ disposables.add(it)
121+ }
122+
105123 ethereumKit.start()
106124 }
107125
@@ -125,26 +143,53 @@ class MainViewModel : ViewModel() {
125143 erc20TokenBalance.postValue(erc20Adapter.balance)
126144 }
127145
146+ private fun updateTransactions () {
147+ ethereumAdapter.transactions()
148+ .observeOn(AndroidSchedulers .mainThread())
149+ .subscribe { list: List <TransactionRecord > ->
150+ transactions.value = list
151+ }.let {
152+ disposables.add(it)
153+ }
154+ }
155+
156+ private fun updateErc20Transactions () {
157+ erc20Adapter.transactions()
158+ .observeOn(AndroidSchedulers .mainThread())
159+ .subscribe { list: List <TransactionRecord > ->
160+ transactions.value = list
161+ }.let {
162+ disposables.add(it)
163+ }
164+ }
165+
166+
128167 //
129168 // Ethereum
130169 //
131170
132171 fun refresh () {
133- ethereumKit.start()
134- fee.postValue(ethereumKit.fee(gasPriceInWei))
172+ ethereumKit.refresh()
173+ fee.postValue(ethereumKit.fee(gasPrice))
174+ }
175+
176+ fun clear () {
177+ EthereumKit .clear(App .instance)
178+ Erc20Kit .clear(App .instance)
179+ init ()
135180 }
136181
137182 fun receiveAddress (): String {
138183 return ethereumKit.receiveAddress
139184 }
140185
141186 fun send (address : String , amount : BigDecimal ) {
142- ethereumAdapter.sendSingle (address, amount)
187+ ethereumAdapter.send (address, amount)
143188 .subscribeOn(io.reactivex.schedulers.Schedulers .io())
144189 .observeOn(AndroidSchedulers .mainThread())
145190 .subscribe({
146191 // success
147- Toast .makeText( App .instance, " Success " , Toast . LENGTH_SHORT ).show()
192+ sendStatus.value = null
148193 }, {
149194 Log .e(" MainViewModel" , " send failed ${it.message} " )
150195 sendStatus.value = it
@@ -157,20 +202,20 @@ class MainViewModel : ViewModel() {
157202 //
158203
159204 fun sendERC20 (address : String , amount : BigDecimal ) {
160- erc20Adapter.sendSingle (address, amount)
205+ erc20Adapter.send (address, amount)
161206 .subscribeOn(io.reactivex.schedulers.Schedulers .io())
162207 .observeOn(AndroidSchedulers .mainThread())
163208 .subscribe({
164209 // success
165- Toast .makeText( App .instance, " Success " , Toast . LENGTH_SHORT ).show()
210+ sendStatus.value = null
166211 }, {
167212 Log .e(" MainViewModel" , " send failed ${it.message} " )
168213 sendStatus.value = it
169214 })?.let { disposables.add(it) }
170215 }
171216
172217 fun filterTransactions (ethTx : Boolean ) {
173- val txMethod = if (ethTx) ethereumAdapter.transactionsSingle () else erc20Adapter.transactionsSingle ()
218+ val txMethod = if (ethTx) ethereumAdapter.transactions () else erc20Adapter.transactions ()
174219
175220 txMethod
176221 .observeOn(AndroidSchedulers .mainThread())
@@ -181,18 +226,4 @@ class MainViewModel : ViewModel() {
181226 }
182227 }
183228
184- //
185- // Private
186- //
187-
188- private fun updateTransactions () {
189- ethereumAdapter.transactionsSingle()
190- .observeOn(AndroidSchedulers .mainThread())
191- .subscribe { list: List <TransactionRecord > ->
192- transactions.value = list
193- }.let {
194- disposables.add(it)
195- }
196- }
197-
198229}
0 commit comments