mempool/frontend/src/app/lightning/lightning-api.service.ts

143 lines
5.2 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
2022-05-01 03:01:27 +04:00
import { HttpClient, HttpParams } from '@angular/common/http';
2023-12-13 21:48:41 +09:00
import { BehaviorSubject, Observable, catchError, filter, of, shareReplay, take, tap } from 'rxjs';
import { StateService } from '../services/state.service';
2023-07-09 01:15:05 -04:00
import { IChannel, INodesRanking, IOldestNodes, ITopNodesPerCapacity, ITopNodesPerChannels } from '../interfaces/node-api.interface';
@Injectable({
providedIn: 'root'
})
export class LightningApiService {
private apiBaseUrl: string; // base URL is protocol, hostname, and port
private apiBasePath = ''; // network path is /testnet, etc. or '' for mainnet
2023-12-13 21:48:41 +09:00
private requestCache = new Map<string, { subject: BehaviorSubject<any>, expiry: number }>;
constructor(
private httpClient: HttpClient,
private stateService: StateService,
) {
this.apiBaseUrl = ''; // use relative URL by default
if (!stateService.isBrowser) { // except when inside AU SSR process
this.apiBaseUrl = this.stateService.env.NGINX_PROTOCOL + '://' + this.stateService.env.NGINX_HOSTNAME + ':' + this.stateService.env.NGINX_PORT;
}
this.apiBasePath = ''; // assume mainnet by default
this.stateService.networkChanged$.subscribe((network) => {
this.apiBasePath = network ? '/' + network : '';
});
}
2023-12-13 21:48:41 +09:00
private generateCacheKey(functionName: string, params: any[]): string {
return functionName + JSON.stringify(params);
}
// delete expired cache entries
private cleanExpiredCache(): void {
this.requestCache.forEach((value, key) => {
if (value.expiry < Date.now()) {
this.requestCache.delete(key);
}
});
}
cachedRequest<T, F extends (...args: any[]) => Observable<T>>(
apiFunction: F,
expireAfter: number, // in ms
...params: Parameters<F>
): Observable<T> {
this.cleanExpiredCache();
const cacheKey = this.generateCacheKey(apiFunction.name, params);
if (!this.requestCache.has(cacheKey)) {
const subject = new BehaviorSubject<T | null>(null);
this.requestCache.set(cacheKey, { subject, expiry: Date.now() + expireAfter });
apiFunction.bind(this)(...params).pipe(
tap(data => {
subject.next(data as T);
}),
catchError((error) => {
subject.error(error);
return of(null);
}),
shareReplay(1),
).subscribe();
}
return this.requestCache.get(cacheKey).subject.asObservable().pipe(filter(val => val !== null), take(1));
}
2022-04-29 03:57:27 +04:00
getNode$(publicKey: string): Observable<any> {
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/' + publicKey);
2022-04-29 03:57:27 +04:00
}
2023-10-11 01:09:10 +00:00
getNodeGroup$(name: string): Observable<any[]> {
return this.httpClient.get<any[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/group/' + name);
2022-09-17 01:26:32 +02:00
}
2022-05-01 03:01:27 +04:00
getChannel$(shortId: string): Observable<any> {
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/channels/' + shortId);
2022-05-01 03:01:27 +04:00
}
2022-05-16 01:36:59 +04:00
getChannelsByNodeId$(publicKey: string, index: number = 0, status = 'open'): Observable<any> {
const params = new HttpParams()
2022-05-01 03:01:27 +04:00
.set('public_key', publicKey)
2022-05-16 01:36:59 +04:00
.set('index', index)
.set('status', status)
2022-05-01 03:01:27 +04:00
;
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/channels', { params, observe: 'response' });
2022-04-29 03:57:27 +04:00
}
getLatestStatistics$(): Observable<any> {
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/statistics/latest');
}
2022-05-05 23:19:24 +04:00
listNodeStats$(publicKey: string): Observable<any> {
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/' + publicKey + '/statistics');
2022-05-05 23:19:24 +04:00
}
2022-10-15 00:45:48 +00:00
getNodeFeeHistogram$(publicKey: string): Observable<any> {
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/' + publicKey + '/fees/histogram');
2022-10-15 00:45:48 +00:00
}
2022-08-17 12:53:26 +02:00
getNodesRanking$(): Observable<INodesRanking> {
return this.httpClient.get<INodesRanking>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/rankings');
}
2022-07-01 16:50:53 +02:00
listChannelStats$(publicKey: string): Observable<any> {
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/channels/' + publicKey + '/statistics');
}
2022-07-06 15:15:08 +02:00
listStatistics$(interval: string | undefined): Observable<any> {
return this.httpClient.get<any>(
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/statistics' +
2022-07-06 15:15:08 +02:00
(interval !== undefined ? `/${interval}` : ''), { observe: 'response' }
);
2022-07-01 16:50:53 +02:00
}
getTopNodesByCapacity$(): Observable<ITopNodesPerCapacity[]> {
return this.httpClient.get<ITopNodesPerCapacity[]>(
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/rankings/liquidity'
);
}
getTopNodesByChannels$(): Observable<ITopNodesPerChannels[]> {
return this.httpClient.get<ITopNodesPerChannels[]>(
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/rankings/connectivity'
);
}
2023-07-09 01:15:05 -04:00
getPenaltyClosedChannels$(): Observable<IChannel[]> {
return this.httpClient.get<IChannel[]>(
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/penalties'
2023-07-09 01:15:05 -04:00
);
}
getOldestNodes$(): Observable<IOldestNodes[]> {
return this.httpClient.get<IOldestNodes[]>(
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/rankings/age'
);
}
}