2019-10-22 11:09:07 +02:00
|
|
|
const config = require('../../../mempool-config.json');
|
|
|
|
import { ITransaction, IMempoolInfo, IBlock } from '../../interfaces';
|
|
|
|
import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory';
|
|
|
|
import axios, { AxiosResponse } from 'axios';
|
|
|
|
|
|
|
|
class EsploraApi implements AbstractBitcoinApi {
|
|
|
|
client: any;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.client = axios.create({
|
|
|
|
baseURL: config.ESPLORA_API_URL,
|
|
|
|
timeout: 15000,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getMempoolInfo(): Promise<IMempoolInfo> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response: AxiosResponse = await this.client.get('/mempool');
|
|
|
|
resolve({
|
|
|
|
size: response.data.count,
|
|
|
|
bytes: response.data.vsize,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getRawMempool(): Promise<ITransaction['txid'][]> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response: AxiosResponse = await this.client.get('/mempool/txids');
|
|
|
|
resolve(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getRawTransaction(txId: string): Promise<ITransaction> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response: AxiosResponse = await this.client.get('/tx/' + txId);
|
|
|
|
|
2019-10-28 09:47:42 +01:00
|
|
|
response.data.vsize = Math.round(response.data.weight / 4);
|
2019-10-22 11:09:07 +02:00
|
|
|
response.data.fee = response.data.fee / 100000000;
|
2019-10-24 05:15:24 +02:00
|
|
|
response.data.blockhash = response.data.status.block_hash;
|
2019-10-22 11:09:07 +02:00
|
|
|
|
|
|
|
resolve(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlockCount(): Promise<number> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response: AxiosResponse = await this.client.get('/blocks/tip/height');
|
|
|
|
resolve(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:39:59 +01:00
|
|
|
getBlockAndTransactions(hash: string): Promise<IBlock> {
|
2019-10-22 11:09:07 +02:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const blockInfo: AxiosResponse = await this.client.get('/block/' + hash);
|
|
|
|
const blockTxs: AxiosResponse = await this.client.get('/block/' + hash + '/txids');
|
|
|
|
|
|
|
|
const block = blockInfo.data;
|
|
|
|
block.hash = hash;
|
|
|
|
block.nTx = block.tx_count;
|
|
|
|
block.time = block.timestamp;
|
|
|
|
block.tx = blockTxs.data;
|
|
|
|
|
|
|
|
resolve(block);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlockHash(height: number): Promise<string> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response: AxiosResponse = await this.client.get('/block-height/' + height);
|
|
|
|
resolve(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-11-06 08:35:02 +01:00
|
|
|
|
|
|
|
getBlocks(): Promise<string> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response: AxiosResponse = await this.client.get('/blocks');
|
|
|
|
resolve(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlocksFromHeight(height: number): Promise<string> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response: AxiosResponse = await this.client.get('/blocks/' + height);
|
|
|
|
resolve(response.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-11-12 09:39:59 +01:00
|
|
|
|
|
|
|
getBlock(hash: string): Promise<IBlock> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const blockInfo: AxiosResponse = await this.client.get('/block/' + hash);
|
|
|
|
resolve(blockInfo.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlockTransactions(hash: string): Promise<IBlock> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const blockInfo: AxiosResponse = await this.client.get('/block/' + hash + '/txs');
|
|
|
|
resolve(blockInfo.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlockTransactionsFromIndex(hash: string, index: number): Promise<IBlock> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const blockInfo: AxiosResponse = await this.client.get('/block/' + hash + '/txs/' + index);
|
|
|
|
resolve(blockInfo.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-11-13 07:51:44 +01:00
|
|
|
|
|
|
|
getAddress(address: string): Promise<IBlock> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const blockInfo: AxiosResponse = await this.client.get('/address/' + address);
|
|
|
|
resolve(blockInfo.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getAddressTransactions(address: string): Promise<IBlock> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const blockInfo: AxiosResponse = await this.client.get('/address/' + address + '/txs');
|
|
|
|
resolve(blockInfo.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getAddressTransactionsFromLastSeenTxid(address: string, lastSeenTxid: string): Promise<IBlock> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const blockInfo: AxiosResponse = await this.client.get('/address/' + address + '/txs/chain/' + lastSeenTxid);
|
|
|
|
resolve(blockInfo.data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-10-22 11:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default EsploraApi;
|