Skip to content

Commit

Permalink
Merge pull request #9 from ProtonProtocol/develop
Browse files Browse the repository at this point in the history
Added Staking/Refund Info to Account
  • Loading branch information
Joey Harward authored Sep 30, 2020
2 parents edf9a65 + a05cdc7 commit 3de12e9
Show file tree
Hide file tree
Showing 20 changed files with 396 additions and 85 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.5.6"
implementation "com.metallicus:protonsdk:0.6.0"
}
```

Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const val kotlinVersion = "1.4.10"
const val orchidVersion = "0.21.1"

object ProtonSdk {
const val versionCode = 18
const val versionName = "0.5.6"
const val versionCode = 21
const val versionName = "0.6.0"
}

object BuildPlugins {
Expand Down
95 changes: 89 additions & 6 deletions protonsdk/src/main/java/com/metallicus/protonsdk/AccountModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package com.metallicus.protonsdk

import android.content.Context
import android.util.Base64
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.metallicus.protonsdk.common.SecureKeys
import com.metallicus.protonsdk.common.Prefs
import com.metallicus.protonsdk.common.Resource
Expand All @@ -33,7 +35,6 @@ import com.metallicus.protonsdk.model.*
import com.metallicus.protonsdk.repository.AccountContactRepository
import com.metallicus.protonsdk.repository.AccountRepository
import timber.log.Timber
import java.nio.charset.Charset
import javax.inject.Inject

/**
Expand Down Expand Up @@ -122,16 +123,16 @@ class AccountModule {
val accountContact = AccountContact(accountName)
accountContact.accountName = accountName

val usersInfoTableScope = context.getString(R.string.protonChainUsersInfoTableScope)
val usersInfoTableCode = context.getString(R.string.protonChainUsersInfoTableCode)
val usersInfoTableName = context.getString(R.string.protonChainUsersInfoTableName)
val usersInfoTableScope = context.getString(R.string.usersInfoTableScope)
val usersInfoTableCode = context.getString(R.string.usersInfoTableCode)
val usersInfoTableName = context.getString(R.string.usersInfoTableName)

val response = accountContactRepository.fetchAccountContact(
chainUrl, accountName, usersInfoTableScope, usersInfoTableCode, usersInfoTableName)
if (response.isSuccessful) {
val userInfoJsonObject = response.body()
val userInfoRows = response.body()

val rows = userInfoJsonObject?.getAsJsonArray("rows")
val rows = userInfoRows?.getAsJsonArray("rows")
val size = rows?.size() ?: 0
if (size > 0) {
val userInfo = rows?.get(0)?.asJsonObject
Expand All @@ -152,15 +153,97 @@ class AccountModule {
return accountContact
}

private suspend fun fetchAccountVotersXPRInfo(chainUrl: String, accountName: String): AccountVotersXPRInfo {
var accountVotersXPRInfo = AccountVotersXPRInfo()

val votersXPRInfoTableScope = context.getString(R.string.votersXPRInfoTableScope)
val votersXPRInfoTableCode = context.getString(R.string.votersXPRInfoTableCode)
val votersXPRInfoTableName = context.getString(R.string.votersXPRInfoTableName)

val response = accountContactRepository.fetchAccountVotersXPRInfo(
chainUrl, accountName, votersXPRInfoTableScope, votersXPRInfoTableCode, votersXPRInfoTableName)
if (response.isSuccessful) {
val votersXPRInfoRows = response.body()

val rows = votersXPRInfoRows?.getAsJsonArray("rows")
val size = rows?.size() ?: 0
if (size > 0) {
val votersXPRInfo = rows?.get(0)?.asJsonObject

try {
accountVotersXPRInfo = Gson().fromJson(votersXPRInfo, AccountVotersXPRInfo::class.java)
} catch(e: JsonSyntaxException) {
Timber.e(e)
}
}
} else {
val msg = response.errorBody()?.string()
val errorMsg = if (msg.isNullOrEmpty()) {
response.message()
} else {
msg
}

Timber.e(errorMsg)
}

return accountVotersXPRInfo
}

private suspend fun fetchAccountRefundsXPRInfo(chainUrl: String, accountName: String): AccountRefundsXPRInfo {
var accountRefundsXPRInfo = AccountRefundsXPRInfo()

val refundsXPRInfoTableScope = accountName
val refundsXPRInfoTableCode = context.getString(R.string.refundsXPRInfoTableCode)
val refundsXPRInfoTableName = context.getString(R.string.refundsXPRInfoTableName)

val response = accountContactRepository.fetchAccountRefundsXPRInfo(
chainUrl, accountName, refundsXPRInfoTableScope, refundsXPRInfoTableCode, refundsXPRInfoTableName)
if (response.isSuccessful) {
val refundsXPRInfoRows = response.body()

val rows = refundsXPRInfoRows?.getAsJsonArray("rows")
val size = rows?.size() ?: 0
if (size > 0) {
val refundsXPRInfo = rows?.get(0)?.asJsonObject

try {
accountRefundsXPRInfo = Gson().fromJson(refundsXPRInfo, AccountRefundsXPRInfo::class.java)
} catch(e: JsonSyntaxException) {
Timber.e(e)
}
}
} else {
val msg = response.errorBody()?.string()
val errorMsg = if (msg.isNullOrEmpty()) {
response.message()
} else {
msg
}

Timber.d(errorMsg)
}

return accountRefundsXPRInfo
}

private suspend fun fetchAccount(chainId: String, chainUrl: String, accountName: String): Account? {
var account: Account? = null

val response = accountRepository.fetchAccount(chainUrl, accountName)
if (response.isSuccessful) {
response.body()?.let { it ->
it.accountChainId = chainId

// fetch contact info
it.accountContact = fetchAccountContact(chainUrl, accountName)

// fetch voter info
it.votersXPRInfo = fetchAccountVotersXPRInfo(chainUrl, accountName)

// fetch refund info
it.refundsXPRInfo = fetchAccountRefundsXPRInfo(chainUrl, accountName)

account = it
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ class ActionsModule {
accountContactRepository.addAccountContact(accountContact)
}

val usersInfoTableScope = context.getString(R.string.protonChainUsersInfoTableScope)
val usersInfoTableCode = context.getString(R.string.protonChainUsersInfoTableCode)
val usersInfoTableName = context.getString(R.string.protonChainUsersInfoTableName)
val usersInfoTableScope = context.getString(R.string.usersInfoTableScope)
val usersInfoTableCode = context.getString(R.string.usersInfoTableCode)
val usersInfoTableName = context.getString(R.string.usersInfoTableName)

val accountContactResponse = accountContactRepository.fetchAccountContact(chainUrl, accountContactId, usersInfoTableScope, usersInfoTableCode, usersInfoTableName)
if (accountContactResponse.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ class CurrencyBalancesModule {
val symbol = token.get("symbol").asString
val amount = token.get("amount").asString

val tokenContractId = tokenContractsMap.getValue("$code:$symbol")
if (tokenContractsMap.containsKey("$code:$symbol")) {
val tokenContractId = tokenContractsMap.getValue("$code:$symbol")

val currencyBalance = CurrencyBalance(code, symbol, amount)
currencyBalance.tokenContractId = tokenContractId
currencyBalance.accountName = accountName
val currencyBalance = CurrencyBalance(code, symbol, amount)
currencyBalance.tokenContractId = tokenContractId
currencyBalance.accountName = accountName

currencyBalanceRepository.addCurrencyBalance(currencyBalance)
currencyBalanceRepository.addCurrencyBalance(currencyBalance)
}
}

val tokenCurrencyBalances = currencyBalanceRepository.getTokenCurrencyBalances(accountName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface AccountDao {
@Update
suspend fun update(account: Account)

@Transaction
@Query("SELECT * FROM account WHERE accountName = :accountName")
suspend fun findByAccountName(accountName: String): ChainAccount

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ interface CurrencyBalanceDao {
@Query("UPDATE currencyBalance SET amount = :amount WHERE accountName = :accountName AND contract = :contract AND symbol = :symbol")
suspend fun updateAmount(accountName: String, contract: String, symbol: String, amount: String)

@Transaction
@Query("SELECT * FROM currencyBalance WHERE accountName = :accountName AND tokenContractId = :tokenContractId")
suspend fun findByTokenContract(accountName: String, tokenContractId: String): TokenCurrencyBalance

@Transaction
@Query("SELECT * FROM currencyBalance WHERE accountName = :accountName")
suspend fun findByAccountName(accountName: String): List<TokenCurrencyBalance>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import com.metallicus.protonsdk.model.*
AccountContact::class,
CurrencyBalance::class,
Action::class],
version = 17,
version = 19,
exportSchema = false
)
abstract class ProtonDb : RoomDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.metallicus.protonsdk.model.AccountContact
import com.metallicus.protonsdk.model.AccountRefundsXPRInfo
import com.metallicus.protonsdk.model.AccountVotersXPRInfo

object ProtonTypeConverters {
@TypeConverter
Expand All @@ -40,4 +42,32 @@ object ProtonTypeConverters {
val type = object : TypeToken<AccountContact>() {}.type
return Gson().toJson(accountContact, type)
}

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

@TypeConverter
@JvmStatic
fun accountVotersXPRInfoToString(accountVotersXPRInfo: AccountVotersXPRInfo): String {
val type = object : TypeToken<AccountVotersXPRInfo>() {}.type
return Gson().toJson(accountVotersXPRInfo, type)
}

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

@TypeConverter
@JvmStatic
fun accountRefundsXPRInfoToString(accountRefundsXPRInfo: AccountRefundsXPRInfo): String {
val type = object : TypeToken<AccountRefundsXPRInfo>() {}.type
return Gson().toJson(accountRefundsXPRInfo, type)
}
}
12 changes: 12 additions & 0 deletions protonsdk/src/main/java/com/metallicus/protonsdk/model/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ data class Account(

lateinit var accountContact: AccountContact

lateinit var votersXPRInfo: AccountVotersXPRInfo

lateinit var refundsXPRInfo: AccountRefundsXPRInfo

fun getBalance(): String {
return coreLiquidBalance ?: "0"
}
Expand Down Expand Up @@ -95,4 +99,12 @@ data class Account(
}
return selfDelegatedResources
}

fun getStakedXPR(): Double {
return votersXPRInfo.getStakedAmount()
}

fun getRefundsXPR(): Double {
return refundsXPRInfo.quantityToDouble()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 AccountRefundsXPRInfo(
@SerializedName("owner") val owner: String? = "",
@SerializedName("quantity") val quantity: String? = "",
@SerializedName("request_time") val requestTime: String? = ""
) {
fun quantityToDouble(): Double {
var quantityDouble = 0.0
if (quantity != "") {
quantityDouble = quantity?.substringBefore(" ")?.toDouble() ?: 0.0
}
return quantityDouble
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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
import com.metallicus.protonsdk.eosio.commander.model.types.TypeAsset
import com.metallicus.protonsdk.eosio.commander.model.types.TypeSymbol

data class AccountVotersXPRInfo(
@SerializedName("owner") val owner: String? = "",
@SerializedName("staked") val staked: Long? = 0,
@SerializedName("isqualified") val isQualified: Long? = 0,
@SerializedName("claimamount") val claimAmount: Long? = 0,
@SerializedName("lastclaim") val lastClaim: String? = ""
) {
fun getStakedAmount(): Double {
var stakedAmount = 0.0
if (staked != null) {
val stakedAsset = TypeAsset(staked, TypeSymbol.fromString("4,XPR"))
stakedAmount = stakedAsset.toString().substringBefore(" ").toDouble()
}
return stakedAmount
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ data class CurrencyBalance(
) {
lateinit var tokenContractId: String
lateinit var accountName: String

fun getAmountDouble(): Double {
return amount.toDouble()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ data class TokenContract(
@SerializedName("desc") val description: String,
@SerializedName("iconurl") val iconUrl: String,
@SerializedName("symbol") val precisionSymbol: String,
@SerializedName("blisted") val blacklisted: Int
@SerializedName("blisted") val blacklisted: Int,
var isSystemToken: Boolean = false
) {
lateinit var rates: Map<String, Double>

Expand Down
Loading

0 comments on commit 3de12e9

Please sign in to comment.