mempool/frontend/src/app/services/electrs-api.service.ts

91 lines
3.3 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
2020-04-28 17:10:31 +07:00
import { Block, Transaction, Address, Outspend, Recent, Asset } from '../interfaces/electrs.interface';
import { StateService } from './state.service';
2020-11-07 04:30:52 +07:00
let API_BASE_URL = '{network}/api';
@Injectable({
providedIn: 'root'
})
export class ElectrsApiService {
2020-11-07 04:30:52 +07:00
private apiBaseUrl: string;
constructor(
private httpClient: HttpClient,
private stateService: StateService,
) {
if (!stateService.isBrowser) {
2020-11-23 21:59:30 +07:00
API_BASE_URL = this.stateService.env.ELECTRS_URL;
2020-11-07 04:30:52 +07:00
}
this.apiBaseUrl = API_BASE_URL.replace('{network}', '');
this.stateService.networkChanged$.subscribe((network) => {
2020-07-25 21:59:52 +07:00
if (network === 'bisq') {
network = '';
}
this.apiBaseUrl = API_BASE_URL.replace('{network}', network ? '/' + network : '');
});
}
getBlock$(hash: string): Observable<Block> {
return this.httpClient.get<Block>(this.apiBaseUrl + '/block/' + hash);
}
listBlocks$(height?: number): Observable<Block[]> {
return this.httpClient.get<Block[]>(this.apiBaseUrl + '/blocks/' + (height || ''));
}
getTransaction$(txId: string): Observable<Transaction> {
return this.httpClient.get<Transaction>(this.apiBaseUrl + '/tx/' + txId);
}
getRecentTransaction$(): Observable<Recent[]> {
return this.httpClient.get<Recent[]>(this.apiBaseUrl + '/mempool/recent');
}
getOutspend$(hash: string, vout: number): Observable<Outspend> {
return this.httpClient.get<Outspend>(this.apiBaseUrl + '/tx/' + hash + '/outspend/' + vout);
}
getOutspends$(hash: string): Observable<Outspend[]> {
return this.httpClient.get<Outspend[]>(this.apiBaseUrl + '/tx/' + hash + '/outspends');
}
getBlockTransactions$(hash: string, index: number = 0): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/block/' + hash + '/txs/' + index);
}
getBlockHashFromHeight$(height: number): Observable<string> {
return this.httpClient.get(this.apiBaseUrl + '/block-height/' + height, {responseType: 'text'});
}
getAddress$(address: string): Observable<Address> {
return this.httpClient.get<Address>(this.apiBaseUrl + '/address/' + address);
}
getAddressTransactions$(address: string): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/address/' + address + '/txs');
}
getAddressTransactionsFromHash$(address: string, txid: string): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/address/' + address + '/txs/chain/' + txid);
}
2020-04-28 17:10:31 +07:00
getAsset$(assetId: string): Observable<Asset> {
return this.httpClient.get<Asset>(this.apiBaseUrl + '/asset/' + assetId);
2020-04-28 17:10:31 +07:00
}
getAssetTransactions$(assetId: string): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/asset/' + assetId + '/txs');
2020-04-28 17:10:31 +07:00
}
getAssetTransactionsFromHash$(assetId: string, txid: string): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/asset/' + assetId + '/txs/chain/' + txid);
2020-04-28 17:10:31 +07:00
}
2020-07-24 22:37:35 +07:00
getAddressesByPrefix$(prefix: string): Observable<string[]> {
return this.httpClient.get<string[]>(this.apiBaseUrl + '/address-prefix/' + prefix);
}
}