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

207 lines
6.1 KiB
TypeScript
Raw Normal View History

2021-11-09 22:25:03 -03:00
import { Component, OnInit, OnDestroy, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { StateService } from 'src/app/services/state.service';
import { Router } from '@angular/router';
2021-11-09 22:25:03 -03:00
import { specialBlocks } from 'src/app/app.constants';
import { BlockExtended } from 'src/app/interfaces/node-api.interface';
@Component({
selector: 'app-blockchain-blocks',
templateUrl: './blockchain-blocks.component.html',
styleUrls: ['./blockchain-blocks.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BlockchainBlocksComponent implements OnInit, OnDestroy {
2021-11-09 22:25:03 -03:00
specialBlocks = specialBlocks;
network = '';
blocks: BlockExtended[] = [];
emptyBlocks: BlockExtended[] = this.mountEmptyBlocks();
markHeight: number;
2019-07-24 23:08:28 +03:00
blocksSubscription: Subscription;
networkSubscription: Subscription;
tabHiddenSubscription: Subscription;
markBlockSubscription: Subscription;
loadingBlocks$: Observable<boolean>;
blockStyles = [];
emptyBlockStyles = [];
2019-11-10 16:44:00 +08:00
interval: any;
tabHidden = false;
feeRounding = '1.0-0';
2020-02-24 03:42:29 +07:00
arrowVisible = false;
2020-02-24 22:51:27 +07:00
arrowLeftPx = 30;
blocksFilled = false;
transition = '1s';
2020-05-10 21:34:25 +07:00
gradientColors = {
'': ['#9339f4', '#105fb0'],
bisq: ['#9339f4', '#105fb0'],
2020-05-10 21:34:25 +07:00
liquid: ['#116761', '#183550'],
'liquidtestnet': ['#494a4a', '#272e46'],
2020-05-10 21:34:25 +07:00
testnet: ['#1d486f', '#183550'],
2021-02-21 01:45:52 +07:00
signet: ['#6f1d5d', '#471850'],
2020-05-10 21:34:25 +07:00
};
constructor(
public stateService: StateService,
private router: Router,
private cd: ChangeDetectorRef,
) { }
2019-07-24 23:08:28 +03:00
ngOnInit() {
if (this.stateService.network === 'liquid' || this.stateService.network === 'liquidtestnet') {
this.feeRounding = '1.0-1';
}
this.emptyBlocks.forEach((b) => this.emptyBlockStyles.push(this.getStyleForEmptyBlock(b)));
this.loadingBlocks$ = this.stateService.isLoadingWebSocket$;
this.networkSubscription = this.stateService.networkChanged$.subscribe((network) => this.network = network);
this.tabHiddenSubscription = this.stateService.isTabHidden$.subscribe((tabHidden) => this.tabHidden = tabHidden);
this.blocksSubscription = this.stateService.blocks$
.subscribe(([block, txConfirmed]) => {
if (this.blocks.some((b) => b.height === block.height)) {
return;
}
if (this.blocks.length && block.height !== this.blocks[0].height + 1) {
this.blocks = [];
this.blocksFilled = false;
}
2019-07-27 18:43:17 +03:00
this.blocks.unshift(block);
this.blocks = this.blocks.slice(0, this.stateService.env.KEEP_BLOCKS_AMOUNT);
if (this.blocksFilled && !this.tabHidden && block.extra) {
block.extra.stage = block.extra.matchRate >= 66 ? 1 : 2;
}
if (txConfirmed) {
this.markHeight = block.height;
this.moveArrowToPosition(true, true);
} else {
this.moveArrowToPosition(true, false);
}
this.blockStyles = [];
this.blocks.forEach((b) => this.blockStyles.push(this.getStyleForBlock(b)));
setTimeout(() => {
this.blockStyles = [];
this.blocks.forEach((b) => this.blockStyles.push(this.getStyleForBlock(b)));
this.cd.markForCheck();
}, 50);
if (this.blocks.length === this.stateService.env.KEEP_BLOCKS_AMOUNT) {
this.blocksFilled = true;
}
this.cd.markForCheck();
2019-07-27 18:43:17 +03:00
});
2019-07-24 23:08:28 +03:00
this.markBlockSubscription = this.stateService.markBlock$
.subscribe((state) => {
this.markHeight = undefined;
if (state.blockHeight) {
this.markHeight = state.blockHeight;
}
this.moveArrowToPosition(false);
this.cd.markForCheck();
});
}
2019-07-24 23:08:28 +03:00
ngOnDestroy() {
this.blocksSubscription.unsubscribe();
this.networkSubscription.unsubscribe();
this.tabHiddenSubscription.unsubscribe();
this.markBlockSubscription.unsubscribe();
2019-11-10 16:44:00 +08:00
clearInterval(this.interval);
2019-07-24 23:08:28 +03:00
}
moveArrowToPosition(animate: boolean, newBlockFromLeft = false) {
if (!this.markHeight) {
this.arrowVisible = false;
return;
}
2020-02-24 22:51:27 +07:00
const blockindex = this.blocks.findIndex((b) => b.height === this.markHeight);
if (blockindex > -1) {
if (!animate) {
this.transition = 'inherit';
}
2020-02-24 22:51:27 +07:00
this.arrowVisible = true;
if (newBlockFromLeft) {
this.arrowLeftPx = blockindex * 155 + 30 - 205;
setTimeout(() => {
this.transition = '2s';
this.arrowLeftPx = blockindex * 155 + 30;
this.cd.markForCheck();
}, 50);
} else {
this.arrowLeftPx = blockindex * 155 + 30;
if (!animate) {
setTimeout(() => {
this.transition = '2s';
this.cd.markForCheck();
});
}
}
}
}
trackByBlocksFn(index: number, item: BlockExtended) {
return item.height;
}
getStyleForBlock(block: BlockExtended) {
const greenBackgroundHeight = 100 - (block.weight / this.stateService.env.BLOCK_WEIGHT_UNITS) * 100;
let addLeft = 0;
if (block?.extra?.stage === 1) {
block.extra.stage = 2;
addLeft = -205;
}
2020-03-05 16:13:46 +07:00
return {
left: addLeft + 155 * this.blocks.indexOf(block) + 'px',
2020-05-10 21:34:25 +07:00
background: `repeating-linear-gradient(
#2d3348,
#2d3348 ${greenBackgroundHeight}%,
${this.gradientColors[this.network][0]} ${Math.max(greenBackgroundHeight, 0)}%,
${this.gradientColors[this.network][1]} 100%
)`,
2020-03-05 16:13:46 +07:00
};
}
getStyleForEmptyBlock(block: BlockExtended) {
let addLeft = 0;
if (block?.extra?.stage === 1) {
block.extra.stage = 2;
addLeft = -205;
}
return {
left: addLeft + 155 * this.emptyBlocks.indexOf(block) + 'px',
background: "#2d3348",
};
}
mountEmptyBlocks() {
const emptyBlocks = [];
for (let i = 0; i < this.stateService.env.KEEP_BLOCKS_AMOUNT; i++) {
emptyBlocks.push({
id: '',
height: 0,
version: 0,
timestamp: 0,
bits: 0,
nonce: 0,
difficulty: 0,
merkle_root: '',
tx_count: 0,
size: 0,
weight: 0,
previousblockhash: '',
matchRate: 0,
stage: 0,
});
}
return emptyBlocks;
}
}