2020-11-22 23:47:27 +07:00
|
|
|
import { Injectable } from '@angular/core';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
|
2023-07-12 16:25:00 +09:00
|
|
|
import { WebsocketResponse } from '../interfaces/websocket.interface';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { StateService } from './state.service';
|
2022-02-04 12:51:45 +09:00
|
|
|
import { Transaction } from '../interfaces/electrs.interface';
|
2023-10-28 23:30:58 +00:00
|
|
|
import { firstValueFrom, Subscription } from 'rxjs';
|
2020-11-07 04:30:52 +07:00
|
|
|
import { ApiService } from './api.service';
|
|
|
|
import { take } from 'rxjs/operators';
|
2023-10-18 23:04:21 +00:00
|
|
|
import { TransferState, makeStateKey } from '@angular/core';
|
2023-07-12 16:25:00 +09:00
|
|
|
import { CacheService } from './cache.service';
|
2024-02-08 22:40:22 +00:00
|
|
|
import { uncompressDeltaChange, uncompressTx } from '../shared/common.utils';
|
2020-02-16 22:15:07 +07:00
|
|
|
|
2023-05-01 18:01:07 -06:00
|
|
|
const OFFLINE_RETRY_AFTER_MS = 2000;
|
|
|
|
const OFFLINE_PING_CHECK_AFTER_MS = 30000;
|
2022-05-12 15:21:27 +04:00
|
|
|
const EXPECT_PING_RESPONSE_AFTER_MS = 5000;
|
2020-04-13 01:26:53 +07:00
|
|
|
|
2020-11-27 18:41:57 +07:00
|
|
|
const initData = makeStateKey('/api/v1/init-data');
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class WebsocketService {
|
2020-11-23 02:30:46 +07:00
|
|
|
private webSocketProtocol = (document.location.protocol === 'https:') ? 'wss:' : 'ws:';
|
|
|
|
private webSocketUrl = this.webSocketProtocol + '//' + document.location.hostname + ':' + document.location.port + '{network}/api/v1/ws';
|
|
|
|
|
2020-05-09 20:37:50 +07:00
|
|
|
private websocketSubject: WebSocketSubject<WebsocketResponse>;
|
2020-02-16 22:15:07 +07:00
|
|
|
private goneOffline = false;
|
2021-03-05 15:38:46 +07:00
|
|
|
private lastWant: string | null = null;
|
2020-04-12 03:03:51 +07:00
|
|
|
private isTrackingTx = false;
|
2021-08-09 13:01:29 +03:00
|
|
|
private trackingTxId: string;
|
2022-05-31 18:00:40 +00:00
|
|
|
private isTrackingMempoolBlock = false;
|
2023-08-03 16:17:17 +09:00
|
|
|
private isTrackingRbf: 'all' | 'fullRbf' | false = false;
|
2023-07-14 16:08:57 +09:00
|
|
|
private isTrackingRbfSummary = false;
|
2023-08-03 16:17:17 +09:00
|
|
|
private isTrackingAddress: string | false = false;
|
2024-02-16 02:30:51 +00:00
|
|
|
private isTrackingAddresses: string[] | false = false;
|
2024-05-04 00:18:33 +00:00
|
|
|
private isTrackingAccelerations: boolean = false;
|
2024-07-25 22:34:52 +00:00
|
|
|
private isTrackingWallet: boolean = false;
|
|
|
|
private trackingWalletName: string;
|
2022-05-30 17:29:30 +00:00
|
|
|
private trackingMempoolBlock: number;
|
2024-08-08 13:12:31 +00:00
|
|
|
private stoppingTrackMempoolBlock: any | null = null;
|
2020-03-01 00:42:41 +07:00
|
|
|
private latestGitCommit = '';
|
2020-03-06 02:05:26 +07:00
|
|
|
private onlineCheckTimeout: number;
|
|
|
|
private onlineCheckTimeoutTwo: number;
|
2020-03-06 13:47:04 +07:00
|
|
|
private subscription: Subscription;
|
2020-05-10 12:31:57 +07:00
|
|
|
private network = '';
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private stateService: StateService,
|
2020-11-07 04:30:52 +07:00
|
|
|
private apiService: ApiService,
|
2020-11-27 18:41:57 +07:00
|
|
|
private transferState: TransferState,
|
2023-07-12 16:25:00 +09:00
|
|
|
private cacheService: CacheService,
|
2020-02-16 22:15:07 +07:00
|
|
|
) {
|
2020-11-22 23:47:27 +07:00
|
|
|
if (!this.stateService.isBrowser) {
|
|
|
|
// @ts-ignore
|
|
|
|
this.websocketSubject = { next: () => {}};
|
2020-11-07 04:30:52 +07:00
|
|
|
this.stateService.isLoadingWebSocket$.next(false);
|
|
|
|
this.apiService.getInitData$()
|
|
|
|
.pipe(take(1))
|
|
|
|
.subscribe((response) => this.handleResponse(response));
|
|
|
|
} else {
|
2024-06-16 10:50:31 +02:00
|
|
|
this.network = this.stateService.network === this.stateService.env.ROOT_NETWORK ? '' : this.stateService.network;
|
2020-11-23 02:30:46 +07:00
|
|
|
this.websocketSubject = webSocket<WebsocketResponse>(this.webSocketUrl.replace('{network}', this.network ? '/' + this.network : ''));
|
2020-11-27 18:41:57 +07:00
|
|
|
|
2023-10-29 01:21:07 +00:00
|
|
|
const { response: theInitData } = this.transferState.get<any>(initData, null) || {};
|
2020-11-27 18:41:57 +07:00
|
|
|
if (theInitData) {
|
2023-10-29 17:48:40 +00:00
|
|
|
if (theInitData.body.blocks) {
|
|
|
|
theInitData.body.blocks = theInitData.body.blocks.reverse();
|
|
|
|
}
|
2024-04-01 03:49:56 +00:00
|
|
|
this.stateService.backend$.next(theInitData.backend);
|
2023-10-28 00:33:29 +00:00
|
|
|
this.stateService.isLoadingWebSocket$.next(false);
|
2020-11-27 18:41:57 +07:00
|
|
|
this.handleResponse(theInitData.body);
|
|
|
|
this.startSubscription(false, true);
|
|
|
|
} else {
|
|
|
|
this.startSubscription();
|
|
|
|
}
|
2020-05-09 20:37:50 +07:00
|
|
|
|
2020-11-07 04:30:52 +07:00
|
|
|
this.stateService.networkChanged$.subscribe((network) => {
|
2024-06-22 17:28:08 +09:00
|
|
|
if (network === this.network || (this.network === '' && network === this.stateService.env.ROOT_NETWORK)) {
|
2020-11-07 04:30:52 +07:00
|
|
|
return;
|
|
|
|
}
|
2024-06-16 10:50:31 +02:00
|
|
|
this.network = network === this.stateService.env.ROOT_NETWORK ? '' : network;
|
2020-11-07 04:30:52 +07:00
|
|
|
clearTimeout(this.onlineCheckTimeout);
|
|
|
|
clearTimeout(this.onlineCheckTimeoutTwo);
|
2020-05-09 20:37:50 +07:00
|
|
|
|
2022-12-27 05:28:57 -06:00
|
|
|
this.stateService.resetChainTip();
|
2020-05-09 20:37:50 +07:00
|
|
|
|
2023-05-01 18:10:51 -06:00
|
|
|
this.reconnectWebsocket();
|
2020-11-07 04:30:52 +07:00
|
|
|
});
|
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2023-05-01 18:10:51 -06:00
|
|
|
reconnectWebsocket(retrying = false, hasInitData = false) {
|
|
|
|
console.log('reconnecting websocket');
|
|
|
|
this.websocketSubject.complete();
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
this.websocketSubject = webSocket<WebsocketResponse>(
|
|
|
|
this.webSocketUrl.replace('{network}', this.network ? '/' + this.network : '')
|
|
|
|
);
|
|
|
|
|
|
|
|
this.startSubscription(retrying, hasInitData);
|
|
|
|
}
|
|
|
|
|
2020-11-27 18:41:57 +07:00
|
|
|
startSubscription(retrying = false, hasInitData = false) {
|
|
|
|
if (!hasInitData) {
|
|
|
|
this.stateService.isLoadingWebSocket$.next(true);
|
|
|
|
this.websocketSubject.next({'action': 'init'});
|
|
|
|
}
|
2020-03-09 17:53:54 +07:00
|
|
|
if (retrying) {
|
|
|
|
this.stateService.connectionState$.next(1);
|
|
|
|
}
|
2020-03-06 13:47:04 +07:00
|
|
|
this.subscription = this.websocketSubject
|
2020-02-16 22:15:07 +07:00
|
|
|
.subscribe((response: WebsocketResponse) => {
|
2020-07-22 19:21:40 +07:00
|
|
|
this.stateService.isLoadingWebSocket$.next(false);
|
2020-11-07 04:30:52 +07:00
|
|
|
this.handleResponse(response);
|
2020-10-07 20:15:42 +07:00
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
if (this.goneOffline === true) {
|
|
|
|
this.goneOffline = false;
|
|
|
|
if (this.lastWant) {
|
2021-03-05 15:38:46 +07:00
|
|
|
this.want(JSON.parse(this.lastWant), true);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
2021-08-09 13:01:29 +03:00
|
|
|
if (this.isTrackingTx) {
|
|
|
|
this.startMultiTrackTransaction(this.trackingTxId);
|
|
|
|
}
|
2022-05-31 18:00:40 +00:00
|
|
|
if (this.isTrackingMempoolBlock) {
|
2024-03-08 16:47:15 +00:00
|
|
|
this.startTrackMempoolBlock(this.trackingMempoolBlock, true);
|
2022-05-31 18:00:40 +00:00
|
|
|
}
|
2023-08-03 16:17:17 +09:00
|
|
|
if (this.isTrackingRbf) {
|
|
|
|
this.startTrackRbf(this.isTrackingRbf);
|
|
|
|
}
|
|
|
|
if (this.isTrackingRbfSummary) {
|
|
|
|
this.startTrackRbfSummary();
|
|
|
|
}
|
|
|
|
if (this.isTrackingAddress) {
|
|
|
|
this.startTrackAddress(this.isTrackingAddress);
|
|
|
|
}
|
2024-02-16 02:30:51 +00:00
|
|
|
if (this.isTrackingAddresses) {
|
|
|
|
this.startTrackAddresses(this.isTrackingAddresses);
|
|
|
|
}
|
2024-05-04 00:18:33 +00:00
|
|
|
if (this.isTrackingAccelerations) {
|
|
|
|
this.startTrackAccelerations();
|
|
|
|
}
|
2024-07-25 22:34:52 +00:00
|
|
|
if (this.isTrackingWallet) {
|
|
|
|
this.startTrackingWallet(this.trackingWalletName);
|
|
|
|
}
|
2020-03-09 17:53:54 +07:00
|
|
|
this.stateService.connectionState$.next(2);
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:16:28 +09:00
|
|
|
if (this.stateService.connectionState$.value !== 2) {
|
2020-03-09 17:53:54 +07:00
|
|
|
this.stateService.connectionState$.next(2);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
2020-03-06 02:05:26 +07:00
|
|
|
|
|
|
|
this.startOnlineCheck();
|
2020-02-16 22:15:07 +07:00
|
|
|
},
|
|
|
|
(err: Error) => {
|
|
|
|
console.log(err);
|
2023-05-01 18:01:07 -06:00
|
|
|
console.log(`WebSocket error`);
|
2020-03-06 02:05:26 +07:00
|
|
|
this.goOffline();
|
2020-02-16 22:15:07 +07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:50:23 +07:00
|
|
|
startTrackTransaction(txId: string) {
|
2020-04-13 02:43:43 +07:00
|
|
|
if (this.isTrackingTx) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-19 23:50:23 +07:00
|
|
|
this.websocketSubject.next({ 'track-tx': txId });
|
2020-04-12 03:03:51 +07:00
|
|
|
this.isTrackingTx = true;
|
2021-08-09 13:01:29 +03:00
|
|
|
this.trackingTxId = txId;
|
2020-04-12 03:03:51 +07:00
|
|
|
}
|
|
|
|
|
2020-04-13 02:06:10 +07:00
|
|
|
startMultiTrackTransaction(txId: string) {
|
|
|
|
this.websocketSubject.next({ 'track-tx': txId, 'watch-mempool': true });
|
|
|
|
this.isTrackingTx = true;
|
2021-08-09 13:01:29 +03:00
|
|
|
this.trackingTxId = txId;
|
2020-04-13 02:06:10 +07:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:03:51 +07:00
|
|
|
stopTrackingTransaction() {
|
2020-04-13 02:43:43 +07:00
|
|
|
if (!this.isTrackingTx) {
|
2020-04-12 03:03:51 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.websocketSubject.next({ 'track-tx': 'stop' });
|
|
|
|
this.isTrackingTx = false;
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2020-02-19 23:50:23 +07:00
|
|
|
startTrackAddress(address: string) {
|
|
|
|
this.websocketSubject.next({ 'track-address': address });
|
2023-08-03 16:17:17 +09:00
|
|
|
this.isTrackingAddress = address;
|
2020-04-12 03:03:51 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
stopTrackingAddress() {
|
|
|
|
this.websocketSubject.next({ 'track-address': 'stop' });
|
2023-08-03 16:17:17 +09:00
|
|
|
this.isTrackingAddress = false;
|
2020-02-19 23:50:23 +07:00
|
|
|
}
|
|
|
|
|
2024-02-16 02:30:51 +00:00
|
|
|
startTrackAddresses(addresses: string[]) {
|
|
|
|
this.websocketSubject.next({ 'track-addresses': addresses });
|
|
|
|
this.isTrackingAddresses = addresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopTrackingAddresses() {
|
|
|
|
this.websocketSubject.next({ 'track-addresses': [] });
|
|
|
|
this.isTrackingAddresses = false;
|
|
|
|
}
|
|
|
|
|
2024-07-25 22:34:52 +00:00
|
|
|
startTrackingWallet(walletName: string) {
|
|
|
|
this.websocketSubject.next({ 'track-wallet': walletName });
|
|
|
|
this.isTrackingWallet = true;
|
|
|
|
this.trackingWalletName = walletName;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopTrackingWallet() {
|
|
|
|
this.websocketSubject.next({ 'track-wallet': 'stop' });
|
|
|
|
this.isTrackingWallet = false;
|
|
|
|
this.trackingWalletName = '';
|
|
|
|
}
|
|
|
|
|
2020-04-28 17:10:31 +07:00
|
|
|
startTrackAsset(asset: string) {
|
|
|
|
this.websocketSubject.next({ 'track-asset': asset });
|
|
|
|
}
|
|
|
|
|
|
|
|
stopTrackingAsset() {
|
|
|
|
this.websocketSubject.next({ 'track-asset': 'stop' });
|
|
|
|
}
|
|
|
|
|
2024-08-08 13:12:31 +00:00
|
|
|
startTrackMempoolBlock(block: number, force: boolean = false): boolean {
|
|
|
|
if (this.stoppingTrackMempoolBlock) {
|
|
|
|
clearTimeout(this.stoppingTrackMempoolBlock);
|
|
|
|
}
|
2023-12-21 13:06:52 +00:00
|
|
|
// skip duplicate tracking requests
|
2024-03-08 16:47:15 +00:00
|
|
|
if (force || this.trackingMempoolBlock !== block) {
|
2023-12-21 13:06:52 +00:00
|
|
|
this.websocketSubject.next({ 'track-mempool-block': block });
|
|
|
|
this.isTrackingMempoolBlock = true;
|
|
|
|
this.trackingMempoolBlock = block;
|
2024-08-08 13:12:31 +00:00
|
|
|
return true;
|
2023-12-21 13:06:52 +00:00
|
|
|
}
|
2024-08-08 13:12:31 +00:00
|
|
|
return false;
|
2022-05-30 17:29:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 13:12:31 +00:00
|
|
|
stopTrackMempoolBlock(): void {
|
|
|
|
if (this.stoppingTrackMempoolBlock) {
|
|
|
|
clearTimeout(this.stoppingTrackMempoolBlock);
|
|
|
|
}
|
2023-12-21 13:06:52 +00:00
|
|
|
this.isTrackingMempoolBlock = false;
|
2024-08-08 13:12:31 +00:00
|
|
|
this.stoppingTrackMempoolBlock = setTimeout(() => {
|
|
|
|
this.stoppingTrackMempoolBlock = null;
|
|
|
|
this.websocketSubject.next({ 'track-mempool-block': -1 });
|
|
|
|
this.trackingMempoolBlock = null;
|
|
|
|
this.stateService.mempoolBlockState = null;
|
|
|
|
}, 2000);
|
2022-05-30 17:29:30 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 16:51:53 -06:00
|
|
|
startTrackRbf(mode: 'all' | 'fullRbf') {
|
|
|
|
this.websocketSubject.next({ 'track-rbf': mode });
|
2023-08-03 16:17:17 +09:00
|
|
|
this.isTrackingRbf = mode;
|
2022-12-14 16:51:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
stopTrackRbf() {
|
|
|
|
this.websocketSubject.next({ 'track-rbf': 'stop' });
|
|
|
|
this.isTrackingRbf = false;
|
|
|
|
}
|
|
|
|
|
2023-07-14 16:08:57 +09:00
|
|
|
startTrackRbfSummary() {
|
2023-10-28 23:30:58 +00:00
|
|
|
this.initRbfSummary();
|
2023-07-14 16:08:57 +09:00
|
|
|
this.websocketSubject.next({ 'track-rbf-summary': true });
|
|
|
|
this.isTrackingRbfSummary = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopTrackRbfSummary() {
|
|
|
|
this.websocketSubject.next({ 'track-rbf-summary': false });
|
|
|
|
this.isTrackingRbfSummary = false;
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:18:33 +00:00
|
|
|
startTrackAccelerations() {
|
|
|
|
this.websocketSubject.next({ 'track-accelerations': true });
|
|
|
|
this.isTrackingAccelerations = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopTrackAccelerations() {
|
|
|
|
if (this.isTrackingAccelerations) {
|
|
|
|
this.websocketSubject.next({ 'track-accelerations': false });
|
|
|
|
this.isTrackingAccelerations = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureTrackAccelerations() {
|
|
|
|
if (!this.isTrackingAccelerations) {
|
|
|
|
this.startTrackAccelerations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
fetchStatistics(historicalDate: string) {
|
|
|
|
this.websocketSubject.next({ historicalDate });
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:52:32 +07:00
|
|
|
want(data: string[], force = false) {
|
2020-11-22 23:47:27 +07:00
|
|
|
if (!this.stateService.isBrowser) {
|
2020-11-07 04:30:52 +07:00
|
|
|
return;
|
|
|
|
}
|
2021-03-05 15:38:46 +07:00
|
|
|
if (JSON.stringify(data) === this.lastWant && !force) {
|
2020-09-26 22:46:26 +07:00
|
|
|
return;
|
|
|
|
}
|
2020-02-17 20:39:20 +07:00
|
|
|
this.websocketSubject.next({action: 'want', data: data});
|
2021-03-05 15:38:46 +07:00
|
|
|
this.lastWant = JSON.stringify(data);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
2020-03-06 02:05:26 +07:00
|
|
|
|
2020-03-13 18:00:34 +07:00
|
|
|
goOffline() {
|
2023-05-01 18:01:07 -06:00
|
|
|
const retryDelay = OFFLINE_RETRY_AFTER_MS + (Math.random() * OFFLINE_RETRY_AFTER_MS);
|
|
|
|
console.log(`trying to reconnect websocket in ${retryDelay} seconds`);
|
2020-03-06 02:05:26 +07:00
|
|
|
this.goneOffline = true;
|
2020-03-09 17:53:54 +07:00
|
|
|
this.stateService.connectionState$.next(0);
|
|
|
|
window.setTimeout(() => {
|
2023-05-01 18:10:51 -06:00
|
|
|
this.reconnectWebsocket(true);
|
2023-05-01 18:01:07 -06:00
|
|
|
}, retryDelay);
|
2020-03-06 02:05:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
startOnlineCheck() {
|
|
|
|
clearTimeout(this.onlineCheckTimeout);
|
|
|
|
clearTimeout(this.onlineCheckTimeoutTwo);
|
|
|
|
|
|
|
|
this.onlineCheckTimeout = window.setTimeout(() => {
|
|
|
|
this.websocketSubject.next({action: 'ping'});
|
|
|
|
this.onlineCheckTimeoutTwo = window.setTimeout(() => {
|
|
|
|
if (!this.goneOffline) {
|
2023-05-01 18:01:07 -06:00
|
|
|
console.log('WebSocket response timeout, force closing');
|
2020-03-06 02:05:26 +07:00
|
|
|
this.websocketSubject.complete();
|
2020-03-06 13:47:04 +07:00
|
|
|
this.subscription.unsubscribe();
|
2020-03-06 02:05:26 +07:00
|
|
|
this.goOffline();
|
|
|
|
}
|
2020-04-13 01:26:53 +07:00
|
|
|
}, EXPECT_PING_RESPONSE_AFTER_MS);
|
|
|
|
}, OFFLINE_PING_CHECK_AFTER_MS);
|
2020-03-06 02:05:26 +07:00
|
|
|
}
|
2020-11-07 04:30:52 +07:00
|
|
|
|
|
|
|
handleResponse(response: WebsocketResponse) {
|
2023-06-12 16:32:04 -04:00
|
|
|
let reinitBlocks = false;
|
|
|
|
|
2024-04-01 03:49:56 +00:00
|
|
|
if (response.backend) {
|
|
|
|
this.stateService.backend$.next(response.backend);
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:30:52 +07:00
|
|
|
if (response.blocks && response.blocks.length) {
|
|
|
|
const blocks = response.blocks;
|
2023-07-08 01:07:06 -04:00
|
|
|
this.stateService.resetBlocks(blocks);
|
|
|
|
const maxHeight = blocks.reduce((max, block) => Math.max(max, block.height), this.stateService.latestBlockHeight);
|
2022-12-28 06:05:46 -06:00
|
|
|
this.stateService.updateChainTip(maxHeight);
|
2020-11-07 04:30:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (response.tx) {
|
|
|
|
this.stateService.mempoolTransactions$.next(response.tx);
|
|
|
|
}
|
|
|
|
|
2023-04-21 08:40:21 +09:00
|
|
|
if (response['txPosition']) {
|
|
|
|
this.stateService.mempoolTxPosition$.next(response['txPosition']);
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:30:52 +07:00
|
|
|
if (response.block) {
|
2023-06-12 16:32:04 -04:00
|
|
|
if (response.block.height === this.stateService.latestBlockHeight + 1) {
|
2022-12-27 05:28:57 -06:00
|
|
|
this.stateService.updateChainTip(response.block.height);
|
2023-07-08 01:07:06 -04:00
|
|
|
this.stateService.addBlock(response.block);
|
|
|
|
this.stateService.txConfirmed$.next([response.txConfirmed, response.block]);
|
2023-06-12 16:32:04 -04:00
|
|
|
} else if (response.block.height > this.stateService.latestBlockHeight + 1) {
|
|
|
|
reinitBlocks = true;
|
2020-11-07 04:30:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (response.txConfirmed) {
|
|
|
|
this.isTrackingTx = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.conversions) {
|
|
|
|
this.stateService.conversions$.next(response.conversions);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.rbfTransaction) {
|
|
|
|
this.stateService.txReplaced$.next(response.rbfTransaction);
|
|
|
|
}
|
|
|
|
|
2022-12-14 08:49:35 -06:00
|
|
|
if (response.rbfInfo) {
|
|
|
|
this.stateService.txRbfInfo$.next(response.rbfInfo);
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:51:53 -06:00
|
|
|
if (response.rbfLatest) {
|
|
|
|
this.stateService.rbfLatest$.next(response.rbfLatest);
|
|
|
|
}
|
|
|
|
|
2024-03-15 05:33:23 +00:00
|
|
|
if (response.rbfLatestSummary !== undefined) {
|
|
|
|
this.stateService.rbfLatestSummary$.next(response.rbfLatestSummary || []);
|
2023-07-14 16:08:57 +09:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:49:25 +01:00
|
|
|
if (response.txReplaced) {
|
|
|
|
this.stateService.txReplaced$.next(response.txReplaced);
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:30:52 +07:00
|
|
|
if (response['mempool-blocks']) {
|
|
|
|
this.stateService.mempoolBlocks$.next(response['mempool-blocks']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.transactions) {
|
2024-03-15 05:44:20 +00:00
|
|
|
this.stateService.transactions$.next(response.transactions.slice(0, 6));
|
2020-11-07 04:30:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (response['bsq-price']) {
|
|
|
|
this.stateService.bsqPrice$.next(response['bsq-price']);
|
|
|
|
}
|
|
|
|
|
2022-03-06 18:27:13 +01:00
|
|
|
if (response.utxoSpent) {
|
2022-03-07 19:45:09 +01:00
|
|
|
this.stateService.utxoSpent$.next(response.utxoSpent);
|
2022-03-06 18:27:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-12 14:55:42 +01:00
|
|
|
if (response.da) {
|
|
|
|
this.stateService.difficultyAdjustment$.next(response.da);
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:52:40 +02:00
|
|
|
if (response.fees) {
|
|
|
|
this.stateService.recommendedFees$.next(response.fees);
|
|
|
|
}
|
|
|
|
|
2021-04-12 22:17:13 +04:00
|
|
|
if (response.backendInfo) {
|
|
|
|
this.stateService.backendInfo$.next(response.backendInfo);
|
2020-11-07 04:30:52 +07:00
|
|
|
|
|
|
|
if (!this.latestGitCommit) {
|
2021-04-12 22:17:13 +04:00
|
|
|
this.latestGitCommit = response.backendInfo.gitCommit;
|
2020-11-07 04:30:52 +07:00
|
|
|
} else {
|
2021-04-12 22:17:13 +04:00
|
|
|
if (this.latestGitCommit !== response.backendInfo.gitCommit) {
|
2020-11-07 04:30:52 +07:00
|
|
|
setTimeout(() => {
|
|
|
|
window.location.reload();
|
|
|
|
}, Math.floor(Math.random() * 60000) + 60000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response['address-transactions']) {
|
|
|
|
response['address-transactions'].forEach((addressTransaction: Transaction) => {
|
|
|
|
this.stateService.mempoolTransactions$.next(addressTransaction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-25 00:57:47 +09:00
|
|
|
if (response['address-removed-transactions']) {
|
|
|
|
response['address-removed-transactions'].forEach((addressTransaction: Transaction) => {
|
|
|
|
this.stateService.mempoolRemovedTransactions$.next(addressTransaction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-16 02:30:51 +00:00
|
|
|
if (response['multi-address-transactions']) {
|
|
|
|
this.stateService.multiAddressTransactions$.next(response['multi-address-transactions']);
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:30:52 +07:00
|
|
|
if (response['block-transactions']) {
|
|
|
|
response['block-transactions'].forEach((addressTransaction: Transaction) => {
|
|
|
|
this.stateService.blockTransactions$.next(addressTransaction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
if (response['projected-block-transactions']) {
|
|
|
|
if (response['projected-block-transactions'].index == this.trackingMempoolBlock) {
|
|
|
|
if (response['projected-block-transactions'].blockTransactions) {
|
2024-04-11 08:09:28 +00:00
|
|
|
this.stateService.mempoolSequence = response['projected-block-transactions'].sequence;
|
2024-05-02 23:48:10 +00:00
|
|
|
this.stateService.mempoolBlockUpdate$.next({
|
2024-08-08 13:12:31 +00:00
|
|
|
block: this.trackingMempoolBlock,
|
2024-05-02 23:48:10 +00:00
|
|
|
transactions: response['projected-block-transactions'].blockTransactions.map(uncompressTx),
|
|
|
|
});
|
2022-06-02 01:29:03 +04:00
|
|
|
} else if (response['projected-block-transactions'].delta) {
|
2024-04-11 08:09:28 +00:00
|
|
|
if (this.stateService.mempoolSequence && response['projected-block-transactions'].sequence !== this.stateService.mempoolSequence + 1) {
|
|
|
|
this.stateService.mempoolSequence = 0;
|
|
|
|
this.startTrackMempoolBlock(this.trackingMempoolBlock, true);
|
|
|
|
} else {
|
|
|
|
this.stateService.mempoolSequence = response['projected-block-transactions'].sequence;
|
2024-08-08 13:12:31 +00:00
|
|
|
this.stateService.mempoolBlockUpdate$.next(uncompressDeltaChange(this.trackingMempoolBlock, response['projected-block-transactions'].delta));
|
2024-04-11 08:09:28 +00:00
|
|
|
}
|
2022-05-31 21:36:42 +00:00
|
|
|
}
|
2022-05-30 17:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 22:34:52 +00:00
|
|
|
if (response['wallet-transactions']) {
|
|
|
|
this.stateService.walletTransactions$.next(response['wallet-transactions']);
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:18:33 +00:00
|
|
|
if (response['accelerations']) {
|
|
|
|
if (response['accelerations'].accelerations) {
|
|
|
|
this.stateService.accelerations$.next({
|
|
|
|
added: response['accelerations'].accelerations,
|
|
|
|
removed: [],
|
|
|
|
reset: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.stateService.accelerations$.next(response['accelerations']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:30:52 +07:00
|
|
|
if (response['live-2h-chart']) {
|
|
|
|
this.stateService.live2Chart$.next(response['live-2h-chart']);
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:57:06 +07:00
|
|
|
if (response.loadingIndicators) {
|
|
|
|
this.stateService.loadingIndicators$.next(response.loadingIndicators);
|
2023-07-24 16:22:35 +09:00
|
|
|
if (response.loadingIndicators.mempool != null && response.loadingIndicators.mempool < 100) {
|
|
|
|
this.stateService.isLoadingMempool$.next(true);
|
|
|
|
} else {
|
|
|
|
this.stateService.isLoadingMempool$.next(false);
|
|
|
|
}
|
2021-01-05 18:57:06 +07:00
|
|
|
}
|
|
|
|
|
2020-11-07 04:30:52 +07:00
|
|
|
if (response.mempoolInfo) {
|
|
|
|
this.stateService.mempoolInfo$.next(response.mempoolInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.vBytesPerSecond !== undefined) {
|
|
|
|
this.stateService.vbytesPerSecond$.next(response.vBytesPerSecond);
|
|
|
|
}
|
|
|
|
|
2021-07-24 19:26:48 -03:00
|
|
|
if (response.previousRetarget !== undefined) {
|
|
|
|
this.stateService.previousRetarget$.next(response.previousRetarget);
|
|
|
|
}
|
|
|
|
|
2024-03-03 20:31:02 +00:00
|
|
|
if (response['tomahawk']) {
|
|
|
|
this.stateService.serverHealth$.next(response['tomahawk']);
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:30:52 +07:00
|
|
|
if (response['git-commit']) {
|
2021-04-12 22:17:13 +04:00
|
|
|
this.stateService.backendInfo$.next(response['git-commit']);
|
2020-11-07 04:30:52 +07:00
|
|
|
}
|
2023-06-12 16:32:04 -04:00
|
|
|
|
|
|
|
if (reinitBlocks) {
|
|
|
|
this.websocketSubject.next({'refresh-blocks': true});
|
|
|
|
}
|
2020-11-07 04:30:52 +07:00
|
|
|
}
|
2023-10-28 23:30:58 +00:00
|
|
|
|
|
|
|
async initRbfSummary(): Promise<void> {
|
|
|
|
if (!this.stateService.isBrowser) {
|
|
|
|
const rbfList = await firstValueFrom(this.apiService.getRbfList$(false));
|
|
|
|
if (rbfList) {
|
|
|
|
const rbfSummary = rbfList.slice(0, 6).map(rbfTree => {
|
|
|
|
let oldFee = 0;
|
|
|
|
let oldVsize = 0;
|
|
|
|
for (const replaced of rbfTree.replaces) {
|
|
|
|
oldFee += replaced.tx.fee;
|
|
|
|
oldVsize += replaced.tx.vsize;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
txid: rbfTree.tx.txid,
|
|
|
|
mined: !!rbfTree.tx.mined,
|
|
|
|
fullRbf: !!rbfTree.tx.fullRbf,
|
|
|
|
oldFee,
|
|
|
|
oldVsize,
|
|
|
|
newFee: rbfTree.tx.fee,
|
|
|
|
newVsize: rbfTree.tx.vsize,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
this.stateService.rbfLatestSummary$.next(rbfSummary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|