mempool/frontend/src/app/components/asset/asset.component.ts

205 lines
7.1 KiB
TypeScript
Raw Normal View History

2020-04-28 12:10:31 +02:00
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { ElectrsApiService } from '../../services/electrs-api.service';
2020-05-02 07:36:35 +02:00
import { switchMap, filter, catchError, take } from 'rxjs/operators';
2020-04-28 12:10:31 +02:00
import { Asset, Transaction } from '../../interfaces/electrs.interface';
import { WebsocketService } from 'src/app/services/websocket.service';
import { StateService } from 'src/app/services/state.service';
import { AudioService } from 'src/app/services/audio.service';
import { ApiService } from 'src/app/services/api.service';
2020-05-02 07:36:35 +02:00
import { of, merge, Subscription, combineLatest } from 'rxjs';
2020-04-28 12:10:31 +02:00
import { SeoService } from 'src/app/services/seo.service';
import { environment } from 'src/environments/environment';
2020-05-02 07:36:35 +02:00
import { AssetsService } from 'src/app/services/assets.service';
import { formatNumber, moveDec } from 'src/app/bitcoin.utils';
2020-04-28 12:10:31 +02:00
@Component({
selector: 'app-asset',
templateUrl: './asset.component.html',
styleUrls: ['./asset.component.scss']
})
export class AssetComponent implements OnInit, OnDestroy {
network = '';
nativeAssetId = environment.nativeAssetId;
2020-04-28 12:10:31 +02:00
asset: Asset;
2020-05-02 07:36:35 +02:00
assetContract: any;
2020-04-28 12:10:31 +02:00
assetString: string;
isLoadingAsset = true;
transactions: Transaction[];
isLoadingTransactions = true;
isNativeAsset = false;
2020-04-28 12:10:31 +02:00
error: any;
mainSubscription: Subscription;
totalConfirmedTxCount = 0;
loadedConfirmedTxCount = 0;
txCount = 0;
receieved = 0;
sent = 0;
private tempTransactions: Transaction[];
private timeTxIndexes: number[];
private lastTransactionTxId: string;
constructor(
private route: ActivatedRoute,
private electrsApiService: ElectrsApiService,
private websocketService: WebsocketService,
private stateService: StateService,
private audioService: AudioService,
private apiService: ApiService,
private seoService: SeoService,
2020-05-02 07:36:35 +02:00
private assetsService: AssetsService,
2020-04-28 12:10:31 +02:00
) { }
ngOnInit() {
this.websocketService.want(['blocks', 'mempool-blocks']);
this.stateService.networkChanged$.subscribe((network) => this.network = network);
2020-04-28 12:10:31 +02:00
this.mainSubscription = this.route.paramMap
.pipe(
switchMap((params: ParamMap) => {
this.error = undefined;
this.isLoadingAsset = true;
this.loadedConfirmedTxCount = 0;
this.asset = null;
2020-05-02 07:36:35 +02:00
this.assetContract = null;
2020-04-28 12:10:31 +02:00
this.isLoadingTransactions = true;
this.transactions = null;
document.body.scrollTo(0, 0);
this.assetString = params.get('id') || '';
2020-08-12 09:04:04 +02:00
this.seoService.setTitle('Asset: ' + this.assetString);
2020-04-28 12:10:31 +02:00
return merge(
of(true),
this.stateService.connectionState$
.pipe(filter((state) => state === 2 && this.transactions && this.transactions.length > 0))
)
.pipe(
2020-05-02 07:36:35 +02:00
switchMap(() => {
return combineLatest([this.electrsApiService.getAsset$(this.assetString)
.pipe(
catchError((err) => {
this.isLoadingAsset = false;
this.error = err;
console.log(err);
return of(null);
})
), this.assetsService.getAssetsMinimalJson$])
2020-04-28 12:10:31 +02:00
.pipe(
2020-05-02 07:36:35 +02:00
take(1)
);
})
2020-04-28 12:10:31 +02:00
);
})
)
.pipe(
2020-05-02 07:36:35 +02:00
switchMap(([asset, assetsData]) => {
2020-04-28 12:10:31 +02:00
this.asset = asset;
2020-05-02 07:36:35 +02:00
this.assetContract = assetsData[this.asset.asset_id];
if (!this.assetContract) {
this.assetContract = [null, '?', 'Unknown', 0];
}
this.isNativeAsset = asset.asset_id === this.nativeAssetId;
2020-04-28 12:10:31 +02:00
this.updateChainStats();
this.websocketService.startTrackAsset(asset.asset_id);
this.isLoadingAsset = false;
this.isLoadingTransactions = true;
return this.electrsApiService.getAssetTransactions$(asset.asset_id);
}),
switchMap((transactions) => {
this.tempTransactions = transactions;
if (transactions.length) {
this.lastTransactionTxId = transactions[transactions.length - 1].txid;
this.loadedConfirmedTxCount += transactions.filter((tx) => tx.status.confirmed).length;
}
const fetchTxs: string[] = [];
this.timeTxIndexes = [];
transactions.forEach((tx, index) => {
if (!tx.status.confirmed) {
fetchTxs.push(tx.txid);
this.timeTxIndexes.push(index);
}
});
if (!fetchTxs.length) {
return of([]);
}
return this.apiService.getTransactionTimes$(fetchTxs);
})
)
.subscribe((times: number[]) => {
times.forEach((time, index) => {
this.tempTransactions[this.timeTxIndexes[index]].firstSeen = time;
});
this.tempTransactions.sort((a, b) => {
return b.status.block_time - a.status.block_time || b.firstSeen - a.firstSeen;
});
this.transactions = this.tempTransactions;
this.isLoadingTransactions = false;
},
(error) => {
console.log(error);
this.error = error;
this.isLoadingAsset = false;
});
2020-05-20 12:00:50 +02:00
this.stateService.mempoolTransactions$
2020-04-28 12:10:31 +02:00
.subscribe((transaction) => {
if (this.transactions.some((t) => t.txid === transaction.txid)) {
return;
}
this.transactions.unshift(transaction);
this.transactions = this.transactions.slice();
this.txCount++;
this.audioService.playSound('chime');
2020-04-28 12:10:31 +02:00
});
this.stateService.blockTransactions$
.subscribe((transaction) => {
const tx = this.transactions.find((t) => t.txid === transaction.txid);
if (tx) {
tx.status = transaction.status;
this.transactions = this.transactions.slice();
this.audioService.playSound('magic');
}
this.totalConfirmedTxCount++;
this.loadedConfirmedTxCount++;
});
}
loadMore() {
if (this.isLoadingTransactions || !this.totalConfirmedTxCount || this.loadedConfirmedTxCount >= this.totalConfirmedTxCount) {
return;
}
this.isLoadingTransactions = true;
2020-05-02 19:56:48 +02:00
this.electrsApiService.getAssetTransactionsFromHash$(this.asset.asset_id, this.lastTransactionTxId)
2020-04-28 12:10:31 +02:00
.subscribe((transactions: Transaction[]) => {
this.lastTransactionTxId = transactions[transactions.length - 1].txid;
this.loadedConfirmedTxCount += transactions.length;
this.transactions = this.transactions.concat(transactions);
this.isLoadingTransactions = false;
});
}
updateChainStats() {
// this.receieved = this.asset.chain_stats.funded_txo_sum + this.asset.mempool_stats.funded_txo_sum;
// this.sent = this.asset.chain_stats.spent_txo_sum + this.asset.mempool_stats.spent_txo_sum;
this.txCount = this.asset.chain_stats.tx_count + this.asset.mempool_stats.tx_count;
2020-05-02 19:56:48 +02:00
this.totalConfirmedTxCount = this.asset.chain_stats.tx_count;
2020-04-28 12:10:31 +02:00
}
formatAmount(value: number, precision = 0): number | string {
return moveDec(value, -precision);
}
2020-04-28 12:10:31 +02:00
ngOnDestroy() {
this.mainSubscription.unsubscribe();
this.websocketService.stopTrackingAsset();
}
}