2020-02-16 22:15:07 +07:00
|
|
|
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';
|
2020-05-09 20:37:50 +07:00
|
|
|
import { StateService } from './state.service';
|
2020-02-16 22:15:07 +07:00
|
|
|
|
2020-02-24 03:43:00 +07:00
|
|
|
const API_BASE_URL = document.location.protocol + '//' + document.location.hostname + ':' + document.location.port + '/electrs';
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ElectrsApiService {
|
2020-05-09 20:37:50 +07:00
|
|
|
apiBaseUrl: string;
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
constructor(
|
|
|
|
private httpClient: HttpClient,
|
2020-05-09 20:37:50 +07:00
|
|
|
private stateService: StateService,
|
2020-02-16 22:15:07 +07:00
|
|
|
) {
|
2020-05-09 20:37:50 +07:00
|
|
|
this.apiBaseUrl = API_BASE_URL;
|
|
|
|
this.stateService.networkChanged$.subscribe((network) => {
|
|
|
|
this.apiBaseUrl = API_BASE_URL + '/' + network;
|
|
|
|
});
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getBlock$(hash: string): Observable<Block> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Block>(this.apiBaseUrl + '/block/' + hash);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
listBlocks$(height?: number): Observable<Block[]> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Block[]>(this.apiBaseUrl + '/blocks/' + (height || ''));
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getTransaction$(txId: string): Observable<Transaction> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Transaction>(this.apiBaseUrl + '/tx/' + txId);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getRecentTransaction$(): Observable<Recent[]> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Recent[]>(this.apiBaseUrl + '/mempool/recent');
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getOutspend$(hash: string, vout: number): Observable<Outspend> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Outspend>(this.apiBaseUrl + '/tx/' + hash + '/outspend/' + vout);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getOutspends$(hash: string): Observable<Outspend[]> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Outspend[]>(this.apiBaseUrl + '/tx/' + hash + '/outspends');
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getBlockTransactions$(hash: string, index: number = 0): Observable<Transaction[]> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/block/' + hash + '/txs/' + index);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getAddress$(address: string): Observable<Address> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Address>(this.apiBaseUrl + '/address/' + address);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getAddressTransactions$(address: string): Observable<Transaction[]> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/address/' + address + '/txs');
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getAddressTransactionsFromHash$(address: string, txid: string): Observable<Transaction[]> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/address/' + address + '/txs/chain/' + txid);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2020-04-28 17:10:31 +07:00
|
|
|
getAsset$(assetId: string): Observable<Asset> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Asset>(this.apiBaseUrl + '/asset/' + assetId);
|
2020-04-28 17:10:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getAssetTransactions$(assetId: string): Observable<Transaction[]> {
|
2020-05-09 20:37:50 +07:00
|
|
|
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[]> {
|
2020-05-09 20:37:50 +07:00
|
|
|
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/asset/' + assetId + '/txs/chain/' + txid);
|
2020-04-28 17:10:31 +07:00
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|