Adding types to callback functions.
This commit is contained in:
parent
912d45165b
commit
0177224eba
@ -1,13 +1,13 @@
|
|||||||
const config = require('../../mempool-config.json');
|
const config = require('../../mempool-config.json');
|
||||||
import bitcoinApi from './bitcoin/electrs-api';
|
import bitcoinApi from './bitcoin/electrs-api';
|
||||||
import memPool from './mempool';
|
import memPool from './mempool';
|
||||||
import { Block, TransactionExtended } from '../interfaces';
|
import { Block, TransactionExtended, TransactionMinerInfo } from '../interfaces';
|
||||||
import { Common } from './common';
|
import { Common } from './common';
|
||||||
|
|
||||||
class Blocks {
|
class Blocks {
|
||||||
private blocks: Block[] = [];
|
private blocks: Block[] = [];
|
||||||
private currentBlockHeight = 0;
|
private currentBlockHeight = 0;
|
||||||
private newBlockCallback: Function = () => {};
|
private newBlockCallback: ((block: Block, txIds: string[], transactions: TransactionExtended[]) => void) | undefined;
|
||||||
|
|
||||||
constructor() { }
|
constructor() { }
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ class Blocks {
|
|||||||
this.blocks = blocks;
|
this.blocks = blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setNewBlockCallback(fn: Function) {
|
public setNewBlockCallback(fn: (block: Block, txIds: string[], transactions: TransactionExtended[]) => void) {
|
||||||
this.newBlockCallback = fn;
|
this.newBlockCallback = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,15 +83,17 @@ class Blocks {
|
|||||||
this.blocks.shift();
|
this.blocks.shift();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.newBlockCallback) {
|
||||||
this.newBlockCallback(block, txIds, transactions);
|
this.newBlockCallback(block, txIds, transactions);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('updateBlocks error', err);
|
console.log('updateBlocks error', err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private stripCoinbaseTransaction(tx: TransactionExtended): any {
|
private stripCoinbaseTransaction(tx: TransactionExtended): TransactionMinerInfo {
|
||||||
return {
|
return {
|
||||||
vin: [{
|
vin: [{
|
||||||
scriptsig: tx.vin[0].scriptsig
|
scriptsig: tx.vin[0].scriptsig
|
||||||
|
@ -6,7 +6,8 @@ class Mempool {
|
|||||||
private inSync: boolean = false;
|
private inSync: boolean = false;
|
||||||
private mempoolCache: { [txId: string]: TransactionExtended } = {};
|
private mempoolCache: { [txId: string]: TransactionExtended } = {};
|
||||||
private mempoolInfo: MempoolInfo = { size: 0, bytes: 0 };
|
private mempoolInfo: MempoolInfo = { size: 0, bytes: 0 };
|
||||||
private mempoolChangedCallback: Function | undefined;
|
private mempoolChangedCallback: ((newMempool: { [txId: string]: TransactionExtended; }, newTransactions: TransactionExtended[],
|
||||||
|
deletedTransactions: TransactionExtended[]) => void) | undefined;
|
||||||
|
|
||||||
private txPerSecondArray: number[] = [];
|
private txPerSecondArray: number[] = [];
|
||||||
private txPerSecond: number = 0;
|
private txPerSecond: number = 0;
|
||||||
@ -22,7 +23,8 @@ class Mempool {
|
|||||||
return this.inSync;
|
return this.inSync;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setMempoolChangedCallback(fn: Function) {
|
public setMempoolChangedCallback(fn: (newMempool: { [txId: string]: TransactionExtended; },
|
||||||
|
newTransactions: TransactionExtended[], deletedTransactions: TransactionExtended[]) => void) {
|
||||||
this.mempoolChangedCallback = fn;
|
this.mempoolChangedCallback = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +124,7 @@ class Mempool {
|
|||||||
|
|
||||||
// Replace mempool to clear deleted transactions
|
// Replace mempool to clear deleted transactions
|
||||||
const newMempool = {};
|
const newMempool = {};
|
||||||
const deletedTransactions: Transaction[] = [];
|
const deletedTransactions: TransactionExtended[] = [];
|
||||||
for (const tx in this.mempoolCache) {
|
for (const tx in this.mempoolCache) {
|
||||||
if (transactions.indexOf(tx) > -1) {
|
if (transactions.indexOf(tx) > -1) {
|
||||||
newMempool[tx] = this.mempoolCache[tx];
|
newMempool[tx] = this.mempoolCache[tx];
|
||||||
|
@ -5,9 +5,9 @@ import { Statistic, TransactionExtended, OptimizedStatistic } from '../interface
|
|||||||
|
|
||||||
class Statistics {
|
class Statistics {
|
||||||
protected intervalTimer: NodeJS.Timer | undefined;
|
protected intervalTimer: NodeJS.Timer | undefined;
|
||||||
protected newStatisticsEntryCallback: Function | undefined;
|
protected newStatisticsEntryCallback: ((stats: OptimizedStatistic) => void) | undefined;
|
||||||
|
|
||||||
public setNewStatisticsEntryCallback(fn: Function) {
|
public setNewStatisticsEntryCallback(fn: (stats: OptimizedStatistic) => void) {
|
||||||
this.newStatisticsEntryCallback = fn;
|
this.newStatisticsEntryCallback = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,9 +128,11 @@ class Statistics {
|
|||||||
|
|
||||||
if (this.newStatisticsEntryCallback && insertId) {
|
if (this.newStatisticsEntryCallback && insertId) {
|
||||||
const newStats = await this.$get(insertId);
|
const newStats = await this.$get(insertId);
|
||||||
|
if (newStats) {
|
||||||
this.newStatisticsEntryCallback(newStats);
|
this.newStatisticsEntryCallback(newStats);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async $create(statistics: Statistic): Promise<number | undefined> {
|
private async $create(statistics: Statistic): Promise<number | undefined> {
|
||||||
try {
|
try {
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
const config = require('../../mempool-config.json');
|
const config = require('../../mempool-config.json');
|
||||||
|
|
||||||
import * as WebSocket from 'ws';
|
import * as WebSocket from 'ws';
|
||||||
import { Block, TransactionExtended, Statistic, WebsocketResponse, MempoolBlock } from '../interfaces';
|
import { Block, TransactionExtended, WebsocketResponse, MempoolBlock, OptimizedStatistic } from '../interfaces';
|
||||||
import blocks from './blocks';
|
import blocks from './blocks';
|
||||||
import memPool from './mempool';
|
import memPool from './mempool';
|
||||||
import backendInfo from './backend-info';
|
import backendInfo from './backend-info';
|
||||||
import mempoolBlocks from './mempool-blocks';
|
import mempoolBlocks from './mempool-blocks';
|
||||||
import fiatConversion from './fiat-conversion';
|
import fiatConversion from './fiat-conversion';
|
||||||
import * as os from 'os';
|
|
||||||
import { Common } from './common';
|
import { Common } from './common';
|
||||||
|
|
||||||
class WebsocketHandler {
|
class WebsocketHandler {
|
||||||
@ -102,7 +101,7 @@ class WebsocketHandler {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleNewStatistic(stats: Statistic) {
|
handleNewStatistic(stats: OptimizedStatistic) {
|
||||||
if (!this.wss) {
|
if (!this.wss) {
|
||||||
throw new Error('WebSocket.Server is not set');
|
throw new Error('WebSocket.Server is not set');
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,19 @@ export interface Transaction {
|
|||||||
status: Status;
|
status: Status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface TransactionMinerInfo {
|
||||||
|
vin: VinStrippedToScriptsig[];
|
||||||
|
vout: VoutStrippedToScriptPubkey[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface VinStrippedToScriptsig {
|
||||||
|
scriptsig: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface VoutStrippedToScriptPubkey {
|
||||||
|
scriptpubkey_address: string | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export interface TransactionExtended extends Transaction {
|
export interface TransactionExtended extends Transaction {
|
||||||
vsize: number;
|
vsize: number;
|
||||||
feePerVsize: number;
|
feePerVsize: number;
|
||||||
@ -109,7 +122,7 @@ export interface Block {
|
|||||||
medianFee?: number;
|
medianFee?: number;
|
||||||
feeRange?: number[];
|
feeRange?: number[];
|
||||||
reward?: number;
|
reward?: number;
|
||||||
coinbaseTx?: Transaction;
|
coinbaseTx?: TransactionMinerInfo;
|
||||||
matchRate?: number;
|
matchRate?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user