-
Notifications
You must be signed in to change notification settings - Fork 289
/
NewExchangePresenter.kt
922 lines (838 loc) · 37.4 KB
/
NewExchangePresenter.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
package piuk.blockchain.android.ui.shapeshift.newexchange
import info.blockchain.api.data.UnspentOutputs
import info.blockchain.balance.CryptoCurrency
import info.blockchain.wallet.api.data.FeeOptions
import info.blockchain.wallet.coin.GenericMetadataAccount
import info.blockchain.wallet.payload.data.Account
import info.blockchain.wallet.shapeshift.data.MarketInfo
import info.blockchain.wallet.shapeshift.data.Quote
import info.blockchain.wallet.shapeshift.data.QuoteRequest
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Function3
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.subjects.PublishSubject
import org.web3j.utils.Convert
import piuk.blockchain.android.R
import piuk.blockchain.android.data.bitcoincash.BchDataManager
import piuk.blockchain.android.data.cache.DynamicFeeCache
import piuk.blockchain.android.data.datamanagers.FeeDataManager
import piuk.blockchain.android.data.ethereum.EthDataManager
import piuk.blockchain.android.data.payments.SendDataManager
import piuk.blockchain.android.ui.receive.WalletAccountHelper
import piuk.blockchain.android.ui.shapeshift.models.ShapeShiftData
import piuk.blockchain.android.util.StringUtils
import piuk.blockchain.android.util.extensions.addToCompositeDisposable
import piuk.blockchain.androidbuysell.datamanagers.BuyDataManager
import piuk.blockchain.androidcore.data.currency.CurrencyFormatManager
import piuk.blockchain.androidcore.data.exchangerate.ExchangeRateDataManager
import piuk.blockchain.androidcore.data.payload.PayloadDataManager
import piuk.blockchain.androidcore.data.settings.SettingsDataManager
import piuk.blockchain.androidcore.data.shapeshift.ShapeShiftDataManager
import com.blockchain.morph.to
import piuk.blockchain.androidcore.data.walletoptions.WalletOptionsDataManager
import piuk.blockchain.androidcore.utils.Either
import piuk.blockchain.androidcore.utils.extensions.applySchedulers
import piuk.blockchain.androidcore.utils.helperfunctions.unsafeLazy
import piuk.blockchain.androidcoreui.ui.base.BasePresenter
import piuk.blockchain.androidcoreui.ui.customviews.ToastCustom
import timber.log.Timber
import java.math.BigDecimal
import java.math.BigInteger
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.NumberFormat
import java.text.ParseException
import java.util.Locale
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class NewExchangePresenter @Inject constructor(
private val payloadDataManager: PayloadDataManager,
private val ethDataManager: EthDataManager,
private val bchDataManager: BchDataManager,
private val sendDataManager: SendDataManager,
private val dynamicFeeCache: DynamicFeeCache,
private val feeDataManager: FeeDataManager,
private val exchangeRateFactory: ExchangeRateDataManager,
private val shapeShiftDataManager: ShapeShiftDataManager,
private val stringUtils: StringUtils,
private val settingsDataManager: SettingsDataManager,
private val buyDataManager: BuyDataManager,
private val walletOptionsDataManager: WalletOptionsDataManager,
private val currencyFormatManager: CurrencyFormatManager,
walletAccountHelper: WalletAccountHelper
) : BasePresenter<NewExchangeView>() {
internal val toCryptoSubject: PublishSubject<String> = PublishSubject.create<String>()
internal val fromCryptoSubject: PublishSubject<String> = PublishSubject.create<String>()
internal val toFiatSubject: PublishSubject<String> = PublishSubject.create<String>()
internal val fromFiatSubject: PublishSubject<String> = PublishSubject.create<String>()
private val btcAccounts = walletAccountHelper.getHdAccounts()
private val bchAccounts = walletAccountHelper.getHdBchAccounts()
private val cryptoFormat by unsafeLazy {
(NumberFormat.getInstance(view.locale) as DecimalFormat).apply {
minimumFractionDigits = 1
maximumFractionDigits = 8
}
}
private var marketInfo: MarketInfo? = null
private var shapeShiftData: ShapeShiftData? = null
private var latestQuote: Quote? = null
private var account: Account? = null
private var bchAccount: GenericMetadataAccount? = null
private var feeOptions: FeeOptions? = null
private var fromCurrency = CryptoCurrency.BTC
private var toCurrency = CryptoCurrency.ETHER
override fun onViewReady() {
view.updateUi(
fromCurrency,
toCurrency,
getCurrencyLabel(fromCurrency),
getCurrencyLabel(toCurrency),
currencyFormatManager.getFormattedFiatValueWithSymbol(0.0)
)
val shapeShiftObservable = getMarketInfoObservable(fromCurrency, toCurrency)
val feesObservable = fetchFeesObservable(fromCurrency)
shapeShiftObservable
.doOnNext { marketInfo = it }
.flatMap { feesObservable }
.doOnSubscribe { view.showProgressDialog(R.string.shapeshift_getting_information) }
.doOnTerminate { view.dismissProgressDialog() }
.addToCompositeDisposable(this)
.doOnError { Timber.e(it) }
.subscribeBy(
onComplete = {
// Only set account the first time
if (account == null) account = payloadDataManager.defaultAccount
if (bchAccount == null) bchAccount =
bchDataManager.getDefaultGenericMetadataAccount()
checkForEmptyBalances()
},
onError = {
view.showToast(
R.string.shapeshift_getting_information_failed,
ToastCustom.TYPE_ERROR
)
view.finishPage()
}
)
subscribeToEditTexts()
}
internal fun onSwitchCurrencyClicked() {
val currentFromCurrencyState = fromCurrency
val currentToCurrencyState = toCurrency
toCurrency = currentFromCurrencyState
fromCurrency = currentToCurrencyState
compositeDisposable.clear()
.run { view.clearEditTexts() }
.run { view.clearError() }
.run {
// This is a bit hacky and should be abstracted out when more currencies are
// available, hence the null checks in onViewReady().
onViewReady()
}
}
internal fun onContinuePressed() {
// State check
if (shapeShiftData == null) {
view.showToast(R.string.invalid_amount, ToastCustom.TYPE_ERROR)
return
}
// Check user isn't submitting an empty page
if (shapeShiftData?.withdrawalAmount?.compareTo(BigDecimal.ZERO) == 0) {
view.showToast(R.string.invalid_amount, ToastCustom.TYPE_ERROR)
return
}
// It's possible that the fee observable can return zero but not kill the chain with an
// error, hence checking here
if (shapeShiftData?.networkFee?.compareTo(BigDecimal.ZERO) == 0) {
view.showToast(R.string.shapeshift_getting_fees_failed, ToastCustom.TYPE_ERROR)
return
}
getMaxCurrencyObservable()
.doOnSubscribe { view.showProgressDialog(R.string.please_wait) }
.doOnNext {
val amount = it.setScale(8, RoundingMode.HALF_DOWN)
if (amount < shapeShiftData!!.depositAmount) {
view.dismissProgressDialog()
// Show warning, inform user
view.showAmountError(stringUtils.getString(R.string.insufficient_funds))
Timber.d("Attempted to send ${shapeShiftData!!.depositAmount} but max available was $amount")
} else {
sendFinalRequest(fromCurrency, toCurrency)
}
}
.subscribeBy(
onError = { setUnknownErrorState(it) }
)
}
internal fun onMaxPressed() {
view.removeAllFocus()
view.showQuoteInProgress(true)
getMaxCurrencyObservable().subscribeBy(
onNext = {
// 'it' can be zero here if amounts insufficient
if (getMinimum() > it) {
view.showAmountError(
stringUtils.getFormattedString(
R.string.shapeshift_amount_to_low,
getMinimum(),
fromCurrency.symbol.toUpperCase()
)
)
view.showQuoteInProgress(false)
} else {
fromCryptoSubject.onNext(cryptoFormat.format(it))
// This is a bit of a hack to bypass focus issues
view.updateFromCryptoText(cryptoFormat.format(it))
}
},
onError = { setUnknownErrorState(it) }
)
}
internal fun onMinPressed() {
view.removeAllFocus()
view.showQuoteInProgress(true)
getMaxCurrencyObservable()
.subscribeBy(
onNext = {
if (getMinimum() > it) {
view.showAmountError(
stringUtils.getFormattedString(
R.string.shapeshift_amount_to_low,
getMinimum(),
fromCurrency.symbol.toUpperCase()
)
)
view.showQuoteInProgress(false)
} else {
with(getMinimum()) {
fromCryptoSubject.onNext(cryptoFormat.format(this))
// This is a bit of a hack to bypass focus issues
view.updateFromCryptoText(cryptoFormat.format(this))
}
}
},
onError = { setUnknownErrorState(it) }
)
}
internal fun onFromChooserClicked() = view.launchAccountChooserActivityFrom()
internal fun onToChooserClicked() = view.launchAccountChooserActivityTo()
internal fun onFromEthSelected() {
fromCurrency = CryptoCurrency.ETHER
// Here we prevent users selecting to and from as the same currency. Default to BTC
if (fromCurrency == toCurrency) toCurrency = CryptoCurrency.BTC
view.clearEditTexts()
onViewReady()
}
internal fun onToEthSelected() {
toCurrency = CryptoCurrency.ETHER
// Here we prevent users selecting to and from as the same currency. Default to BTC
if (fromCurrency == toCurrency) fromCurrency = CryptoCurrency.BTC
view.clearEditTexts()
onViewReady()
}
internal fun onFromAccountChanged(account: Account) {
fromCurrency = CryptoCurrency.BTC
// Here we prevent users selecting to and from as the same currency. Default to ETH
if (fromCurrency == toCurrency) toCurrency = CryptoCurrency.ETHER
this.account = account
view.clearEditTexts()
onViewReady()
}
internal fun onToAccountChanged(account: Account) {
toCurrency = CryptoCurrency.BTC
// Here we prevent users selecting to and from as the same currency. Default to ETH
if (fromCurrency == toCurrency) fromCurrency = CryptoCurrency.ETHER
this.account = account
view.clearEditTexts()
onViewReady()
}
fun onFromBchAccountChanged(account: GenericMetadataAccount) {
fromCurrency = CryptoCurrency.BCH
// Here we prevent users selecting to and from as the same currency. Default to BTC
if (fromCurrency == toCurrency) toCurrency = CryptoCurrency.BTC
this.bchAccount = account
view.clearEditTexts()
onViewReady()
}
fun onToBchAccountChanged(account: GenericMetadataAccount) {
toCurrency = CryptoCurrency.BCH
// Here we prevent users selecting to and from as the same currency. Default to BTC
if (fromCurrency == toCurrency) fromCurrency = CryptoCurrency.BTC
this.bchAccount = account
view.clearEditTexts()
onViewReady()
}
private fun checkForEmptyBalances() {
hasEmptyBalances()
.addToCompositeDisposable(this)
.applySchedulers()
.doOnSubscribe { view.showProgressDialog(R.string.please_wait) }
.doOnTerminate { view.dismissProgressDialog() }
.flatMap { empty ->
if (empty) buyDataManager.canBuy else Observable.empty<Boolean>()
}
.subscribeBy(
onNext = { canBuy -> view.showNoFunds(canBuy && view.isBuyPermitted) },
onError = { Timber.e(it) }
)
}
private fun subscribeToEditTexts() {
fromCryptoSubject.applyDefaults()
// Update to Fiat as it's not dependent on web results
.doOnNext { updateFromFiat(it) }
// Update results dependent on Shapeshift
.flatMap { amount ->
getQuoteFromRequest(amount, fromCurrency, toCurrency)
.doOnNext { updateToFields(it.withdrawalAmount) }
}
.subscribeBy(
onError = { setUnknownErrorState(it) }
)
fromFiatSubject.applyDefaults()
// Convert to fromCrypto amount
.map {
val (_, toExchangeRate) = getExchangeRates(
currencyFormatManager.fiatCountryCode,
toCurrency,
fromCurrency
)
return@map it.divide(toExchangeRate, 18, RoundingMode.HALF_UP)
}
// Update from amount view
.doOnNext { view.updateFromCryptoText(cryptoFormat.format(it)) }
// Update results dependent on Shapeshift
.flatMap { amount ->
getQuoteFromRequest(amount, fromCurrency, toCurrency)
.doOnNext { updateToFields(it.withdrawalAmount) }
}
.subscribeBy(
onError = { setUnknownErrorState(it) }
)
toCryptoSubject.applyDefaults()
// Update to Fiat as it's not dependent on web results
.doOnNext { updateToFiat(it) }
// Update results dependent on Shapeshift
.flatMap { amount ->
getQuoteToRequest(amount, fromCurrency, toCurrency)
.doOnNext { updateFromFields(it.depositAmount) }
}
.subscribeBy(
onError = { setUnknownErrorState(it) }
)
toFiatSubject.applyDefaults()
// Convert to toCrypto amount
.map {
val (fromExchangeRate, _) = getExchangeRates(
currencyFormatManager.fiatCountryCode,
toCurrency,
fromCurrency
)
return@map it.divide(fromExchangeRate, 18, RoundingMode.HALF_UP)
}
// Update from amount view
.doOnNext { view.updateToCryptoText(cryptoFormat.format(it)) }
// Update results dependent on Shapeshift
.flatMap { amount ->
getQuoteToRequest(amount, fromCurrency, toCurrency)
.doOnNext { updateFromFields(it.depositAmount) }
}
.subscribeBy(
onError = { setUnknownErrorState(it) }
)
}
private fun setUnknownErrorState(throwable: Throwable) {
Timber.e(throwable)
view.clearEditTexts()
view.setButtonEnabled(false)
view.showToast(R.string.shapeshift_getting_information_failed, ToastCustom.TYPE_ERROR)
}
private fun getExchangeRates(
currencyCode: String,
toCurrency: CryptoCurrency,
fromCurrency: CryptoCurrency
): ExchangeRates = ExchangeRates(
getExchangeRate(toCurrency, currencyCode),
getExchangeRate(fromCurrency, currencyCode)
)
private fun getExchangeRate(
cryptoCurrency: CryptoCurrency,
currencyCode: String
) = exchangeRateFactory.getLastPrice(cryptoCurrency, currencyCode).toBigDecimal()
private fun getUnspentApiResponseBtc(address: String): Observable<UnspentOutputs> {
return if (payloadDataManager.getAddressBalance(address).toLong() > 0) {
sendDataManager.getUnspentOutputs(address)
} else {
Observable.error(Throwable("No funds - skipping call to unspent API"))
}
}
private fun getUnspentApiResponseBch(address: String): Observable<UnspentOutputs> {
return if (bchDataManager.getAddressBalance(address).toLong() > 0) {
sendDataManager.getUnspentBchOutputs(address)
} else {
Observable.error(Throwable("No funds - skipping call to unspent API"))
}
}
private fun getBtcLabel() = if (btcAccounts.size > 1) {
if (account != null) {
account!!.label
} else {
payloadDataManager.defaultAccount.label
}
} else {
stringUtils.getString(R.string.shapeshift_btc)
}
private fun getEthLabel() = if (btcAccounts.size > 1) {
stringUtils.getString(R.string.eth_default_account_label)
} else {
stringUtils.getString(R.string.shapeshift_eth)
}
private fun getBchLabel() = if (bchAccounts.size > 1) {
if (bchAccount != null) {
bchAccount!!.label
} else {
bchDataManager.getDefaultGenericMetadataAccount()!!.label
}
} else {
stringUtils.getString(R.string.shapeshift_bch)
}
private fun getCurrencyLabel(currency: CryptoCurrency) = when (currency) {
CryptoCurrency.BTC -> getBtcLabel()
CryptoCurrency.ETHER -> getEthLabel()
CryptoCurrency.BCH -> getBchLabel()
}
private fun getShapeShiftPair(fromCurrency: CryptoCurrency, toCurrency: CryptoCurrency) =
fromCurrency to toCurrency
private fun getMaximum() = marketInfo?.maxLimit ?: BigDecimal.ZERO
private fun getMinimum() = marketInfo?.minimum ?: BigDecimal.ZERO
// region Field Updates
private fun updateFromFiat(amount: BigDecimal) {
view.updateFromFiatText(
currencyFormatManager.getFormattedFiatValueWithSymbol(
amount.multiply(
getExchangeRates(
currencyFormatManager.fiatCountryCode,
toCurrency,
fromCurrency
).fromRate
).toDouble()
)
)
}
private fun updateToFiat(amount: BigDecimal) {
view.updateToFiatText(
currencyFormatManager.getFormattedFiatValueWithSymbol(
amount.multiply(
getExchangeRates(
currencyFormatManager.fiatCountryCode,
toCurrency,
fromCurrency
).toRate
).toDouble()
)
)
}
private fun updateToFields(toAmount: BigDecimal) {
val amount = toAmount.max(BigDecimal.ZERO)
view.updateToCryptoText(cryptoFormat.format(amount))
updateToFiat(amount)
}
private fun updateFromFields(fromAmount: BigDecimal) {
val amount = fromAmount.max(BigDecimal.ZERO)
view.updateFromCryptoText(cryptoFormat.format(amount))
updateFromFiat(amount)
}
// endregion
// region Observables
/**
* Sends a complete [QuoteRequest] object to ShapeShift and sends all of the required fields
* serialized to the next Activity.
*/
private fun sendFinalRequest(fromCurrency: CryptoCurrency, toCurrency: CryptoCurrency) {
val quoteRequest = QuoteRequest().apply {
with(shapeShiftData!!) {
this@apply.depositAmount = depositAmount
this@apply.withdrawalAmount = withdrawalAmount
this@apply.withdrawal = withdrawalAddress
this@apply.pair = getShapeShiftPair(fromCurrency, toCurrency).pairCode
this@apply.returnAddress = shapeShiftData!!.returnAddress
this@apply.apiKey = view.shapeShiftApiKey
}
}
// Update quote with final data
getQuoteObservable(quoteRequest, fromCurrency, toCurrency)
.doOnTerminate { view.dismissProgressDialog() }
.addToCompositeDisposable(this)
.subscribeBy(
onNext = { view.launchConfirmationPage(shapeShiftData!!) },
onError = {
view.showToast(R.string.unexpected_error, ToastCustom.TYPE_ERROR)
}
)
}
private fun getQuoteFromRequest(
fromAmount: BigDecimal,
fromCurrency: CryptoCurrency,
toCurrency: CryptoCurrency
): Observable<Quote> {
val quoteRequest = QuoteRequest().apply {
depositAmount = fromAmount.setScale(8, RoundingMode.HALF_DOWN)
pair = getShapeShiftPair(fromCurrency, toCurrency).pairCode
apiKey = view.shapeShiftApiKey
}
return getQuoteObservable(quoteRequest, fromCurrency, toCurrency)
}
private fun getQuoteToRequest(
toAmount: BigDecimal,
fromCurrency: CryptoCurrency,
toCurrency: CryptoCurrency
): Observable<Quote> {
val quoteRequest = QuoteRequest().apply {
withdrawalAmount = toAmount.setScale(8, RoundingMode.HALF_DOWN)
pair = getShapeShiftPair(fromCurrency, toCurrency).pairCode
apiKey = view.shapeShiftApiKey
}
return getQuoteObservable(quoteRequest, fromCurrency, toCurrency)
}
private fun fetchFeesObservable(selectedCurrency: CryptoCurrency) = when (selectedCurrency) {
CryptoCurrency.BTC -> feeDataManager.btcFeeOptions
.doOnSubscribe { feeOptions = dynamicFeeCache.btcFeeOptions!! }
.doOnNext { dynamicFeeCache.btcFeeOptions = it }
CryptoCurrency.ETHER -> feeDataManager.ethFeeOptions
.doOnSubscribe { feeOptions = dynamicFeeCache.ethFeeOptions!! }
.doOnNext { dynamicFeeCache.ethFeeOptions = it }
CryptoCurrency.BCH -> feeDataManager.bchFeeOptions
.doOnSubscribe { feeOptions = dynamicFeeCache.bchFeeOptions!! }
.doOnNext { dynamicFeeCache.bchFeeOptions = it }
}
private fun getMarketInfoObservable(
fromCurrency: CryptoCurrency,
toCurrency: CryptoCurrency
): Observable<MarketInfo> =
shapeShiftDataManager.getRate(fromCurrency to toCurrency)
private fun getQuoteObservable(
quoteRequest: QuoteRequest,
fromCurrency: CryptoCurrency,
toCurrency: CryptoCurrency
): Observable<Quote> =
// Get quote for Quote Request
shapeShiftDataManager.getQuote(quoteRequest)
.addToCompositeDisposable(this)
.map {
when (it) {
is Either.Right<Quote> -> return@map it.value
is Either.Left<String> -> {
// Show error in UI, fallback to initial quote rates
view.showAmountError(it.value)
return@map Quote().apply {
orderId = ""
quotedRate = marketInfo?.rate ?: BigDecimal.ONE
minerFee = marketInfo?.minerFee ?: BigDecimal.ZERO
withdrawalAmount = quoteRequest.withdrawalAmount ?: BigDecimal.ZERO
depositAmount = quoteRequest.depositAmount ?: BigDecimal.ZERO
expiration = 0L
}
}
}
}
.flatMap { quote ->
// Get fee for the proposed payment amount
getFeeForPayment(quote.depositAmount, fromCurrency)
.flatMap { fee ->
// Get receive/change address pair
getAddressPair(fromCurrency, toCurrency)
.map { addresses ->
latestQuote = quote
// Update ShapeShift Data
shapeShiftData = ShapeShiftData(
orderId = quote.orderId,
fromCurrency = fromCurrency,
toCurrency = toCurrency,
depositAmount = quote.depositAmount
?: BigDecimal.ZERO,
changeAddress = addresses.changeAddress,
depositAddress = quote.deposit ?: "",
withdrawalAmount = quote.withdrawalAmount
?: BigDecimal.ZERO,
withdrawalAddress = addresses.withdrawalAddress,
exchangeRate = quote.quotedRate,
transactionFee = fee,
networkFee = quote.minerFee,
returnAddress = addresses.returnAddress,
xPub = getSelectedXpub(),
expiration = quote.expiration,
gasLimit = BigInteger.valueOf(
feeOptions?.gasLimit ?: 0L
),
gasPrice = BigInteger.valueOf(
feeOptions?.regularFee ?: 0L
),
feePerKb = BigInteger.valueOf(
when (fromCurrency) {
CryptoCurrency.BTC, CryptoCurrency.BCH -> feeOptions?.priorityFee
?: 0 * 1000L
CryptoCurrency.ETHER -> 0L
}
)
)
return@map quote
}
}
.doOnError { setUnknownErrorState(it) }
}
.doAfterNext {
view.showQuoteInProgress(false)
view.setButtonEnabled(true)
}
.doOnError { setUnknownErrorState(it) }
.doOnError { Timber.e(it) }
private fun getSelectedXpub(): String = if (fromCurrency == CryptoCurrency.BCH) {
bchAccount?.xpub ?: throw IllegalStateException("BCH Selected but bchAccount was null")
} else {
account?.xpub ?: throw IllegalStateException("account should never be null at this point")
}
private fun hasEmptyBalances(): Observable<Boolean> =
Observable.zip(
getBtcMaxObservable(),
getEthMaxObservable(),
getBchMaxObservable(),
Function3 { btc, eth, bch ->
btc == BigDecimal.ZERO &&
eth == BigDecimal.ZERO &&
bch == BigDecimal.ZERO
}
)
// region Fees Observables
private fun getFeeForPayment(
amountToSend: BigDecimal,
selectedCurrency: CryptoCurrency
): Observable<BigInteger> = when (selectedCurrency) {
CryptoCurrency.BTC -> getFeeForBtcPaymentObservable(
amountToSend,
BigInteger.valueOf(feeOptions!!.priorityFee * 1000)
)
CryptoCurrency.ETHER -> getFeeForEthPaymentObservable()
CryptoCurrency.BCH -> getFeeForBchPaymentObservable(
amountToSend,
BigInteger.valueOf(feeOptions!!.priorityFee * 1000)
)
}.doOnError { view.showToast(R.string.confirm_payment_fee_sync_error, ToastCustom.TYPE_ERROR) }
/**
* Returns the ETH fee in Wei
*/
private fun getFeeForEthPaymentObservable(): Observable<BigInteger> {
val gwei = BigDecimal.valueOf(feeOptions!!.gasLimit * feeOptions!!.regularFee)
val feeInGwei = Convert.toWei(gwei, Convert.Unit.GWEI)
return Observable.just(feeInGwei.toBigInteger())
}
/**
* Returns the BTC fee in Satoshis
*/
private fun getFeeForBtcPaymentObservable(
amountToSend: BigDecimal,
feePerKb: BigInteger
): Observable<BigInteger> = getUnspentApiResponseBtc(account!!.xpub)
.addToCompositeDisposable(this)
.map {
val satoshis = amountToSend.multiply(BigDecimal.valueOf(100000000))
return@map sendDataManager.getSpendableCoins(
it,
satoshis.toBigInteger(),
feePerKb
).absoluteFee
}
private fun getFeeForBchPaymentObservable(
amountToSend: BigDecimal,
feePerKb: BigInteger
): Observable<BigInteger> = getUnspentApiResponseBch(bchAccount!!.xpub)
.addToCompositeDisposable(this)
.map {
val satoshis = amountToSend.multiply(BigDecimal.valueOf(100000000))
return@map sendDataManager.getSpendableCoins(
it,
satoshis.toBigInteger(),
feePerKb
).absoluteFee
}
// endregion
// region Address Pair Observables
private fun getAddressPair(
fromCurrency: CryptoCurrency,
toCurrency: CryptoCurrency
): Observable<Addresses> = Observable.zip(
getReceiveAddress(fromCurrency),
getWithdrawalAddress(toCurrency),
getChangeAddress(fromCurrency),
Function3<String, String, String, Addresses> { receive, withdrawal, change ->
Addresses(withdrawal, receive, change)
})
.doOnError {
view.showToast(
R.string.shapeshift_deriving_address_failed,
ToastCustom.TYPE_ERROR
)
}
private fun getReceiveAddress(cryptoCurrency: CryptoCurrency): Observable<String> =
when (cryptoCurrency) {
CryptoCurrency.BTC -> getBtcReceiveAddress()
CryptoCurrency.ETHER -> getEthAddress()
CryptoCurrency.BCH -> getBchReceiveAddress()
}
private fun getChangeAddress(cryptoCurrency: CryptoCurrency): Observable<String> =
when (cryptoCurrency) {
CryptoCurrency.BTC -> getBtcChangeAddress()
CryptoCurrency.ETHER -> getEthAddress()
CryptoCurrency.BCH -> getBchChangeAddress()
}
private fun getWithdrawalAddress(cryptoCurrency: CryptoCurrency): Observable<String> =
when (cryptoCurrency) {
CryptoCurrency.BTC -> getBtcReceiveAddress()
CryptoCurrency.ETHER -> getEthAddress()
CryptoCurrency.BCH -> getBchReceiveAddress()
}
private fun getEthAddress(): Observable<String> =
Observable.just(ethDataManager.getEthWallet()!!.account.address)
private fun getBtcReceiveAddress(): Observable<String> =
payloadDataManager.getNextReceiveAddress(account!!)
private fun getBtcChangeAddress(): Observable<String> =
payloadDataManager.getNextChangeAddress(account!!)
private fun getBchReceiveAddress(): Observable<String> {
val position =
bchDataManager.getActiveAccounts().indexOfFirst { it.xpub == bchAccount!!.xpub }
return bchDataManager.getNextReceiveAddress(position)
}
private fun getBchChangeAddress(): Observable<String> {
val position =
bchDataManager.getActiveAccounts().indexOfFirst { it.xpub == bchAccount!!.xpub }
return bchDataManager.getNextChangeAddress(position)
}
// endregion
// region Max Amounts Observables
private fun getMaxCurrencyObservable(): Observable<BigDecimal> =
when (fromCurrency) {
CryptoCurrency.BTC -> getBtcMaxObservable()
CryptoCurrency.ETHER -> getEthMaxObservable()
CryptoCurrency.BCH -> getBchMaxObservable()
}.doOnError { Timber.e(it) }
private fun getEthMaxObservable(): Observable<BigDecimal> = ethDataManager.fetchEthAddress()
.addToCompositeDisposable(this)
.map {
val gwei = BigDecimal.valueOf(feeOptions!!.gasLimit * feeOptions!!.regularFee)
val wei = Convert.toWei(gwei, Convert.Unit.GWEI)
val addressResponse = it.getAddressResponse()
val maxAvailable =
addressResponse!!.balance!!.minus(wei.toBigInteger()).max(BigInteger.ZERO)
val availableEth = Convert.fromWei(maxAvailable.toString(), Convert.Unit.ETHER)
val amount = if (availableEth > getMaximum()) getMaximum() else availableEth
return@map amount to Convert.fromWei(wei, Convert.Unit.ETHER)
}
.flatMap { (amount, fee) -> getRegionalMaxAmount(fee, amount) }
.onErrorReturn { BigDecimal.ZERO }
private fun getBtcMaxObservable(): Observable<BigDecimal> =
getUnspentApiResponseBtc(account!!.xpub)
.addToCompositeDisposable(this)
.map { unspentOutputs ->
val sweepBundle = sendDataManager.getMaximumAvailable(
unspentOutputs,
BigInteger.valueOf(feeOptions!!.priorityFee * 1000)
)
val sweepableAmount =
BigDecimal(sweepBundle.left).divide(BigDecimal.valueOf(1e8))
val amount =
if (sweepableAmount > getMaximum()) getMaximum() else sweepableAmount
return@map amount to BigDecimal(sweepBundle.right).divide(
BigDecimal.valueOf(1e8)
)
}
.flatMap { (amount, fee) -> getRegionalMaxAmount(fee, amount) }
.onErrorReturn { BigDecimal.ZERO }
private fun getBchMaxObservable(): Observable<BigDecimal> =
getUnspentApiResponseBch(bchAccount!!.xpub)
.addToCompositeDisposable(this)
.map { unspentOutputs ->
val sweepBundle = sendDataManager.getMaximumAvailable(
unspentOutputs,
BigInteger.valueOf(feeOptions!!.priorityFee * 1000)
)
val sweepableAmount =
BigDecimal(sweepBundle.left).divide(BigDecimal.valueOf(1e8))
val amount =
if (sweepableAmount > getMaximum()) getMaximum() else sweepableAmount
return@map amount to BigDecimal(sweepBundle.right).divide(
BigDecimal.valueOf(1e8)
)
}
.flatMap { (amount, fee) -> getRegionalMaxAmount(fee, amount) }
.onErrorReturn { BigDecimal.ZERO }
/**
* If the amount passed to this function is greater than $500 or €500 (region dependent),
* the function returns the amount of crypto equal to $500 or €500, minus the fee for sending
* it to ShapeShift (therefore the total amount sent is equal to $500 or €500). If the amount
* passed to the function is less than the limit, the amount is returned.
*
* @param fee The fee in Ether or BTC (no sub-units)
* @param amount The amount to be checked against in Ether or BTC (no sub-units)
* @return An [Observable] wrapping a [BigDecimal]
*/
private fun getRegionalMaxAmount(fee: BigDecimal, amount: BigDecimal): Observable<BigDecimal> {
return settingsDataManager.getSettings().map {
val rate = when {
it.countryCode == "US" -> getExchangeRates("USD", toCurrency, fromCurrency).fromRate
else -> getExchangeRates("EUR", toCurrency, fromCurrency).fromRate
}
val limit = walletOptionsDataManager.getShapeShiftLimit().toBigDecimal()
// Multiply to get fiat amount
val fiatAmount = amount.multiply(rate)
if (fiatAmount >= limit) {
// Get crypto amount equal to 500 $ or €, then subtract fee
return@map (limit.divide(rate, 8, RoundingMode.HALF_DOWN)).minus(fee)
} else {
return@map amount
}
}
}
// endregion
private fun PublishSubject<String>.applyDefaults(): Observable<BigDecimal> = this.doOnNext {
view.clearError()
view.setButtonEnabled(false)
view.showQuoteInProgress(true)
}.debounce(1000, TimeUnit.MILLISECONDS)
// Here we kill any quotes in flight already, as they take up to ten seconds to fulfill
.doOnNext { compositeDisposable.clear() }
// Strip out localised information for predictable formatting
.map { it.sanitise().parse(view.locale) }
// Logging
.doOnError(Timber::wtf)
// Return zero if empty or some other error
.onErrorReturn { BigDecimal.ZERO }
// Scheduling for UI updates if necessary
.observeOn(AndroidSchedulers.mainThread())
// If zero, clear all EditTexts and reset UI state
.doOnNext {
if (it <= BigDecimal.ZERO) {
view.clearEditTexts()
view.setButtonEnabled(false)
view.showQuoteInProgress(false)
}
}
// Don't pass zero events to the API as they're invalid
.filter { it > BigDecimal.ZERO }
// endregion
// region Extension Functions
private fun String.sanitise() = if (isNotEmpty()) this else "0"
@Throws(ParseException::class)
private fun String.parse(locale: Locale): BigDecimal {
val format = NumberFormat.getNumberInstance(locale)
if (format is DecimalFormat) {
format.isParseBigDecimal = true
}
return format.parse(this.replace("[^\\d.,]".toRegex(), "")) as BigDecimal
}
// endregion
private data class ExchangeRates(val toRate: BigDecimal, val fromRate: BigDecimal)
private data class Addresses(
val withdrawalAddress: String,
val returnAddress: String,
val changeAddress: String
)
}