/
MainViewModel.kt
197 lines (157 loc) · 5.83 KB
/
MainViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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.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() {
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 etherState = MutableLiveData<SyncState>()
val erc20State = MutableLiveData<SyncState>()
val erc20TokenBalance = MutableLiveData<BigDecimal>()
val sendStatus = SingleLiveEvent<Throwable?>()
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.ethereumKit(App.instance, words, "unique-wallet-id", testMode, infuraKey = infuraKey, etherscanKey = etherscanKey)
ethereumAdapter = EthereumAdapter(ethereumKit)
erc20Adapter = Erc20Adapter(ethereumKit, contractAddress, contractDecimal)
ethereumKit.start()
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.start()
fee.postValue(ethereumKit.fee())
}
fun receiveAddress(): String {
return ethereumKit.receiveAddress
}
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) }
}
//
// 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) }
}
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)
}
}
//
// Private
//
private fun updateTransactions() {
ethereumAdapter.transactionsSingle()
.observeOn(AndroidSchedulers.mainThread())
.subscribe { list: List<TransactionRecord> ->
transactions.value = list
}.let {
disposables.add(it)
}
}
}