import config from '../../config'; import axios, { AxiosRequestConfig } from 'axios'; import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory'; import { IEsploraApi } from './esplora-api.interface'; class ElectrsApi implements AbstractBitcoinApi { axiosConfig: AxiosRequestConfig = { timeout: 10000, }; constructor() { } $getRawMempool(): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/mempool/txids', this.axiosConfig) .then((response) => response.data); } $getRawTransaction(txId: string): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/tx/' + txId, this.axiosConfig) .then((response) => response.data); } $getBlockHeightTip(): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/blocks/tip/height', this.axiosConfig) .then((response) => response.data); } $getBlockHashTip(): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/blocks/tip/hash', this.axiosConfig) .then((response) => response.data); } $getTxIdsForBlock(hash: string): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/block/' + hash + '/txids', this.axiosConfig) .then((response) => response.data); } $getBlockHash(height: number): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/block-height/' + height, this.axiosConfig) .then((response) => response.data); } $getBlockHeader(hash: string): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/block/' + hash + '/header', this.axiosConfig) .then((response) => response.data); } $getBlock(hash: string): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/block/' + hash, this.axiosConfig) .then((response) => response.data); } $getAddress(address: string): Promise { throw new Error('Method getAddress not implemented.'); } $getAddressTransactions(address: string, txId?: string): Promise { throw new Error('Method getAddressTransactions not implemented.'); } $getAddressPrefix(prefix: string): string[] { throw new Error('Method not implemented.'); } $sendRawTransaction(rawTransaction: string): Promise { throw new Error('Method not implemented.'); } $getOutspend(txId: string, vout: number): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/tx/' + txId + '/outspend/' + vout, this.axiosConfig) .then((response) => response.data); } $getOutspends(txId: string): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/tx/' + txId + '/outspends', this.axiosConfig) .then((response) => response.data); } async $getBatchedOutspends(txId: string[]): Promise { const outspends: IEsploraApi.Outspend[][] = []; for (const tx of txId) { const outspend = await this.$getOutspends(tx); outspends.push(outspend); } return outspends; } } export default ElectrsApi;