Bisq stats page.

BSQ prices.
This commit is contained in:
softsimon
2020-07-14 21:26:02 +07:00
parent b7376fbd8d
commit ca0cf23d66
21 changed files with 207 additions and 61 deletions

View File

@@ -1,6 +1,8 @@
const config = require('../../mempool-config.json');
import * as fs from 'fs';
import { BisqBlocks, BisqBlock, BisqTransaction, BisqStats } from '../interfaces';
import * as request from 'request';
import { BisqBlocks, BisqBlock, BisqTransaction, BisqStats, BisqTrade } from '../interfaces';
import { Common } from './common';
class Bisq {
private blocks: BisqBlock[] = [];
@@ -15,6 +17,8 @@ class Bisq {
unspent_txos: 0,
spent_txos: 0,
};
private price: number = 0;
private priceUpdateCallbackFunction: ((price: number) => void) | undefined;
constructor() {}
@@ -22,7 +26,7 @@ class Bisq {
this.loadBisqDumpFile();
let fsWait: NodeJS.Timeout | null = null;
fs.watch(config.BSQ_BLOCKS_DATA_PATH, (event, filename) => {
fs.watch(config.BSQ_BLOCKS_DATA_PATH, (event: string, filename: string) => {
if (filename) {
if (fsWait) {
clearTimeout(fsWait);
@@ -33,6 +37,9 @@ class Bisq {
}, 1000);
}
});
setInterval(this.updatePrice.bind(this), 1000 * 60 * 60);
this.updatePrice();
}
getTransaction(txId: string): BisqTransaction | undefined {
@@ -59,6 +66,26 @@ class Bisq {
return this.stats;
}
setPriceCallbackFunction(fn: (price: number) => void) {
this.priceUpdateCallbackFunction = fn;
}
private updatePrice() {
request('https://markets.bisq.network/api/trades/?market=bsq_btc', { json: true }, (err, res, trades: BisqTrade[]) => {
if (err) { return console.log(err); }
const prices: number[] = [];
trades.forEach((trade) => {
prices.push(parseFloat(trade.price) * 100000000);
});
prices.sort((a, b) => a - b);
this.price = Common.median(prices);
if (this.priceUpdateCallbackFunction) {
this.priceUpdateCallbackFunction(this.price);
}
});
}
private async loadBisqDumpFile(): Promise<void> {
try {
const data = await this.loadData();

View File

@@ -12,6 +12,7 @@ import { Common } from './common';
class WebsocketHandler {
private wss: WebSocket.Server | undefined;
private nativeAssetId = '6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d';
private extraInitProperties = {};
constructor() { }
@@ -19,6 +20,10 @@ class WebsocketHandler {
this.wss = wss;
}
setExtraInitProperties(property: string, value: any) {
this.extraInitProperties[property] = value;
}
setupConnectionHandling() {
if (!this.wss) {
throw new Error('WebSocket.Server is not set');
@@ -84,6 +89,7 @@ class WebsocketHandler {
'mempool-blocks': mempoolBlocks.getMempoolBlocks(),
'git-commit': backendInfo.gitCommitHash,
'hostname': backendInfo.hostname,
...this.extraInitProperties
}));
}

View File

@@ -53,6 +53,7 @@ class Server {
if (config.BISQ_ENABLED) {
bisq.startBisqService();
bisq.setPriceCallbackFunction((price) => websocketHandler.setExtraInitProperties('bsq-price', price));
}
this.server.listen(config.HTTP_PORT, () => {

View File

@@ -312,3 +312,13 @@ interface SpentInfo {
inputIndex: number;
txId: string;
}
export interface BisqTrade {
direction: string;
price: string;
amount: string;
volume: string;
payment_method: string;
trade_id: string;
trade_date: number;
}