mempool/frontend/src/app/components/blockchain-blocks/blockchain-blocks.component.ts

116 lines
3.5 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, OnDestroy, Input, OnChanges } from '@angular/core';
2019-07-24 23:08:28 +03:00
import { Subscription } from 'rxjs';
import { Block } from 'src/app/interfaces/electrs.interface';
import { StateService } from 'src/app/services/state.service';
@Component({
selector: 'app-blockchain-blocks',
templateUrl: './blockchain-blocks.component.html',
styleUrls: ['./blockchain-blocks.component.scss']
})
export class BlockchainBlocksComponent implements OnInit, OnChanges, OnDestroy {
@Input() markHeight = 0;
2020-02-24 03:42:29 +07:00
@Input() txFeePerVSize: number;
blocks: Block[] = [];
2019-07-24 23:08:28 +03:00
blocksSubscription: Subscription;
2019-11-10 16:44:00 +08:00
interval: any;
trigger = 0;
2020-02-24 03:42:29 +07:00
blockWidth = 125;
blockPadding = 30;
arrowLeftPx = 30;
2020-02-24 03:42:29 +07:00
rightPosition = 0;
arrowVisible = false;
constructor(
private stateService: StateService,
) { }
2019-07-24 23:08:28 +03:00
ngOnInit() {
this.blocksSubscription = this.stateService.blocks$
2019-07-27 18:43:17 +03:00
.subscribe((block) => {
if (this.blocks.some((b) => b.height === block.height)) {
return;
}
2019-07-27 18:43:17 +03:00
this.blocks.unshift(block);
this.blocks = this.blocks.slice(0, 8);
this.moveArrowToPosition();
2019-07-27 18:43:17 +03:00
});
2019-11-10 16:44:00 +08:00
this.interval = setInterval(() => this.trigger++, 10 * 1000);
2019-07-24 23:08:28 +03:00
}
ngOnChanges() {
this.moveArrowToPosition();
}
2019-07-24 23:08:28 +03:00
ngOnDestroy() {
this.blocksSubscription.unsubscribe();
2019-11-10 16:44:00 +08:00
clearInterval(this.interval);
2019-07-24 23:08:28 +03:00
}
moveArrowToPosition() {
if (!this.markHeight) {
this.arrowVisible = false;
return;
}
2020-02-24 03:42:29 +07:00
const block = this.blocks.find((b) => b.height === this.markHeight);
if (!block) {
return;
}
const blockindex = this.blocks.indexOf(block);
this.arrowVisible = true;
this.rightPosition = blockindex * -(this.blockWidth + this.blockPadding) - 30;
if (!this.txFeePerVSize) {
return;
}
for (let i = 0; i < block.feeRange.length - 1; i++) {
if (this.txFeePerVSize < block.feeRange[i + 1] && this.txFeePerVSize >= block.feeRange[i]) {
const feeRangeIndex = block.feeRange.findIndex((val, index) => this.txFeePerVSize < block.feeRange[index + 1]);
const feeRangeChunkSize = 1 / (block.feeRange.length - 1);
const txFee = this.txFeePerVSize - block.feeRange[i];
const max = block.feeRange[i + 1] - block.feeRange[i];
const blockLocation = txFee / max;
const chunkPositionOffset = blockLocation * feeRangeChunkSize;
const feePosition = feeRangeChunkSize * feeRangeIndex + chunkPositionOffset;
const blockedFilledPercentage = (block.weight > 4000000 ? 4000000 : block.weight) / 4000000;
const arrowRightPosition = blockindex * (-this.blockWidth + this.blockPadding)
+ ((1 - feePosition) * blockedFilledPercentage * this.blockWidth);
this.rightPosition = arrowRightPosition - 93;
break;
}
}
}
trackByBlocksFn(index: number, item: Block) {
return item.height;
}
getStyleForBlock(block: Block) {
const greenBackgroundHeight = 100 - (block.weight / 4000000) * 100;
if (window.innerWidth <= 768) {
return {
top: 155 * this.blocks.indexOf(block) + 'px',
2020-02-23 19:16:50 +07:00
background: `repeating-linear-gradient(to right, #2d3348, #2d3348 ${greenBackgroundHeight}%,
#9339f4 ${Math.max(greenBackgroundHeight, 0)}%, #105fb0 100%)`,
};
} else {
return {
left: 155 * this.blocks.indexOf(block) + 'px',
2020-02-23 19:16:50 +07:00
background: `repeating-linear-gradient(to right, #2d3348, #2d3348 ${greenBackgroundHeight}%,
#9339f4 ${Math.max(greenBackgroundHeight, 0)}%, #105fb0 100%)`,
};
}
}
}