Skip to content

Commit

Permalink
optimized chain provider initialization
Browse files Browse the repository at this point in the history
fixed bugs due to new token contracts
  • Loading branch information
Joey Harward committed May 19, 2021
1 parent 51abe7e commit e5d9e22
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class CurrencyBalancesModule {
}
}

private fun isValidEmptyToken(contract: String): Boolean {
return (contract == "eosio.token" || contract == "xtokens")
}

suspend fun getTokenCurrencyBalances(
hyperionHistoryUrl: String,
accountName: String,
Expand Down Expand Up @@ -121,14 +125,14 @@ class CurrencyBalancesModule {

val tokenCurrencyBalance = TokenCurrencyBalance(tokenContract, currencyBalance)
tokenCurrencyBalances.add(tokenCurrencyBalance)
} else if (addEmptyTokens) {
} else if (addEmptyTokens && isValidEmptyToken(contract)) { // only add valid empty tokens
val tokenCurrencyBalance = TokenCurrencyBalance(tokenContract, currencyBalance)
tokenCurrencyBalances.add(tokenCurrencyBalance)
}
}

// add custom tokens
currencyBalancesMap.forEach { currencyBalancesMapEntry ->
/*currencyBalancesMap.forEach { currencyBalancesMapEntry ->
val token = currencyBalancesMapEntry.value.asJsonObject
val contract = token.get("contract").asString
val symbol = token.get("symbol").asString
Expand Down Expand Up @@ -157,7 +161,7 @@ class CurrencyBalancesModule {
val tokenCurrencyBalance = TokenCurrencyBalance(tokenContract, currencyBalance)
tokenCurrencyBalances.add(tokenCurrencyBalance)
}
}
}*/
}

Resource.success(tokenCurrencyBalances)
Expand Down
31 changes: 31 additions & 0 deletions protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,37 @@ class Proton private constructor(context: Context) {
}
}

fun getMarketContracts(updateExchangeRates: Boolean = false): LiveData<Resource<List<TokenContract>>> = liveData {
emit(Resource.loading())

try {
val tokenContracts = getTokenContractsAsync()

val marketContracts = tokenContracts.filter {
it.contract == "eosio.token" || it.contract == "xtokens"
}

if (updateExchangeRates) {
val activeAccount = getActiveAccountAsync()

val tokenContractsMap = marketContracts.associateBy { "${it.contract}:${it.getSymbol()}" }

val exchangeRateUrl =
activeAccount.chainProvider.protonChainUrl + activeAccount.chainProvider.exchangeRatePath

tokenContractsModule.updateExchangeRates(exchangeRateUrl, tokenContractsMap)
}

emit(Resource.success(marketContracts))
} catch (e: ProtonException) {
val error: Resource<List<TokenContract>> = Resource.error(e)
emit(error)
} catch (e: Exception) {
val error: Resource<List<TokenContract>> = Resource.error(e.localizedMessage.orEmpty())
emit(error)
}
}

fun generatePrivateKey(): Pair<String, String> {
val privateKey = EosPrivateKey()
val publicKeyStr = privateKey.publicKey.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ interface ProtonChainService {
@Url url: String,
@Body body: JsonToBinBody): Response<JsonToBinResponse>

@POST//("/v1/chain/get_info")
suspend fun getChainInfo(@Url url: String): Response<ChainInfo>

@GET//("/v2/health")
suspend fun getHealth(@Url url: String): Response<JsonObject>

@POST//("/v1/chain/get_required_keys")
suspend fun getRequiredKeys(
@Url url: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2021 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.api

import com.google.gson.JsonObject
import com.metallicus.protonsdk.model.*
import retrofit2.Response
import retrofit2.http.*

interface ProtonChainStatsService {
@POST//("/v1/chain/get_info")
suspend fun getChainInfo(@Url url: String): Response<ChainInfo>

@GET//("/v2/health")
suspend fun getHealth(@Url url: String): Response<JsonObject>
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.google.gson.GsonBuilder
import com.metallicus.protonsdk.R
import com.metallicus.protonsdk.api.ESRCallbackService
import com.metallicus.protonsdk.api.ProtonChainService
import com.metallicus.protonsdk.api.ProtonChainStatsService
import com.metallicus.protonsdk.common.SecureKeys
import com.metallicus.protonsdk.common.Prefs
import com.metallicus.protonsdk.db.*
Expand Down Expand Up @@ -93,6 +94,33 @@ class ProtonModule {
return db.esrSessionDao()
}

@Singleton
@Provides
fun provideProtonChainStatsService(context: Context): ProtonChainStatsService {
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY

val httpClient = OkHttpClient.Builder()
.callTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.addInterceptor(logging)

val gson = GsonBuilder()
.registerTypeAdapterFactory(GsonEosTypeAdapterFactory())
.serializeNulls()
// .excludeFieldsWithoutExposeAnnotation()
.create()

return Retrofit.Builder()
.baseUrl(context.getString(R.string.defaultProtonChainUrl))
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build()
.create(ProtonChainStatsService::class.java)
}

@Singleton
@Provides
fun provideProtonChainService(context: Context): ProtonChainService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
package com.metallicus.protonsdk.repository

import com.google.gson.JsonObject
import com.metallicus.protonsdk.api.AccountBody
import com.metallicus.protonsdk.api.ProtonChainService
import com.metallicus.protonsdk.api.TableRowsBody
import com.metallicus.protonsdk.api.TableRowsIndexPosition
import com.metallicus.protonsdk.api.*
import com.metallicus.protonsdk.db.ChainProviderDao
import com.metallicus.protonsdk.model.ChainInfo
import com.metallicus.protonsdk.model.ChainProvider
Expand All @@ -36,7 +33,8 @@ import javax.inject.Singleton
@Singleton
class ChainProviderRepository @Inject constructor(
private val chainProviderDao: ChainProviderDao,
private val protonChainService: ProtonChainService
private val protonChainService: ProtonChainService,
private val protonChainStatsService: ProtonChainStatsService
) {
suspend fun removeAll() {
chainProviderDao.removeAll()
Expand Down Expand Up @@ -67,11 +65,11 @@ class ChainProviderRepository @Inject constructor(
}

suspend fun getChainInfo(chainUrl: String): Response<ChainInfo> {
return protonChainService.getChainInfo("$chainUrl/v1/chain/get_info")
return protonChainStatsService.getChainInfo("$chainUrl/v1/chain/get_info")
}

suspend fun getHealth(chainUrl: String): Response<JsonObject> {
return protonChainService.getHealth("$chainUrl/v2/health")
return protonChainStatsService.getHealth("$chainUrl/v2/health")
}

suspend fun getAbi(chainUrl: String, accountName: String): Response<JsonObject> {
Expand Down

0 comments on commit e5d9e22

Please sign in to comment.