2020-07-29 10:16:09 +02:00
|
|
|
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
|
|
|
|
import { StateService } from 'src/app/services/state.service';
|
2020-07-29 10:20:23 +02:00
|
|
|
import { map, filter } from 'rxjs/operators';
|
2020-09-28 11:32:48 +02:00
|
|
|
import { merge, Observable } from 'rxjs';
|
2020-10-05 06:42:54 +02:00
|
|
|
import { MempoolBlock } from 'src/app/interfaces/websocket.interface';
|
2020-07-29 10:16:09 +02:00
|
|
|
|
|
|
|
interface FeeEstimations {
|
|
|
|
fastestFee: number;
|
|
|
|
halfHourFee: number;
|
|
|
|
hourFee: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-fees-box',
|
|
|
|
templateUrl: './fees-box.component.html',
|
|
|
|
styleUrls: ['./fees-box.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
})
|
|
|
|
export class FeesBoxComponent implements OnInit {
|
|
|
|
feeEstimations$: Observable<FeeEstimations>;
|
2020-07-30 08:13:22 +02:00
|
|
|
isLoadingWebSocket$: Observable<boolean>;
|
2020-10-05 06:42:54 +02:00
|
|
|
defaultFee: number;
|
2020-07-29 10:16:09 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private stateService: StateService,
|
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2020-10-05 06:42:54 +02:00
|
|
|
this.defaultFee = this.stateService.network === 'liquid' ? 0.1 : 1;
|
2020-09-27 13:12:36 +02:00
|
|
|
|
2020-07-30 08:13:22 +02:00
|
|
|
this.isLoadingWebSocket$ = this.stateService.isLoadingWebSocket$;
|
2020-07-29 10:16:09 +02:00
|
|
|
this.feeEstimations$ = this.stateService.mempoolBlocks$
|
|
|
|
.pipe(
|
|
|
|
map((pBlocks) => {
|
2020-09-28 11:32:48 +02:00
|
|
|
if (!pBlocks.length) {
|
|
|
|
return {
|
2020-10-05 06:42:54 +02:00
|
|
|
'fastestFee': this.defaultFee,
|
|
|
|
'halfHourFee': this.defaultFee,
|
|
|
|
'hourFee': this.defaultFee,
|
2020-09-28 11:32:48 +02:00
|
|
|
};
|
|
|
|
}
|
2020-07-29 10:16:09 +02:00
|
|
|
|
2020-10-05 06:42:54 +02:00
|
|
|
const firstMedianFee = this.optimizeMedianFee(pBlocks[0]);
|
|
|
|
const secondMedianFee = pBlocks[1] ? this.optimizeMedianFee(pBlocks[1], firstMedianFee) : this.defaultFee;
|
|
|
|
const thirdMedianFee = pBlocks[2] ? this.optimizeMedianFee(pBlocks[2], secondMedianFee) : this.defaultFee;
|
2020-07-29 10:16:09 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
'fastestFee': firstMedianFee,
|
|
|
|
'halfHourFee': secondMedianFee,
|
|
|
|
'hourFee': thirdMedianFee,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-05 06:42:54 +02:00
|
|
|
optimizeMedianFee(pBlock: MempoolBlock, previousFee?: number): number {
|
|
|
|
const useFee = previousFee ? (pBlock.medianFee + previousFee / 2) : pBlock.medianFee;
|
|
|
|
if (pBlock.blockVSize <= 500000) {
|
|
|
|
return this.defaultFee;
|
|
|
|
}
|
|
|
|
if (pBlock.blockVSize <= 950000) {
|
|
|
|
const multiplier = (pBlock.blockVSize - 500000) / 500000;
|
|
|
|
return Math.max(Math.round(useFee * multiplier), this.defaultFee);
|
|
|
|
}
|
|
|
|
return Math.round(useFee);
|
|
|
|
}
|
|
|
|
|
2020-07-29 10:16:09 +02:00
|
|
|
}
|