2020-12-28 04:47:22 +07:00
|
|
|
import config from '../../config';
|
2021-01-30 16:25:22 +07:00
|
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
2020-12-28 04:47:22 +07:00
|
|
|
import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory';
|
|
|
|
import { IEsploraApi } from './esplora-api.interface';
|
|
|
|
|
|
|
|
class ElectrsApi implements AbstractBitcoinApi {
|
2021-01-30 16:25:22 +07:00
|
|
|
axiosConfig: AxiosRequestConfig = {
|
|
|
|
timeout: 10000,
|
|
|
|
};
|
2020-12-28 04:47:22 +07:00
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
$getRawMempool(): Promise<IEsploraApi.Transaction['txid'][]> {
|
2021-01-30 16:25:22 +07:00
|
|
|
return axios.get<IEsploraApi.Transaction['txid'][]>(config.ESPLORA.REST_API_URL + '/mempool/txids', this.axiosConfig)
|
2020-12-28 04:47:22 +07:00
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$getRawTransaction(txId: string): Promise<IEsploraApi.Transaction> {
|
2021-01-30 16:25:22 +07:00
|
|
|
return axios.get<IEsploraApi.Transaction>(config.ESPLORA.REST_API_URL + '/tx/' + txId, this.axiosConfig)
|
2020-12-28 04:47:22 +07:00
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$getBlockHeightTip(): Promise<number> {
|
2021-01-30 16:25:22 +07:00
|
|
|
return axios.get<number>(config.ESPLORA.REST_API_URL + '/blocks/tip/height', this.axiosConfig)
|
2020-12-28 04:47:22 +07:00
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
2022-06-22 13:15:44 +02:00
|
|
|
$getBlockHashTip(): Promise<string> {
|
|
|
|
return axios.get<string>(config.ESPLORA.REST_API_URL + '/blocks/tip/hash', this.axiosConfig)
|
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
2020-12-28 04:47:22 +07:00
|
|
|
$getTxIdsForBlock(hash: string): Promise<string[]> {
|
2021-01-30 16:25:22 +07:00
|
|
|
return axios.get<string[]>(config.ESPLORA.REST_API_URL + '/block/' + hash + '/txids', this.axiosConfig)
|
2020-12-28 04:47:22 +07:00
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$getBlockHash(height: number): Promise<string> {
|
2021-01-30 16:25:22 +07:00
|
|
|
return axios.get<string>(config.ESPLORA.REST_API_URL + '/block-height/' + height, this.axiosConfig)
|
2020-12-28 04:47:22 +07:00
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:56:16 +05:30
|
|
|
$getBlockHeader(hash: string): Promise<string> {
|
|
|
|
return axios.get<string>(config.ESPLORA.REST_API_URL + '/block/' + hash + '/header', this.axiosConfig)
|
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
2020-12-28 04:47:22 +07:00
|
|
|
$getBlock(hash: string): Promise<IEsploraApi.Block> {
|
2021-01-30 16:25:22 +07:00
|
|
|
return axios.get<IEsploraApi.Block>(config.ESPLORA.REST_API_URL + '/block/' + hash, this.axiosConfig)
|
2020-12-28 04:47:22 +07:00
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
2022-07-25 14:54:00 -03:00
|
|
|
$getRawBlock(hash: string): Promise<string> {
|
|
|
|
return axios.get<string>(config.ESPLORA.REST_API_URL + '/block/' + hash + "/raw", this.axiosConfig)
|
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
2020-12-28 04:47:22 +07:00
|
|
|
$getAddress(address: string): Promise<IEsploraApi.Address> {
|
|
|
|
throw new Error('Method getAddress not implemented.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$getAddressTransactions(address: string, txId?: string): Promise<IEsploraApi.Transaction[]> {
|
|
|
|
throw new Error('Method getAddressTransactions not implemented.');
|
|
|
|
}
|
|
|
|
|
2021-01-11 01:51:57 +07:00
|
|
|
$getAddressPrefix(prefix: string): string[] {
|
|
|
|
throw new Error('Method not implemented.');
|
|
|
|
}
|
2021-09-26 22:18:44 +04:00
|
|
|
|
|
|
|
$sendRawTransaction(rawTransaction: string): Promise<string> {
|
|
|
|
throw new Error('Method not implemented.');
|
|
|
|
}
|
2022-01-15 22:09:04 +04:00
|
|
|
|
2022-07-06 11:58:06 +02:00
|
|
|
$getOutspend(txId: string, vout: number): Promise<IEsploraApi.Outspend> {
|
|
|
|
return axios.get<IEsploraApi.Outspend>(config.ESPLORA.REST_API_URL + '/tx/' + txId + '/outspend/' + vout, this.axiosConfig)
|
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
2022-06-22 23:34:44 +02:00
|
|
|
$getOutspends(txId: string): Promise<IEsploraApi.Outspend[]> {
|
2022-06-23 11:55:56 +02:00
|
|
|
return axios.get<IEsploraApi.Outspend[]>(config.ESPLORA.REST_API_URL + '/tx/' + txId + '/outspends', this.axiosConfig)
|
2022-06-22 23:34:44 +02:00
|
|
|
.then((response) => response.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async $getBatchedOutspends(txId: string[]): Promise<IEsploraApi.Outspend[][]> {
|
|
|
|
const outspends: IEsploraApi.Outspend[][] = [];
|
|
|
|
for (const tx of txId) {
|
|
|
|
const outspend = await this.$getOutspends(tx);
|
|
|
|
outspends.push(outspend);
|
|
|
|
}
|
|
|
|
return outspends;
|
2022-01-15 22:09:04 +04:00
|
|
|
}
|
2020-12-28 04:47:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ElectrsApi;
|