Skip to content

Commit

Permalink
added staking and refund info to account model
Browse files Browse the repository at this point in the history
updated version
  • Loading branch information
Joey Harward committed Sep 30, 2020
1 parent 99c0fb2 commit 816e29c
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 78 deletions.
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 = 20
const val versionName = "0.5.8"
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 @@ -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 816e29c

Please sign in to comment.