2022-04-27 02:52:23 +04:00
|
|
|
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';
|
2022-07-06 11:58:06 +02:00
|
|
|
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';
|
2022-04-27 02:52:23 +04:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class LightningApiService {
|
2023-11-02 23:26:17 +00:00
|
|
|
private apiBaseUrl: string; // base URL is protocol, hostname, and port
|
2022-07-06 11:58:06 +02:00
|
|
|
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 }>;
|
2022-07-06 11:58:06 +02:00
|
|
|
|
2022-04-27 02:52:23 +04:00
|
|
|
constructor(
|
|
|
|
private httpClient: HttpClient,
|
2022-07-06 11:58:06 +02:00
|
|
|
private stateService: StateService,
|
|
|
|
) {
|
2023-11-02 23:26:17 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-07-06 11:58:06 +02:00
|
|
|
this.apiBasePath = ''; // assume mainnet by default
|
|
|
|
this.stateService.networkChanged$.subscribe((network) => {
|
|
|
|
this.apiBasePath = network ? '/' + network : '';
|
|
|
|
});
|
|
|
|
}
|
2022-04-27 02:52:23 +04:00
|
|
|
|
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> {
|
2023-11-02 23:26:17 +00:00
|
|
|
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[]> {
|
2023-11-02 23:26:17 +00:00
|
|
|
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> {
|
2023-11-02 23:26:17 +00:00
|
|
|
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> {
|
2022-08-23 11:26:00 +02:00
|
|
|
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
|
|
|
;
|
|
|
|
|
2023-11-02 23:26:17 +00: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
|
|
|
}
|
|
|
|
|
2022-04-27 02:52:23 +04:00
|
|
|
getLatestStatistics$(): Observable<any> {
|
2023-11-02 23:26:17 +00:00
|
|
|
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/statistics/latest');
|
2022-04-27 02:52:23 +04:00
|
|
|
}
|
|
|
|
|
2022-05-05 23:19:24 +04:00
|
|
|
listNodeStats$(publicKey: string): Observable<any> {
|
2023-11-02 23:26:17 +00:00
|
|
|
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> {
|
2023-11-02 23:26:17 +00:00
|
|
|
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> {
|
2023-11-02 23:26:17 +00:00
|
|
|
return this.httpClient.get<INodesRanking>(this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/rankings');
|
2022-04-27 02:52:23 +04:00
|
|
|
}
|
2022-07-01 16:50:53 +02:00
|
|
|
|
2022-07-06 14:56:10 +02:00
|
|
|
listChannelStats$(publicKey: string): Observable<any> {
|
2023-11-02 23:26:17 +00:00
|
|
|
return this.httpClient.get<any>(this.apiBaseUrl + this.apiBasePath + '/channels/' + publicKey + '/statistics');
|
2022-07-06 14:56:10 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 15:15:08 +02:00
|
|
|
listStatistics$(interval: string | undefined): Observable<any> {
|
|
|
|
return this.httpClient.get<any>(
|
2023-11-02 23:26:17 +00:00
|
|
|
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
|
|
|
}
|
2022-08-17 16:00:30 +02:00
|
|
|
|
|
|
|
getTopNodesByCapacity$(): Observable<ITopNodesPerCapacity[]> {
|
|
|
|
return this.httpClient.get<ITopNodesPerCapacity[]>(
|
2023-11-02 23:26:17 +00:00
|
|
|
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/rankings/liquidity'
|
2022-08-17 16:00:30 +02:00
|
|
|
);
|
|
|
|
}
|
2022-08-17 16:19:01 +02:00
|
|
|
|
|
|
|
getTopNodesByChannels$(): Observable<ITopNodesPerChannels[]> {
|
|
|
|
return this.httpClient.get<ITopNodesPerChannels[]>(
|
2023-11-02 23:26:17 +00:00
|
|
|
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/rankings/connectivity'
|
2022-08-17 16:19:01 +02:00
|
|
|
);
|
|
|
|
}
|
2022-08-17 21:20:11 +02:00
|
|
|
|
2023-07-09 01:15:05 -04:00
|
|
|
getPenaltyClosedChannels$(): Observable<IChannel[]> {
|
|
|
|
return this.httpClient.get<IChannel[]>(
|
2023-11-02 23:26:17 +00:00
|
|
|
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/penalties'
|
2023-07-09 01:15:05 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:20:11 +02:00
|
|
|
getOldestNodes$(): Observable<IOldestNodes[]> {
|
|
|
|
return this.httpClient.get<IOldestNodes[]>(
|
2023-11-02 23:26:17 +00:00
|
|
|
this.apiBaseUrl + this.apiBasePath + '/api/v1/lightning/nodes/rankings/age'
|
2022-08-17 21:20:11 +02:00
|
|
|
);
|
|
|
|
}
|
2022-04-27 02:52:23 +04:00
|
|
|
}
|