2020-10-19 11:57:02 +07:00
|
|
|
import config from '../../config';
|
2020-02-17 20:39:20 +07:00
|
|
|
import { Transaction, Block, MempoolInfo } from '../../interfaces';
|
2020-11-15 14:22:47 +07:00
|
|
|
import axios from 'axios';
|
2019-10-22 17:09:07 +08:00
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
class ElectrsApi {
|
2019-10-22 17:09:07 +08:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
2020-02-17 20:39:20 +07:00
|
|
|
getMempoolInfo(): Promise<MempoolInfo> {
|
2020-11-15 14:22:47 +07:00
|
|
|
return axios.get<any>(config.ELECTRS.REST_API_URL + '/mempool', { timeout: 10000 })
|
|
|
|
.then((response) => {
|
|
|
|
return {
|
|
|
|
size: response.data.count,
|
|
|
|
bytes: response.data.vsize,
|
|
|
|
};
|
2020-02-17 20:39:20 +07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
getRawMempool(): Promise<Transaction['txid'][]> {
|
2020-11-15 14:22:47 +07:00
|
|
|
return axios.get<Transaction['txid'][]>(config.ELECTRS.REST_API_URL + '/mempool/txids')
|
|
|
|
.then((response) => response.data);
|
2019-10-22 17:09:07 +08:00
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
getRawTransaction(txId: string): Promise<Transaction> {
|
2020-11-15 14:22:47 +07:00
|
|
|
return axios.get<Transaction>(config.ELECTRS.REST_API_URL + '/tx/' + txId)
|
|
|
|
.then((response) => response.data);
|
2019-10-22 17:09:07 +08:00
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
getBlockHeightTip(): Promise<number> {
|
2020-11-15 14:22:47 +07:00
|
|
|
return axios.get<number>(config.ELECTRS.REST_API_URL + '/blocks/tip/height')
|
|
|
|
.then((response) => response.data);
|
2019-10-22 17:09:07 +08:00
|
|
|
}
|
|
|
|
|
2020-02-23 19:16:50 +07:00
|
|
|
getTxIdsForBlock(hash: string): Promise<string[]> {
|
2020-11-15 14:22:47 +07:00
|
|
|
return axios.get<string[]>(config.ELECTRS.REST_API_URL + '/block/' + hash + '/txids')
|
|
|
|
.then((response) => response.data);
|
2019-10-22 17:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getBlockHash(height: number): Promise<string> {
|
2020-11-15 14:22:47 +07:00
|
|
|
return axios.get<string>(config.ELECTRS.REST_API_URL + '/block-height/' + height)
|
|
|
|
.then((response) => response.data);
|
2019-10-22 17:09:07 +08:00
|
|
|
}
|
2019-11-06 15:35:02 +08:00
|
|
|
|
|
|
|
getBlocksFromHeight(height: number): Promise<string> {
|
2020-11-15 14:22:47 +07:00
|
|
|
return axios.get<string>(config.ELECTRS.REST_API_URL + '/blocks/' + height)
|
|
|
|
.then((response) => response.data);
|
2019-11-06 15:35:02 +08:00
|
|
|
}
|
2019-11-12 16:39:59 +08:00
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
getBlock(hash: string): Promise<Block> {
|
2020-11-15 14:22:47 +07:00
|
|
|
return axios.get<Block>(config.ELECTRS.REST_API_URL + '/block/' + hash)
|
|
|
|
.then((response) => response.data);
|
2019-11-12 16:39:59 +08:00
|
|
|
}
|
2019-10-22 17:09:07 +08:00
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
export default new ElectrsApi();
|