2020-10-19 06:57:02 +02:00
|
|
|
import config from './config';
|
2020-12-27 22:47:22 +01: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-09-10 09:46:23 +02:00
|
|
|
import bisq from './api/bisq/bisq';
|
2020-11-22 20:38:56 +01:00
|
|
|
import websocketHandler from './api/websocket-handler';
|
2020-09-10 09:46:23 +02:00
|
|
|
import bisqMarket from './api/bisq/markets-api';
|
2021-02-06 20:20:07 +01:00
|
|
|
import { RequiredSpec, TransactionExtended } from './mempool.interfaces';
|
2020-09-10 09:46:23 +02:00
|
|
|
import { MarketsApiError } from './api/bisq/interfaces';
|
2020-12-29 14:41:16 +01:00
|
|
|
import { IEsploraApi } from './api/bitcoin/esplora-api.interface';
|
2020-10-13 10:27:52 +02:00
|
|
|
import logger from './logger';
|
2020-12-21 17:08:34 +01:00
|
|
|
import bitcoinApi from './api/bitcoin/bitcoin-api-factory';
|
|
|
|
import transactionUtils from './api/transaction-utils';
|
2020-12-29 14:41:16 +01:00
|
|
|
import blocks from './api/blocks';
|
2021-01-08 15:44:36 +01:00
|
|
|
import loadingIndicators from './api/loading-indicators';
|
2021-01-24 19:09:42 +01:00
|
|
|
import { Common } from './api/common';
|
2021-09-14 23:47:24 +02:00
|
|
|
import bitcoinClient from './api/bitcoin/bitcoin-client';
|
2021-09-18 11:37:25 +02:00
|
|
|
import elementsParser from './api/liquid/elements-parser';
|
2021-12-14 13:06:03 +01:00
|
|
|
import icons from './api/liquid/icons';
|
2022-01-06 11:59:33 +01:00
|
|
|
import miningStats from './api/mining';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
class Routes {
|
2021-01-30 15:40:04 +01:00
|
|
|
constructor() {}
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2022-01-12 17:57:25 +01:00
|
|
|
public async $getStatisticsByTime(time: '2h' | '24h' | '1w' | '1m' | '3m' | '6m' | '1y' | '2y' | '3y', req: Request, res: Response) {
|
|
|
|
res.header('Pragma', 'public');
|
|
|
|
res.header('Cache-control', 'public');
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString());
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2022-01-12 17:57:25 +01:00
|
|
|
try {
|
|
|
|
let result;
|
|
|
|
switch (time as string) {
|
|
|
|
case '2h':
|
|
|
|
result = await statistics.$list2H();
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 30).toUTCString());
|
|
|
|
break;
|
|
|
|
case '24h':
|
|
|
|
result = await statistics.$list24H();
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
|
|
|
break;
|
|
|
|
case '1w':
|
|
|
|
result = await statistics.$list1W();
|
|
|
|
break;
|
|
|
|
case '1m':
|
|
|
|
result = await statistics.$list1M();
|
|
|
|
break;
|
|
|
|
case '3m':
|
|
|
|
result = await statistics.$list3M();
|
|
|
|
break;
|
|
|
|
case '6m':
|
|
|
|
result = await statistics.$list6M();
|
|
|
|
break;
|
|
|
|
case '1y':
|
|
|
|
result = await statistics.$list1Y();
|
|
|
|
break;
|
|
|
|
case '2y':
|
|
|
|
result = await statistics.$list2Y();
|
|
|
|
break;
|
|
|
|
case '3y':
|
|
|
|
result = await statistics.$list3Y();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = await statistics.$list2H();
|
|
|
|
}
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
2021-11-02 02:06:10 +01:00
|
|
|
}
|
|
|
|
|
2020-11-22 20:38:56 +01:00
|
|
|
public getInitData(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = websocketHandler.getInitData();
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2020-11-22 20:38:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:57:25 +01:00
|
|
|
public getRecommendedFees(req: Request, res: Response) {
|
2020-08-12 08:33:58 +02:00
|
|
|
if (!mempool.isInSync()) {
|
|
|
|
res.statusCode = 503;
|
|
|
|
res.send('Service Unavailable');
|
|
|
|
return;
|
|
|
|
}
|
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) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2019-11-06 08:35:02 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2021-03-18 17:47:40 +01:00
|
|
|
public getCpfpInfo(req: Request, res: Response) {
|
|
|
|
if (!/^[a-fA-F0-9]{64}$/.test(req.params.txId)) {
|
|
|
|
res.status(501).send(`Invalid transaction ID.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tx = mempool.getMempool()[req.params.txId];
|
|
|
|
if (!tx) {
|
|
|
|
res.status(404).send(`Transaction doesn't exist in the mempool.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tx.cpfpChecked) {
|
|
|
|
res.json({
|
|
|
|
ancestors: tx.ancestors,
|
|
|
|
bestDescendant: tx.bestDescendant || null,
|
|
|
|
});
|
2021-03-18 17:53:39 +01:00
|
|
|
return;
|
2021-03-18 17:47:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const cpfpInfo = Common.setRelativesAndGetCpfpInfo(tx, mempool.getMempool());
|
|
|
|
|
|
|
|
res.json(cpfpInfo);
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 09:46:23 +02:00
|
|
|
|
|
|
|
public getBisqMarketCurrencies(req: Request, res: Response) {
|
|
|
|
const constraints: RequiredSpec = {
|
|
|
|
'type': {
|
|
|
|
required: false,
|
|
|
|
types: ['crypto', 'fiat', 'all']
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-10-07 15:15:42 +02:00
|
|
|
const p = this.parseRequestParameters(req.query, constraints);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (p.error) {
|
2020-09-14 22:11:52 +02:00
|
|
|
res.status(400).json(this.getBisqMarketErrorResponse(p.error));
|
2020-09-10 09:46:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = bisqMarket.getCurrencies(p.type);
|
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketCurrencies error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqMarketDepth(req: Request, res: Response) {
|
|
|
|
const constraints: RequiredSpec = {
|
|
|
|
'market': {
|
|
|
|
required: true,
|
|
|
|
types: ['@string']
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-10-07 15:15:42 +02:00
|
|
|
const p = this.parseRequestParameters(req.query, constraints);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (p.error) {
|
2020-09-14 22:11:52 +02:00
|
|
|
res.status(400).json(this.getBisqMarketErrorResponse(p.error));
|
2020-09-10 09:46:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = bisqMarket.getDepth(p.market);
|
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketDepth error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqMarketMarkets(req: Request, res: Response) {
|
|
|
|
const result = bisqMarket.getMarkets();
|
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketMarkets error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqMarketTrades(req: Request, res: Response) {
|
|
|
|
const constraints: RequiredSpec = {
|
|
|
|
'market': {
|
|
|
|
required: true,
|
|
|
|
types: ['@string']
|
|
|
|
},
|
|
|
|
'timestamp_from': {
|
|
|
|
required: false,
|
|
|
|
types: ['@number']
|
|
|
|
},
|
|
|
|
'timestamp_to': {
|
|
|
|
required: false,
|
|
|
|
types: ['@number']
|
|
|
|
},
|
|
|
|
'trade_id_to': {
|
|
|
|
required: false,
|
|
|
|
types: ['@string']
|
|
|
|
},
|
|
|
|
'trade_id_from': {
|
|
|
|
required: false,
|
|
|
|
types: ['@string']
|
|
|
|
},
|
|
|
|
'direction': {
|
|
|
|
required: false,
|
|
|
|
types: ['buy', 'sell']
|
|
|
|
},
|
|
|
|
'limit': {
|
|
|
|
required: false,
|
|
|
|
types: ['@number']
|
|
|
|
},
|
|
|
|
'sort': {
|
|
|
|
required: false,
|
|
|
|
types: ['asc', 'desc']
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-07 15:15:42 +02:00
|
|
|
const p = this.parseRequestParameters(req.query, constraints);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (p.error) {
|
2020-09-14 22:11:52 +02:00
|
|
|
res.status(400).json(this.getBisqMarketErrorResponse(p.error));
|
2020-09-10 09:46:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = bisqMarket.getTrades(p.market, p.timestamp_from,
|
|
|
|
p.timestamp_to, p.trade_id_from, p.trade_id_to, p.direction, p.limit, p.sort);
|
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketTrades error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqMarketOffers(req: Request, res: Response) {
|
|
|
|
const constraints: RequiredSpec = {
|
|
|
|
'market': {
|
|
|
|
required: true,
|
|
|
|
types: ['@string']
|
|
|
|
},
|
|
|
|
'direction': {
|
|
|
|
required: false,
|
2020-09-11 20:26:51 +02:00
|
|
|
types: ['buy', 'sell']
|
2020-09-10 09:46:23 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-10-07 15:15:42 +02:00
|
|
|
const p = this.parseRequestParameters(req.query, constraints);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (p.error) {
|
2020-09-14 22:11:52 +02:00
|
|
|
res.status(400).json(this.getBisqMarketErrorResponse(p.error));
|
2020-09-10 09:46:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-11 20:26:51 +02:00
|
|
|
const result = bisqMarket.getOffers(p.market, p.direction);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketOffers error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqMarketVolumes(req: Request, res: Response) {
|
|
|
|
const constraints: RequiredSpec = {
|
|
|
|
'market': {
|
2020-09-14 22:11:52 +02:00
|
|
|
required: false,
|
2020-09-10 09:46:23 +02:00
|
|
|
types: ['@string']
|
|
|
|
},
|
|
|
|
'interval': {
|
|
|
|
required: false,
|
|
|
|
types: ['minute', 'half_hour', 'hour', 'half_day', 'day', 'week', 'month', 'year', 'auto']
|
|
|
|
},
|
|
|
|
'timestamp_from': {
|
|
|
|
required: false,
|
|
|
|
types: ['@number']
|
|
|
|
},
|
|
|
|
'timestamp_to': {
|
|
|
|
required: false,
|
|
|
|
types: ['@number']
|
|
|
|
},
|
2020-09-14 22:11:52 +02:00
|
|
|
'milliseconds': {
|
|
|
|
required: false,
|
|
|
|
types: ['@boolean']
|
|
|
|
},
|
2020-09-17 10:38:25 +02:00
|
|
|
'timestamp': {
|
|
|
|
required: false,
|
|
|
|
types: ['no', 'yes']
|
|
|
|
},
|
2020-09-10 09:46:23 +02:00
|
|
|
};
|
|
|
|
|
2020-10-07 15:15:42 +02:00
|
|
|
const p = this.parseRequestParameters(req.query, constraints);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (p.error) {
|
2020-09-14 22:11:52 +02:00
|
|
|
res.status(400).json(this.getBisqMarketErrorResponse(p.error));
|
2020-09-10 09:46:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:38:25 +02:00
|
|
|
const result = bisqMarket.getVolumes(p.market, p.timestamp_from, p.timestamp_to, p.interval, p.milliseconds, p.timestamp);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketVolumes error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqMarketHloc(req: Request, res: Response) {
|
|
|
|
const constraints: RequiredSpec = {
|
|
|
|
'market': {
|
|
|
|
required: true,
|
|
|
|
types: ['@string']
|
|
|
|
},
|
|
|
|
'interval': {
|
|
|
|
required: false,
|
|
|
|
types: ['minute', 'half_hour', 'hour', 'half_day', 'day', 'week', 'month', 'year', 'auto']
|
|
|
|
},
|
|
|
|
'timestamp_from': {
|
|
|
|
required: false,
|
|
|
|
types: ['@number']
|
|
|
|
},
|
|
|
|
'timestamp_to': {
|
|
|
|
required: false,
|
|
|
|
types: ['@number']
|
|
|
|
},
|
2020-09-13 12:51:53 +02:00
|
|
|
'milliseconds': {
|
|
|
|
required: false,
|
|
|
|
types: ['@boolean']
|
|
|
|
},
|
2020-09-17 10:38:25 +02:00
|
|
|
'timestamp': {
|
|
|
|
required: false,
|
|
|
|
types: ['no', 'yes']
|
|
|
|
},
|
2020-09-10 09:46:23 +02:00
|
|
|
};
|
|
|
|
|
2020-10-07 15:15:42 +02:00
|
|
|
const p = this.parseRequestParameters(req.query, constraints);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (p.error) {
|
2020-09-14 22:11:52 +02:00
|
|
|
res.status(400).json(this.getBisqMarketErrorResponse(p.error));
|
2020-09-10 09:46:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:38:25 +02:00
|
|
|
const result = bisqMarket.getHloc(p.market, p.interval, p.timestamp_from, p.timestamp_to, p.milliseconds, p.timestamp);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketHloc error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getBisqMarketTicker(req: Request, res: Response) {
|
|
|
|
const constraints: RequiredSpec = {
|
|
|
|
'market': {
|
|
|
|
required: false,
|
|
|
|
types: ['@string']
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-10-07 15:15:42 +02:00
|
|
|
const p = this.parseRequestParameters(req.query, constraints);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (p.error) {
|
2020-09-14 22:11:52 +02:00
|
|
|
res.status(400).json(this.getBisqMarketErrorResponse(p.error));
|
2020-09-10 09:46:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-14 22:11:52 +02:00
|
|
|
const result = bisqMarket.getTicker(p.market);
|
2020-09-10 09:46:23 +02:00
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketTicker error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 17:02:55 +01:00
|
|
|
public getBisqMarketVolumes7d(req: Request, res: Response) {
|
|
|
|
const result = bisqMarket.getVolumesByTime(604800);
|
2021-02-28 11:18:29 +01:00
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
2021-03-10 17:02:55 +01:00
|
|
|
res.status(500).json(this.getBisqMarketErrorResponse('getBisqMarketVolumes7d error'));
|
2021-02-28 11:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 15:15:42 +02:00
|
|
|
private parseRequestParameters(requestParams: object, params: RequiredSpec): { [name: string]: any; } {
|
2020-09-11 20:26:51 +02:00
|
|
|
const final = {};
|
|
|
|
for (const i in params) {
|
|
|
|
if (params.hasOwnProperty(i)) {
|
2020-10-07 15:15:42 +02:00
|
|
|
if (params[i].required && requestParams[i] === undefined) {
|
2020-09-11 20:26:51 +02:00
|
|
|
return { error: i + ' parameter missing'};
|
|
|
|
}
|
2020-10-07 15:15:42 +02:00
|
|
|
if (typeof requestParams[i] === 'string') {
|
|
|
|
const str = (requestParams[i] || '').toString().toLowerCase();
|
2020-09-11 20:26:51 +02:00
|
|
|
if (params[i].types.indexOf('@number') > -1) {
|
|
|
|
const number = parseInt((str).toString(), 10);
|
|
|
|
final[i] = number;
|
|
|
|
} else if (params[i].types.indexOf('@string') > -1) {
|
|
|
|
final[i] = str;
|
2020-09-13 12:51:53 +02:00
|
|
|
} else if (params[i].types.indexOf('@boolean') > -1) {
|
|
|
|
final[i] = str === 'true' || str === 'yes';
|
2020-09-11 20:26:51 +02:00
|
|
|
} else if (params[i].types.indexOf(str) > -1) {
|
|
|
|
final[i] = str;
|
|
|
|
} else {
|
|
|
|
return { error: i + ' parameter invalid'};
|
|
|
|
}
|
2020-10-07 15:15:42 +02:00
|
|
|
} else if (typeof requestParams[i] === 'number') {
|
|
|
|
final[i] = requestParams[i];
|
2020-09-11 20:26:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return final;
|
|
|
|
}
|
|
|
|
|
2020-09-10 09:46:23 +02:00
|
|
|
private getBisqMarketErrorResponse(message: string): MarketsApiError {
|
|
|
|
return {
|
|
|
|
'success': 0,
|
|
|
|
'error': message
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:08:34 +01:00
|
|
|
public async getTransaction(req: Request, res: Response) {
|
|
|
|
try {
|
2021-01-23 22:15:06 +01:00
|
|
|
const transaction = await transactionUtils.$getTransactionExtended(req.params.txId, true);
|
2021-01-23 20:51:22 +01:00
|
|
|
res.json(transaction);
|
2020-12-21 17:08:34 +01:00
|
|
|
} catch (e) {
|
2021-01-23 20:51:22 +01:00
|
|
|
let statusCode = 500;
|
2021-08-31 14:09:33 +02:00
|
|
|
if (e instanceof Error && e instanceof Error && e.message && e.message.indexOf('No such mempool or blockchain transaction') > -1) {
|
2021-01-23 20:51:22 +01:00
|
|
|
statusCode = 404;
|
|
|
|
}
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(statusCode).send(e instanceof Error ? e.message : e);
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 13:27:02 +02:00
|
|
|
public async getRawTransaction(req: Request, res: Response) {
|
|
|
|
try {
|
2021-08-03 17:15:33 +02:00
|
|
|
const transaction: IEsploraApi.Transaction = await bitcoinApi.$getRawTransaction(req.params.txId, true);
|
2021-08-03 13:27:02 +02:00
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
res.send(transaction.hex);
|
|
|
|
} catch (e) {
|
|
|
|
let statusCode = 500;
|
2021-08-31 14:09:33 +02:00
|
|
|
if (e instanceof Error && e.message && e.message.indexOf('No such mempool or blockchain transaction') > -1) {
|
2021-08-03 13:27:02 +02:00
|
|
|
statusCode = 404;
|
|
|
|
}
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(statusCode).send(e instanceof Error ? e.message : e);
|
2021-08-03 13:27:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 19:09:42 +01:00
|
|
|
public async getTransactionStatus(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const transaction = await transactionUtils.$getTransactionExtended(req.params.txId, true);
|
|
|
|
res.json(transaction.status);
|
|
|
|
} catch (e) {
|
|
|
|
let statusCode = 500;
|
2021-08-31 14:09:33 +02:00
|
|
|
if (e instanceof Error && e.message && e.message.indexOf('No such mempool or blockchain transaction') > -1) {
|
2021-01-24 19:09:42 +01:00
|
|
|
statusCode = 404;
|
|
|
|
}
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(statusCode).send(e instanceof Error ? e.message : e);
|
2021-01-24 19:09:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 11:59:33 +01:00
|
|
|
public async getPools(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
let stats = await miningStats.$getPoolsStats(req.query.interval as string);
|
|
|
|
res.json(stats);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:08:34 +01:00
|
|
|
public async getBlock(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinApi.$getBlock(req.params.hash);
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 01:26:16 +02:00
|
|
|
public async getBlockHeader(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const blockHeader = await bitcoinApi.$getBlockHeader(req.params.hash);
|
2021-07-19 01:34:01 +02:00
|
|
|
res.setHeader('content-type', 'text/plain');
|
2021-07-19 01:26:16 +02:00
|
|
|
res.send(blockHeader);
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2021-07-19 01:26:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:08:34 +01:00
|
|
|
public async getBlocks(req: Request, res: Response) {
|
2020-12-29 14:41:16 +01:00
|
|
|
try {
|
2021-01-08 15:44:36 +01:00
|
|
|
loadingIndicators.setProgress('blocks', 0);
|
|
|
|
|
2020-12-29 14:41:16 +01:00
|
|
|
const returnBlocks: IEsploraApi.Block[] = [];
|
2020-12-29 19:47:07 +01:00
|
|
|
const fromHeight = parseInt(req.params.height, 10) || blocks.getCurrentBlockHeight();
|
2020-12-29 14:41:16 +01:00
|
|
|
|
2020-12-29 19:47:07 +01:00
|
|
|
// Check if block height exist in local cache to skip the hash lookup
|
|
|
|
const blockByHeight = blocks.getBlocks().find((b) => b.height === fromHeight);
|
2020-12-29 14:41:16 +01:00
|
|
|
let startFromHash: string | null = null;
|
|
|
|
if (blockByHeight) {
|
|
|
|
startFromHash = blockByHeight.id;
|
|
|
|
} else {
|
|
|
|
startFromHash = await bitcoinApi.$getBlockHash(fromHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
let nextHash = startFromHash;
|
|
|
|
for (let i = 0; i < 10; i++) {
|
2020-12-29 19:47:07 +01:00
|
|
|
const localBlock = blocks.getBlocks().find((b) => b.id === nextHash);
|
2020-12-29 14:41:16 +01:00
|
|
|
if (localBlock) {
|
|
|
|
returnBlocks.push(localBlock);
|
|
|
|
nextHash = localBlock.previousblockhash;
|
|
|
|
} else {
|
|
|
|
const block = await bitcoinApi.$getBlock(nextHash);
|
|
|
|
returnBlocks.push(block);
|
|
|
|
nextHash = block.previousblockhash;
|
|
|
|
}
|
2021-01-08 15:44:36 +01:00
|
|
|
loadingIndicators.setProgress('blocks', i / 10 * 100);
|
2020-12-29 14:41:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json(returnBlocks);
|
|
|
|
} catch (e) {
|
2021-01-08 15:44:36 +01:00
|
|
|
loadingIndicators.setProgress('blocks', 100);
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2020-12-29 14:41:16 +01:00
|
|
|
}
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getBlockTransactions(req: Request, res: Response) {
|
2020-12-27 22:47:22 +01:00
|
|
|
try {
|
2021-01-08 15:44:36 +01:00
|
|
|
loadingIndicators.setProgress('blocktxs-' + req.params.hash, 0);
|
|
|
|
|
2020-12-27 22:47:22 +01:00
|
|
|
const txIds = await bitcoinApi.$getTxIdsForBlock(req.params.hash);
|
|
|
|
const transactions: TransactionExtended[] = [];
|
2021-01-24 19:09:42 +01:00
|
|
|
const startingIndex = Math.max(0, parseInt(req.params.index || '0', 10));
|
2020-12-27 22:47:22 +01:00
|
|
|
|
2021-01-08 15:44:36 +01:00
|
|
|
const endIndex = Math.min(startingIndex + 10, txIds.length);
|
|
|
|
for (let i = startingIndex; i < endIndex; i++) {
|
2021-01-23 20:51:22 +01:00
|
|
|
try {
|
2021-01-23 22:15:06 +01:00
|
|
|
const transaction = await transactionUtils.$getTransactionExtended(txIds[i], true);
|
2020-12-27 22:47:22 +01:00
|
|
|
transactions.push(transaction);
|
2021-01-08 15:44:36 +01:00
|
|
|
loadingIndicators.setProgress('blocktxs-' + req.params.hash, (i + 1) / endIndex * 100);
|
2021-01-23 20:51:22 +01:00
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
logger.debug('getBlockTransactions error: ' + (e instanceof Error ? e.message : e));
|
2020-12-27 22:47:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res.json(transactions);
|
|
|
|
} catch (e) {
|
2021-01-08 15:44:36 +01:00
|
|
|
loadingIndicators.setProgress('blocktxs-' + req.params.hash, 100);
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2020-12-27 22:47:22 +01:00
|
|
|
}
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getBlockHeight(req: Request, res: Response) {
|
2020-12-28 14:17:32 +01:00
|
|
|
try {
|
|
|
|
const blockHash = await bitcoinApi.$getBlockHash(parseInt(req.params.height, 10));
|
|
|
|
res.send(blockHash);
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2020-12-28 14:17:32 +01:00
|
|
|
}
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getAddress(req: Request, res: Response) {
|
2020-12-27 22:47:22 +01:00
|
|
|
if (config.MEMPOOL.BACKEND === 'none') {
|
2020-12-22 00:04:31 +01:00
|
|
|
res.status(405).send('Address lookups cannot be used with bitcoind as backend.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-27 22:47:22 +01:00
|
|
|
try {
|
|
|
|
const addressData = await bitcoinApi.$getAddress(req.params.address);
|
|
|
|
res.json(addressData);
|
2020-12-21 17:08:34 +01:00
|
|
|
} catch (e) {
|
2021-10-23 09:46:38 +02:00
|
|
|
if (e instanceof Error && e.message && (e.message.indexOf('too long') > 0 || e.message.indexOf('confirmed status') > 0)) {
|
2021-08-31 14:09:33 +02:00
|
|
|
return res.status(413).send(e instanceof Error ? e.message : e);
|
2021-01-05 11:30:53 +01:00
|
|
|
}
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getAddressTransactions(req: Request, res: Response) {
|
2020-12-27 22:47:22 +01:00
|
|
|
if (config.MEMPOOL.BACKEND === 'none') {
|
2020-12-22 00:04:31 +01:00
|
|
|
res.status(405).send('Address lookups cannot be used with bitcoind as backend.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-12-27 22:47:22 +01:00
|
|
|
const transactions = await bitcoinApi.$getAddressTransactions(req.params.address, req.params.txId);
|
2020-12-22 00:04:31 +01:00
|
|
|
res.json(transactions);
|
|
|
|
} catch (e) {
|
2021-10-23 09:46:38 +02:00
|
|
|
if (e instanceof Error && e.message && (e.message.indexOf('too long') > 0 || e.message.indexOf('confirmed status') > 0)) {
|
2021-08-31 14:09:33 +02:00
|
|
|
return res.status(413).send(e instanceof Error ? e.message : e);
|
2021-01-05 11:30:53 +01:00
|
|
|
}
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2020-12-22 00:04:31 +01:00
|
|
|
}
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getAdressTxChain(req: Request, res: Response) {
|
2021-01-10 18:26:36 +01:00
|
|
|
res.status(501).send('Not implemented');
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getAddressPrefix(req: Request, res: Response) {
|
2021-01-10 19:51:57 +01:00
|
|
|
try {
|
|
|
|
const blockHash = await bitcoinApi.$getAddressPrefix(req.params.prefix);
|
|
|
|
res.send(blockHash);
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2021-01-10 19:51:57 +01:00
|
|
|
}
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
|
|
|
|
2021-01-24 19:09:42 +01:00
|
|
|
public async getRecentMempoolTransactions(req: Request, res: Response) {
|
|
|
|
const latestTransactions = Object.entries(mempool.getMempool())
|
|
|
|
.sort((a, b) => (b[1].firstSeen || 0) - (a[1].firstSeen || 0))
|
|
|
|
.slice(0, 10).map((tx) => Common.stripTransaction(tx[1]));
|
|
|
|
|
|
|
|
res.json(latestTransactions);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getMempool(req: Request, res: Response) {
|
|
|
|
res.status(501).send('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getMempoolTxIds(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const rawMempool = await bitcoinApi.$getRawMempool();
|
|
|
|
res.send(rawMempool);
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2021-01-24 19:09:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getBlockTipHeight(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinApi.$getBlockHeightTip();
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2021-01-24 19:09:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getTxIdsForBlock(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinApi.$getTxIdsForBlock(req.params.hash);
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2021-01-24 19:09:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 08:20:31 +02:00
|
|
|
public async validateAddress(req: Request, res: Response) {
|
|
|
|
try {
|
2021-09-14 23:47:24 +02:00
|
|
|
const result = await bitcoinClient.validateAddress(req.params.address);
|
2021-09-06 08:20:31 +02:00
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 19:09:04 +01:00
|
|
|
public async getTransactionOutspends(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinApi.$getOutspends(req.params.txId);
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
2020-12-21 17:08:34 +01:00
|
|
|
}
|
2021-07-18 23:45:45 +02:00
|
|
|
|
|
|
|
public getDifficultyChange(req: Request, res: Response) {
|
|
|
|
try {
|
2021-07-23 13:35:04 +02:00
|
|
|
const DATime = blocks.getLastDifficultyAdjustmentTime();
|
|
|
|
const previousRetarget = blocks.getPreviousDifficultyRetarget();
|
|
|
|
const blockHeight = blocks.getCurrentBlockHeight();
|
2021-08-26 02:14:01 +02:00
|
|
|
|
|
|
|
const now = new Date().getTime() / 1000;
|
|
|
|
const diff = now - DATime;
|
2021-07-18 23:45:45 +02:00
|
|
|
const blocksInEpoch = blockHeight % 2016;
|
2021-08-26 02:14:01 +02:00
|
|
|
const progressPercent = (blocksInEpoch >= 0) ? blocksInEpoch / 2016 * 100 : 100;
|
|
|
|
const remainingBlocks = 2016 - blocksInEpoch;
|
|
|
|
const nextRetargetHeight = blockHeight + remainingBlocks;
|
|
|
|
|
|
|
|
let difficultyChange = 0;
|
2021-11-17 10:05:14 +01:00
|
|
|
if (remainingBlocks < 1870) {
|
|
|
|
if (blocksInEpoch > 0) {
|
|
|
|
difficultyChange = (600 / (diff / blocksInEpoch ) - 1) * 100;
|
|
|
|
}
|
|
|
|
if (difficultyChange > 300) {
|
|
|
|
difficultyChange = 300;
|
|
|
|
}
|
|
|
|
if (difficultyChange < -75) {
|
|
|
|
difficultyChange = -75;
|
|
|
|
}
|
2021-08-26 02:14:01 +02:00
|
|
|
}
|
2021-07-18 23:45:45 +02:00
|
|
|
|
|
|
|
const timeAvgDiff = difficultyChange * 0.1;
|
|
|
|
|
|
|
|
let timeAvgMins = 10;
|
2021-08-26 02:14:01 +02:00
|
|
|
if (timeAvgDiff > 0) {
|
2021-07-18 23:45:45 +02:00
|
|
|
timeAvgMins -= Math.abs(timeAvgDiff);
|
|
|
|
} else {
|
|
|
|
timeAvgMins += Math.abs(timeAvgDiff);
|
|
|
|
}
|
|
|
|
|
2021-08-26 02:14:01 +02:00
|
|
|
const timeAvg = timeAvgMins * 60;
|
|
|
|
const remainingTime = remainingBlocks * timeAvg;
|
|
|
|
const estimatedRetargetDate = remainingTime + now;
|
2021-09-06 08:20:31 +02:00
|
|
|
|
2021-08-26 02:14:01 +02:00
|
|
|
const result = {
|
2021-07-18 23:45:45 +02:00
|
|
|
progressPercent,
|
|
|
|
difficultyChange,
|
|
|
|
estimatedRetargetDate,
|
|
|
|
remainingBlocks,
|
|
|
|
remainingTime,
|
2021-07-23 13:35:04 +02:00
|
|
|
previousRetarget,
|
2021-08-26 02:14:01 +02:00
|
|
|
nextRetargetHeight,
|
|
|
|
timeAvg,
|
2021-09-06 08:20:31 +02:00
|
|
|
};
|
2021-07-18 23:45:45 +02:00
|
|
|
res.json(result);
|
|
|
|
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
2021-07-18 23:45:45 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-18 11:37:25 +02:00
|
|
|
|
|
|
|
public async $getElementsPegsByMonth(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const pegs = await elementsParser.$getPegDataByMonth();
|
|
|
|
res.json(pegs);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 20:18:44 +02:00
|
|
|
|
|
|
|
public async $postTransaction(req: Request, res: Response) {
|
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
try {
|
2021-10-19 13:37:45 +02:00
|
|
|
let rawTx;
|
|
|
|
if (typeof req.body === 'object') {
|
|
|
|
rawTx = Object.keys(req.body)[0];
|
|
|
|
} else {
|
|
|
|
rawTx = req.body;
|
|
|
|
}
|
|
|
|
const txIdResult = await bitcoinApi.$sendRawTransaction(rawTx);
|
2021-09-26 20:18:44 +02:00
|
|
|
res.send(txIdResult);
|
|
|
|
} catch (e: any) {
|
|
|
|
res.status(400).send(e.message && e.code ? 'sendrawtransaction RPC error: ' + JSON.stringify({ code: e.code, message: e.message })
|
|
|
|
: (e.message || 'Error'));
|
|
|
|
}
|
|
|
|
}
|
2021-10-20 21:55:36 +02:00
|
|
|
|
|
|
|
public async $postTransactionForm(req: Request, res: Response) {
|
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
const matches = /tx=([a-z0-9]+)/.exec(req.body);
|
|
|
|
let txHex = '';
|
|
|
|
if (matches && matches[1]) {
|
|
|
|
txHex = matches[1];
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const txIdResult = await bitcoinClient.sendRawTransaction(txHex);
|
|
|
|
res.send(txIdResult);
|
|
|
|
} catch (e: any) {
|
|
|
|
res.status(400).send(e.message && e.code ? 'sendrawtransaction RPC error: ' + JSON.stringify({ code: e.code, message: e.message })
|
|
|
|
: (e.message || 'Error'));
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 13:06:03 +01:00
|
|
|
|
|
|
|
public getLiquidIcon(req: Request, res: Response) {
|
|
|
|
const result = icons.getIconByAssetId(req.params.assetId);
|
|
|
|
if (result) {
|
|
|
|
res.setHeader('content-type', 'image/png');
|
|
|
|
res.setHeader('content-length', result.length);
|
|
|
|
res.send(result);
|
|
|
|
} else {
|
|
|
|
res.status(404).send('Asset icon not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getAllLiquidIcon(req: Request, res: Response) {
|
|
|
|
const result = icons.getAllIconIds();
|
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
|
|
|
res.status(404).send('Asset icons not found');
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new Routes();
|