2021-07-10 10:04:15 -03:00
|
|
|
import { Component, OnInit, OnDestroy, ChangeDetectionStrategy, ChangeDetectorRef, Input } from '@angular/core';
|
2021-07-31 13:40:15 -03:00
|
|
|
import { Subscription } from 'rxjs';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { Block } from 'src/app/interfaces/electrs.interface';
|
|
|
|
import { StateService } from 'src/app/services/state.service';
|
2020-03-21 03:38:18 +07:00
|
|
|
import { Router } from '@angular/router';
|
2019-07-23 18:13:36 +03:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-blockchain-blocks',
|
|
|
|
templateUrl: './blockchain-blocks.component.html',
|
2020-07-30 17:01:13 +07:00
|
|
|
styleUrls: ['./blockchain-blocks.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
2019-07-23 18:13:36 +03:00
|
|
|
})
|
2020-03-22 17:44:36 +07:00
|
|
|
export class BlockchainBlocksComponent implements OnInit, OnDestroy {
|
2021-07-31 13:40:15 -03:00
|
|
|
|
2020-05-10 01:34:28 +07:00
|
|
|
network = '';
|
2021-07-10 10:04:15 -03:00
|
|
|
blocks: Block[] = this.mountEmptyBlocks();
|
2020-03-22 17:44:36 +07:00
|
|
|
markHeight: number;
|
2019-07-24 23:08:28 +03:00
|
|
|
blocksSubscription: Subscription;
|
2020-07-30 17:01:13 +07:00
|
|
|
networkSubscriotion: Subscription;
|
|
|
|
tabHiddenSubscription: Subscription;
|
|
|
|
markBlockSubscription: Subscription;
|
2020-06-10 23:52:14 +07:00
|
|
|
blockStyles = [];
|
2019-11-10 16:44:00 +08:00
|
|
|
interval: any;
|
2020-07-30 17:01:13 +07:00
|
|
|
tabHidden = false;
|
2021-07-31 13:40:15 -03:00
|
|
|
loadingBlocks = false;
|
2020-02-24 22:51:27 +07:00
|
|
|
|
2020-02-24 03:42:29 +07:00
|
|
|
arrowVisible = false;
|
2020-02-24 22:51:27 +07:00
|
|
|
arrowLeftPx = 30;
|
2020-07-21 12:47:51 +07:00
|
|
|
blocksFilled = false;
|
2020-03-21 03:38:18 +07:00
|
|
|
transition = '1s';
|
|
|
|
|
2020-05-10 21:34:25 +07:00
|
|
|
gradientColors = {
|
|
|
|
'': ['#9339f4', '#105fb0'],
|
2020-07-03 23:45:19 +07:00
|
|
|
bisq: ['#9339f4', '#105fb0'],
|
2020-05-10 21:34:25 +07:00
|
|
|
liquid: ['#116761', '#183550'],
|
|
|
|
testnet: ['#1d486f', '#183550'],
|
2021-02-21 01:45:52 +07:00
|
|
|
signet: ['#6f1d5d', '#471850'],
|
2020-05-10 21:34:25 +07:00
|
|
|
};
|
|
|
|
|
2019-07-23 18:13:36 +03:00
|
|
|
constructor(
|
2020-02-16 22:15:07 +07:00
|
|
|
private stateService: StateService,
|
2020-03-21 03:38:18 +07:00
|
|
|
private router: Router,
|
2020-07-30 17:01:13 +07:00
|
|
|
private cd: ChangeDetectorRef,
|
2019-07-23 18:13:36 +03:00
|
|
|
) { }
|
|
|
|
|
2019-07-24 23:08:28 +03:00
|
|
|
ngOnInit() {
|
2021-07-10 10:04:15 -03:00
|
|
|
this.blocks.forEach((b) => this.blockStyles.push(this.getStyleForBlock(b)));
|
2020-07-30 17:01:13 +07:00
|
|
|
this.networkSubscriotion = this.stateService.networkChanged$.subscribe((network) => this.network = network);
|
|
|
|
this.tabHiddenSubscription = this.stateService.isTabHidden$.subscribe((tabHidden) => this.tabHidden = tabHidden);
|
2020-05-10 01:34:28 +07:00
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
this.blocksSubscription = this.stateService.blocks$
|
2020-07-21 12:47:51 +07:00
|
|
|
.subscribe(([block, txConfirmed]) => {
|
2019-10-22 17:09:07 +08:00
|
|
|
if (this.blocks.some((b) => b.height === block.height)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-22 20:09:02 +07:00
|
|
|
|
2021-07-31 13:40:15 -03:00
|
|
|
this.loadingBlocks = true;
|
|
|
|
|
2020-07-22 20:09:02 +07:00
|
|
|
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);
|
2021-07-30 23:14:45 +03:00
|
|
|
this.blocks = this.blocks.slice(0, this.stateService.env.KEEP_BLOCKS_AMOUNT);
|
2020-02-19 23:50:23 +07:00
|
|
|
|
2020-07-21 12:47:51 +07:00
|
|
|
if (this.blocksFilled && !this.tabHidden) {
|
2020-06-18 17:10:07 +07:00
|
|
|
block.stage = block.matchRate >= 66 ? 1 : 2;
|
2020-06-10 23:52:14 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)));
|
2020-07-30 17:01:13 +07:00
|
|
|
this.cd.markForCheck();
|
2020-06-10 23:52:14 +07:00
|
|
|
}, 50);
|
|
|
|
|
2020-11-23 02:30:46 +07:00
|
|
|
if (this.blocks.length === this.stateService.env.KEEP_BLOCKS_AMOUNT) {
|
2020-07-21 12:47:51 +07:00
|
|
|
this.blocksFilled = true;
|
|
|
|
}
|
2020-07-30 17:01:13 +07:00
|
|
|
this.cd.markForCheck();
|
2019-07-27 18:43:17 +03:00
|
|
|
});
|
2019-07-24 23:08:28 +03:00
|
|
|
|
2020-07-30 17:01:13 +07:00
|
|
|
this.markBlockSubscription = this.stateService.markBlock$
|
2020-03-22 17:44:36 +07:00
|
|
|
.subscribe((state) => {
|
|
|
|
this.markHeight = undefined;
|
|
|
|
if (state.blockHeight) {
|
|
|
|
this.markHeight = state.blockHeight;
|
|
|
|
}
|
|
|
|
this.moveArrowToPosition(false);
|
2020-07-30 17:01:13 +07:00
|
|
|
this.cd.markForCheck();
|
2020-03-22 17:44:36 +07:00
|
|
|
});
|
2020-05-13 13:03:57 +07:00
|
|
|
|
|
|
|
this.stateService.keyNavigation$.subscribe((event) => {
|
|
|
|
if (!this.markHeight) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === 'ArrowRight') {
|
|
|
|
const blockindex = this.blocks.findIndex((b) => b.height === this.markHeight);
|
|
|
|
if (this.blocks[blockindex + 1]) {
|
|
|
|
this.router.navigate([(this.network ? '/' + this.network : '') + '/block/',
|
|
|
|
this.blocks[blockindex + 1].id], { state: { data: { block: this.blocks[blockindex + 1] } } });
|
|
|
|
}
|
|
|
|
} else if (event.key === 'ArrowLeft') {
|
|
|
|
const blockindex = this.blocks.findIndex((b) => b.height === this.markHeight);
|
|
|
|
if (blockindex === 0) {
|
|
|
|
this.router.navigate([(this.network ? '/' + this.network : '') + '/mempool-block/', '0']);
|
|
|
|
} else {
|
|
|
|
this.router.navigate([(this.network ? '/' + this.network : '') + '/block/',
|
|
|
|
this.blocks[blockindex - 1].id], { state: { data: { block: this.blocks[blockindex - 1] }}});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-02-19 23:50:23 +07:00
|
|
|
}
|
|
|
|
|
2019-07-24 23:08:28 +03:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.blocksSubscription.unsubscribe();
|
2020-07-30 17:01:13 +07:00
|
|
|
this.networkSubscriotion.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
|
|
|
}
|
|
|
|
|
2020-06-10 23:52:14 +07:00
|
|
|
moveArrowToPosition(animate: boolean, newBlockFromLeft = false) {
|
2020-02-19 23:50:23 +07:00
|
|
|
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);
|
2020-06-10 23:52:14 +07:00
|
|
|
if (blockindex > -1) {
|
2020-03-21 03:38:18 +07:00
|
|
|
if (!animate) {
|
|
|
|
this.transition = 'inherit';
|
|
|
|
}
|
2020-02-24 22:51:27 +07:00
|
|
|
this.arrowVisible = true;
|
2020-06-10 23:52:14 +07:00
|
|
|
if (newBlockFromLeft) {
|
|
|
|
this.arrowLeftPx = blockindex * 155 + 30 - 205;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.transition = '2s';
|
|
|
|
this.arrowLeftPx = blockindex * 155 + 30;
|
2020-07-30 17:01:13 +07:00
|
|
|
this.cd.markForCheck();
|
2020-06-10 23:52:14 +07:00
|
|
|
}, 50);
|
|
|
|
} else {
|
|
|
|
this.arrowLeftPx = blockindex * 155 + 30;
|
|
|
|
if (!animate) {
|
2020-07-30 17:01:13 +07:00
|
|
|
setTimeout(() => {
|
|
|
|
this.transition = '2s';
|
|
|
|
this.cd.markForCheck();
|
|
|
|
});
|
2020-06-10 23:52:14 +07:00
|
|
|
}
|
2020-03-21 03:38:18 +07:00
|
|
|
}
|
2020-02-19 23:50:23 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
trackByBlocksFn(index: number, item: Block) {
|
2019-07-23 18:13:36 +03:00
|
|
|
return item.height;
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
getStyleForBlock(block: Block) {
|
2019-07-23 18:13:36 +03:00
|
|
|
const greenBackgroundHeight = 100 - (block.weight / 4000000) * 100;
|
2020-06-10 23:52:14 +07:00
|
|
|
let addLeft = 0;
|
|
|
|
|
|
|
|
if (block.stage === 1) {
|
|
|
|
block.stage = 2;
|
|
|
|
addLeft = -205;
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:13:46 +07:00
|
|
|
return {
|
2020-06-10 23:52:14 +07:00
|
|
|
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
|
|
|
};
|
2019-07-23 18:13:36 +03:00
|
|
|
}
|
2021-07-10 10:04:15 -03:00
|
|
|
mountEmptyBlocks() {
|
|
|
|
const emptyBlocks = [];
|
2021-07-31 17:56:10 +03:00
|
|
|
for (let i = 0; i < this.stateService.env.KEEP_BLOCKS_AMOUNT; i++) {
|
2021-07-10 10:04:15 -03:00
|
|
|
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;
|
|
|
|
}
|
2019-07-23 18:13:36 +03:00
|
|
|
}
|