2020-02-16 16:15:07 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
|
|
|
|
import { WebsocketResponse } from '../interfaces/websocket.interface';
|
|
|
|
import { retryWhen, tap, delay } from 'rxjs/operators';
|
|
|
|
import { StateService } from './state.service';
|
2020-02-23 13:16:50 +01:00
|
|
|
import { Block, Transaction } from '../interfaces/electrs.interface';
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
const WEB_SOCKET_PROTOCOL = (document.location.protocol === 'https:') ? 'wss:' : 'ws:';
|
2020-02-23 21:43:00 +01:00
|
|
|
const WEB_SOCKET_URL = WEB_SOCKET_PROTOCOL + '//' + document.location.hostname + ':' + document.location.port + '/ws';
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class WebsocketService {
|
|
|
|
private websocketSubject: WebSocketSubject<WebsocketResponse> = webSocket<WebsocketResponse | any>(WEB_SOCKET_URL);
|
|
|
|
private goneOffline = false;
|
|
|
|
private lastWant: string[] | null = null;
|
|
|
|
private trackingTxId: string | null = null;
|
2020-02-19 17:50:23 +01:00
|
|
|
private trackingAddress: string | null = null;
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private stateService: StateService,
|
|
|
|
) {
|
|
|
|
this.startSubscription();
|
|
|
|
}
|
|
|
|
|
|
|
|
startSubscription() {
|
2020-02-17 14:39:20 +01:00
|
|
|
this.websocketSubject.next({'action': 'init'});
|
2020-02-16 16:15:07 +01:00
|
|
|
this.websocketSubject
|
|
|
|
.subscribe((response: WebsocketResponse) => {
|
|
|
|
if (response.blocks && response.blocks.length) {
|
|
|
|
const blocks = response.blocks;
|
2020-02-17 14:39:20 +01:00
|
|
|
blocks.forEach((block: Block) => {
|
|
|
|
if (block.height > this.stateService.latestBlockHeight) {
|
|
|
|
this.stateService.latestBlockHeight = block.height;
|
|
|
|
this.stateService.blocks$.next(block);
|
|
|
|
}
|
|
|
|
});
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (response.block) {
|
2020-02-17 14:39:20 +01:00
|
|
|
if (response.block.height > this.stateService.latestBlockHeight) {
|
|
|
|
this.stateService.latestBlockHeight = response.block.height;
|
2020-02-16 16:15:07 +01:00
|
|
|
this.stateService.blocks$.next(response.block);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.txConfirmed) {
|
|
|
|
this.trackingTxId = null;
|
2020-02-23 13:16:50 +01:00
|
|
|
this.stateService.txConfirmed$.next(response.block);
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.conversions) {
|
|
|
|
this.stateService.conversions$.next(response.conversions);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response['mempool-blocks']) {
|
|
|
|
this.stateService.mempoolBlocks$.next(response['mempool-blocks']);
|
|
|
|
}
|
|
|
|
|
2020-02-23 13:16:50 +01:00
|
|
|
if (response['address-transactions']) {
|
|
|
|
response['address-transactions'].forEach((addressTransaction: Transaction) => {
|
|
|
|
this.stateService.mempoolTransactions$.next(addressTransaction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response['address-block-transactions']) {
|
|
|
|
response['address-block-transactions'].forEach((addressTransaction: Transaction) => {
|
|
|
|
this.stateService.blockTransactions$.next(addressTransaction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-17 14:39:20 +01:00
|
|
|
if (response['live-2h-chart']) {
|
|
|
|
this.stateService.live2Chart$.next(response['live-2h-chart']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.mempoolInfo) {
|
|
|
|
this.stateService.mempoolStats$.next({
|
|
|
|
memPoolInfo: response.mempoolInfo,
|
|
|
|
vBytesPerSecond: response.vBytesPerSecond,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
if (this.goneOffline === true) {
|
|
|
|
this.goneOffline = false;
|
|
|
|
if (this.lastWant) {
|
|
|
|
this.want(this.lastWant);
|
|
|
|
}
|
|
|
|
if (this.trackingTxId) {
|
2020-02-19 17:50:23 +01:00
|
|
|
this.startTrackTransaction(this.trackingTxId);
|
|
|
|
}
|
|
|
|
if (this.trackingAddress) {
|
|
|
|
this.startTrackTransaction(this.trackingAddress);
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|
|
|
|
this.stateService.isOffline$.next(false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
console.log(err);
|
|
|
|
this.goneOffline = true;
|
2020-02-26 17:21:16 +01:00
|
|
|
this.stateService.isOffline$.next(true);
|
2020-02-16 16:15:07 +01:00
|
|
|
console.log('Error, retrying in 10 sec');
|
|
|
|
window.setTimeout(() => this.startSubscription(), 10000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-19 17:50:23 +01:00
|
|
|
startTrackTransaction(txId: string) {
|
|
|
|
this.websocketSubject.next({ 'track-tx': txId });
|
2020-02-16 16:15:07 +01:00
|
|
|
this.trackingTxId = txId;
|
|
|
|
}
|
|
|
|
|
2020-02-19 17:50:23 +01:00
|
|
|
startTrackAddress(address: string) {
|
|
|
|
this.websocketSubject.next({ 'track-address': address });
|
|
|
|
this.trackingAddress = address;
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
fetchStatistics(historicalDate: string) {
|
|
|
|
this.websocketSubject.next({ historicalDate });
|
|
|
|
}
|
|
|
|
|
|
|
|
want(data: string[]) {
|
2020-02-17 14:39:20 +01:00
|
|
|
this.websocketSubject.next({action: 'want', data: data});
|
2020-02-16 16:15:07 +01:00
|
|
|
this.lastWant = data;
|
|
|
|
}
|
|
|
|
}
|