mempool/frontend/src/app/components/tx-fee-rating/tx-fee-rating.component.ts

78 lines
2.3 KiB
TypeScript
Raw Normal View History

import { Component, ChangeDetectionStrategy, OnChanges, Input, OnInit, ChangeDetectorRef, OnDestroy } from '@angular/core';
2022-09-21 17:23:45 +02:00
import { Transaction } from '../../interfaces/electrs.interface';
import { StateService } from '../../services/state.service';
import { Subscription } from 'rxjs';
2022-09-21 17:23:45 +02:00
import { BlockExtended } from '../../interfaces/node-api.interface';
2023-03-12 15:54:39 +09:00
import { CacheService } from '../../services/cache.service';
@Component({
selector: 'app-tx-fee-rating',
templateUrl: './tx-fee-rating.component.html',
styleUrls: ['./tx-fee-rating.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TxFeeRatingComponent implements OnInit, OnChanges, OnDestroy {
@Input() tx: Transaction;
blocksSubscription: Subscription;
medianFeeNeeded: number;
overpaidTimes: number;
feeRating: number;
blocks: BlockExtended[] = [];
constructor(
private stateService: StateService,
2023-03-12 15:54:39 +09:00
private cacheService: CacheService,
private cd: ChangeDetectorRef,
) { }
ngOnInit() {
2023-03-12 15:54:39 +09:00
this.blocksSubscription = this.cacheService.loadedBlocks$.subscribe((block) => {
2022-02-04 19:28:00 +09:00
if (this.tx.status.confirmed && this.tx.status.block_height === block.height && block?.extras?.medianFee > 0) {
this.calculateRatings(block);
this.cd.markForCheck();
}
});
}
ngOnChanges() {
this.feeRating = undefined;
if (!this.tx.status.confirmed) {
return;
}
2023-03-12 15:54:39 +09:00
this.cacheService.loadBlock(this.tx.status.block_height);
2023-03-12 15:54:39 +09:00
const foundBlock = this.cacheService.getCachedBlock(this.tx.status.block_height) || null;
2022-02-04 19:28:00 +09:00
if (foundBlock && foundBlock?.extras?.medianFee > 0) {
this.calculateRatings(foundBlock);
}
}
ngOnDestroy() {
this.blocksSubscription.unsubscribe();
}
calculateRatings(block: BlockExtended) {
const feePervByte = this.tx.effectiveFeePerVsize || this.tx.fee / (this.tx.weight / 4);
2022-02-04 19:28:00 +09:00
this.medianFeeNeeded = block?.extras?.medianFee;
// Block not filled
if (block.weight < this.stateService.env.BLOCK_WEIGHT_UNITS * 0.95) {
this.medianFeeNeeded = 1;
}
this.overpaidTimes = Math.round(feePervByte / this.medianFeeNeeded);
if (feePervByte <= this.medianFeeNeeded || this.overpaidTimes < 2) {
this.feeRating = 1;
} else {
this.feeRating = 2;
if (this.overpaidTimes > 10) {
this.feeRating = 3;
}
}
}
}