2020-03-22 11:44:36 +01:00
|
|
|
import { Component, OnInit, OnDestroy, HostListener } from '@angular/core';
|
2020-07-24 09:11:49 +02:00
|
|
|
import { Subscription, pipe } from 'rxjs';
|
2020-02-16 16:15:07 +01:00
|
|
|
import { MempoolBlock } from 'src/app/interfaces/websocket.interface';
|
|
|
|
import { StateService } from 'src/app/services/state.service';
|
2020-03-20 21:38:18 +01:00
|
|
|
import { Router } from '@angular/router';
|
2020-07-24 09:11:49 +02:00
|
|
|
import { take, map } from 'rxjs/operators';
|
2020-05-24 17:23:56 +02:00
|
|
|
import { feeLevels, mempoolFeeColors } from 'src/app/app.constants';
|
2020-05-09 15:37:50 +02:00
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'app-mempool-blocks',
|
|
|
|
templateUrl: './mempool-blocks.component.html',
|
2020-05-24 17:23:56 +02:00
|
|
|
styleUrls: ['./mempool-blocks.component.scss'],
|
2020-02-16 16:15:07 +01:00
|
|
|
})
|
2020-03-22 11:44:36 +01:00
|
|
|
export class MempoolBlocksComponent implements OnInit, OnDestroy {
|
2020-02-16 16:15:07 +01:00
|
|
|
mempoolBlocks: MempoolBlock[];
|
2020-03-12 15:56:07 +01:00
|
|
|
mempoolBlocksFull: MempoolBlock[];
|
2020-05-24 17:23:56 +02:00
|
|
|
mempoolBlockStyles = [];
|
2020-02-16 16:15:07 +01:00
|
|
|
mempoolBlocksSubscription: Subscription;
|
2020-05-09 15:37:50 +02:00
|
|
|
network = '';
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
blockWidth = 125;
|
2020-02-22 23:23:24 +01:00
|
|
|
blockPadding = 30;
|
|
|
|
arrowVisible = false;
|
2020-06-10 20:38:59 +02:00
|
|
|
tabHidden = true;
|
2020-02-22 23:23:24 +01:00
|
|
|
|
|
|
|
rightPosition = 0;
|
2020-06-10 18:52:14 +02:00
|
|
|
transition = '2s';
|
2020-02-16 16:15:07 +01:00
|
|
|
|
2020-03-22 11:44:36 +01:00
|
|
|
markIndex: number;
|
|
|
|
txFeePerVSize: number;
|
2020-02-16 16:15:07 +01:00
|
|
|
|
2020-03-24 21:59:30 +01:00
|
|
|
resetTransitionTimeout: number;
|
2020-06-10 18:52:14 +02:00
|
|
|
|
|
|
|
blockIndex = 1;
|
2020-05-11 20:12:38 +02:00
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
constructor(
|
2020-03-20 21:38:18 +01:00
|
|
|
private router: Router,
|
2020-02-16 16:15:07 +01:00
|
|
|
private stateService: StateService,
|
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-06-10 20:38:59 +02:00
|
|
|
this.stateService.isTabHidden$.subscribe((tabHidden) => this.tabHidden = tabHidden);
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
this.mempoolBlocksSubscription = this.stateService.mempoolBlocks$
|
2020-07-24 09:11:49 +02:00
|
|
|
.pipe(
|
|
|
|
map((blocks) => {
|
|
|
|
if (!blocks.length) {
|
|
|
|
return [{ index: 0, blockSize: 0, blockVSize: 0, feeRange: [0, 0], medianFee: 0, nTx: 0, totalFees: 0 }];
|
|
|
|
}
|
|
|
|
return blocks;
|
|
|
|
}),
|
|
|
|
)
|
2020-02-16 16:15:07 +01:00
|
|
|
.subscribe((blocks) => {
|
2020-06-10 18:52:14 +02:00
|
|
|
blocks.forEach((block, i) => {
|
|
|
|
block.index = this.blockIndex + i;
|
|
|
|
});
|
2020-03-25 12:41:16 +01:00
|
|
|
const stringifiedBlocks = JSON.stringify(blocks);
|
|
|
|
this.mempoolBlocksFull = JSON.parse(stringifiedBlocks);
|
|
|
|
this.mempoolBlocks = this.reduceMempoolBlocksToFitScreen(JSON.parse(stringifiedBlocks));
|
2020-05-24 17:23:56 +02:00
|
|
|
this.updateMempoolBlockStyles();
|
2020-02-16 16:15:07 +01:00
|
|
|
this.calculateTransactionPosition();
|
|
|
|
});
|
2020-03-22 11:44:36 +01:00
|
|
|
|
|
|
|
this.stateService.markBlock$
|
|
|
|
.subscribe((state) => {
|
|
|
|
this.markIndex = undefined;
|
|
|
|
this.txFeePerVSize = undefined;
|
|
|
|
if (state.mempoolBlockIndex !== undefined) {
|
|
|
|
this.markIndex = state.mempoolBlockIndex;
|
|
|
|
}
|
|
|
|
if (state.txFeePerVSize) {
|
|
|
|
this.txFeePerVSize = state.txFeePerVSize;
|
|
|
|
}
|
|
|
|
this.calculateTransactionPosition();
|
|
|
|
});
|
2020-05-09 15:37:50 +02:00
|
|
|
|
2020-06-10 18:52:14 +02:00
|
|
|
this.stateService.blocks$
|
|
|
|
.subscribe(([block]) => {
|
2020-07-22 14:21:40 +02:00
|
|
|
if (block.matchRate >= 66 && !this.tabHidden) {
|
2020-06-10 18:52:14 +02:00
|
|
|
this.blockIndex++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-09 15:37:50 +02:00
|
|
|
this.stateService.networkChanged$
|
|
|
|
.subscribe((network) => this.network = network);
|
2020-03-12 15:56:07 +01:00
|
|
|
|
2020-05-13 08:03:57 +02:00
|
|
|
this.stateService.keyNavigation$.subscribe((event) => {
|
2020-03-22 11:44:36 +01:00
|
|
|
if (this.markIndex === undefined) {
|
|
|
|
return;
|
2020-03-20 21:38:18 +01:00
|
|
|
}
|
2020-05-13 08:03:57 +02:00
|
|
|
|
2020-03-22 11:44:36 +01:00
|
|
|
if (event.key === 'ArrowRight') {
|
|
|
|
if (this.mempoolBlocks[this.markIndex - 1]) {
|
2020-05-09 20:34:28 +02:00
|
|
|
this.router.navigate([(this.network ? '/' + this.network : '') + '/mempool-block/', this.markIndex - 1]);
|
2020-03-22 11:44:36 +01:00
|
|
|
} else {
|
|
|
|
this.stateService.blocks$
|
2020-03-22 17:45:16 +01:00
|
|
|
.pipe(take(8))
|
2020-06-10 18:52:14 +02:00
|
|
|
.subscribe(([block]) => {
|
2020-03-22 11:44:36 +01:00
|
|
|
if (this.stateService.latestBlockHeight === block.height) {
|
2020-05-09 20:34:28 +02:00
|
|
|
this.router.navigate([(this.network ? '/' + this.network : '') + '/block/', block.id], { state: { data: { block } }});
|
2020-03-22 11:44:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (event.key === 'ArrowLeft') {
|
|
|
|
if (this.mempoolBlocks[this.markIndex + 1]) {
|
2020-05-09 20:34:28 +02:00
|
|
|
this.router.navigate([(this.network ? '/' + this.network : '') + '/mempool-block/', this.markIndex + 1]);
|
2020-03-22 11:44:36 +01:00
|
|
|
}
|
2020-03-20 21:38:18 +01:00
|
|
|
}
|
2020-03-22 11:44:36 +01:00
|
|
|
});
|
2020-02-22 23:23:24 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 08:03:57 +02:00
|
|
|
@HostListener('window:resize', ['$event'])
|
|
|
|
onResize() {
|
|
|
|
if (this.mempoolBlocks && this.mempoolBlocks.length) {
|
|
|
|
this.mempoolBlocks = this.reduceMempoolBlocksToFitScreen(JSON.parse(JSON.stringify(this.mempoolBlocksFull)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.mempoolBlocksSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:52:14 +02:00
|
|
|
trackByFn(index: number, block: MempoolBlock) {
|
|
|
|
return block.index;
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 15:56:07 +01:00
|
|
|
reduceMempoolBlocksToFitScreen(blocks: MempoolBlock[]): MempoolBlock[] {
|
2020-06-04 10:08:24 +02:00
|
|
|
const blocksAmount = Math.max(2, Math.floor(window.innerWidth / 2 / (this.blockWidth + this.blockPadding)));
|
2020-03-12 15:56:07 +01:00
|
|
|
while (blocks.length > blocksAmount) {
|
|
|
|
const block = blocks.pop();
|
|
|
|
const lastBlock = blocks[blocks.length - 1];
|
|
|
|
lastBlock.blockSize += block.blockSize;
|
|
|
|
lastBlock.blockVSize += block.blockVSize;
|
|
|
|
lastBlock.nTx += block.nTx;
|
|
|
|
lastBlock.feeRange = lastBlock.feeRange.concat(block.feeRange);
|
|
|
|
lastBlock.feeRange.sort((a, b) => a - b);
|
|
|
|
lastBlock.medianFee = this.median(lastBlock.feeRange);
|
|
|
|
}
|
|
|
|
return blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
median(numbers: number[]) {
|
|
|
|
let medianNr = 0;
|
|
|
|
const numsLen = numbers.length;
|
|
|
|
if (numsLen % 2 === 0) {
|
|
|
|
medianNr = (numbers[numsLen / 2 - 1] + numbers[numsLen / 2]) / 2;
|
|
|
|
} else {
|
|
|
|
medianNr = numbers[(numsLen - 1) / 2];
|
|
|
|
}
|
|
|
|
return medianNr;
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:23:56 +02:00
|
|
|
updateMempoolBlockStyles() {
|
|
|
|
this.mempoolBlockStyles = [];
|
|
|
|
this.mempoolBlocksFull.forEach((block, i) => this.mempoolBlockStyles.push(this.getStyleForMempoolBlock(block, i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
getStyleForMempoolBlock(mempoolBlock: MempoolBlock, index: number) {
|
|
|
|
const emptyBackgroundSpacePercentage = Math.max(100 - mempoolBlock.blockVSize / 1000000 * 100, 0);
|
2020-05-31 13:34:03 +02:00
|
|
|
const usedBlockSpace = 100 - emptyBackgroundSpacePercentage;
|
2020-05-24 17:23:56 +02:00
|
|
|
const backgroundGradients = [`repeating-linear-gradient(to right, #554b45, #554b45 ${emptyBackgroundSpacePercentage}%`];
|
|
|
|
const gradientColors = [];
|
|
|
|
|
|
|
|
const trimmedFeeRange = index === 0 ? mempoolBlock.feeRange.slice(0, -1) : mempoolBlock.feeRange;
|
|
|
|
|
|
|
|
trimmedFeeRange.forEach((fee: number) => {
|
|
|
|
let feeLevelIndex = feeLevels.slice().reverse().findIndex((feeLvl) => fee >= feeLvl);
|
|
|
|
feeLevelIndex = feeLevelIndex >= 0 ? feeLevels.length - feeLevelIndex : feeLevelIndex;
|
|
|
|
gradientColors.push(mempoolFeeColors[feeLevelIndex - 1] || mempoolFeeColors[mempoolFeeColors.length - 1]);
|
|
|
|
});
|
|
|
|
|
2020-05-31 13:34:03 +02:00
|
|
|
|
2020-05-24 17:23:56 +02:00
|
|
|
gradientColors.forEach((color, i, gc) => {
|
|
|
|
backgroundGradients.push(`
|
2020-05-31 13:34:03 +02:00
|
|
|
#${i === 0 ? color : gc[i - 1]} ${ i === 0 ? emptyBackgroundSpacePercentage : ((i / gradientColors.length) * 100) * usedBlockSpace / 100 + emptyBackgroundSpacePercentage }%,
|
|
|
|
#${color} ${Math.round(((i + 1) / gradientColors.length) * 100) * usedBlockSpace / 100 + emptyBackgroundSpacePercentage}%
|
2020-05-24 17:23:56 +02:00
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
return {
|
|
|
|
'right': 40 + index * 155 + 'px',
|
2020-05-24 17:23:56 +02:00
|
|
|
'background': backgroundGradients.join(',') + ')'
|
2020-02-16 16:15:07 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateTransactionPosition() {
|
2020-03-19 20:11:40 +01:00
|
|
|
if ((!this.txFeePerVSize && (this.markIndex === undefined || this.markIndex === -1)) || !this.mempoolBlocks) {
|
2020-02-22 23:23:24 +01:00
|
|
|
this.arrowVisible = false;
|
2020-02-16 16:15:07 +01:00
|
|
|
return;
|
2020-03-19 20:11:40 +01:00
|
|
|
} else if (this.markIndex > -1) {
|
2020-03-24 21:59:30 +01:00
|
|
|
clearTimeout(this.resetTransitionTimeout);
|
2020-03-20 21:38:18 +01:00
|
|
|
this.transition = 'inherit';
|
2020-03-17 15:53:20 +01:00
|
|
|
this.rightPosition = this.markIndex * (this.blockWidth + this.blockPadding) + 0.5 * this.blockWidth;
|
2020-03-19 20:11:40 +01:00
|
|
|
this.arrowVisible = true;
|
2020-06-10 18:52:14 +02:00
|
|
|
this.resetTransitionTimeout = window.setTimeout(() => this.transition = '2s', 100);
|
2020-03-17 15:53:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:11:40 +01:00
|
|
|
this.arrowVisible = true;
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
for (const block of this.mempoolBlocks) {
|
|
|
|
for (let i = 0; i < block.feeRange.length - 1; i++) {
|
|
|
|
if (this.txFeePerVSize < block.feeRange[i + 1] && this.txFeePerVSize >= block.feeRange[i]) {
|
|
|
|
const txInBlockIndex = this.mempoolBlocks.indexOf(block);
|
|
|
|
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.blockVSize > 1000000 ? 1000000 : block.blockVSize) / 1000000;
|
2020-02-22 23:23:24 +01:00
|
|
|
const arrowRightPosition = txInBlockIndex * (this.blockWidth + this.blockPadding)
|
2020-02-16 16:15:07 +01:00
|
|
|
+ ((1 - feePosition) * blockedFilledPercentage * this.blockWidth);
|
|
|
|
|
2020-02-22 23:23:24 +01:00
|
|
|
this.rightPosition = arrowRightPosition;
|
2020-02-16 16:15:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|