mempool/backend/src/api/bitcoin/esplora-api.ts

185 lines
5.2 KiB
TypeScript
Raw Normal View History

const config = require('../../../mempool-config.json');
import { ITransaction, IMempoolInfo, IBlock } from '../../interfaces';
import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory';
import * as request from 'request';
class EsploraApi implements AbstractBitcoinApi {
constructor() {
}
getMempoolInfo(): Promise<IMempoolInfo> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/mempool', { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve({
size: response.count,
bytes: response.vsize,
});
});
});
}
getRawMempool(): Promise<ITransaction['txid'][]> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/mempool/txids', { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
});
}
getRawTransaction(txId: string): Promise<ITransaction> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/tx/' + txId, { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
response.vsize = Math.round(response.weight / 4);
response.fee = response.fee / 100000000;
response.blockhash = response.status.block_hash;
resolve(response);
});
});
}
getBlockCount(): Promise<number> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/blocks/tip/height', { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
});
}
2019-11-12 09:39:59 +01:00
getBlockAndTransactions(hash: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/block/' + hash, { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
request(config.ESPLORA_API_URL + '/block/' + hash + '/txids', { json: true }, (err2, res2, response2) => {
if (err2) {
reject(err2);
}
const block = response;
block.hash = hash;
block.nTx = block.tx_count;
block.time = block.timestamp;
block.tx = response2;
resolve(block);
});
});
});
}
getBlockHash(height: number): Promise<string> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/block-height/' + height, { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
});
}
2019-11-06 08:35:02 +01:00
getBlocks(): Promise<string> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/blocks', { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
2019-11-06 08:35:02 +01:00
});
}
getBlocksFromHeight(height: number): Promise<string> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/blocks/' + height, { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
2019-11-06 08:35:02 +01:00
});
}
2019-11-12 09:39:59 +01:00
getBlock(hash: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/block/' + hash, { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
2019-11-12 09:39:59 +01:00
});
}
getBlockTransactions(hash: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/block/' + hash + '/txs', { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
2019-11-12 09:39:59 +01:00
});
}
getBlockTransactionsFromIndex(hash: string, index: number): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/block/' + hash + '/txs/' + index, { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
2019-11-12 09:39:59 +01:00
});
}
2019-11-13 07:51:44 +01:00
getAddress(address: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/address/' + address, { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
2019-11-13 07:51:44 +01:00
});
}
getAddressTransactions(address: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/address/' + address + '/txs', { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
2019-11-13 07:51:44 +01:00
});
}
getAddressTransactionsFromLastSeenTxid(address: string, lastSeenTxid: string): Promise<IBlock> {
return new Promise(async (resolve, reject) => {
request(config.ESPLORA_API_URL + '/address/' + address + '/txs/chain/' + lastSeenTxid, { json: true }, (err, res, response) => {
if (err) {
reject(err);
}
resolve(response);
});
2019-11-13 07:51:44 +01:00
});
}
}
export default EsploraApi;