From a5099fed75477e1a342d14b9c146a7f46d5155cb Mon Sep 17 00:00:00 2001 From: natsoni Date: Sun, 10 Mar 2024 17:12:19 +0100 Subject: [PATCH] Add backend checks for enabling fiat prices and update config paths --- backend/src/index.ts | 14 ++++++++++---- backend/src/indexer.ts | 12 +++++++----- backend/src/repositories/PricesRepository.ts | 10 +++++----- backend/src/tasks/price-updater.ts | 8 ++++---- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/backend/src/index.ts b/backend/src/index.ts index 213319946..1988c7c56 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -131,7 +131,7 @@ class Server { .use(express.text({ type: ['text/plain', 'application/base64'] })) ; - if (config.DATABASE.ENABLED) { + if (config.DATABASE.ENABLED && config.FIAT_PRICE.ENABLED) { await priceUpdater.$initializeLatestPriceWithDb(); } @@ -168,7 +168,9 @@ class Server { setInterval(refreshIcons, 3600_000); } - priceUpdater.$run(); + if (config.FIAT_PRICE.ENABLED) { + priceUpdater.$run(); + } await chainTips.updateOrphanedBlocks(); this.setUpHttpApiRoutes(); @@ -220,7 +222,9 @@ class Server { await memPool.$updateMempool(newMempool, newAccelerations, pollRate); } indexer.$run(); - priceUpdater.$run(); + if (config.FIAT_PRICE.ENABLED) { + priceUpdater.$run(); + } // rerun immediately if we skipped the mempool update, otherwise wait POLL_RATE_MS const elapsed = Date.now() - start; @@ -284,7 +288,9 @@ class Server { memPool.setAsyncMempoolChangedCallback(websocketHandler.$handleMempoolChange.bind(websocketHandler)); blocks.setNewAsyncBlockCallback(websocketHandler.handleNewBlock.bind(websocketHandler)); } - priceUpdater.setRatesChangedCallback(websocketHandler.handleNewConversionRates.bind(websocketHandler)); + if (config.FIAT_PRICE.ENABLED) { + priceUpdater.setRatesChangedCallback(websocketHandler.handleNewConversionRates.bind(websocketHandler)); + } loadingIndicators.setProgressChangedCallback(websocketHandler.handleLoadingChanged.bind(websocketHandler)); } diff --git a/backend/src/indexer.ts b/backend/src/indexer.ts index dcb91d010..663a64c6d 100644 --- a/backend/src/indexer.ts +++ b/backend/src/indexer.ts @@ -116,7 +116,7 @@ class Indexer { switch (task) { case 'blocksPrices': { - if (!['testnet', 'signet'].includes(config.MEMPOOL.NETWORK)) { + if (!['testnet', 'signet'].includes(config.MEMPOOL.NETWORK) && config.FIAT_PRICE.ENABLED) { let lastestPriceId; try { lastestPriceId = await PricesRepository.$getLatestPriceId(); @@ -148,10 +148,12 @@ class Indexer { return; } - try { - await priceUpdater.$run(); - } catch (e) { - logger.err(`Running priceUpdater failed. Reason: ` + (e instanceof Error ? e.message : e)); + if (config.FIAT_PRICE.ENABLED) { + try { + await priceUpdater.$run(); + } catch (e) { + logger.err(`Running priceUpdater failed. Reason: ` + (e instanceof Error ? e.message : e)); + } } // Do not attempt to index anything unless Bitcoin Core is fully synced diff --git a/backend/src/repositories/PricesRepository.ts b/backend/src/repositories/PricesRepository.ts index e5acec861..f9498f313 100644 --- a/backend/src/repositories/PricesRepository.ts +++ b/backend/src/repositories/PricesRepository.ts @@ -40,7 +40,7 @@ export interface ApiPrice { ZAR: number, } -const ApiPriceFields = config.MEMPOOL.CURRENCY_API_KEY ? +const ApiPriceFields = config.FIAT_PRICE.API_KEY ? ` UNIX_TIMESTAMP(time) as time, USD, @@ -181,7 +181,7 @@ class PricesRepository { } try { - if (!config.MEMPOOL.CURRENCY_API_KEY) { // Store only the 7 main currencies + if (!config.FIAT_PRICE.API_KEY) { // Store only the 7 main currencies await DB.query(` INSERT INTO prices(time, USD, EUR, GBP, CAD, CHF, AUD, JPY) VALUE (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ? )`, @@ -330,7 +330,7 @@ class PricesRepository { } let pricesUsedForExchangeRates; // If we don't have a fx API key, we need to use the latest prices to compute the exchange rates - if (!config.MEMPOOL.CURRENCY_API_KEY) { + if (!config.FIAT_PRICE.API_KEY) { const [latestPrices] = await DB.query(` SELECT ${ApiPriceFields} FROM prices @@ -353,7 +353,7 @@ class PricesRepository { const computeFx = (usd: number, other: number): number => usd <= 0.05 ? 0 : Math.round(Math.max(other, 0) / usd * 100) / 100; - const exchangeRates: ExchangeRates = config.MEMPOOL.CURRENCY_API_KEY ? + const exchangeRates: ExchangeRates = config.FIAT_PRICE.API_KEY ? { USDEUR: computeFx(latestPrice.USD, latestPrice.EUR), USDGBP: computeFx(latestPrice.USD, latestPrice.GBP), @@ -443,7 +443,7 @@ class PricesRepository { const computeFx = (usd: number, other: number): number => usd <= 0 ? 0 : Math.round(Math.max(other, 0) / usd * 100) / 100; - const exchangeRates: ExchangeRates = config.MEMPOOL.CURRENCY_API_KEY ? + const exchangeRates: ExchangeRates = config.FIAT_PRICE.API_KEY ? { USDEUR: computeFx(latestPrice.USD, latestPrice.EUR), USDGBP: computeFx(latestPrice.USD, latestPrice.GBP), diff --git a/backend/src/tasks/price-updater.ts b/backend/src/tasks/price-updater.ts index f23879831..bfdce5f8c 100644 --- a/backend/src/tasks/price-updater.ts +++ b/backend/src/tasks/price-updater.ts @@ -70,7 +70,7 @@ class PriceUpdater { this.feeds.push(new BitfinexApi()); this.feeds.push(new GeminiApi()); - this.currencyConversionFeed = new FreeCurrencyApi(config.MEMPOOL.CURRENCY_API_KEY); + this.currencyConversionFeed = new FreeCurrencyApi(config.FIAT_PRICE.API_KEY); this.setCyclePosition(); } @@ -146,7 +146,7 @@ class PriceUpdater { this.additionalCurrenciesHistoryInserted = false; } - if (config.MEMPOOL.CURRENCY_API_KEY && this.currencyConversionFeed && (Math.round(new Date().getTime() / 1000) - this.lastTimeConversionsRatesFetched) > 3600 * 24) { + if (config.FIAT_PRICE.API_KEY && this.currencyConversionFeed && (Math.round(new Date().getTime() / 1000) - this.lastTimeConversionsRatesFetched) > 3600 * 24) { // Once a day, fetch conversion rates from api: we don't need more granularity for fiat currencies and have a limited number of requests try { this.latestConversionsRatesFromFeed = await this.currencyConversionFeed.$fetchLatestConversionRates(); @@ -162,7 +162,7 @@ class PriceUpdater { if (this.historyInserted === false && config.DATABASE.ENABLED === true) { await this.$insertHistoricalPrices(); } - if (this.additionalCurrenciesHistoryInserted === false && config.DATABASE.ENABLED === true && config.MEMPOOL.CURRENCY_API_KEY && !this.additionalCurrenciesHistoryRunning) { + if (this.additionalCurrenciesHistoryInserted === false && config.DATABASE.ENABLED === true && config.FIAT_PRICE.API_KEY && !this.additionalCurrenciesHistoryRunning) { await this.$insertMissingAdditionalPrices(); } } catch (e: any) { @@ -244,7 +244,7 @@ class PriceUpdater { } } - if (config.MEMPOOL.CURRENCY_API_KEY && this.latestPrices.USD > 0 && Object.keys(this.latestConversionsRatesFromFeed).length > 0) { + if (config.FIAT_PRICE.API_KEY && this.latestPrices.USD > 0 && Object.keys(this.latestConversionsRatesFromFeed).length > 0) { for (const conversionCurrency of this.newCurrencies) { if (this.latestConversionsRatesFromFeed[conversionCurrency] > 0 && this.latestPrices.USD * this.latestConversionsRatesFromFeed[conversionCurrency] < MAX_PRICES[conversionCurrency]) { this.latestPrices[conversionCurrency] = Math.round(this.latestPrices.USD * this.latestConversionsRatesFromFeed[conversionCurrency]);