2024-01-23 09:57:26 +01:00
|
|
|
import { Component, OnInit, ChangeDetectionStrategy, Input } from '@angular/core';
|
2024-02-27 16:39:28 +01:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { BehaviorSubject, Observable, Subject, combineLatest, of, timer } from 'rxjs';
|
2024-01-27 19:19:14 +01:00
|
|
|
import { delayWhen, filter, map, share, shareReplay, switchMap, takeUntil, tap, throttleTime } from 'rxjs/operators';
|
2024-01-21 13:33:20 +01:00
|
|
|
import { ApiService } from '../../../services/api.service';
|
|
|
|
import { Env, StateService } from '../../../services/state.service';
|
2024-01-26 18:52:07 +01:00
|
|
|
import { AuditStatus, CurrentPegs, FederationUtxo } from '../../../interfaces/node-api.interface';
|
2024-01-21 13:33:20 +01:00
|
|
|
import { WebsocketService } from '../../../services/websocket.service';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-federation-utxos-list',
|
|
|
|
templateUrl: './federation-utxos-list.component.html',
|
|
|
|
styleUrls: ['./federation-utxos-list.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
})
|
|
|
|
export class FederationUtxosListComponent implements OnInit {
|
|
|
|
@Input() widget: boolean = false;
|
|
|
|
@Input() federationUtxos$: Observable<FederationUtxo[]>;
|
|
|
|
|
|
|
|
env: Env;
|
|
|
|
isLoading = true;
|
|
|
|
page = 1;
|
|
|
|
pageSize = 15;
|
|
|
|
maxSize = window.innerWidth <= 767.98 ? 3 : 5;
|
|
|
|
skeletonLines: number[] = [];
|
|
|
|
auditStatus$: Observable<AuditStatus>;
|
2024-01-23 09:57:26 +01:00
|
|
|
auditUpdated$: Observable<boolean>;
|
2024-02-27 16:39:28 +01:00
|
|
|
showExpiredUtxos: boolean = false;
|
|
|
|
showExpiredUtxosToggleSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(this.showExpiredUtxos);
|
|
|
|
showExpiredUtxosToggle$: Observable<boolean> = this.showExpiredUtxosToggleSubject.asObservable();
|
2024-01-23 09:57:26 +01:00
|
|
|
lastReservesBlockUpdate: number = 0;
|
2024-01-26 18:52:07 +01:00
|
|
|
currentPeg$: Observable<CurrentPegs>;
|
|
|
|
lastPegBlockUpdate: number = 0;
|
|
|
|
lastPegAmount: string = '';
|
2024-01-26 20:43:34 +01:00
|
|
|
isLoad: boolean = true;
|
2024-01-21 13:33:20 +01:00
|
|
|
|
2024-01-27 19:19:14 +01:00
|
|
|
private destroy$ = new Subject();
|
2024-01-28 21:46:31 +01:00
|
|
|
|
2024-01-21 13:33:20 +01:00
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
|
|
|
public stateService: StateService,
|
|
|
|
private websocketService: WebsocketService,
|
2024-02-27 16:39:28 +01:00
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router
|
2024-01-21 13:33:20 +01:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.isLoading = !this.widget;
|
|
|
|
this.env = this.stateService.env;
|
2024-01-29 12:22:47 +01:00
|
|
|
this.skeletonLines = this.widget === true ? [...Array(6).keys()] : [...Array(15).keys()];
|
2024-01-28 21:46:31 +01:00
|
|
|
|
2024-01-21 13:33:20 +01:00
|
|
|
if (!this.widget) {
|
2024-02-27 16:39:28 +01:00
|
|
|
this.route.fragment.subscribe((fragment) => {
|
|
|
|
this.showExpiredUtxosToggleSubject.next(['expired'].indexOf(fragment) > -1);
|
|
|
|
});
|
|
|
|
|
2024-01-21 13:33:20 +01:00
|
|
|
this.websocketService.want(['blocks']);
|
2024-02-27 16:39:28 +01:00
|
|
|
|
2024-01-26 20:43:34 +01:00
|
|
|
this.auditStatus$ = this.stateService.blocks$.pipe(
|
2024-01-27 19:19:14 +01:00
|
|
|
takeUntil(this.destroy$),
|
2024-01-26 20:43:34 +01:00
|
|
|
throttleTime(40000),
|
|
|
|
delayWhen(_ => this.isLoad ? timer(0) : timer(2000)),
|
|
|
|
tap(() => this.isLoad = false),
|
|
|
|
switchMap(() => this.apiService.federationAuditSynced$()),
|
|
|
|
shareReplay(1)
|
2024-01-21 13:33:20 +01:00
|
|
|
);
|
|
|
|
|
2024-01-26 18:52:07 +01:00
|
|
|
this.currentPeg$ = this.auditStatus$.pipe(
|
2024-01-21 13:33:20 +01:00
|
|
|
filter(auditStatus => auditStatus.isAuditSynced === true),
|
2024-01-26 18:52:07 +01:00
|
|
|
switchMap(_ =>
|
|
|
|
this.apiService.liquidPegs$().pipe(
|
|
|
|
filter((currentPegs) => currentPegs.lastBlockUpdate >= this.lastPegBlockUpdate),
|
|
|
|
tap((currentPegs) => {
|
|
|
|
this.lastPegBlockUpdate = currentPegs.lastBlockUpdate;
|
|
|
|
})
|
|
|
|
)
|
|
|
|
),
|
|
|
|
share()
|
|
|
|
);
|
|
|
|
|
|
|
|
this.auditUpdated$ = combineLatest([
|
|
|
|
this.auditStatus$,
|
2024-02-27 16:39:28 +01:00
|
|
|
this.currentPeg$,
|
|
|
|
this.showExpiredUtxosToggle$
|
2024-01-26 18:52:07 +01:00
|
|
|
]).pipe(
|
2024-02-27 16:39:28 +01:00
|
|
|
filter(([auditStatus, _, __]) => auditStatus.isAuditSynced === true),
|
|
|
|
map(([auditStatus, currentPeg, showExpiredUtxos]) => ({
|
2024-01-26 18:52:07 +01:00
|
|
|
lastBlockAudit: auditStatus.lastBlockAudit,
|
2024-02-27 16:39:28 +01:00
|
|
|
currentPegAmount: currentPeg.amount,
|
|
|
|
showExpiredUtxos: showExpiredUtxos
|
2024-01-26 18:52:07 +01:00
|
|
|
})),
|
2024-02-27 16:39:28 +01:00
|
|
|
switchMap(({ lastBlockAudit, currentPegAmount, showExpiredUtxos }) => {
|
2024-01-26 18:52:07 +01:00
|
|
|
const blockAuditCheck = lastBlockAudit > this.lastReservesBlockUpdate;
|
|
|
|
const amountCheck = currentPegAmount !== this.lastPegAmount;
|
2024-02-27 16:39:28 +01:00
|
|
|
const expiredCheck = showExpiredUtxos !== this.showExpiredUtxos;
|
2024-01-26 18:52:07 +01:00
|
|
|
this.lastReservesBlockUpdate = lastBlockAudit;
|
|
|
|
this.lastPegAmount = currentPegAmount;
|
2024-02-27 16:39:28 +01:00
|
|
|
this.showExpiredUtxos = showExpiredUtxos;
|
|
|
|
return of(blockAuditCheck || amountCheck || expiredCheck);
|
2024-01-26 18:52:07 +01:00
|
|
|
}),
|
|
|
|
share()
|
2024-01-23 09:57:26 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.federationUtxos$ = this.auditUpdated$.pipe(
|
|
|
|
filter(auditUpdated => auditUpdated === true),
|
2024-02-27 16:39:28 +01:00
|
|
|
switchMap(_ => this.showExpiredUtxos ? this.apiService.expiredUtxos$() : this.apiService.federationUtxos$()),
|
2024-01-21 13:33:20 +01:00
|
|
|
tap(_ => this.isLoading = false),
|
|
|
|
share()
|
|
|
|
);
|
|
|
|
}
|
2024-01-27 19:19:14 +01:00
|
|
|
}
|
2024-01-21 13:33:20 +01:00
|
|
|
|
2024-01-27 19:19:14 +01:00
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.destroy$.next(1);
|
|
|
|
this.destroy$.complete();
|
2024-01-21 13:33:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pageChange(page: number): void {
|
|
|
|
this.page = page;
|
|
|
|
}
|
|
|
|
|
2024-02-27 16:39:28 +01:00
|
|
|
getGradientColor(value: number): string {
|
|
|
|
const distanceToGreen = Math.abs(4032 - value);
|
2024-04-10 14:39:29 +09:00
|
|
|
const green = 'var(--green)';
|
|
|
|
const red = 'var(--red)';
|
2024-02-27 16:39:28 +01:00
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
return red;
|
|
|
|
} else if (value >= 4032) {
|
|
|
|
return green;
|
|
|
|
} else {
|
|
|
|
const scaleFactor = 1 - distanceToGreen / 4032;
|
|
|
|
const r = parseInt(red.slice(1, 3), 16);
|
|
|
|
const g = parseInt(green.slice(1, 3), 16);
|
|
|
|
const b = parseInt(red.slice(5, 7), 16);
|
|
|
|
|
|
|
|
const newR = Math.floor(r + (g - r) * scaleFactor);
|
|
|
|
const newG = Math.floor(g - (g - r) * scaleFactor);
|
|
|
|
const newB = b;
|
|
|
|
|
|
|
|
return '#' + this.componentToHex(newR) + this.componentToHex(newG) + this.componentToHex(newB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentToHex(c: number): string {
|
|
|
|
const hex = c.toString(16);
|
|
|
|
return hex.length == 1 ? '0' + hex : hex;
|
|
|
|
}
|
|
|
|
|
2024-01-21 13:33:20 +01:00
|
|
|
}
|