Skip to content

Commit

Permalink
Add Somnium Space coin.
Browse files Browse the repository at this point in the history
Fix minor bug when creating widget.
New schema for coin/exchange json file.
  • Loading branch information
hwki committed Nov 14, 2021
1 parent 763238c commit ee5265a
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 26 deletions.
4 changes: 2 additions & 2 deletions bitcoin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "com.brentpanther.bitcoinwidget"
minSdk 23
targetSdk 31
versionCode 285
versionName "8.1.5"
versionCode 286
versionName "8.1.6"

javaCompileOptions {
annotationProcessorOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum class Coin(val coinName: String, vararg val themes: IconTheme) : Parcelable
COMP("Compound", IconTheme(SOLID, ic_comp_black, ic_comp_white)),
CRO("Crypto.com Coin", IconTheme(SOLID, ic_cro, ic_cro_white)),
CRV("Curve DAO Token", IconTheme(SOLID, ic_crv)),
CUBE("Somnium Space CUBEs", IconTheme(SOLID, ic_cube_black, ic_cube_white)),
DAI("Dai", IconTheme(SOLID, ic_dai_color)),
DASH("Dash", IconTheme(SOLID, ic_dash, ic_dash_dark)),
DCR("Decred", IconTheme(SOLID, ic_dcr)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ enum class Exchange(val exchangeName: String, shortName: String? = null) {
"COMP" to "compound-coin",
"CRO" to "crypto-com-chain",
"CRV" to "curve-dao-token",
"CUBE" to "somnium-space-cubes",
"DAI" to "dai",
"DASH" to "dash",
"DCR" to "decred",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ open class ExchangeData(val coinEntry: CoinEntry, json: InputStream) {

lateinit var name: String
lateinit var coins: List<JsonCoin>
@SerializedName("currency_overrides")
@SerializedName("ccy_ovr")
var currencyOverrides: Map<String, String>? = null
@SerializedName("coin_overrides")
@SerializedName("c_ovr")
var coinOverrides: Map<String, String>? = null

fun loadExchange(coin: String): List<String> {
Expand All @@ -78,7 +78,10 @@ open class ExchangeData(val coinEntry: CoinEntry, json: InputStream) {

class JsonCoin : Serializable {
lateinit var name: String
lateinit var currencies: List<String>
@SerializedName("ccy")
var currencies: List<String> = listOf()
get() = field + all
var all: List<String> = listOf()
}

fun getExchanges(currency: String): Array<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SettingsPriceFragment : SettingsFragment() {
currency = data.defaultCurrency!!,
coinCustomId = if (data.coinEntry.coin == Coin.CUSTOM) data.coinEntry.id else null,
coinCustomName = if (data.coinEntry.coin == Coin.CUSTOM) data.coinEntry.name else null,
currencyCustomName = null,
currencyCustomName = data.getExchangeCurrencyName(defaultExchange, data.defaultCurrency!!),
showExchangeLabel = false,
showCoinLabel = false,
showIcon = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SettingsValueFragment : SettingsFragment() {
currency = data.defaultCurrency!!,
coinCustomId = if (data.coinEntry.coin == Coin.CUSTOM) data.coinEntry.id else null,
coinCustomName = if (data.coinEntry.coin == Coin.CUSTOM) data.coinEntry.name else null,
currencyCustomName = null,
currencyCustomName = data.getExchangeCurrencyName(defaultExchange, data.defaultCurrency!!),
showExchangeLabel = false,
showCoinLabel = false,
showIcon = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class SettingsViewModel(app: Application) : AndroidViewModel(app) {
return if (java.io.File(context.filesDir, Repository.CURRENCY_FILE_NAME).exists()) {
context.openFileInput(Repository.CURRENCY_FILE_NAME)
} else {
context.resources.openRawResource(R.raw.cryptowidgetcoins)
context.resources.openRawResource(R.raw.cryptowidgetcoins_v2)
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions bitcoin/src/main/res/raw/cryptowidgetcoins_v2.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bitcoin/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
<item>ISO</item>
<item>NONE</item>
</string-array>
<string name="json_last_modified" translatable="false">Sat, 06 Nov 2021 15:27:56 GMT</string>
<string name="json_url" translatable="false">https://www.brentpanther.com/cryptowidgetcoins.json</string>
<string name="json_last_modified" translatable="false">Sun, 14 Nov 2021 18:25:48 GMT</string>
<string name="json_url" translatable="false">https://www.brentpanther.com/cryptowidgetcoins_v2.json</string>

<string name="settings_updating_data">Updating coin data…</string>
<string name="error_no_currencies">No currencies/exchanges found for this coin!</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.*
class ExchangeTest {

private fun loadJSON(): InputStream {
return ClassLoader.getSystemResourceAsStream("raw/cryptowidgetcoins.json")
return ClassLoader.getSystemResourceAsStream("raw/cryptowidgetcoins_v2.json")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GeneratePartiallySupportedCoins {
private val configuration = Configuration.defaultConfiguration().mappingProvider(GsonMappingProvider())
private val allSupportedCoins = Coin.values().filterNot { it == Coin.CUSTOM }.map { it.name }

private val fullySupportedCoinsJsonPath = JsonPath.parse(ClassLoader.getSystemResourceAsStream("raw/cryptowidgetcoins.json"))
private val fullySupportedCoinsJsonPath = JsonPath.parse(ClassLoader.getSystemResourceAsStream("raw/cryptowidgetcoins_v2.json"))

class CoinGeckoCoin(val id: String, val symbol: String, val name: String, var icon: String, val score: Double) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GenerateSupportedCoinsJson {

private val allCoins = Coin.values().filterNot { it == Coin.CUSTOM }.map { it.name }
private var allCurrencies = setOf<String>()
private val json = JsonPath.parse(ClassLoader.getSystemResourceAsStream("raw/cryptowidgetcoins.json"))
private val json = JsonPath.parse(ClassLoader.getSystemResourceAsStream("raw/cryptowidgetcoins_v2.json"))
private val allCoinOverrides = mapOf("BCHABC" to "BCH", "BCC" to "BCH", "BCHSV" to "BSV", "XBT" to "BTC",
"XDG" to "DOGE", "MIOTA" to "IOTA", "STR" to "XLM", "DSH" to "DASH", "IOT" to "IOTA",
"BAB" to "BCH", "ALG" to "ALGO", "ATO" to "ATOM", "QTM" to "QTUM", "DRK" to "DASH", "NEM" to "XEM",
Expand All @@ -21,7 +21,7 @@ class GenerateSupportedCoinsJson {

@Test
fun generate() {
allCurrencies = (json.read("$..currencies.*") as List<String>).toSortedSet()
allCurrencies = (json.read("$..ccy.*") as List<String>).toSortedSet()

val exchanges =
listOf(this::abucoins, this::ascendex, this::bibox, this::bigone, this::binance, this::binance_us, this::bit2c,
Expand All @@ -48,9 +48,6 @@ class GenerateSupportedCoinsJson {
val currencyOverrides = mutableMapOf<String, String>()
val existing = getExistingPairs(name.name)
var pairs = extractOverrides(normalize(exchange.invoke()), coinOverrides, currencyOverrides)
pairs = pairs.filterNot {
it.substringBefore("_") == it.substringAfter("_")
}
val removed = existing.minus(pairs).sorted()
if (removed.isNotEmpty()) {
System.err.println("$name: Removed: ${removed.joinToString()}")
Expand All @@ -68,6 +65,9 @@ class GenerateSupportedCoinsJson {
jsonExchanges.add(json.read("$.exchanges[?(@.name=='$name')]", List::class.java)[0] as Map<*, *>)
}
}
for(exchange in jsonExchanges) {
reduceExchangeMap(exchange)
}
jsonMap["exchanges"] = jsonExchanges
println(Gson().toJson(jsonMap))
println("Potential coins to add:")
Expand All @@ -76,6 +76,30 @@ class GenerateSupportedCoinsJson {
}
}

@Suppress("UNCHECKED_CAST")
private fun reduceExchangeMap(exchange: Map<*, *>) {
val exchangeMap = exchange as MutableMap<String, Any>
// loop through coins, find common ones to put in "all" field
var allCurrencies : MutableList<String>? = null
for (coinMap in exchangeMap["coins"] as List<MutableMap<String, *>>) {
val currencies = coinMap["ccy"] as List<String>
if (allCurrencies == null) {
allCurrencies = currencies.sorted().toMutableList()
continue
}
// remove any currencies in all that are not in this coin
allCurrencies.removeAll { !currencies.contains(it) || coinMap["name"] as String == it }
}
if (allCurrencies != null) {
exchangeMap["all"] = allCurrencies
for (coinMap in exchangeMap["coins"] as List<MutableMap<String, List<String>>>) {
val currencies = coinMap["ccy"] as List<String>
coinMap["ccy"] = currencies.subtract(allCurrencies).toList()
}
}

}

private fun otherCoins(pairs: Sequence<String>, map: MutableMap<String, Int>) {
pairs.map{ it.split("_")[0] }.distinct().filterNot { allCoins.contains(it) }.forEach {
map[it] = map.getOrDefault(it, 0) + 1
Expand All @@ -91,13 +115,13 @@ class GenerateSupportedCoinsJson {
it.split("_")
}.groupBy( { it[0] }, {it[1]}).toSortedMap()
map["coins"] = pairsMap.map {
mapOf("name" to it.key, "currencies" to it.value.sorted())
mapOf("name" to it.key, "ccy" to it.value.sorted())
}
if (currencyOverrides.isNotEmpty()) {
map["currency_overrides"] = currencyOverrides.toSortedMap()
map["ccy_ovr"] = currencyOverrides.toSortedMap()
}
if (coinOverrides.isNotEmpty()) {
map["coin_overrides"] = coinOverrides.toSortedMap()
map["c_ovr"] = coinOverrides.toSortedMap()
}
return map
}
Expand Down Expand Up @@ -143,7 +167,7 @@ class GenerateSupportedCoinsJson {
}
}
}
}.map { it.joinToString("_") }
}.map { it.joinToString("_") }.distinct()
}

private fun normalize(pairs: List<String>): List<String> {
Expand All @@ -163,9 +187,6 @@ class GenerateSupportedCoinsJson {
} else {
it
}
}.filterNot {
// remove pairs with same symbols
it[0] == it[1]
}.map {
it[0] + "_" + it[1]
}.toList()
Expand All @@ -176,9 +197,13 @@ class GenerateSupportedCoinsJson {
val existing = json.read("$.exchanges[?(@.name=='${name}')]", List::class.java).firstOrNull() as? Map<String, *>
?: return listOf()
val coins = existing["coins"] as List<Map<String, *>>
val allCoins = existing["all"] as? List<String> ?: emptyList()
return coins.map { coin ->
(coin["currencies"] as List<String>).map { currency ->
"${coin["name"].toString()}_$currency"
val coinName = coin["name"].toString()
(coin["ccy"] as List<String>).plus(allCoins).filterNot {
coinName == it
}.map { currency ->
"${coinName}_$currency"
}
}.flatten().sorted()
}
Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/285.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added Bitrue exchange.
Updated Bithumb Pro to Bitglobal.
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/286.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added Somnium Space coin.
Fixed some minor bugs.

0 comments on commit ee5265a

Please sign in to comment.