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

235 lines
8.5 KiB
TypeScript
Raw Normal View History

2020-02-23 19:16:50 +07:00
import { Component, OnInit, OnDestroy } from '@angular/core';
2019-11-13 14:51:44 +08:00
import { ActivatedRoute, ParamMap } from '@angular/router';
import { ElectrsApiService } from '../../services/electrs-api.service';
import { switchMap, filter, catchError, map, tap } from 'rxjs/operators';
import { Address, Transaction } from '../../interfaces/electrs.interface';
2020-02-23 19:16:50 +07:00
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';
import { of, merge, Subscription, Observable } from 'rxjs';
2020-03-24 00:52:08 +07:00
import { SeoService } from 'src/app/services/seo.service';
import { AddressInformation } from 'src/app/interfaces/node-api.interface';
2019-11-10 16:44:00 +08:00
@Component({
selector: 'app-address',
templateUrl: './address.component.html',
styleUrls: ['./address.component.scss']
})
2020-02-23 19:16:50 +07:00
export class AddressComponent implements OnInit, OnDestroy {
network = '';
2020-03-25 21:55:18 +07:00
address: Address;
addressString: string;
2019-11-13 14:51:44 +08:00
isLoadingAddress = true;
transactions: Transaction[];
2019-11-13 14:51:44 +08:00
isLoadingTransactions = true;
retryLoadmore = false;
2019-11-13 14:51:44 +08:00
error: any;
mainSubscription: Subscription;
addressLoadingStatus$: Observable<number>;
addressInfo: null | AddressInformation = null;
totalConfirmedTxCount = 0;
loadedConfirmedTxCount = 0;
txCount = 0;
receieved = 0;
sent = 0;
2019-11-10 16:44:00 +08:00
private tempTransactions: Transaction[];
private timeTxIndexes: number[];
private lastTransactionTxId: string;
2019-11-13 14:51:44 +08:00
constructor(
private route: ActivatedRoute,
private electrsApiService: ElectrsApiService,
2020-02-23 19:16:50 +07:00
private websocketService: WebsocketService,
private stateService: StateService,
private audioService: AudioService,
private apiService: ApiService,
2020-03-24 00:52:08 +07:00
private seoService: SeoService,
2019-11-13 14:51:44 +08:00
) { }
2019-11-10 16:44:00 +08:00
ngOnInit() {
this.stateService.networkChanged$.subscribe((network) => this.network = network);
this.websocketService.want(['blocks']);
this.addressLoadingStatus$ = this.route.paramMap
.pipe(
switchMap(() => this.stateService.loadingIndicators$),
map((indicators) => indicators['address-' + this.addressString] !== undefined ? indicators['address-' + this.addressString] : 0)
);
2020-02-23 19:16:50 +07:00
this.mainSubscription = this.route.paramMap
2020-03-24 21:38:11 +07:00
.pipe(
switchMap((params: ParamMap) => {
this.error = undefined;
this.isLoadingAddress = true;
this.loadedConfirmedTxCount = 0;
this.address = null;
this.isLoadingTransactions = true;
this.transactions = null;
this.addressInfo = null;
2020-03-24 21:38:11 +07:00
document.body.scrollTo(0, 0);
this.addressString = params.get('id') || '';
if (/^[A-Z]{2,5}1[AC-HJ-NP-Z02-9]{8,100}$/.test(this.addressString)) {
this.addressString = this.addressString.toLowerCase();
}
this.seoService.setTitle($localize`:@@address.component.browser-title:Address: ${this.addressString}:INTERPOLATION:`);
2020-03-24 21:38:11 +07:00
return merge(
of(true),
this.stateService.connectionState$
.pipe(filter((state) => state === 2 && this.transactions && this.transactions.length > 0))
)
.pipe(
2020-03-30 14:54:48 +07:00
switchMap(() => this.electrsApiService.getAddress$(this.addressString)
.pipe(
catchError((err) => {
this.isLoadingAddress = false;
this.error = err;
console.log(err);
return of(null);
})
)
)
2020-03-24 21:38:11 +07:00
);
})
)
.pipe(
2020-03-30 14:54:48 +07:00
filter((address) => !!address),
tap((address: Address) => {
if (this.stateService.network === 'liquid' && /^([m-zA-HJ-NP-Z1-9]{26,35}|[a-z]{2,5}1[ac-hj-np-z02-9]{8,100}|[a-km-zA-HJ-NP-Z1-9]{80})$/.test(address.address)) {
this.apiService.validateAddress$(address.address)
.subscribe((addressInfo) => {
this.addressInfo = addressInfo;
this.websocketService.startTrackAddress(addressInfo.unconfidential);
});
} else {
this.websocketService.startTrackAddress(address.address);
}
}),
switchMap((address) => {
this.address = address;
this.updateChainStats();
this.isLoadingAddress = false;
this.isLoadingTransactions = true;
return this.electrsApiService.getAddressTransactions$(address.address);
}),
switchMap((transactions) => {
this.tempTransactions = transactions;
2020-03-30 14:54:48 +07:00
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) => {
if (b.status.confirmed) {
if (b.status.block_height === a.status.block_height) {
return b.status.block_time - a.status.block_time;
}
return b.status.block_height - a.status.block_height;
}
return b.firstSeen - a.firstSeen;
});
this.transactions = this.tempTransactions;
this.isLoadingTransactions = false;
},
(error) => {
console.log(error);
this.error = error;
this.isLoadingAddress = false;
});
2020-03-24 21:38:11 +07:00
this.stateService.mempoolTransactions$
.subscribe((transaction) => {
if (this.transactions.some((t) => t.txid === transaction.txid)) {
return;
}
this.transactions.unshift(transaction);
this.transactions = this.transactions.slice();
this.txCount++;
if (transaction.vout.some((vout) => vout.scriptpubkey_address === this.address.address)) {
this.audioService.playSound('cha-ching');
} else {
this.audioService.playSound('chime');
}
transaction.vin.forEach((vin) => {
if (vin.prevout.scriptpubkey_address === this.address.address) {
this.sent += vin.prevout.value;
}
});
transaction.vout.forEach((vout) => {
if (vout.scriptpubkey_address === this.address.address) {
this.receieved += vout.value;
}
});
});
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++;
});
}
2019-11-13 14:51:44 +08:00
loadMore() {
if (this.isLoadingTransactions || !this.totalConfirmedTxCount || this.loadedConfirmedTxCount >= this.totalConfirmedTxCount) {
return;
}
2019-11-13 14:51:44 +08:00
this.isLoadingTransactions = true;
this.retryLoadmore = false;
this.electrsApiService.getAddressTransactionsFromHash$(this.address.address, this.lastTransactionTxId)
.subscribe((transactions: Transaction[]) => {
2020-03-01 18:09:42 +07:00
this.lastTransactionTxId = transactions[transactions.length - 1].txid;
this.loadedConfirmedTxCount += transactions.length;
this.transactions = this.transactions.concat(transactions);
2019-11-13 14:51:44 +08:00
this.isLoadingTransactions = false;
},
(error) => {
this.isLoadingTransactions = false;
this.retryLoadmore = true;
2019-11-13 14:51:44 +08:00
});
}
2020-02-23 19:16:50 +07:00
updateChainStats() {
this.receieved = this.address.chain_stats.funded_txo_sum + this.address.mempool_stats.funded_txo_sum;
this.sent = this.address.chain_stats.spent_txo_sum + this.address.mempool_stats.spent_txo_sum;
this.txCount = this.address.chain_stats.tx_count + this.address.mempool_stats.tx_count;
this.totalConfirmedTxCount = this.address.chain_stats.tx_count;
}
2020-02-23 19:16:50 +07:00
ngOnDestroy() {
this.mainSubscription.unsubscribe();
this.websocketService.stopTrackingAddress();
2020-02-23 19:16:50 +07:00
}
2019-11-10 16:44:00 +08:00
}