2020-03-22 17:44:36 +07:00
|
|
|
import { Component, OnInit, OnDestroy } from '@angular/core';
|
2019-11-12 16:39:59 +08:00
|
|
|
import { ActivatedRoute, ParamMap } from '@angular/router';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { ElectrsApiService } from '../../services/electrs-api.service';
|
2020-03-22 23:45:16 +07:00
|
|
|
import { switchMap, tap, debounceTime, catchError } from 'rxjs/operators';
|
2020-03-04 15:10:30 +07:00
|
|
|
import { Block, Transaction, Vout } from '../../interfaces/electrs.interface';
|
2020-03-24 00:52:08 +07:00
|
|
|
import { of } from 'rxjs';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { StateService } from '../../services/state.service';
|
2020-03-24 00:52:08 +07:00
|
|
|
import { SeoService } from 'src/app/services/seo.service';
|
2019-11-06 15:35:02 +08:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-block',
|
|
|
|
templateUrl: './block.component.html',
|
|
|
|
styleUrls: ['./block.component.scss']
|
|
|
|
})
|
2020-03-22 17:44:36 +07:00
|
|
|
export class BlockComponent implements OnInit, OnDestroy {
|
2020-02-16 22:15:07 +07:00
|
|
|
block: Block;
|
|
|
|
blockHeight: number;
|
|
|
|
blockHash: string;
|
2019-11-12 16:39:59 +08:00
|
|
|
isLoadingBlock = true;
|
2020-02-16 22:15:07 +07:00
|
|
|
latestBlock: Block;
|
|
|
|
transactions: Transaction[];
|
2019-11-12 16:39:59 +08:00
|
|
|
isLoadingTransactions = true;
|
2019-11-13 14:51:44 +08:00
|
|
|
error: any;
|
2020-03-04 15:10:30 +07:00
|
|
|
blockSubsidy: number;
|
|
|
|
fees: number;
|
2019-11-06 15:35:02 +08:00
|
|
|
|
2019-11-12 16:39:59 +08:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
2020-02-16 22:15:07 +07:00
|
|
|
private electrsApiService: ElectrsApiService,
|
|
|
|
private stateService: StateService,
|
2020-03-24 00:52:08 +07:00
|
|
|
private seoService: SeoService,
|
2019-11-12 16:39:59 +08:00
|
|
|
) { }
|
2019-11-06 15:35:02 +08:00
|
|
|
|
|
|
|
ngOnInit() {
|
2020-03-22 17:44:36 +07:00
|
|
|
this.route.paramMap
|
|
|
|
.pipe(
|
2019-11-12 16:39:59 +08:00
|
|
|
switchMap((params: ParamMap) => {
|
|
|
|
const blockHash: string = params.get('id') || '';
|
2020-02-16 22:15:07 +07:00
|
|
|
this.error = undefined;
|
2020-03-04 15:10:30 +07:00
|
|
|
this.fees = undefined;
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
if (history.state.data && history.state.data.blockHeight) {
|
|
|
|
this.blockHeight = history.state.data.blockHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.blockHash = blockHash;
|
2020-02-24 03:42:29 +07:00
|
|
|
document.body.scrollTo(0, 0);
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
if (history.state.data && history.state.data.block) {
|
2020-02-17 20:39:20 +07:00
|
|
|
this.blockHeight = history.state.data.block.height;
|
2020-02-16 22:15:07 +07:00
|
|
|
return of(history.state.data.block);
|
|
|
|
} else {
|
|
|
|
this.isLoadingBlock = true;
|
|
|
|
return this.electrsApiService.getBlock$(blockHash);
|
|
|
|
}
|
2020-03-22 17:44:36 +07:00
|
|
|
}),
|
|
|
|
tap((block: Block) => {
|
|
|
|
this.block = block;
|
|
|
|
this.blockHeight = block.height;
|
2020-03-24 00:52:08 +07:00
|
|
|
this.seoService.setTitle('Block: #' + block.height + ': ' + block.id);
|
2020-03-22 17:44:36 +07:00
|
|
|
this.isLoadingBlock = false;
|
|
|
|
this.setBlockSubsidy();
|
|
|
|
if (block.reward) {
|
|
|
|
this.fees = block.reward / 100000000 - this.blockSubsidy;
|
|
|
|
}
|
|
|
|
this.stateService.markBlock$.next({ blockHeight: this.blockHeight });
|
|
|
|
this.isLoadingTransactions = true;
|
|
|
|
this.transactions = null;
|
|
|
|
}),
|
2020-03-22 23:45:16 +07:00
|
|
|
debounceTime(300),
|
|
|
|
switchMap((block) => this.electrsApiService.getBlockTransactions$(block.id)
|
|
|
|
.pipe(
|
|
|
|
catchError((err) => {
|
|
|
|
console.log(err);
|
|
|
|
return of([]);
|
|
|
|
}))
|
|
|
|
),
|
2019-11-12 16:39:59 +08:00
|
|
|
)
|
2020-03-22 17:44:36 +07:00
|
|
|
.subscribe((transactions: Transaction[]) => {
|
2020-03-25 21:29:40 +07:00
|
|
|
if (this.fees === undefined && transactions[0]) {
|
2020-03-22 17:44:36 +07:00
|
|
|
this.fees = transactions[0].vout.reduce((acc: number, curr: Vout) => acc + curr.value, 0) / 100000000 - this.blockSubsidy;
|
2020-03-04 15:10:30 +07:00
|
|
|
}
|
2020-03-22 17:44:36 +07:00
|
|
|
this.transactions = transactions;
|
|
|
|
this.isLoadingTransactions = false;
|
2019-11-13 14:51:44 +08:00
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.error = error;
|
|
|
|
this.isLoadingBlock = false;
|
2019-11-12 16:39:59 +08:00
|
|
|
});
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
this.stateService.blocks$
|
|
|
|
.subscribe((block) => this.latestBlock = block);
|
2020-02-26 04:29:57 +07:00
|
|
|
}
|
|
|
|
|
2020-03-22 17:44:36 +07:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.stateService.markBlock$.next({});
|
|
|
|
}
|
|
|
|
|
2020-02-26 04:29:57 +07:00
|
|
|
setBlockSubsidy() {
|
2020-03-04 15:10:30 +07:00
|
|
|
this.blockSubsidy = 50;
|
2020-02-24 22:51:27 +07:00
|
|
|
let halvenings = Math.floor(this.block.height / 210000);
|
|
|
|
while (halvenings > 0) {
|
|
|
|
this.blockSubsidy = this.blockSubsidy / 2;
|
|
|
|
halvenings--;
|
|
|
|
}
|
2019-11-12 16:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
loadMore() {
|
2020-03-01 03:32:12 +07:00
|
|
|
if (this.isLoadingTransactions || !this.transactions.length || this.transactions.length === this.block.tx_count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:39:59 +08:00
|
|
|
this.isLoadingTransactions = true;
|
2020-02-16 22:15:07 +07:00
|
|
|
this.electrsApiService.getBlockTransactions$(this.block.id, this.transactions.length)
|
2019-11-12 16:39:59 +08:00
|
|
|
.subscribe((transactions) => {
|
|
|
|
this.transactions = this.transactions.concat(transactions);
|
|
|
|
this.isLoadingTransactions = false;
|
|
|
|
});
|
2019-11-06 15:35:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|