Adding types to callback functions.

This commit is contained in:
softsimon 2020-06-09 02:08:46 +07:00
parent 912d45165b
commit 0177224eba
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
5 changed files with 33 additions and 15 deletions

View File

@ -1,13 +1,13 @@
const config = require('../../mempool-config.json');
import bitcoinApi from './bitcoin/electrs-api';
import memPool from './mempool';
import { Block, TransactionExtended } from '../interfaces';
import { Block, TransactionExtended, TransactionMinerInfo } from '../interfaces';
import { Common } from './common';
class Blocks {
private blocks: Block[] = [];
private currentBlockHeight = 0;
private newBlockCallback: Function = () => {};
private newBlockCallback: ((block: Block, txIds: string[], transactions: TransactionExtended[]) => void) | undefined;
constructor() { }
@ -19,7 +19,7 @@ class Blocks {
this.blocks = blocks;
}
public setNewBlockCallback(fn: Function) {
public setNewBlockCallback(fn: (block: Block, txIds: string[], transactions: TransactionExtended[]) => void) {
this.newBlockCallback = fn;
}
@ -83,7 +83,9 @@ class Blocks {
this.blocks.shift();
}
this.newBlockCallback(block, txIds, transactions);
if (this.newBlockCallback) {
this.newBlockCallback(block, txIds, transactions);
}
}
} catch (err) {
@ -91,7 +93,7 @@ class Blocks {
}
}
private stripCoinbaseTransaction(tx: TransactionExtended): any {
private stripCoinbaseTransaction(tx: TransactionExtended): TransactionMinerInfo {
return {
vin: [{
scriptsig: tx.vin[0].scriptsig

View File

@ -6,7 +6,8 @@ class Mempool {
private inSync: boolean = false;
private mempoolCache: { [txId: string]: TransactionExtended } = {};
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 txPerSecond: number = 0;
@ -22,7 +23,8 @@ class Mempool {
return this.inSync;
}
public setMempoolChangedCallback(fn: Function) {
public setMempoolChangedCallback(fn: (newMempool: { [txId: string]: TransactionExtended; },
newTransactions: TransactionExtended[], deletedTransactions: TransactionExtended[]) => void) {
this.mempoolChangedCallback = fn;
}
@ -122,7 +124,7 @@ class Mempool {
// Replace mempool to clear deleted transactions
const newMempool = {};
const deletedTransactions: Transaction[] = [];
const deletedTransactions: TransactionExtended[] = [];
for (const tx in this.mempoolCache) {
if (transactions.indexOf(tx) > -1) {
newMempool[tx] = this.mempoolCache[tx];

View File

@ -5,9 +5,9 @@ import { Statistic, TransactionExtended, OptimizedStatistic } from '../interface
class Statistics {
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;
}
@ -128,7 +128,9 @@ class Statistics {
if (this.newStatisticsEntryCallback && insertId) {
const newStats = await this.$get(insertId);
this.newStatisticsEntryCallback(newStats);
if (newStats) {
this.newStatisticsEntryCallback(newStats);
}
}
}

View File

@ -1,13 +1,12 @@
const config = require('../../mempool-config.json');
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 memPool from './mempool';
import backendInfo from './backend-info';
import mempoolBlocks from './mempool-blocks';
import fiatConversion from './fiat-conversion';
import * as os from 'os';
import { Common } from './common';
class WebsocketHandler {
@ -102,7 +101,7 @@ class WebsocketHandler {
});
}
handleNewStatistic(stats: Statistic) {
handleNewStatistic(stats: OptimizedStatistic) {
if (!this.wss) {
throw new Error('WebSocket.Server is not set');
}

View File

@ -34,6 +34,19 @@ export interface Transaction {
status: Status;
}
export interface TransactionMinerInfo {
vin: VinStrippedToScriptsig[];
vout: VoutStrippedToScriptPubkey[];
}
interface VinStrippedToScriptsig {
scriptsig: string;
}
interface VoutStrippedToScriptPubkey {
scriptpubkey_address: string | undefined;
}
export interface TransactionExtended extends Transaction {
vsize: number;
feePerVsize: number;
@ -109,7 +122,7 @@ export interface Block {
medianFee?: number;
feeRange?: number[];
reward?: number;
coinbaseTx?: Transaction;
coinbaseTx?: TransactionMinerInfo;
matchRate?: number;
}