From 6ab79b3c357e44ff5105c6c51d50eabe675c762f Mon Sep 17 00:00:00 2001 From: softsimon Date: Fri, 31 Dec 2021 02:28:40 +0400 Subject: [PATCH] Refactoring the Liquid and LiquidTestnet check to a common function. --- backend/src/api/common.ts | 8 ++++++-- backend/src/api/fee-api.ts | 3 ++- backend/src/api/statistics.ts | 7 +++---- backend/src/api/transaction-utils.ts | 3 ++- backend/src/index.ts | 9 +++++---- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/backend/src/api/common.ts b/backend/src/api/common.ts index e58ffee34..5e99e870c 100644 --- a/backend/src/api/common.ts +++ b/backend/src/api/common.ts @@ -4,6 +4,11 @@ export class Common { static nativeAssetId = config.MEMPOOL.NETWORK === 'liquidtestnet' ? '144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a49' : '6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d'; + static _isLiquid = config.MEMPOOL.NETWORK === 'liquid' || config.MEMPOOL.NETWORK === 'liquidtestnet'; + + static isLiquid(): boolean { + return this._isLiquid; + } static median(numbers: number[]) { let medianNr = 0; @@ -109,8 +114,7 @@ export class Common { totalFees += tx.bestDescendant.fee; } - tx.effectiveFeePerVsize = Math.max(config.MEMPOOL.NETWORK === 'liquid' - || config.MEMPOOL.NETWORK === 'liquidtestnet' ? 0.1 : 1, totalFees / (totalWeight / 4)); + tx.effectiveFeePerVsize = Math.max(Common.isLiquid() ? 0.1 : 1, totalFees / (totalWeight / 4)); tx.cpfpChecked = true; return { diff --git a/backend/src/api/fee-api.ts b/backend/src/api/fee-api.ts index 14c6d7ed9..7bc7d06f3 100644 --- a/backend/src/api/fee-api.ts +++ b/backend/src/api/fee-api.ts @@ -1,12 +1,13 @@ import config from '../config'; import { MempoolBlock } from '../mempool.interfaces'; +import { Common } from './common'; import mempool from './mempool'; import projectedBlocks from './mempool-blocks'; class FeeApi { constructor() { } - defaultFee = config.MEMPOOL.NETWORK === 'liquid' || config.MEMPOOL.NETWORK === 'liquidtestnet' ? 0.1 : 1; + defaultFee = Common.isLiquid() ? 0.1 : 1; public getRecommendedFee() { const pBlocks = projectedBlocks.getMempoolBlocks(); diff --git a/backend/src/api/statistics.ts b/backend/src/api/statistics.ts index f4f2a75d4..ec5dd89e2 100644 --- a/backend/src/api/statistics.ts +++ b/backend/src/api/statistics.ts @@ -4,6 +4,7 @@ import logger from '../logger'; import { Statistic, TransactionExtended, OptimizedStatistic } from '../mempool.interfaces'; import config from '../config'; +import { Common } from './common'; class Statistics { protected intervalTimer: NodeJS.Timer | undefined; @@ -90,11 +91,9 @@ class Statistics { memPoolArray.forEach((transaction) => { for (let i = 0; i < logFees.length; i++) { if ( - ((config.MEMPOOL.NETWORK === 'liquid' || config.MEMPOOL.NETWORK === 'liquidtestnet') - && (i === lastItem || transaction.effectiveFeePerVsize * 10 < logFees[i + 1])) + (Common.isLiquid() && (i === lastItem || transaction.effectiveFeePerVsize * 10 < logFees[i + 1])) || - ((config.MEMPOOL.NETWORK !== 'liquid' && config.MEMPOOL.NETWORK !== 'liquidtestnet') - && (i === lastItem || transaction.effectiveFeePerVsize < logFees[i + 1])) + (!Common.isLiquid() && (i === lastItem || transaction.effectiveFeePerVsize < logFees[i + 1])) ) { if (weightVsizeFees[logFees[i]]) { weightVsizeFees[logFees[i]] += transaction.vsize; diff --git a/backend/src/api/transaction-utils.ts b/backend/src/api/transaction-utils.ts index d45e54ac8..1496b810b 100644 --- a/backend/src/api/transaction-utils.ts +++ b/backend/src/api/transaction-utils.ts @@ -2,6 +2,7 @@ import bitcoinApi from './bitcoin/bitcoin-api-factory'; import { TransactionExtended, TransactionMinerInfo } from '../mempool.interfaces'; import { IEsploraApi } from './bitcoin/esplora-api.interface'; import config from '../config'; +import { Common } from './common'; class TransactionUtils { constructor() { } @@ -31,7 +32,7 @@ class TransactionUtils { // @ts-ignore return transaction; } - const feePerVbytes = Math.max(config.MEMPOOL.NETWORK === 'liquid' || config.MEMPOOL.NETWORK === 'liquidtestnet' ? 0.1 : 1, + const feePerVbytes = Math.max(Common.isLiquid() ? 0.1 : 1, (transaction.fee || 0) / (transaction.weight / 4)); const transactionExtended: TransactionExtended = Object.assign({ vsize: Math.round(transaction.weight / 4), diff --git a/backend/src/index.ts b/backend/src/index.ts index 9ea4e0b05..f6fe237a2 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -24,6 +24,7 @@ import elementsParser from './api/liquid/elements-parser'; import databaseMigration from './api/database-migration'; import syncAssets from './sync-assets'; import icons from './api/liquid/icons'; +import { Common } from './api/common'; class Server { private wss: WebSocket.Server | undefined; @@ -96,7 +97,7 @@ class Server { statistics.startStatistics(); } - if (config.MEMPOOL.NETWORK === 'liquid' || config.MEMPOOL.NETWORK === 'liquidtestnet') { + if (Common.isLiquid()) { icons.loadIcons(); } @@ -156,7 +157,7 @@ class Server { if (this.wss) { websocketHandler.setWebsocketServer(this.wss); } - if ((config.MEMPOOL.NETWORK === 'liquid' || config.MEMPOOL.NETWORK === 'liquidtestnet') && config.DATABASE.ENABLED) { + if (Common.isLiquid() && config.DATABASE.ENABLED) { blocks.setNewBlockCallback(async () => { try { await elementsParser.$parse(); @@ -283,14 +284,14 @@ class Server { ; } - if (config.MEMPOOL.NETWORK === 'liquid' || config.MEMPOOL.NETWORK === 'liquidtestnet') { + if (Common.isLiquid()) { this.app .get(config.MEMPOOL.API_URL_PREFIX + 'assets/icons', routes.getAllLiquidIcon) .get(config.MEMPOOL.API_URL_PREFIX + 'asset/:assetId/icon', routes.getLiquidIcon) ; } - if ((config.MEMPOOL.NETWORK === 'liquid' || config.MEMPOOL.NETWORK === 'liquidtestnet') && config.DATABASE.ENABLED) { + if (Common.isLiquid() && config.DATABASE.ENABLED) { this.app .get(config.MEMPOOL.API_URL_PREFIX + 'liquid/pegs/month', routes.$getElementsPegsByMonth) ;