From 452375aaf7aadd3fc0f6350d4a306fa6d4c62adb Mon Sep 17 00:00:00 2001 From: Felipe Knorr Kuhn Date: Fri, 4 Feb 2022 22:48:16 -0800 Subject: [PATCH] Make the Currency Conversion Service URLs configurable and log when queried --- README.md | 14 ++++++++++++++ backend/mempool-config.sample.json | 4 ++++ backend/src/api/fiat-conversion.ts | 23 ++++++++++++++++++----- backend/src/config.ts | 10 ++++++++++ docker/backend/mempool-config.json | 4 ++++ docker/backend/start.sh | 7 +++++++ 6 files changed, 57 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 151edfab7..c50282311 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,20 @@ docker-compose overrides: SOCKS5PROXY_PASSWORD: "" ``` +JSON: +``` + "PRICENODE": { + "TOR_URL": "http://wizpriceje6q5tdrxkyiazsgu7irquiqjy2dptezqhrtu7l2qelqktid.onion/getAllMarketPrices", + "CLEARNET_URL": "https://price.bisq.wiz.biz/getAllMarketPrices" + } +``` + +docker-compose overrides: +``` + PRICENODE_TOR_URL: "" + PRICENODE_CLEARNET_URL: "" +``` + # Manual Installation The following instructions are for a manual installation on Linux or FreeBSD. The file and directory paths may need to be changed to match your OS. diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index e977d8046..1b874c665 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -68,5 +68,9 @@ "PORT": 9050, "USERNAME": "", "PASSWORD": "" + }, + "PRICENODE": { + "TOR_URL": "http://wizpriceje6q5tdrxkyiazsgu7irquiqjy2dptezqhrtu7l2qelqktid.onion/getAllMarketPrices", + "CLEARNET_URL": "https://price.bisq.wiz.biz/getAllMarketPrices" } } diff --git a/backend/src/api/fiat-conversion.ts b/backend/src/api/fiat-conversion.ts index e6b44300a..df10d7698 100644 --- a/backend/src/api/fiat-conversion.ts +++ b/backend/src/api/fiat-conversion.ts @@ -1,5 +1,5 @@ import logger from '../logger'; -import axios from 'axios'; +import axios, { AxiosResponse } from 'axios'; import { IConversionRates } from '../mempool.interfaces'; import config from '../config'; import backendInfo from './backend-info'; @@ -20,7 +20,9 @@ class FiatConversion { public startService() { logger.info('Starting currency rates service'); if (config.SOCKS5PROXY.ENABLED) { - logger.info('Currency rates service will be queried over the Tor network'); + logger.info(`Currency rates service will be queried over the Tor network using ${config.PRICENODE.TOR_URL}`); + } else { + logger.info(`Currency rates service will be queried over clearnet using ${config.PRICENODE.CLEARNET_URL}`); } setInterval(this.updateCurrency.bind(this), 1000 * config.MEMPOOL.PRICE_FEED_UPDATE_INTERVAL); this.updateCurrency(); @@ -32,9 +34,10 @@ class FiatConversion { private async updateCurrency(): Promise { const headers = { 'User-Agent': `mempool/v${backendInfo.getBackendInfo().version}` }; - let response; + let fiatConversionUrl: string; + let response: AxiosResponse; + try { - let fiatConversionUrl = 'https://price.bisq.wiz.biz/getAllMarketPrices'; if (config.SOCKS5PROXY.ENABLED) { let socksOptions: any = { agentOptions: { @@ -43,20 +46,30 @@ class FiatConversion { host: config.SOCKS5PROXY.HOST, port: config.SOCKS5PROXY.PORT }; + if (config.SOCKS5PROXY.USERNAME && config.SOCKS5PROXY.PASSWORD) { socksOptions.username = config.SOCKS5PROXY.USERNAME; socksOptions.password = config.SOCKS5PROXY.PASSWORD; } + const agent = new SocksProxyAgent(socksOptions); - fiatConversionUrl = 'http://wizpriceje6q5tdrxkyiazsgu7irquiqjy2dptezqhrtu7l2qelqktid.onion/getAllMarketPrices'; + fiatConversionUrl = config.PRICENODE.TOR_URL; + logger.info('Querying currency rates service...'); response = await axios.get(fiatConversionUrl, { httpAgent: agent, headers: headers, timeout: 30000 }); } else { + fiatConversionUrl = config.PRICENODE.CLEARNET_URL; + logger.info('Querying currency rates service...'); response = await axios.get(fiatConversionUrl, { headers: headers, timeout: 10000 }); } + const usd = response.data.data.find((item: any) => item.currencyCode === 'USD'); + this.conversionRates = { 'USD': usd.price, }; + + logger.info(`USD Conversion Rate: ${usd.price}`); + if (this.ratesChangedCallback) { this.ratesChangedCallback(this.conversionRates); } diff --git a/backend/src/config.ts b/backend/src/config.ts index e610e9f47..058895efb 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -69,6 +69,10 @@ interface IConfig { USERNAME: string; PASSWORD: string; }; + PRICENODE: { + TOR_URL: string; + CLEARNET_URL: string; + }; } const defaults: IConfig = { @@ -141,6 +145,10 @@ const defaults: IConfig = { 'PORT': 9050, 'USERNAME': '', 'PASSWORD': '' + }, + "PRICENODE": { + 'TOR_URL': 'http://wizpriceje6q5tdrxkyiazsgu7irquiqjy2dptezqhrtu7l2qelqktid.onion/getAllMarketPrices', + 'CLEARNET_URL': 'https://price.bisq.wiz.biz/getAllMarketPrices' } }; @@ -155,6 +163,7 @@ class Config implements IConfig { STATISTICS: IConfig['STATISTICS']; BISQ: IConfig['BISQ']; SOCKS5PROXY: IConfig['SOCKS5PROXY']; + PRICENODE: IConfig['PRICENODE']; constructor() { const configs = this.merge(configFile, defaults); @@ -168,6 +177,7 @@ class Config implements IConfig { this.STATISTICS = configs.STATISTICS; this.BISQ = configs.BISQ; this.SOCKS5PROXY = configs.SOCKS5PROXY; + this.PRICENODE = configs.PRICENODE; } merge = (...objects: object[]): IConfig => { diff --git a/docker/backend/mempool-config.json b/docker/backend/mempool-config.json index 788ab6505..5a069455d 100644 --- a/docker/backend/mempool-config.json +++ b/docker/backend/mempool-config.json @@ -65,5 +65,9 @@ "PORT": "__SOCKS5PROXY_PORT__", "USERNAME": "__SOCKS5PROXY_USERNAME__", "PASSWORD": "__SOCKS5PROXY_PASSWORD__" + }, + "PRICENODE": { + "TOR_URL": "__PRICENODE_TOR_URL__", + "CLEARNET_URL": "__PRICENODE_CLEARNET_URL__" } } diff --git a/docker/backend/start.sh b/docker/backend/start.sh index 1d1155a66..0ae1e32a2 100644 --- a/docker/backend/start.sh +++ b/docker/backend/start.sh @@ -68,6 +68,10 @@ __SOCKS5PROXY_PORT__=${SOCKS5PROXY_PORT:=9050} __SOCKS5PROXY_USERNAME__=${SOCKS5PROXY_USERNAME:=""} __SOCKS5PROXY_PASSWORD__=${SOCKS5PROXY_PASSWORD:=""} +# PRICENODE +__PRICENODE_TOR_URL__=${PRICENODE_TOR_URL:=http://wizpriceje6q5tdrxkyiazsgu7irquiqjy2dptezqhrtu7l2qelqktid.onion/getAllMarketPrices} +__PRICENODE_CLEARNET_URL__=${PRICENODE_CLEARNET_URL:=https://price.bisq.wiz.biz/getAllMarketPrices} + mkdir -p "${__MEMPOOL_CACHE_DIR__}" sed -i "s/__MEMPOOL_NETWORK__/${__MEMPOOL_NETWORK__}/g" mempool-config.json @@ -128,4 +132,7 @@ sed -i "s/__SOCKS5PROXY_PORT__/${__SOCKS5PROXY_PORT__}/g" mempool-config.json sed -i "s/__SOCKS5PROXY_USERNAME__/${__SOCKS5PROXY_USERNAME__}/g" mempool-config.json sed -i "s/__SOCKS5PROXY_PASSWORD__/${__SOCKS5PROXY_PASSWORD__}/g" mempool-config.json +sed -i "s!__PRICENODE_TOR_URL__!${__PRICENODE_TOR_URL__}!g" mempool-config.json +sed -i "s!__PRICENODE_CLEARNET_URL__!${__PRICENODE_CLEARNET_URL__}!g" mempool-config.json + node /backend/dist/index.js