diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/CurrencyBalancesModule.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/CurrencyBalancesModule.kt index ebb6dd36..39a70b21 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/CurrencyBalancesModule.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/CurrencyBalancesModule.kt @@ -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, @@ -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 @@ -157,7 +161,7 @@ class CurrencyBalancesModule { val tokenCurrencyBalance = TokenCurrencyBalance(tokenContract, currencyBalance) tokenCurrencyBalances.add(tokenCurrencyBalance) } - } + }*/ } Resource.success(tokenCurrencyBalances) diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt index d4cc43f9..701516ef 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt @@ -221,6 +221,37 @@ class Proton private constructor(context: Context) { } } + fun getMarketContracts(updateExchangeRates: Boolean = false): LiveData>> = 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> = Resource.error(e) + emit(error) + } catch (e: Exception) { + val error: Resource> = Resource.error(e.localizedMessage.orEmpty()) + emit(error) + } + } + fun generatePrivateKey(): Pair { val privateKey = EosPrivateKey() val publicKeyStr = privateKey.publicKey.toString() diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/api/ProtonChainService.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/api/ProtonChainService.kt index 951172c1..06c721c4 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/api/ProtonChainService.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/api/ProtonChainService.kt @@ -123,12 +123,6 @@ interface ProtonChainService { @Url url: String, @Body body: JsonToBinBody): Response - @POST//("/v1/chain/get_info") - suspend fun getChainInfo(@Url url: String): Response - - @GET//("/v2/health") - suspend fun getHealth(@Url url: String): Response - @POST//("/v1/chain/get_required_keys") suspend fun getRequiredKeys( @Url url: String, diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/api/ProtonChainStatsService.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/api/ProtonChainStatsService.kt new file mode 100644 index 00000000..42c89c51 --- /dev/null +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/api/ProtonChainStatsService.kt @@ -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 + + @GET//("/v2/health") + suspend fun getHealth(@Url url: String): Response +} \ No newline at end of file diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/di/ProtonModule.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/di/ProtonModule.kt index 45eed0fe..dcff80f4 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/di/ProtonModule.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/di/ProtonModule.kt @@ -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.* @@ -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 { diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/repository/ChainProviderRepository.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/repository/ChainProviderRepository.kt index b7ce9f80..c62287e4 100755 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/repository/ChainProviderRepository.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/repository/ChainProviderRepository.kt @@ -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 @@ -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() @@ -67,11 +65,11 @@ class ChainProviderRepository @Inject constructor( } suspend fun getChainInfo(chainUrl: String): Response { - return protonChainService.getChainInfo("$chainUrl/v1/chain/get_info") + return protonChainStatsService.getChainInfo("$chainUrl/v1/chain/get_info") } suspend fun getHealth(chainUrl: String): Response { - return protonChainService.getHealth("$chainUrl/v2/health") + return protonChainStatsService.getHealth("$chainUrl/v2/health") } suspend fun getAbi(chainUrl: String, accountName: String): Response {