Adding latest blocks and transactions to dashboard.

This commit is contained in:
softsimon
2020-09-26 02:11:30 +07:00
parent aa3559e634
commit d4f768e3b6
10 changed files with 135 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { TransactionExtended } from '../interfaces';
import { Transaction, TransactionExtended, TransactionStripped } from '../interfaces';
export class Common {
static median(numbers: number[]) {
@@ -47,4 +47,13 @@ export class Common {
});
return matches;
}
static stripTransaction(tx: TransactionExtended): TransactionStripped {
return {
txid: tx.txid,
fee: tx.fee,
weight: tx.weight,
value: tx.vin.reduce((acc, vin) => acc + (vin.prevout ? vin.prevout.value : 0), 0),
};
}
}

View File

@@ -1,6 +1,7 @@
const config = require('../../mempool-config.json');
import bitcoinApi from './bitcoin/electrs-api';
import { MempoolInfo, TransactionExtended, Transaction, VbytesPerSecond } from '../interfaces';
import { Common } from './common';
class Mempool {
private inSync: boolean = false;
@@ -15,6 +16,7 @@ class Mempool {
private vBytesPerSecondArray: VbytesPerSecond[] = [];
private vBytesPerSecond: number = 0;
private mempoolProtection = 0;
private latestTransactions: any[] = [];
constructor() {
setInterval(this.updateTxPerSecond.bind(this), 1000);
@@ -24,6 +26,10 @@ class Mempool {
return this.inSync;
}
public getLatestTransactions() {
return this.latestTransactions;
}
public setMempoolChangedCallback(fn: (newMempool: { [txId: string]: TransactionExtended; },
newTransactions: TransactionExtended[], deletedTransactions: TransactionExtended[]) => void) {
this.mempoolChangedCallback = fn;
@@ -159,6 +165,9 @@ class Mempool {
newMempool = this.mempoolCache;
}
const newTransactionsStripped = newTransactions.map((tx) => Common.stripTransaction(tx));
this.latestTransactions = newTransactionsStripped.concat(this.latestTransactions).slice(0, 6);
if (!this.inSync && transactions.length === Object.keys(newMempool).length) {
this.inSync = true;
console.log('The mempool is now in sync!');

View File

@@ -88,6 +88,7 @@ class WebsocketHandler {
'blocks': _blocks.slice(Math.max(_blocks.length - config.INITIAL_BLOCK_AMOUNT, 0)),
'conversions': fiatConversion.getTickers()['BTCUSD'],
'mempool-blocks': mempoolBlocks.getMempoolBlocks(),
'transactions': memPool.getLatestTransactions(),
'git-commit': backendInfo.gitCommitHash,
'hostname': backendInfo.hostname,
...this.extraInitProperties
@@ -150,6 +151,7 @@ class WebsocketHandler {
if (client['want-stats']) {
response['mempoolInfo'] = mempoolInfo;
response['vBytesPerSecond'] = vBytesPerSecond;
response['transactions'] = newTransactions.splice(0, 6).map((tx) => Common.stripTransaction(tx));
}
if (client['want-mempool-blocks']) {