New base code for mempool blockchain explorerer
This commit is contained in:
@@ -1,192 +1,39 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { webSocket } from 'rxjs/webSocket';
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { IMempoolDefaultResponse, IMempoolStats, IBlockTransaction, IBlock } from '../blockchain/interfaces';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { MempoolStats, BlockTransaction } from '../interfaces/node-api.interface';
|
||||
import { Observable } from 'rxjs';
|
||||
import { MemPoolService } from './mem-pool.service';
|
||||
import { tap, retryWhen, delay } from 'rxjs/operators';
|
||||
|
||||
const WEB_SOCKET_PROTOCOL = (document.location.protocol === 'https:') ? 'wss:' : 'ws:';
|
||||
const WEB_SOCKET_URL = WEB_SOCKET_PROTOCOL + '//' + document.location.hostname + ':' + document.location.port + '/ws';
|
||||
const API_BASE_URL = '/api/v1';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ApiService {
|
||||
private websocketSubject: Observable<IMempoolDefaultResponse> = webSocket<IMempoolDefaultResponse | any>(WEB_SOCKET_URL);
|
||||
private lastWant: string[] | null = null;
|
||||
private goneOffline = false;
|
||||
private lastTrackedTxId = '';
|
||||
|
||||
constructor(
|
||||
private httpClient: HttpClient,
|
||||
private memPoolService: MemPoolService,
|
||||
) {
|
||||
this.startSubscription();
|
||||
) { }
|
||||
|
||||
list2HStatistics$(): Observable<MempoolStats[]> {
|
||||
return this.httpClient.get<MempoolStats[]>(API_BASE_URL + '/statistics/2h');
|
||||
}
|
||||
|
||||
startSubscription() {
|
||||
this.websocketSubject
|
||||
.pipe(
|
||||
retryWhen((errors: any) => errors
|
||||
.pipe(
|
||||
tap(() => {
|
||||
this.goneOffline = true;
|
||||
this.memPoolService.isOffline$.next(true);
|
||||
}),
|
||||
delay(5000),
|
||||
)
|
||||
),
|
||||
)
|
||||
.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);
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
if (response.conversions) {
|
||||
this.memPoolService.conversions$.next(response.conversions);
|
||||
}
|
||||
|
||||
if (response['track-tx'] && !this.goneOffline) {
|
||||
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']);
|
||||
}
|
||||
|
||||
if (this.goneOffline === true) {
|
||||
this.goneOffline = false;
|
||||
if (this.lastWant) {
|
||||
this.webSocketWant(this.lastWant);
|
||||
}
|
||||
if (this.memPoolService.txTracking$.value.enabled) {
|
||||
this.webSocketStartTrackTx(this.lastTrackedTxId);
|
||||
}
|
||||
}
|
||||
},
|
||||
(err: Error) => {
|
||||
console.log(err);
|
||||
this.goneOffline = true;
|
||||
console.log('Error, retrying in 10 sec');
|
||||
setTimeout(() => this.startSubscription(), 10000);
|
||||
});
|
||||
list24HStatistics$(): Observable<MempoolStats[]> {
|
||||
return this.httpClient.get<MempoolStats[]>(API_BASE_URL + '/statistics/24h');
|
||||
}
|
||||
|
||||
webSocketStartTrackTx(txId: string) {
|
||||
// @ts-ignore
|
||||
this.websocketSubject.next({'action': 'track-tx', 'txId': txId});
|
||||
this.lastTrackedTxId = txId;
|
||||
list1WStatistics$(): Observable<MempoolStats[]> {
|
||||
return this.httpClient.get<MempoolStats[]>(API_BASE_URL + '/statistics/1w');
|
||||
}
|
||||
|
||||
webSocketWant(data: string[]) {
|
||||
// @ts-ignore
|
||||
this.websocketSubject.next({'action': 'want', data: data});
|
||||
this.lastWant = data;
|
||||
list1MStatistics$(): Observable<MempoolStats[]> {
|
||||
return this.httpClient.get<MempoolStats[]>(API_BASE_URL + '/statistics/1m');
|
||||
}
|
||||
|
||||
listTransactionsForBlock$(height: number): Observable<IBlockTransaction[]> {
|
||||
return this.httpClient.get<IBlockTransaction[]>(API_BASE_URL + '/transactions/height/' + height);
|
||||
list3MStatistics$(): Observable<MempoolStats[]> {
|
||||
return this.httpClient.get<MempoolStats[]>(API_BASE_URL + '/statistics/3m');
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
listBlocks$(height?: number): Observable<any[]> {
|
||||
return this.httpClient.get<any[]>(API_BASE_URL + '/explorer/blocks/' + (height || ''));
|
||||
}
|
||||
|
||||
getTransaction$(txId: string): Observable<any[]> {
|
||||
return this.httpClient.get<any[]>(API_BASE_URL + '/explorer/tx/' + txId);
|
||||
}
|
||||
|
||||
getBlock$(hash: string): Observable<any> {
|
||||
return this.httpClient.get<any>(API_BASE_URL + '/explorer/block/' + hash);
|
||||
}
|
||||
|
||||
getBlockTransactions$(hash: string, index?: number): Observable<any[]> {
|
||||
return this.httpClient.get<any[]>(API_BASE_URL + '/explorer/block/' + hash + '/tx/' + (index || ''));
|
||||
}
|
||||
|
||||
getAddress$(address: string): Observable<any> {
|
||||
return this.httpClient.get<any>(API_BASE_URL + '/explorer/address/' + address);
|
||||
}
|
||||
|
||||
getAddressTransactions$(address: string): Observable<any[]> {
|
||||
return this.httpClient.get<any[]>(API_BASE_URL + '/explorer/address/' + address+ '/tx/');
|
||||
}
|
||||
|
||||
getAddressTransactionsFromHash$(address: string, txid: string): Observable<any[]> {
|
||||
return this.httpClient.get<any[]>(API_BASE_URL + '/explorer/address/' + address + '/tx/chain/' + txid);
|
||||
list6MStatistics$(): Observable<MempoolStats[]> {
|
||||
return this.httpClient.get<MempoolStats[]>(API_BASE_URL + '/statistics/6m');
|
||||
}
|
||||
}
|
||||
|
||||
57
frontend/src/app/services/electrs-api.service.ts
Normal file
57
frontend/src/app/services/electrs-api.service.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Block, Transaction, Address, Outspend, Recent } from '../interfaces/electrs.interface';
|
||||
|
||||
const API_BASE_URL = 'https://www.blockstream.info/api';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ElectrsApiService {
|
||||
constructor(
|
||||
private httpClient: HttpClient,
|
||||
) {
|
||||
}
|
||||
|
||||
getBlock$(hash: string): Observable<Block> {
|
||||
return this.httpClient.get<Block>(API_BASE_URL + '/block/' + hash);
|
||||
}
|
||||
|
||||
listBlocks$(height?: number): Observable<Block[]> {
|
||||
return this.httpClient.get<Block[]>(API_BASE_URL + '/blocks/' + (height || ''));
|
||||
}
|
||||
|
||||
getTransaction$(txId: string): Observable<Transaction> {
|
||||
return this.httpClient.get<Transaction>(API_BASE_URL + '/tx/' + txId);
|
||||
}
|
||||
|
||||
getRecentTransaction$(): Observable<Recent[]> {
|
||||
return this.httpClient.get<Recent[]>(API_BASE_URL + '/mempool/recent');
|
||||
}
|
||||
|
||||
getOutspend$(hash: string, vout: number): Observable<Outspend> {
|
||||
return this.httpClient.get<Outspend>(API_BASE_URL + '/tx/' + hash + '/outspend/' + vout);
|
||||
}
|
||||
|
||||
getOutspends$(hash: string): Observable<Outspend[]> {
|
||||
return this.httpClient.get<Outspend[]>(API_BASE_URL + '/tx/' + hash + '/outspends');
|
||||
}
|
||||
|
||||
getBlockTransactions$(hash: string, index: number = 0): Observable<Transaction[]> {
|
||||
return this.httpClient.get<Transaction[]>(API_BASE_URL + '/block/' + hash + '/txs/' + index);
|
||||
}
|
||||
|
||||
getAddress$(address: string): Observable<Address> {
|
||||
return this.httpClient.get<Address>(API_BASE_URL + '/address/' + address);
|
||||
}
|
||||
|
||||
getAddressTransactions$(address: string): Observable<Transaction[]> {
|
||||
return this.httpClient.get<Transaction[]>(API_BASE_URL + '/address/' + address + '/txs');
|
||||
}
|
||||
|
||||
getAddressTransactionsFromHash$(address: string, txid: string): Observable<Transaction[]> {
|
||||
return this.httpClient.get<Transaction[]>(API_BASE_URL + '/address/' + address + '/txs/chain/' + txid);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ReplaySubject, BehaviorSubject, Subject } from 'rxjs';
|
||||
import { IMempoolInfo, IBlock, IProjectedBlock, ITransaction, IMempoolStats } from '../blockchain/interfaces';
|
||||
|
||||
export interface IMemPoolState {
|
||||
memPoolInfo: IMempoolInfo;
|
||||
txPerSecond: number;
|
||||
vBytesPerSecond: number;
|
||||
}
|
||||
|
||||
export interface ITxTracking {
|
||||
enabled: boolean;
|
||||
tx: ITransaction | null;
|
||||
blockHeight: number;
|
||||
notFound: boolean;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MemPoolService {
|
||||
mempoolStats$ = new ReplaySubject<IMemPoolState>();
|
||||
isOffline$ = new ReplaySubject<boolean>();
|
||||
txIdSearch$ = new ReplaySubject<string>();
|
||||
conversions$ = new ReplaySubject<any>();
|
||||
mempoolWeight$ = new ReplaySubject<number>();
|
||||
live2Chart$ = new Subject<IMempoolStats>();
|
||||
txTracking$ = new BehaviorSubject<ITxTracking>({
|
||||
enabled: false,
|
||||
tx: null,
|
||||
blockHeight: 0,
|
||||
notFound: false,
|
||||
});
|
||||
blocks$ = new ReplaySubject<IBlock>(8);
|
||||
projectedBlocks$ = new BehaviorSubject<IProjectedBlock[]>([]);
|
||||
}
|
||||
20
frontend/src/app/services/state.service.ts
Normal file
20
frontend/src/app/services/state.service.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ReplaySubject, BehaviorSubject, Subject } from 'rxjs';
|
||||
import { Block } from '../interfaces/electrs.interface';
|
||||
import { MempoolBlock } from '../interfaces/websocket.interface';
|
||||
import { MempoolStats } from '../interfaces/node-api.interface';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class StateService {
|
||||
latestBlockHeight = 0;
|
||||
blocks$ = new ReplaySubject<Block>(8);
|
||||
conversions$ = new ReplaySubject<any>(1);
|
||||
mempoolBlocks$ = new ReplaySubject<MempoolBlock[]>(1);
|
||||
txConfirmed = new Subject<Block>();
|
||||
live2Chart$ = new Subject<MempoolStats>();
|
||||
|
||||
viewFiat$ = new BehaviorSubject<boolean>(false);
|
||||
isOffline$ = new BehaviorSubject<boolean>(false);
|
||||
}
|
||||
97
frontend/src/app/services/websocket.service.ts
Normal file
97
frontend/src/app/services/websocket.service.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
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';
|
||||
import { Block } from '../interfaces/electrs.interface';
|
||||
|
||||
const WEB_SOCKET_PROTOCOL = (document.location.protocol === 'https:') ? 'wss:' : 'ws:';
|
||||
const WEB_SOCKET_URL = WEB_SOCKET_PROTOCOL + '//' + document.location.hostname + ':8999';
|
||||
|
||||
@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;
|
||||
|
||||
constructor(
|
||||
private stateService: StateService,
|
||||
) {
|
||||
this.startSubscription();
|
||||
}
|
||||
|
||||
startSubscription() {
|
||||
this.websocketSubject
|
||||
.pipe(
|
||||
retryWhen((errors: any) => errors
|
||||
.pipe(
|
||||
tap(() => {
|
||||
this.goneOffline = true;
|
||||
this.stateService.isOffline$.next(true);
|
||||
}),
|
||||
delay(5000),
|
||||
)
|
||||
),
|
||||
)
|
||||
.subscribe((response: WebsocketResponse) => {
|
||||
if (response.blocks && response.blocks.length) {
|
||||
const blocks = response.blocks;
|
||||
blocks.forEach((block: Block) => this.stateService.blocks$.next(block));
|
||||
}
|
||||
|
||||
if (response.block) {
|
||||
if (this.stateService.latestBlockHeight < response.block.height) {
|
||||
this.stateService.blocks$.next(response.block);
|
||||
}
|
||||
|
||||
if (response.txConfirmed) {
|
||||
this.trackingTxId = null;
|
||||
this.stateService.txConfirmed.next(response.block);
|
||||
}
|
||||
}
|
||||
|
||||
if (response.conversions) {
|
||||
this.stateService.conversions$.next(response.conversions);
|
||||
}
|
||||
|
||||
if (response['mempool-blocks']) {
|
||||
this.stateService.mempoolBlocks$.next(response['mempool-blocks']);
|
||||
}
|
||||
|
||||
if (this.goneOffline === true) {
|
||||
this.goneOffline = false;
|
||||
if (this.lastWant) {
|
||||
this.want(this.lastWant);
|
||||
}
|
||||
if (this.trackingTxId) {
|
||||
this.startTrackTx(this.trackingTxId);
|
||||
}
|
||||
this.stateService.isOffline$.next(false);
|
||||
}
|
||||
},
|
||||
(err: Error) => {
|
||||
console.log(err);
|
||||
this.goneOffline = true;
|
||||
console.log('Error, retrying in 10 sec');
|
||||
window.setTimeout(() => this.startSubscription(), 10000);
|
||||
});
|
||||
}
|
||||
|
||||
startTrackTx(txId: string) {
|
||||
this.websocketSubject.next({ txId });
|
||||
this.trackingTxId = txId;
|
||||
}
|
||||
|
||||
fetchStatistics(historicalDate: string) {
|
||||
this.websocketSubject.next({ historicalDate });
|
||||
}
|
||||
|
||||
want(data: string[]) {
|
||||
// @ts-ignore
|
||||
this.websocketSubject.next({action: 'want', data});
|
||||
this.lastWant = data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user