diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt index 8cc53f03..d4cc43f9 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt @@ -965,18 +965,19 @@ class Proton private constructor(context: Context) { val swapPoolJsonObject = it.asJsonObject val swapPool = gson.fromJson(swapPoolJsonObject, SwapPool::class.java) - - val pool1 = "${swapPool.getPool1Contract()}:${swapPool.getPool1Symbol()}" - val pool2 = "${swapPool.getPool2Contract()}:${swapPool.getPool2Symbol()}" - - tokenCurrencyBalancesMap[pool1]?.let { tokenCurrencyBalance -> - swapPoolTokens.add(tokenCurrencyBalance) + if (swapPool.active == 1) { + val pool1 = "${swapPool.getPool1Contract()}:${swapPool.getPool1Symbol()}" + val pool2 = "${swapPool.getPool2Contract()}:${swapPool.getPool2Symbol()}" + + tokenCurrencyBalancesMap[pool1]?.let { tokenCurrencyBalance -> + swapPoolTokens.add(tokenCurrencyBalance) + } + tokenCurrencyBalancesMap[pool2]?.let { tokenCurrencyBalance -> + swapPoolTokens.add(tokenCurrencyBalance) + } + + swapPools.add(swapPool) } - tokenCurrencyBalancesMap[pool2]?.let { tokenCurrencyBalance -> - swapPoolTokens.add(tokenCurrencyBalance) - } - - swapPools.add(swapPool) } } diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt index c652f762..0432bc6c 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/WorkersModule.kt @@ -94,13 +94,6 @@ class WorkersModule { .then(initTokenContracts) .enqueue() } - - // start periodic worker to update exchange rates - /*val updateTokenContractRates = PeriodicWorkRequest.Builder(UpdateTokenContractRatesWorker::class.java, 15L, TimeUnit.MINUTES) - .setConstraints(constraints) - .setInitialDelay(1L, TimeUnit.MINUTES) - .build() - workManager.enqueueUniquePeriodicWork(UPDATE_RATES, ExistingPeriodicWorkPolicy.REPLACE, updateTokenContractRates)*/ } fun onInitChainProvider(callback: (Boolean, Data?) -> Unit) { diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/di/WorkerModule.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/di/WorkerModule.kt index 0cc2a1f9..904fc9aa 100755 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/di/WorkerModule.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/di/WorkerModule.kt @@ -45,11 +45,6 @@ abstract class WorkerModule { @WorkerKey(InitActiveAccountWorker::class) abstract fun bindInitActiveAccountWorker(factory: InitActiveAccountWorker.Factory): ChildWorkerFactory - @Binds - @IntoMap - @WorkerKey(UpdateTokenContractRatesWorker::class) - abstract fun bindUpdateTokenContractRatesWorker(factory: UpdateTokenContractRatesWorker.Factory): ChildWorkerFactory - @Binds abstract fun bindWorkerFactory(factory: ProtonWorkerFactory): WorkerFactory } \ No newline at end of file diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/workers/ProtonWorkerFactory.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/workers/ProtonWorkerFactory.kt index 4813c935..57698612 100644 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/workers/ProtonWorkerFactory.kt +++ b/protonsdk/src/main/java/com/metallicus/protonsdk/workers/ProtonWorkerFactory.kt @@ -38,16 +38,14 @@ class ProtonWorkerFactory @Inject constructor( workerParameters: WorkerParameters ): ListenableWorker? { return try { - val factoryEntry = - workerFactory.entries.find { Class.forName(workerClassName).isAssignableFrom(it.key) } + val factoryEntry = workerFactory.entries.find { Class.forName(workerClassName).isAssignableFrom(it.key) } if (factoryEntry != null) { val factoryProvider = factoryEntry.value factoryProvider.get().create(context, workerParameters) } else { // fallback if no factory was found val workerClass = Class.forName(workerClassName).asSubclass(ListenableWorker::class.java) - val constructor = - workerClass.getDeclaredConstructor(Context::class.java, WorkerParameters::class.java) + val constructor = workerClass.getDeclaredConstructor(Context::class.java, WorkerParameters::class.java) constructor.newInstance(context, workerParameters) } } catch (e: ClassNotFoundException) { diff --git a/protonsdk/src/main/java/com/metallicus/protonsdk/workers/UpdateTokenContractRatesWorker.kt b/protonsdk/src/main/java/com/metallicus/protonsdk/workers/UpdateTokenContractRatesWorker.kt deleted file mode 100644 index 94d9c2e9..00000000 --- a/protonsdk/src/main/java/com/metallicus/protonsdk/workers/UpdateTokenContractRatesWorker.kt +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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.workers - -import android.content.Context -import androidx.work.CoroutineWorker -import androidx.work.WorkerParameters -import com.google.gson.Gson -import com.metallicus.protonsdk.common.Prefs -import com.metallicus.protonsdk.model.TokenContractRate -import com.metallicus.protonsdk.repository.ChainProviderRepository -import com.metallicus.protonsdk.repository.TokenContractRepository -import com.squareup.inject.assisted.Assisted -import com.squareup.inject.assisted.AssistedInject -import timber.log.Timber -import java.lang.Exception - -class UpdateTokenContractRatesWorker -@AssistedInject constructor( - @Assisted context: Context, - @Assisted params: WorkerParameters, - private val prefs: Prefs, - private val chainProviderRepository: ChainProviderRepository, - private val tokenContractRepository: TokenContractRepository -) : CoroutineWorker(context, params) { - - override suspend fun doWork(): Result { - return try { - val chainProvider = chainProviderRepository.getChainProvider(prefs.activeChainId) - - val tokenContracts = tokenContractRepository.getTokenContracts() - - val tokenContractsMap = mutableMapOf() - tokenContracts.forEach { - tokenContractsMap["${it.contract}:${it.getSymbol()}"] = it.id - } - - val exchangeRateUrl = chainProvider.protonChainUrl + chainProvider.exchangeRatePath - - val exchangeRatesResponse = tokenContractRepository.fetchExchangeRates(exchangeRateUrl) - if (exchangeRatesResponse.isSuccessful) { - val exchangeRatesJsonArray = exchangeRatesResponse.body() - exchangeRatesJsonArray?.forEach { - val exchangeRate = it.asJsonObject - val contract = exchangeRate.get("contract").asString - val symbol = exchangeRate.get("symbol").asString - val rank = exchangeRate.get("rank").asInt - - val ratesMap = mutableMapOf() - - 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 - } - - try { - val tokenContractId = tokenContractsMap.getValue("$contract:$symbol") - tokenContractRepository.updateRates(tokenContractId, ratesMap, rank) - } catch (e: Exception) { - Timber.d(e.localizedMessage) - } - } - } - - Result.success() - } catch (e: Exception) { - Timber.d(e) - - Result.failure() - } - } - - @AssistedInject.Factory - interface Factory : ChildWorkerFactory -} \ No newline at end of file