Skip to content

Commit

Permalink
refactor(ShapeShift): AND-1249 Move some general types to Morph (#1063)
Browse files Browse the repository at this point in the history
* refactor(ShapeShift): AND-1249 Remove ToFromPair, rename CoinPair

* refactor(ShapeShift): AND-1249 Noticed do not need to go via string here

* refactor(ShapeShift): AND-1249 Use an infix pairing function

* refactor(ShapeShift): AND-1249 Fix coverage
  • Loading branch information
westonal committed Jul 27, 2018
1 parent 5432dfc commit 351d51b
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 124 deletions.
@@ -1,6 +1,8 @@
package piuk.blockchain.android.ui.shapeshift.detail

import info.blockchain.wallet.shapeshift.ShapeShiftPairs
import com.blockchain.morph.CoinPair
import com.blockchain.morph.to
import info.blockchain.balance.CryptoCurrency
import info.blockchain.wallet.shapeshift.data.Trade
import info.blockchain.wallet.shapeshift.data.TradeStatusResponse
import io.reactivex.Observable
Expand All @@ -9,7 +11,6 @@ import piuk.blockchain.android.R
import piuk.blockchain.android.ui.shapeshift.models.TradeDetailUiState
import piuk.blockchain.android.util.StringUtils
import piuk.blockchain.android.util.extensions.addToCompositeDisposable
import info.blockchain.balance.CryptoCurrency
import piuk.blockchain.androidcore.data.shapeshift.ShapeShiftDataManager
import piuk.blockchain.androidcore.utils.extensions.applySchedulers
import piuk.blockchain.androidcore.utils.helperfunctions.unsafeLazy
Expand Down Expand Up @@ -88,13 +89,12 @@ class ShapeShiftDetailPresenter @Inject constructor(
val toCoin: CryptoCurrency = CryptoCurrency.fromSymbol(outgoingType ?: "eth")!!
val fromAmount: BigDecimal? = incomingCoin
val toAmount: BigDecimal? = outgoingCoin
val pair = """${fromCoin.symbol.toLowerCase()}_${toCoin.symbol.toLowerCase()}"""
val (to, from) = getToFromPair(pair)
val pair = fromCoin to toCoin

fromAmount?.let { updateDeposit(from, it) }
toAmount?.let { updateReceive(to, it) }
fromAmount?.let { updateDeposit(pair.from, it) }
toAmount?.let { updateReceive(pair.to, it) }

if (to == from) {
if (pair.sameInputOutput) {
onRefunded()
return
}
Expand All @@ -114,14 +114,14 @@ class ShapeShiftDetailPresenter @Inject constructor(
updateOrderId(quote.orderId)
// Web don't store everything, but we do. Check here and make an assumption
if (quote.pair.isNullOrEmpty() || quote.pair == "_") {
quote.pair = ShapeShiftPairs.BTC_ETH
quote.pair = "btc_eth"
}
val (to, from) = getToFromPair(quote.pair)
val pair = CoinPair.fromPairCode(quote.pair)

updateDeposit(from, quote.depositAmount ?: BigDecimal.ZERO)
updateReceive(to, quote.withdrawalAmount ?: BigDecimal.ZERO)
updateExchangeRate(quote.quotedRate ?: BigDecimal.ZERO, from, to)
updateTransactionFee(to, quote.minerFee ?: BigDecimal.ZERO)
updateDeposit(pair.from, quote.depositAmount ?: BigDecimal.ZERO)
updateReceive(pair.to, quote.withdrawalAmount ?: BigDecimal.ZERO)
updateExchangeRate(quote.quotedRate ?: BigDecimal.ZERO, pair)
updateTransactionFee(pair.to, quote.minerFee ?: BigDecimal.ZERO)
}
}

Expand All @@ -144,16 +144,15 @@ class ShapeShiftDetailPresenter @Inject constructor(

private fun updateExchangeRate(
exchangeRate: BigDecimal,
fromCurrency: CryptoCurrency,
toCurrency: CryptoCurrency
pair: CoinPair
) {
val formattedExchangeRate = exchangeRate.setScale(8, RoundingMode.HALF_DOWN)
.toLocalisedString()
val formattedString = stringUtils.getFormattedString(
R.string.shapeshift_exchange_rate_formatted,
fromCurrency.symbol,
pair.from.symbol,
formattedExchangeRate,
toCurrency.symbol
pair.to.symbol
)

view.updateExchangeRate(formattedString)
Expand Down Expand Up @@ -188,8 +187,8 @@ class ShapeShiftDetailPresenter @Inject constructor(

// region UI State
private fun handleTrade(trade: Trade) {
val (to, from) = getToFromPair(trade.quote.pair)
if (to == from) {
val pair = CoinPair.fromPairCode(trade.quote.pair)
if (pair.sameInputOutput) {
onRefunded()
} else {
handleState(trade.status)
Expand Down Expand Up @@ -264,37 +263,5 @@ class ShapeShiftDetailPresenter @Inject constructor(
Trade.STATUS.COMPLETE, Trade.STATUS.FAILED, Trade.STATUS.RESOLVED -> true
}

// TODO: This is kind of ridiculous, but it'll do for now
private fun getToFromPair(pair: String): ToFromPair {
return when (pair.toLowerCase()) {
ShapeShiftPairs.ETH_BTC -> ToFromPair(CryptoCurrency.BTC, CryptoCurrency.ETHER)
ShapeShiftPairs.ETH_BCH -> ToFromPair(CryptoCurrency.BCH, CryptoCurrency.ETHER)
ShapeShiftPairs.BTC_ETH -> ToFromPair(CryptoCurrency.ETHER, CryptoCurrency.BTC)
ShapeShiftPairs.BTC_BCH -> ToFromPair(CryptoCurrency.BCH, CryptoCurrency.BTC)
ShapeShiftPairs.BCH_BTC -> ToFromPair(CryptoCurrency.BTC, CryptoCurrency.BCH)
ShapeShiftPairs.BCH_ETH -> ToFromPair(CryptoCurrency.ETHER, CryptoCurrency.BCH)
else -> {
// Refunded trade pairs
return when {
pair.equals("eth_eth", true) -> ToFromPair(
CryptoCurrency.ETHER,
CryptoCurrency.ETHER
)
pair.equals("bch_bch", true) -> ToFromPair(
CryptoCurrency.BCH,
CryptoCurrency.BCH
)
pair.equals("btc_btc", true) -> ToFromPair(
CryptoCurrency.BTC,
CryptoCurrency.BTC
)
else -> throw IllegalStateException("Attempt to get invalid pair $pair")
}
}
}
}

private fun BigDecimal.toLocalisedString(): String = decimalFormat.format(this)

private data class ToFromPair(val to: CryptoCurrency, val from: CryptoCurrency)
}
Expand Up @@ -30,7 +30,7 @@ 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 piuk.blockchain.androidcore.data.shapeshift.models.CoinPairings
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
Expand Down Expand Up @@ -441,7 +441,7 @@ class NewExchangePresenter @Inject constructor(
}

private fun getShapeShiftPair(fromCurrency: CryptoCurrency, toCurrency: CryptoCurrency) =
CoinPairings.getPair(fromCurrency, toCurrency)
fromCurrency to toCurrency

private fun getMaximum() = marketInfo?.maxLimit ?: BigDecimal.ZERO

Expand Down Expand Up @@ -563,7 +563,7 @@ class NewExchangePresenter @Inject constructor(
fromCurrency: CryptoCurrency,
toCurrency: CryptoCurrency
): Observable<MarketInfo> =
shapeShiftDataManager.getRate(CoinPairings.getPair(fromCurrency, toCurrency))
shapeShiftDataManager.getRate(fromCurrency to toCurrency)

private fun getQuoteObservable(
quoteRequest: QuoteRequest,
Expand Down
1 change: 1 addition & 0 deletions morph/common/build.gradle
Expand Up @@ -15,6 +15,7 @@ sourceCompatibility = Versions.javaCompatibilityVersion
targetCompatibility = Versions.javaCompatibilityVersion

dependencies {
implementation project(':balance')
implementation Libraries.kotlin

// Testing
Expand Down
68 changes: 68 additions & 0 deletions morph/common/src/main/java/com/blockchain/morph/CoinPair.kt
@@ -0,0 +1,68 @@
package com.blockchain.morph

import com.blockchain.morph.CoinPair.BCH_TO_BCH
import com.blockchain.morph.CoinPair.BCH_TO_BTC
import com.blockchain.morph.CoinPair.BCH_TO_ETH
import com.blockchain.morph.CoinPair.BTC_TO_BCH
import com.blockchain.morph.CoinPair.BTC_TO_BTC
import com.blockchain.morph.CoinPair.BTC_TO_ETH
import com.blockchain.morph.CoinPair.ETH_TO_BCH
import com.blockchain.morph.CoinPair.ETH_TO_BTC
import com.blockchain.morph.CoinPair.ETH_TO_ETH
import info.blockchain.balance.CryptoCurrency

enum class CoinPair(
val pairCode: String,
val from: CryptoCurrency,
val to: CryptoCurrency
) {

BTC_TO_BTC("btc_btc", CryptoCurrency.BTC, CryptoCurrency.BTC),
BTC_TO_ETH("btc_eth", CryptoCurrency.BTC, CryptoCurrency.ETHER),
BTC_TO_BCH("btc_bch", CryptoCurrency.BTC, CryptoCurrency.BCH),

ETH_TO_ETH("eth_eth", CryptoCurrency.ETHER, CryptoCurrency.ETHER),
ETH_TO_BTC("eth_btc", CryptoCurrency.ETHER, CryptoCurrency.BTC),
ETH_TO_BCH("eth_bch", CryptoCurrency.ETHER, CryptoCurrency.BCH),

BCH_TO_BCH("bch_bch", CryptoCurrency.BCH, CryptoCurrency.BCH),
BCH_TO_BTC("bch_btc", CryptoCurrency.BCH, CryptoCurrency.BTC),
BCH_TO_ETH("bch_eth", CryptoCurrency.BCH, CryptoCurrency.ETHER);

val sameInputOutput = from == to

companion object {

fun fromPairCode(pairCode: String): CoinPair {
pairCode.split('_').let {
if (it.size == 2) {
val from = CryptoCurrency.fromSymbol(it.first())
val to = CryptoCurrency.fromSymbol(it.last())
if (from != null && to != null) {
return from to to
}
}
throw IllegalStateException("Attempt to get invalid pair $pairCode")
}
}
}
}

infix fun CryptoCurrency.to(other: CryptoCurrency) =
when (this) {
CryptoCurrency.BTC -> when (other) {
CryptoCurrency.BTC -> BTC_TO_BTC
CryptoCurrency.ETHER -> BTC_TO_ETH
CryptoCurrency.BCH -> BTC_TO_BCH
}
CryptoCurrency.ETHER -> when (other) {
CryptoCurrency.ETHER -> ETH_TO_ETH
CryptoCurrency.BTC -> ETH_TO_BTC
CryptoCurrency.BCH -> ETH_TO_BCH
}
CryptoCurrency.BCH -> when (other) {
CryptoCurrency.BCH -> BCH_TO_BCH
CryptoCurrency.BTC -> BCH_TO_BTC
CryptoCurrency.ETHER -> BCH_TO_ETH
}
}

0 comments on commit 351d51b

Please sign in to comment.