Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ShapeShift): AND-1249 Move some general types to Morph (#1063)
* 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
Showing
13 changed files
with
298 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
morph/common/src/main/java/com/blockchain/morph/CoinPair.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.