2020-07-29 08:57:49 +02:00
|
|
|
const config = require('../mempool-config.json');
|
2020-06-07 12:30:32 +02:00
|
|
|
import { Request, Response } from 'express';
|
2019-07-21 16:59:47 +02:00
|
|
|
import statistics from './api/statistics';
|
|
|
|
import feeApi from './api/fee-api';
|
2020-05-27 10:18:04 +02:00
|
|
|
import backendInfo from './api/backend-info';
|
2020-02-16 16:15:07 +01:00
|
|
|
import mempoolBlocks from './api/mempool-blocks';
|
2020-02-27 19:09:07 +01:00
|
|
|
import mempool from './api/mempool';
|
2020-07-03 18:45:19 +02:00
|
|
|
import bisq from './api/bisq';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
class Routes {
|
2019-08-15 13:06:08 +02:00
|
|
|
private cache = {};
|
|
|
|
|
|
|
|
constructor() {
|
2020-07-29 08:57:49 +02:00
|
|
|
if (!config.DB_DISABLED) {
|
|
|
|
this.createCache();
|
|
|
|
setInterval(this.createCache.bind(this), 600000);
|
|
|
|
}
|
2019-08-15 13:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async createCache() {
|
|
|
|
this.cache['24h'] = await statistics.$list24H();
|
|
|
|
this.cache['1w'] = await statistics.$list1W();
|
|
|
|
this.cache['1m'] = await statistics.$list1M();
|
|
|
|
this.cache['3m'] = await statistics.$list3M();
|
|
|
|
this.cache['6m'] = await statistics.$list6M();
|
2020-02-16 18:26:57 +01:00
|
|
|
this.cache['1y'] = await statistics.$list1Y();
|
2019-08-15 13:06:08 +02:00
|
|
|
console.log('Statistics cache created');
|
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public async get2HStatistics(req: Request, res: Response) {
|
2019-07-21 16:59:47 +02:00
|
|
|
const result = await statistics.$list2H();
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(result);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public get24HStatistics(req: Request, res: Response) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(this.cache['24h']);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public get1WHStatistics(req: Request, res: Response) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(this.cache['1w']);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public get1MStatistics(req: Request, res: Response) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(this.cache['1m']);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public get3MStatistics(req: Request, res: Response) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(this.cache['3m']);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public get6MStatistics(req: Request, res: Response) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(this.cache['6m']);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public get1YStatistics(req: Request, res: Response) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(this.cache['1y']);
|
2020-02-16 18:26:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public async getRecommendedFees(req: Request, res: Response) {
|
2019-07-21 16:59:47 +02:00
|
|
|
const result = feeApi.getRecommendedFee();
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(result);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public getMempoolBlocks(req: Request, res: Response) {
|
2019-11-06 08:35:02 +01:00
|
|
|
try {
|
2020-06-07 12:30:32 +02:00
|
|
|
const result = mempoolBlocks.getMempoolBlocks();
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(result);
|
2019-11-06 08:35:02 +01:00
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e.message);
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 19:09:07 +01:00
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public getTransactionTimes(req: Request, res: Response) {
|
2020-02-27 19:09:07 +01:00
|
|
|
if (!Array.isArray(req.query.txId)) {
|
|
|
|
res.status(500).send('Not an array');
|
|
|
|
return;
|
|
|
|
}
|
2020-06-07 12:30:32 +02:00
|
|
|
const txIds: string[] = [];
|
|
|
|
for (const _txId in req.query.txId) {
|
|
|
|
if (typeof req.query.txId[_txId] === 'string') {
|
|
|
|
txIds.push(req.query.txId[_txId].toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 19:09:07 +01:00
|
|
|
const times = mempool.getFirstSeenForTransactions(txIds);
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(times);
|
2020-02-27 19:09:07 +01:00
|
|
|
}
|
2020-05-26 13:06:14 +02:00
|
|
|
|
2020-06-07 12:30:32 +02:00
|
|
|
public getBackendInfo(req: Request, res: Response) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(backendInfo.getBackendInfo());
|
2020-05-26 13:06:14 +02:00
|
|
|
}
|
2020-07-03 18:45:19 +02:00
|
|
|
|
2020-07-14 09:38:52 +02:00
|
|
|
public getBisqStats(req: Request, res: Response) {
|
|
|
|
const result = bisq.getStats();
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(result);
|
2020-07-14 09:38:52 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 08:10:13 +02:00
|
|
|
public getBisqTip(req: Request, res: Response) {
|
|
|
|
const result = bisq.getLatestBlockHeight();
|
2020-07-18 13:46:33 +02:00
|
|
|
res.type('text/plain');
|
2020-07-15 08:10:13 +02:00
|
|
|
res.send(result.toString());
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:45:19 +02:00
|
|
|
public getBisqTransaction(req: Request, res: Response) {
|
|
|
|
const result = bisq.getTransaction(req.params.txId);
|
|
|
|
if (result) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(result);
|
2020-07-03 18:45:19 +02:00
|
|
|
} else {
|
|
|
|
res.status(404).send('Bisq transaction not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqTransactions(req: Request, res: Response) {
|
2020-07-24 13:41:15 +02:00
|
|
|
const types: string[] = [];
|
2020-07-24 14:38:39 +02:00
|
|
|
req.query.types = req.query.types || [];
|
|
|
|
if (!Array.isArray(req.query.types)) {
|
2020-07-24 13:41:15 +02:00
|
|
|
res.status(500).send('Types is not an array');
|
|
|
|
return;
|
2020-07-24 14:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const _type in req.query.types) {
|
|
|
|
if (typeof req.query.types[_type] === 'string') {
|
|
|
|
types.push(req.query.types[_type].toString());
|
2020-07-24 13:41:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:45:19 +02:00
|
|
|
const index = parseInt(req.params.index, 10) || 0;
|
|
|
|
const length = parseInt(req.params.length, 10) > 100 ? 100 : parseInt(req.params.length, 10) || 25;
|
2020-07-24 13:41:15 +02:00
|
|
|
const [transactions, count] = bisq.getTransactions(index, length, types);
|
2020-07-10 09:13:07 +02:00
|
|
|
res.header('X-Total-Count', count.toString());
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(transactions);
|
2020-07-03 18:45:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqBlock(req: Request, res: Response) {
|
2020-07-13 10:16:12 +02:00
|
|
|
const result = bisq.getBlock(req.params.hash);
|
2020-07-03 18:45:19 +02:00
|
|
|
if (result) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(result);
|
2020-07-03 18:45:19 +02:00
|
|
|
} else {
|
|
|
|
res.status(404).send('Bisq block not found');
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 09:13:07 +02:00
|
|
|
|
2020-07-13 10:16:12 +02:00
|
|
|
public getBisqBlocks(req: Request, res: Response) {
|
2020-07-10 09:13:07 +02:00
|
|
|
const index = parseInt(req.params.index, 10) || 0;
|
|
|
|
const length = parseInt(req.params.length, 10) > 100 ? 100 : parseInt(req.params.length, 10) || 25;
|
2020-07-13 10:16:12 +02:00
|
|
|
const [transactions, count] = bisq.getBlocks(index, length);
|
2020-07-10 09:13:07 +02:00
|
|
|
res.header('X-Total-Count', count.toString());
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(transactions);
|
2020-07-10 09:13:07 +02:00
|
|
|
}
|
2020-07-13 16:46:25 +02:00
|
|
|
|
|
|
|
public getBisqAddress(req: Request, res: Response) {
|
|
|
|
const result = bisq.getAddress(req.params.address.substr(1));
|
|
|
|
if (result) {
|
2020-07-18 13:46:33 +02:00
|
|
|
res.json(result);
|
2020-07-13 16:46:25 +02:00
|
|
|
} else {
|
|
|
|
res.status(404).send('Bisq address not found');
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new Routes();
|