mempool/backend/src/routes.ts

199 lines
5.0 KiB
TypeScript
Raw Normal View History

2019-07-21 16:59:47 +02:00
import statistics from './api/statistics';
import feeApi from './api/fee-api';
import projectedBlocks from './api/projected-blocks';
2019-11-06 08:35:02 +01:00
import bitcoinApi from './api/bitcoin/bitcoin-api-factory';
2019-07-21 16:59:47 +02:00
class Routes {
private cache = {};
constructor() {
this.createCache();
2019-08-19 09:31:14 +02:00
setInterval(this.createCache.bind(this), 600000);
}
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();
console.log('Statistics cache created');
}
2019-07-21 16:59:47 +02:00
public async get2HStatistics(req, res) {
const result = await statistics.$list2H();
res.send(result);
}
public get24HStatistics(req, res) {
res.send(this.cache['24h']);
2019-07-21 16:59:47 +02:00
}
public get1WHStatistics(req, res) {
res.send(this.cache['1w']);
2019-07-21 16:59:47 +02:00
}
public get1MStatistics(req, res) {
res.send(this.cache['1m']);
2019-07-21 16:59:47 +02:00
}
public get3MStatistics(req, res) {
res.send(this.cache['3m']);
2019-07-21 16:59:47 +02:00
}
public get6MStatistics(req, res) {
res.send(this.cache['6m']);
2019-07-21 16:59:47 +02:00
}
public async getRecommendedFees(req, res) {
const result = feeApi.getRecommendedFee();
res.send(result);
}
public async $getgetTransactionsForBlock(req, res) {
const result = await feeApi.$getTransactionsForBlock(req.params.id);
res.send(result);
}
public async getgetTransactionsForProjectedBlock(req, res) {
try {
const result = await projectedBlocks.getProjectedBlockFeesForBlock(req.params.id);
res.send(result);
} catch (e) {
res.status(500).send(e.message);
}
}
2019-07-26 16:51:12 +02:00
public async getProjectedBlocks(req, res) {
try {
let txId: string | undefined;
if (req.query.txId && /^[a-fA-F0-9]{64}$/.test(req.query.txId)) {
txId = req.query.txId;
2019-07-26 16:51:12 +02:00
}
const result = await projectedBlocks.getProjectedBlocks(txId, 6);
res.send(result);
} catch (e) {
res.status(500).send(e.message);
}
}
2019-11-06 08:35:02 +01:00
public async getBlocks(req, res) {
try {
let result: string;
if (req.params.height) {
result = await bitcoinApi.getBlocksFromHeight(req.params.height);
} else {
result = await bitcoinApi.getBlocks();
}
res.send(result);
} catch (e) {
res.status(500).send(e.message);
}
}
2019-11-10 09:44:00 +01:00
public async getRawTransaction(req, res) {
try {
const result = await bitcoinApi.getRawTransaction(req.params.id);
res.send(result);
} catch (e) {
2019-11-14 08:03:01 +01:00
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
2019-11-14 09:31:40 +01:00
res.status(500, e.message);
2019-11-14 08:03:01 +01:00
}
2019-11-10 09:44:00 +01:00
}
}
2019-11-12 09:39:59 +01:00
public async getBlock(req, res) {
try {
const result = await bitcoinApi.getBlock(req.params.hash);
res.send(result);
} catch (e) {
2019-11-14 08:03:01 +01:00
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
2019-11-14 09:31:40 +01:00
res.status(500, e.message);
2019-11-14 08:03:01 +01:00
}
2019-11-12 09:39:59 +01:00
}
}
public async getBlockTransactions(req, res) {
try {
const result = await bitcoinApi.getBlockTransactions(req.params.hash);
res.send(result);
} catch (e) {
2019-11-14 08:03:01 +01:00
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
2019-11-14 09:31:40 +01:00
res.status(500, e.message);
2019-11-14 08:03:01 +01:00
}
}
2019-11-12 09:39:59 +01:00
}
}
public async getBlockTransactionsFromIndex(req, res) {
try {
const result = await bitcoinApi.getBlockTransactionsFromIndex(req.params.hash, req.params.index);
res.send(result);
} catch (e) {
2019-11-14 08:03:01 +01:00
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
2019-11-14 09:31:40 +01:00
res.status(500, e.message);
2019-11-14 08:03:01 +01:00
}
}
2019-11-12 09:39:59 +01:00
}
}
2019-11-13 07:51:44 +01:00
public async getAddress(req, res) {
try {
const result = await bitcoinApi.getAddress(req.params.address);
res.send(result);
} catch (e) {
2019-11-14 08:03:01 +01:00
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
2019-11-14 09:31:40 +01:00
res.status(500, e.message);
2019-11-14 08:03:01 +01:00
}
}
2019-11-13 07:51:44 +01:00
}
}
public async getAddressTransactions(req, res) {
try {
const result = await bitcoinApi.getAddressTransactions(req.params.address);
res.send(result);
} catch (e) {
2019-11-14 08:03:01 +01:00
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
2019-11-14 09:31:40 +01:00
res.status(500, e.message);
2019-11-14 08:03:01 +01:00
}
2019-11-13 07:51:44 +01:00
}
}
public async getAddressTransactionsFromTxid(req, res) {
try {
const result = await bitcoinApi.getAddressTransactionsFromLastSeenTxid(req.params.address, req.params.txid);
res.send(result);
} catch (e) {
2019-11-14 08:03:01 +01:00
if (e.response) {
res.status(e.response.status).send(e.response.data);
} else {
2019-11-14 09:31:40 +01:00
res.status(500, e.message);
2019-11-14 08:03:01 +01:00
}
2019-11-13 07:51:44 +01:00
}
}
2019-07-21 16:59:47 +02:00
}
export default new Routes();