From 5845f2380e9939fdb7d486d35ddce92574542265 Mon Sep 17 00:00:00 2001 From: softsimon Date: Tue, 21 Dec 2021 02:00:50 +0400 Subject: [PATCH] Adding sync external assets feature. --- backend/.gitignore | 6 ++++-- backend/mempool-config.sample.json | 3 ++- backend/src/api/liquid/icons.ts | 8 ++++---- backend/src/config.ts | 2 ++ backend/src/index.ts | 7 +++++++ backend/src/sync-assets.ts | 32 ++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 backend/src/sync-assets.ts diff --git a/backend/.gitignore b/backend/.gitignore index 40572a888..5476d633c 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,7 +1,9 @@ # See http://help.github.com/ignore-files/ for more about ignoring files. -# production config -mempool-config.json +# production config and external assets +*.json +!mempool-config.sample.json + icons.json # compiled output diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index 6ff103bf1..ea656c1de 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -13,7 +13,8 @@ "INITIAL_BLOCKS_AMOUNT": 8, "MEMPOOL_BLOCKS_AMOUNT": 8, "PRICE_FEED_UPDATE_INTERVAL": 3600, - "USE_SECOND_NODE_FOR_MINFEE": false + "USE_SECOND_NODE_FOR_MINFEE": false, + "EXTERNAL_ASSETS": [] }, "CORE_RPC": { "HOST": "127.0.0.1", diff --git a/backend/src/api/liquid/icons.ts b/backend/src/api/liquid/icons.ts index 82ef66697..1c7c658af 100644 --- a/backend/src/api/liquid/icons.ts +++ b/backend/src/api/liquid/icons.ts @@ -6,10 +6,10 @@ class Icons { private static FILE_NAME = './icons.json'; private iconIds: string[] = []; private icons: { [assetId: string]: string; } = {}; - constructor() { - if (config.MEMPOOL.NETWORK !== 'liquid') { - return; - } + + constructor() {} + + public loadIcons() { if (!fs.existsSync(Icons.FILE_NAME)) { logger.warn(`${Icons.FILE_NAME} does not exist. No Liquid icons loaded.`); return; diff --git a/backend/src/config.ts b/backend/src/config.ts index 2addc176a..9513324d8 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -16,6 +16,7 @@ interface IConfig { MEMPOOL_BLOCKS_AMOUNT: number; PRICE_FEED_UPDATE_INTERVAL: number; USE_SECOND_NODE_FOR_MINFEE: boolean; + EXTERNAL_ASSETS: string[]; }; ESPLORA: { REST_API_URL: string; @@ -78,6 +79,7 @@ const defaults: IConfig = { 'MEMPOOL_BLOCKS_AMOUNT': 8, 'PRICE_FEED_UPDATE_INTERVAL': 3600, 'USE_SECOND_NODE_FOR_MINFEE': false, + 'EXTERNAL_ASSETS': [], }, 'ESPLORA': { 'REST_API_URL': 'http://127.0.0.1:3000', diff --git a/backend/src/index.ts b/backend/src/index.ts index 98a55ee18..d1bd86994 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -21,6 +21,8 @@ import backendInfo from './api/backend-info'; import loadingIndicators from './api/loading-indicators'; import mempool from './api/mempool'; import elementsParser from './api/liquid/elements-parser'; +import syncAssets from './sync-assets'; +import icons from './api/liquid/icons'; class Server { private wss: WebSocket.Server | undefined; @@ -77,6 +79,7 @@ class Server { this.setUpWebsocketHandling(); + await syncAssets.syncAssets(); diskCache.loadMempoolCache(); if (config.DATABASE.ENABLED) { @@ -87,6 +90,10 @@ class Server { statistics.startStatistics(); } + if (config.MEMPOOL.NETWORK === 'liquid') { + icons.loadIcons(); + } + fiatConversion.startService(); this.setUpHttpApiRoutes(); diff --git a/backend/src/sync-assets.ts b/backend/src/sync-assets.ts new file mode 100644 index 000000000..302196458 --- /dev/null +++ b/backend/src/sync-assets.ts @@ -0,0 +1,32 @@ +import axios from 'axios'; +import * as fs from 'fs'; +const fsPromises = fs.promises; +import config from './config'; +import logger from './logger'; + +const PATH = './'; + +class SyncAssets { + constructor() { } + + public async syncAssets() { + for (const url of config.MEMPOOL.EXTERNAL_ASSETS) { + await this.downloadFile(url); + } + } + + private async downloadFile(url: string) { + const fileName = url.split('/').slice(-1)[0]; + logger.info(`Downloading external asset: ${fileName}...`); + try { + const response = await axios.get(url, { + responseType: 'stream', timeout: 30000 + }); + await fsPromises.writeFile(PATH + fileName, response.data); + } catch (e: any) { + throw new Error(`Failed to download external asset. ` + e); + } + } +} + +export default new SyncAssets();