Add backend checks for enabling fiat prices and update config paths

This commit is contained in:
natsoni 2024-03-10 17:12:19 +01:00
parent b11164005c
commit a5099fed75
No known key found for this signature in database
GPG Key ID: C65917583181743B
4 changed files with 26 additions and 18 deletions

View File

@ -131,7 +131,7 @@ class Server {
.use(express.text({ type: ['text/plain', 'application/base64'] })) .use(express.text({ type: ['text/plain', 'application/base64'] }))
; ;
if (config.DATABASE.ENABLED) { if (config.DATABASE.ENABLED && config.FIAT_PRICE.ENABLED) {
await priceUpdater.$initializeLatestPriceWithDb(); await priceUpdater.$initializeLatestPriceWithDb();
} }
@ -168,7 +168,9 @@ class Server {
setInterval(refreshIcons, 3600_000); setInterval(refreshIcons, 3600_000);
} }
priceUpdater.$run(); if (config.FIAT_PRICE.ENABLED) {
priceUpdater.$run();
}
await chainTips.updateOrphanedBlocks(); await chainTips.updateOrphanedBlocks();
this.setUpHttpApiRoutes(); this.setUpHttpApiRoutes();
@ -220,7 +222,9 @@ class Server {
await memPool.$updateMempool(newMempool, newAccelerations, pollRate); await memPool.$updateMempool(newMempool, newAccelerations, pollRate);
} }
indexer.$run(); indexer.$run();
priceUpdater.$run(); if (config.FIAT_PRICE.ENABLED) {
priceUpdater.$run();
}
// rerun immediately if we skipped the mempool update, otherwise wait POLL_RATE_MS // rerun immediately if we skipped the mempool update, otherwise wait POLL_RATE_MS
const elapsed = Date.now() - start; const elapsed = Date.now() - start;
@ -284,7 +288,9 @@ class Server {
memPool.setAsyncMempoolChangedCallback(websocketHandler.$handleMempoolChange.bind(websocketHandler)); memPool.setAsyncMempoolChangedCallback(websocketHandler.$handleMempoolChange.bind(websocketHandler));
blocks.setNewAsyncBlockCallback(websocketHandler.handleNewBlock.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)); loadingIndicators.setProgressChangedCallback(websocketHandler.handleLoadingChanged.bind(websocketHandler));
} }

View File

@ -116,7 +116,7 @@ class Indexer {
switch (task) { switch (task) {
case 'blocksPrices': { case 'blocksPrices': {
if (!['testnet', 'signet'].includes(config.MEMPOOL.NETWORK)) { if (!['testnet', 'signet'].includes(config.MEMPOOL.NETWORK) && config.FIAT_PRICE.ENABLED) {
let lastestPriceId; let lastestPriceId;
try { try {
lastestPriceId = await PricesRepository.$getLatestPriceId(); lastestPriceId = await PricesRepository.$getLatestPriceId();
@ -148,10 +148,12 @@ class Indexer {
return; return;
} }
try { if (config.FIAT_PRICE.ENABLED) {
await priceUpdater.$run(); try {
} catch (e) { await priceUpdater.$run();
logger.err(`Running priceUpdater failed. Reason: ` + (e instanceof Error ? e.message : e)); } 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 // Do not attempt to index anything unless Bitcoin Core is fully synced

View File

@ -40,7 +40,7 @@ export interface ApiPrice {
ZAR: number, ZAR: number,
} }
const ApiPriceFields = config.MEMPOOL.CURRENCY_API_KEY ? const ApiPriceFields = config.FIAT_PRICE.API_KEY ?
` `
UNIX_TIMESTAMP(time) as time, UNIX_TIMESTAMP(time) as time,
USD, USD,
@ -181,7 +181,7 @@ class PricesRepository {
} }
try { 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(` await DB.query(`
INSERT INTO prices(time, USD, EUR, GBP, CAD, CHF, AUD, JPY) INSERT INTO prices(time, USD, EUR, GBP, CAD, CHF, AUD, JPY)
VALUE (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ? )`, 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 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(` const [latestPrices] = await DB.query(`
SELECT ${ApiPriceFields} SELECT ${ApiPriceFields}
FROM prices 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 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), USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP), USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),
@ -443,7 +443,7 @@ class PricesRepository {
const computeFx = (usd: number, other: number): number => const computeFx = (usd: number, other: number): number =>
usd <= 0 ? 0 : Math.round(Math.max(other, 0) / usd * 100) / 100; 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), USDEUR: computeFx(latestPrice.USD, latestPrice.EUR),
USDGBP: computeFx(latestPrice.USD, latestPrice.GBP), USDGBP: computeFx(latestPrice.USD, latestPrice.GBP),

View File

@ -70,7 +70,7 @@ class PriceUpdater {
this.feeds.push(new BitfinexApi()); this.feeds.push(new BitfinexApi());
this.feeds.push(new GeminiApi()); 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(); this.setCyclePosition();
} }
@ -146,7 +146,7 @@ class PriceUpdater {
this.additionalCurrenciesHistoryInserted = false; 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 // 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 { try {
this.latestConversionsRatesFromFeed = await this.currencyConversionFeed.$fetchLatestConversionRates(); this.latestConversionsRatesFromFeed = await this.currencyConversionFeed.$fetchLatestConversionRates();
@ -162,7 +162,7 @@ class PriceUpdater {
if (this.historyInserted === false && config.DATABASE.ENABLED === true) { if (this.historyInserted === false && config.DATABASE.ENABLED === true) {
await this.$insertHistoricalPrices(); 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(); await this.$insertMissingAdditionalPrices();
} }
} catch (e: any) { } 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) { for (const conversionCurrency of this.newCurrencies) {
if (this.latestConversionsRatesFromFeed[conversionCurrency] > 0 && this.latestPrices.USD * this.latestConversionsRatesFromFeed[conversionCurrency] < MAX_PRICES[conversionCurrency]) { 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]); this.latestPrices[conversionCurrency] = Math.round(this.latestPrices.USD * this.latestConversionsRatesFromFeed[conversionCurrency]);