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

170 lines
6.0 KiB
TypeScript
Raw Normal View History

2019-07-21 17:59:47 +03:00
import { Injectable } from '@angular/core';
import { webSocket } from 'rxjs/webSocket';
import { HttpClient, HttpParams } from '@angular/common/http';
2019-07-24 23:08:28 +03:00
import { IMempoolDefaultResponse, IMempoolStats, IBlockTransaction, IBlock } from '../blockchain/interfaces';
2019-07-21 17:59:47 +03:00
import { Observable } from 'rxjs';
2019-07-24 23:08:28 +03:00
import { MemPoolService } from './mem-pool.service';
2019-07-25 14:20:02 +03:00
import { tap, retryWhen, delay } from 'rxjs/operators';
2019-07-21 17:59:47 +03:00
const WEB_SOCKET_PROTOCOL = (document.location.protocol === 'https:') ? 'wss:' : 'ws:';
const WEB_SOCKET_URL = WEB_SOCKET_PROTOCOL + '//' + document.location.hostname + ':' + document.location.port + '/ws';
2019-07-21 18:35:58 +03:00
const API_BASE_URL = '/api/v1';
2019-07-21 17:59:47 +03:00
@Injectable({
providedIn: 'root'
})
export class ApiService {
private websocketSubject: Observable<IMempoolDefaultResponse> = webSocket<IMempoolDefaultResponse | any>(WEB_SOCKET_URL);
2019-08-09 21:45:31 +03:00
private lastWant: string[] | null = null;
private goneOffline = false;
private lastTrackedTxId = '';
2019-07-24 23:08:28 +03:00
2019-07-21 17:59:47 +03:00
constructor(
private httpClient: HttpClient,
2019-07-24 23:08:28 +03:00
private memPoolService: MemPoolService,
) {
this.startSubscription();
}
startSubscription() {
this.websocketSubject
.pipe(
retryWhen((errors: any) => errors
.pipe(
2019-08-09 21:45:31 +03:00
tap(() => {
this.goneOffline = true;
this.memPoolService.isOffline$.next(true);
}),
2019-07-25 14:20:02 +03:00
delay(5000),
2019-07-24 23:08:28 +03:00
)
),
)
.subscribe((response: IMempoolDefaultResponse) => {
this.memPoolService.isOffline$.next(false);
if (response.blocks && response.blocks.length) {
const blocks = response.blocks;
// blocks.reverse();
blocks.forEach((block: IBlock) => this.memPoolService.blocks$.next(block));
}
if (response.block) {
this.memPoolService.blocks$.next(response.block);
}
if (response.projectedBlocks && response.projectedBlocks.length) {
const mempoolWeight = response.projectedBlocks.map((block: any) => block.blockWeight).reduce((a: any, b: any) => a + b);
this.memPoolService.mempoolWeight$.next(mempoolWeight);
2019-07-24 23:08:28 +03:00
this.memPoolService.projectedBlocks$.next(response.projectedBlocks);
}
if (response.mempoolInfo && response.txPerSecond !== undefined) {
this.memPoolService.mempoolStats$.next({
memPoolInfo: response.mempoolInfo,
txPerSecond: response.txPerSecond,
vBytesPerSecond: response.vBytesPerSecond,
});
}
2019-07-21 17:59:47 +03:00
2019-07-24 23:08:28 +03:00
if (response.conversions) {
this.memPoolService.conversions$.next(response.conversions);
}
if (response['track-tx'] && !this.goneOffline) {
2019-07-24 23:08:28 +03:00
let txTrackingEnabled;
let txTrackingBlockHeight;
let txTrackingTx = null;
let txShowTxNotFound = false;
if (response['track-tx'].tracking) {
txTrackingEnabled = true;
txTrackingBlockHeight = response['track-tx'].blockHeight;
if (response['track-tx'].tx) {
txTrackingTx = response['track-tx'].tx;
}
} else {
txTrackingEnabled = false;
txTrackingTx = null;
txTrackingBlockHeight = 0;
}
if (response['track-tx'].message && response['track-tx'].message === 'not-found') {
txShowTxNotFound = true;
}
this.memPoolService.txTracking$.next({
enabled: txTrackingEnabled,
tx: txTrackingTx,
blockHeight: txTrackingBlockHeight,
notFound: txShowTxNotFound,
});
}
if (response['live-2h-chart']) {
this.memPoolService.live2Chart$.next(response['live-2h-chart']);
}
2019-08-09 21:45:31 +03:00
if (this.goneOffline === true) {
this.goneOffline = false;
if (this.lastWant) {
this.webSocketWant(this.lastWant);
}
if (this.memPoolService.txTracking$.value.enabled) {
this.webSocketStartTrackTx(this.lastTrackedTxId);
}
2019-08-09 21:45:31 +03:00
}
},
2019-07-24 23:08:28 +03:00
(err: Error) => {
console.log(err);
2019-08-09 21:45:31 +03:00
this.goneOffline = true;
2019-07-24 23:08:28 +03:00
console.log('Error, retrying in 10 sec');
setTimeout(() => this.startSubscription(), 10000);
});
2019-07-24 23:08:28 +03:00
}
2019-08-09 21:45:31 +03:00
webSocketStartTrackTx(txId: string) {
// @ts-ignore
this.websocketSubject.next({'action': 'track-tx', 'txId': txId});
this.lastTrackedTxId = txId;
2019-08-09 21:45:31 +03:00
}
webSocketWant(data: string[]) {
2019-07-24 23:08:28 +03:00
// @ts-ignore
2019-08-09 21:45:31 +03:00
this.websocketSubject.next({'action': 'want', data: data});
this.lastWant = data;
2019-07-24 23:08:28 +03:00
}
2019-07-21 17:59:47 +03:00
listTransactionsForBlock$(height: number): Observable<IBlockTransaction[]> {
return this.httpClient.get<IBlockTransaction[]>(API_BASE_URL + '/transactions/height/' + height);
}
listTransactionsForProjectedBlock$(index: number): Observable<IBlockTransaction[]> {
return this.httpClient.get<IBlockTransaction[]>(API_BASE_URL + '/transactions/projected/' + index);
}
list2HStatistics$(): Observable<IMempoolStats[]> {
return this.httpClient.get<IMempoolStats[]>(API_BASE_URL + '/statistics/2h');
}
list24HStatistics$(): Observable<IMempoolStats[]> {
return this.httpClient.get<IMempoolStats[]>(API_BASE_URL + '/statistics/24h');
}
list1WStatistics$(): Observable<IMempoolStats[]> {
return this.httpClient.get<IMempoolStats[]>(API_BASE_URL + '/statistics/1w');
}
list1MStatistics$(): Observable<IMempoolStats[]> {
return this.httpClient.get<IMempoolStats[]>(API_BASE_URL + '/statistics/1m');
}
list3MStatistics$(): Observable<IMempoolStats[]> {
return this.httpClient.get<IMempoolStats[]>(API_BASE_URL + '/statistics/3m');
}
list6MStatistics$(): Observable<IMempoolStats[]> {
return this.httpClient.get<IMempoolStats[]>(API_BASE_URL + '/statistics/6m');
}
2019-11-06 15:35:02 +08:00
listBlocks$(height?: number): Observable<IBlockTransaction[]> {
return this.httpClient.get<IBlockTransaction[]>(API_BASE_URL + '/explorer/blocks/' + (height || ''));
}
2019-07-21 17:59:47 +03:00
}