2024-07-08 14:36:38 +00:00
|
|
|
import { Component, Input, Output, OnChanges, EventEmitter, HostListener, OnInit, ViewChild, ElementRef, AfterViewInit, OnDestroy, ChangeDetectorRef } from '@angular/core';
|
|
|
|
import { Transaction } from '../../interfaces/electrs.interface';
|
2024-06-27 09:10:32 +00:00
|
|
|
import { AccelerationEstimate, RateOption } from './accelerate-checkout.component';
|
|
|
|
|
|
|
|
interface GraphBar {
|
|
|
|
rate: number;
|
2024-07-08 14:36:38 +00:00
|
|
|
style?: Record<string,string>;
|
2024-06-27 09:10:32 +00:00
|
|
|
class: 'tx' | 'target' | 'max';
|
|
|
|
label: string;
|
|
|
|
active?: boolean;
|
|
|
|
rateIndex?: number;
|
|
|
|
fee?: number;
|
2024-07-08 14:36:38 +00:00
|
|
|
height?: number;
|
2024-06-27 09:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-accelerate-fee-graph',
|
|
|
|
templateUrl: './accelerate-fee-graph.component.html',
|
|
|
|
styleUrls: ['./accelerate-fee-graph.component.scss'],
|
|
|
|
})
|
2024-07-08 14:36:38 +00:00
|
|
|
export class AccelerateFeeGraphComponent implements OnInit, AfterViewInit, OnChanges, OnDestroy {
|
2024-06-27 09:10:32 +00:00
|
|
|
@Input() tx: Transaction;
|
|
|
|
@Input() estimate: AccelerationEstimate;
|
|
|
|
@Input() showEstimate = false;
|
|
|
|
@Input() maxRateOptions: RateOption[] = [];
|
|
|
|
@Input() maxRateIndex: number = 0;
|
|
|
|
@Output() setUserBid = new EventEmitter<{ fee: number, index: number }>();
|
|
|
|
|
2024-07-08 14:36:38 +00:00
|
|
|
@ViewChild('feeGraph')
|
|
|
|
container: ElementRef<HTMLDivElement>;
|
|
|
|
height: number;
|
|
|
|
observer: ResizeObserver;
|
|
|
|
stopResizeLoop = false;
|
|
|
|
|
2024-06-27 09:10:32 +00:00
|
|
|
bars: GraphBar[] = [];
|
|
|
|
tooltipPosition = { x: 0, y: 0 };
|
|
|
|
|
2024-07-08 14:36:38 +00:00
|
|
|
constructor(
|
|
|
|
private cd: ChangeDetectorRef,
|
|
|
|
) {}
|
|
|
|
|
2024-06-27 09:10:32 +00:00
|
|
|
ngOnInit(): void {
|
|
|
|
this.initGraph();
|
|
|
|
}
|
|
|
|
|
2024-07-08 14:36:38 +00:00
|
|
|
ngAfterViewInit(): void {
|
|
|
|
if (ResizeObserver) {
|
|
|
|
this.observer = new ResizeObserver(entries => {
|
|
|
|
for (const entry of entries) {
|
|
|
|
this.height = entry.contentRect.height;
|
|
|
|
this.initGraph();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.observer.observe(this.container.nativeElement);
|
|
|
|
} else {
|
|
|
|
this.startResizeFallbackLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 09:10:32 +00:00
|
|
|
ngOnChanges(): void {
|
|
|
|
this.initGraph();
|
|
|
|
}
|
|
|
|
|
|
|
|
initGraph(): void {
|
|
|
|
if (!this.tx || !this.estimate) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-08 14:36:38 +00:00
|
|
|
const hasNextBlockRate = (this.estimate.nextBlockFee > this.estimate.txSummary.effectiveFee);
|
|
|
|
const numBars = hasNextBlockRate ? 4 : 3;
|
2024-06-27 09:10:32 +00:00
|
|
|
const maxRate = Math.max(...this.maxRateOptions.map(option => option.rate));
|
|
|
|
const baseRate = this.estimate.txSummary.effectiveFee / this.estimate.txSummary.effectiveVsize;
|
2024-07-08 14:36:38 +00:00
|
|
|
let baseHeight = Math.max(this.height - (numBars * 30), this.height * (baseRate / maxRate));
|
|
|
|
const bars: GraphBar[] = [];
|
|
|
|
let lastHeight = 0;
|
|
|
|
if (hasNextBlockRate) {
|
|
|
|
lastHeight = Math.max(lastHeight + 30, (this.height * ((this.estimate.targetFeeRate - baseRate) / maxRate)));
|
2024-06-27 09:10:32 +00:00
|
|
|
bars.push({
|
|
|
|
rate: this.estimate.targetFeeRate,
|
2024-07-08 14:36:38 +00:00
|
|
|
height: lastHeight,
|
2024-06-27 09:10:32 +00:00
|
|
|
class: 'target',
|
|
|
|
label: $localize`:@@bdf0e930eb22431140a2eaeacd809cc5f8ebd38c:Next Block`.toLowerCase(),
|
|
|
|
fee: this.estimate.nextBlockFee - this.estimate.txSummary.effectiveFee
|
|
|
|
});
|
|
|
|
}
|
2024-07-08 14:36:38 +00:00
|
|
|
this.maxRateOptions.forEach((option, index) => {
|
|
|
|
lastHeight = Math.max(lastHeight + 30, (this.height * ((option.rate - baseRate) / maxRate)));
|
|
|
|
bars.push({
|
|
|
|
rate: option.rate,
|
|
|
|
height: lastHeight,
|
|
|
|
class: 'max',
|
2024-07-12 23:20:18 +09:00
|
|
|
label: this.showEstimate ? $localize`maximum` : $localize`accelerated`,
|
2024-07-08 14:36:38 +00:00
|
|
|
active: option.index === this.maxRateIndex,
|
|
|
|
rateIndex: option.index,
|
|
|
|
fee: option.fee,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
bars.reverse();
|
|
|
|
|
|
|
|
baseHeight = this.height - lastHeight;
|
|
|
|
|
|
|
|
for (const bar of bars) {
|
|
|
|
bar.style = this.getStyle(bar.height, baseHeight);
|
|
|
|
}
|
|
|
|
|
2024-06-27 09:10:32 +00:00
|
|
|
bars.push({
|
|
|
|
rate: baseRate,
|
2024-07-08 14:36:38 +00:00
|
|
|
style: this.getStyle(baseHeight, 0),
|
|
|
|
height: baseHeight,
|
2024-06-27 09:10:32 +00:00
|
|
|
class: 'tx',
|
|
|
|
label: '',
|
|
|
|
fee: this.estimate.txSummary.effectiveFee,
|
|
|
|
});
|
2024-07-08 14:36:38 +00:00
|
|
|
|
2024-06-27 09:10:32 +00:00
|
|
|
this.bars = bars;
|
2024-07-08 14:36:38 +00:00
|
|
|
this.cd.detectChanges();
|
2024-06-27 09:10:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 14:36:38 +00:00
|
|
|
getStyle(height: number, base: number): Record<string,string> {
|
2024-06-27 09:10:32 +00:00
|
|
|
return {
|
2024-07-08 14:36:38 +00:00
|
|
|
height: `${height}px`,
|
|
|
|
bottom: base ? `${base}px` : '0',
|
2024-06-27 09:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick(event, bar): void {
|
|
|
|
if (bar.rateIndex != null) {
|
|
|
|
this.setUserBid.emit({ fee: bar.fee, index: bar.rateIndex });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('pointermove', ['$event'])
|
|
|
|
onPointerMove(event) {
|
|
|
|
this.tooltipPosition = { x: event.offsetX, y: event.offsetY };
|
|
|
|
}
|
2024-07-08 14:36:38 +00:00
|
|
|
|
|
|
|
startResizeFallbackLoop(): void {
|
|
|
|
if (this.stopResizeLoop) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.height = this.container?.nativeElement?.clientHeight || 0;
|
|
|
|
this.initGraph();
|
|
|
|
this.startResizeFallbackLoop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.stopResizeLoop = true;
|
|
|
|
this.observer.disconnect();
|
|
|
|
}
|
2024-06-27 09:10:32 +00:00
|
|
|
}
|