Skip to content

Commit

Permalink
Merge pull request #26 from ProtonProtocol/develop
Browse files Browse the repository at this point in the history
Update Exchange Rates Route
  • Loading branch information
Joey Harward authored Apr 8, 2021
2 parents aa805f2 + 120c8d6 commit f9ec500
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Then add the following dependency to your module's build.gradle
```gradle
dependencies {
...
implementation "com.metallicus:protonsdk:0.9.3"
implementation "com.metallicus:protonsdk:0.9.4"
}
```

Expand Down
10 changes: 5 additions & 5 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
const val kotlinVersion = "1.4.30"
const val kotlinVersion = "1.4.32"
const val orchidVersion = "0.21.1"

object ProtonSdk {
const val versionCode = 36
const val versionName = "0.9.3"
const val versionCode = 37
const val versionName = "0.9.4"
}

object BuildPlugins {
object Versions {
const val gradle = "4.1.2"
const val gradle = "4.1.3"
const val dokka = "0.10.1" // TODO: 1.4.0
const val bintray = "1.8.5"
}
Expand All @@ -48,7 +48,7 @@ object Android {
const val minSdk = 21
const val compileSdk = 30
const val targetSdk = compileSdk
const val buildTools = "31.0.0-rc01"
const val buildTools = "31.0.0-rc02"

object Progaurd {
const val consumeFile = "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class CurrencyBalancesModule {
iconUrl = "",
precisionSymbol = precisionSymbol,
blacklisted = 0)
tokenContract.rates = mapOf(Pair("USD", 0.0))
tokenContract.rates = mapOf(Pair("USD", TokenContractRate()))

tokenContractRepository.addTokenContract(tokenContract)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
package com.metallicus.protonsdk

import android.content.Context
import com.google.gson.Gson
import com.metallicus.protonsdk.common.Prefs
import com.metallicus.protonsdk.di.DaggerInjector
import com.metallicus.protonsdk.model.ChainProvider
import com.metallicus.protonsdk.model.TokenContract
import com.metallicus.protonsdk.model.TokenContractRate
import com.metallicus.protonsdk.repository.TokenContractRepository
import timber.log.Timber
import javax.inject.Inject
Expand Down Expand Up @@ -63,12 +66,21 @@ class TokenContractsModule {
val exchangeRate = it.asJsonObject
val contract = exchangeRate.get("contract").asString
val symbol = exchangeRate.get("symbol").asString
val rates = exchangeRate.get("rates").asJsonObject
val priceChangePercent = exchangeRate.get("priceChangePercent").asDouble
val rank = exchangeRate.get("rank").asInt

val ratesMap = mutableMapOf<String, TokenContractRate>()

val ratesJsonArray = exchangeRate.get("rates").asJsonArray
ratesJsonArray.forEach { rateJsonElement ->
val rateJsonObj = rateJsonElement.asJsonObject
val currency = rateJsonObj.get("counterCurrency").asString
val rate = Gson().fromJson(rateJsonObj, TokenContractRate::class.java)
ratesMap[currency] = rate
}

val tokenContractId = tokenContractIdsMap.getOrElse("$contract:$symbol") { null }
if (tokenContractId != null) {
tokenContractRepository.updateRates(tokenContractId, rates.toString(), priceChangePercent)
tokenContractRepository.updateRates(tokenContractId, ratesMap, rank)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interface ProtonChainService {
@GET//("/v1/chain/info")
suspend fun getChainProvider(@Url url: String): Response<JsonObject>

@GET//("/v1/chain/exchange-rates")
@GET//("/v1/chain/exchange-rates/info")
suspend fun getExchangeRates(@Url url: String): Response<JsonArray>

@PUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,4 @@ object DefaultTypeConverters {
fun stringListToString(strings: List<String>?): String? {
return strings?.joinToString(",")
}

@TypeConverter
@JvmStatic
fun stringToStringDoubleMap(value: String?): Map<String, Double>? {
val type = object : TypeToken<Map<String, Double>>() {}.type
return Gson().fromJson(value, type)
}

@TypeConverter
@JvmStatic
fun stringDoubleMapToString(map: Map<String, Double>): String {
val type = object : TypeToken<Map<String, Double>>() {}.type
return Gson().toJson(map, type)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package com.metallicus.protonsdk.db

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.metallicus.protonsdk.model.*

@Database(
Expand All @@ -34,9 +35,10 @@ import com.metallicus.protonsdk.model.*
CurrencyBalance::class,
Action::class,
ESRSession::class],
version = 30,
version = 31,
exportSchema = false
)
@TypeConverters(DefaultTypeConverters::class, EOSTypeConverters::class, ProtonTypeConverters::class)
abstract class ProtonDb : RoomDatabase() {
abstract fun chainProviderDao(): ChainProviderDao
abstract fun tokenContractDao(): TokenContractDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,18 @@ object ProtonTypeConverters {
val type = object : TypeToken<List<ChainUrlInfo>>() {}.type
return Gson().toJson(value, type)
}

@TypeConverter
@JvmStatic
fun stringToStringTokenContractRateMap(value: String?): Map<String, TokenContractRate>? {
val type = object : TypeToken<Map<String, TokenContractRate>>() {}.type
return Gson().fromJson(value, type)
}

@TypeConverter
@JvmStatic
fun stringTokenContractRateMapToString(map: Map<String, TokenContractRate>): String {
val type = object : TypeToken<Map<String, TokenContractRate>>() {}.type
return Gson().toJson(map, type)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package com.metallicus.protonsdk.db

import androidx.room.*
import com.metallicus.protonsdk.model.TokenContract
import com.metallicus.protonsdk.model.TokenContractRate

/**
* Interface for database access for [TokenContract] related operations
Expand All @@ -41,8 +42,8 @@ interface TokenContractDao {
@Query("SELECT * FROM tokenContract")
suspend fun findAll(): List<TokenContract>

@Query("UPDATE tokenContract SET rates = :rates, priceChangePercent = :priceChangePercent WHERE id = :tokenContractId")
suspend fun updateRates(tokenContractId: String, rates: String, priceChangePercent: Double)
@Query("UPDATE tokenContract SET rates = :rates, rank = :rank WHERE id = :tokenContractId")
suspend fun updateRates(tokenContractId: String, rates: Map<String, TokenContractRate>, rank: Int)

@Query("DELETE FROM tokenContract")
suspend fun removeAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ package com.metallicus.protonsdk.model

import androidx.room.*
import com.google.gson.annotations.SerializedName
import com.metallicus.protonsdk.db.DefaultTypeConverters
import com.metallicus.protonsdk.db.EOSTypeConverters
import com.metallicus.protonsdk.db.ProtonTypeConverters

@Entity
@TypeConverters(DefaultTypeConverters::class, EOSTypeConverters::class, ProtonTypeConverters::class)
data class Account(
@PrimaryKey
@SerializedName("account_name") val accountName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ package com.metallicus.protonsdk.model

import android.util.Base64
import androidx.annotation.NonNull
import androidx.room.TypeConverters
import com.metallicus.protonsdk.db.DefaultTypeConverters

//@Entity(
// indices = [(Index("id", "accountName"))],
// primaryKeys = ["id", "accountName"]
//)
@TypeConverters(DefaultTypeConverters::class)
data class AccountContact(
val id: String, // accountName
var name: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ import androidx.annotation.NonNull
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.Index
import androidx.room.TypeConverters
import com.google.gson.annotations.SerializedName
import com.metallicus.protonsdk.db.DefaultTypeConverters
import com.metallicus.protonsdk.db.EOSTypeConverters
import com.metallicus.protonsdk.db.ProtonTypeConverters
import java.text.NumberFormat
import java.util.*
import kotlin.math.sign
Expand All @@ -42,7 +38,6 @@ import kotlin.math.sign
))],
primaryKeys = ["accountName", "action_trace_trxId", "action_trace_act_name", "action_trace_act_authorization"]
)
@TypeConverters(DefaultTypeConverters::class, EOSTypeConverters::class, ProtonTypeConverters::class)
data class Action(
@SerializedName("global_action_seq") val globalActionSeq: Int,
@SerializedName("block_num") val blockNum: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ package com.metallicus.protonsdk.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.google.gson.annotations.SerializedName
import com.metallicus.protonsdk.db.DefaultTypeConverters
import com.metallicus.protonsdk.db.ProtonTypeConverters

@Entity
@TypeConverters(DefaultTypeConverters::class, ProtonTypeConverters::class)
data class ChainProvider(
@PrimaryKey
@SerializedName("chainId") val chainId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ package com.metallicus.protonsdk.model
import androidx.annotation.NonNull
import androidx.room.*
import com.google.gson.annotations.SerializedName
import com.metallicus.protonsdk.db.DefaultTypeConverters

@Entity(
indices = [(Index("tokenContractId", "accountName", "contract", "symbol"))],
primaryKeys = ["tokenContractId", "accountName"])
@TypeConverters(DefaultTypeConverters::class)
data class CurrencyBalance(
@SerializedName("contract") val contract: String,
@SerializedName("symbol") val symbol: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ package com.metallicus.protonsdk.model
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.RoomWarnings
import com.google.gson.annotations.SerializedName

@SuppressWarnings(RoomWarnings.PRIMARY_KEY_FROM_EMBEDDED_IS_DROPPED)
@Entity
class ESRSession(
@PrimaryKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
*/
package com.metallicus.protonsdk.model

import androidx.room.TypeConverters
import com.google.gson.annotations.SerializedName
import com.metallicus.protonsdk.db.DefaultTypeConverters

@TypeConverters(DefaultTypeConverters::class)
data class JsonToBinResponse(
@SerializedName("binargs") val binArgs: String,
@SerializedName("required_scope") val requiredScope: List<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
*/
package com.metallicus.protonsdk.model

import androidx.room.TypeConverters
import com.google.gson.annotations.SerializedName
import com.metallicus.protonsdk.db.DefaultTypeConverters

@TypeConverters(DefaultTypeConverters::class)
data class KeyAccount(
@SerializedName("account_names") val accountNames: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
*/
package com.metallicus.protonsdk.model

import androidx.room.TypeConverters
import com.google.gson.annotations.SerializedName
import com.metallicus.protonsdk.db.DefaultTypeConverters

@TypeConverters(DefaultTypeConverters::class)
class RequiredKeysResponse(
@SerializedName("required_keys") val requiredKeys: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ package com.metallicus.protonsdk.model

import androidx.room.*
import com.google.gson.annotations.SerializedName
import com.metallicus.protonsdk.db.DefaultTypeConverters
import java.text.NumberFormat
import java.util.*

@Entity
@TypeConverters(DefaultTypeConverters::class)
data class TokenContract(
@PrimaryKey
@SerializedName("id") var id: String,
Expand All @@ -41,8 +39,8 @@ data class TokenContract(
@SerializedName("symbol") val precisionSymbol: String,
@SerializedName("blisted") val blacklisted: Int,
var isSystemToken: Boolean = false,
var rates: Map<String, Double> = mapOf(Pair("USD", 0.0)),
var priceChangePercent: Double = 0.0
var rank: Int = 0,
var rates: Map<String, TokenContractRate> = mapOf(Pair("USD", TokenContractRate())),
) {
// lateinit var supply: String
// lateinit var maxSupply: String
Expand All @@ -59,9 +57,9 @@ data class TokenContract(

fun getRate(currency: String): Double {
return if (rates.contains(currency)) {
rates.getOrElse(currency) { 0.0 }
rates[currency]?.price ?: 0.0
} else {
rates.getOrElse("USD") { 0.0 }
rates["USD"]?.price ?: 0.0
}
}

Expand Down Expand Up @@ -92,11 +90,28 @@ data class TokenContract(
return nf.format(amountCurrency)
}

fun formatPriceChangePercent(): String {
fun getPriceChangePercent(currency: String): Double {
return if (rates.contains(currency)) {
rates[currency]?.priceChangePercent ?: 0.0
} else {
rates["USD"]?.priceChangePercent ?: 0.0
}
}

fun formatPriceChangePercent(currency: String): String {
val priceChangePercent = getPriceChangePercent(currency)
return if (priceChangePercent <= 0.0) {
"$priceChangePercent%"
} else {
"+$priceChangePercent%"
}
}

fun getMarketCap(currency: String): Double {
return if (rates.contains(currency)) {
rates[currency]?.marketCap ?: 0.0
} else {
rates["USD"]?.marketCap ?: 0.0
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020 Proton Chain LLC, Delaware
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.metallicus.protonsdk.model

import com.google.gson.annotations.SerializedName

data class TokenContractRate(
@SerializedName("price") val price: Double = 0.0,
@SerializedName("priceChangePercent") val priceChangePercent: Double = 0.0,
@SerializedName("marketCap") val marketCap: Double = 0.0,
@SerializedName("volume") val volume: Double = 0.0,
@SerializedName("timestamp") val timestamp: Long = 0L
)
Loading

0 comments on commit f9ec500

Please sign in to comment.