import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { Block, Transaction, Address, Outspend, Recent, Asset } from '../interfaces/electrs.interface'; import { StateService } from './state.service'; const API_BASE_URL = document.location.protocol + '//' + document.location.hostname + ':' + document.location.port + '/electrs'; @Injectable({ providedIn: 'root' }) export class ElectrsApiService { apiBaseUrl: string; constructor( private httpClient: HttpClient, private stateService: StateService, ) { this.apiBaseUrl = API_BASE_URL; this.stateService.networkChanged$.subscribe((network) => { this.apiBaseUrl = API_BASE_URL + '/' + network; }); } getBlock$(hash: string): Observable { return this.httpClient.get(this.apiBaseUrl + '/block/' + hash); } listBlocks$(height?: number): Observable { return this.httpClient.get(this.apiBaseUrl + '/blocks/' + (height || '')); } getTransaction$(txId: string): Observable { return this.httpClient.get(this.apiBaseUrl + '/tx/' + txId); } getRecentTransaction$(): Observable { return this.httpClient.get(this.apiBaseUrl + '/mempool/recent'); } getOutspend$(hash: string, vout: number): Observable { return this.httpClient.get(this.apiBaseUrl + '/tx/' + hash + '/outspend/' + vout); } getOutspends$(hash: string): Observable { return this.httpClient.get(this.apiBaseUrl + '/tx/' + hash + '/outspends'); } getBlockTransactions$(hash: string, index: number = 0): Observable { return this.httpClient.get(this.apiBaseUrl + '/block/' + hash + '/txs/' + index); } getBlockHashFromHeight$(height: number): Observable { return this.httpClient.get(this.apiBaseUrl + '/block-height/' + height, {responseType: 'text'}); } getAddress$(address: string): Observable
{ return this.httpClient.get
(this.apiBaseUrl + '/address/' + address); } getAddressTransactions$(address: string): Observable { return this.httpClient.get(this.apiBaseUrl + '/address/' + address + '/txs'); } getAddressTransactionsFromHash$(address: string, txid: string): Observable { return this.httpClient.get(this.apiBaseUrl + '/address/' + address + '/txs/chain/' + txid); } getAsset$(assetId: string): Observable { return this.httpClient.get(this.apiBaseUrl + '/asset/' + assetId); } getAssetTransactions$(assetId: string): Observable { return this.httpClient.get(this.apiBaseUrl + '/asset/' + assetId + '/txs'); } getAssetTransactionsFromHash$(assetId: string, txid: string): Observable { return this.httpClient.get(this.apiBaseUrl + '/asset/' + assetId + '/txs/chain/' + txid); } }