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

411 lines
14 KiB
TypeScript
Raw Normal View History

2024-06-10 23:04:37 +00:00
import { Component, OnInit, OnDestroy, HostListener } 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';
2024-06-10 23:04:37 +00:00
import { Address, ChainStats, Transaction, Vin, Vout } from '../../interfaces/electrs.interface';
2022-09-21 17:23:45 +02:00
import { WebsocketService } from '../../services/websocket.service';
import { StateService } from '../../services/state.service';
import { AudioService } from '../../services/audio.service';
import { ApiService } from '../../services/api.service';
import { of, merge, Subscription, Observable } from 'rxjs';
2022-09-21 17:23:45 +02:00
import { SeoService } from '../../services/seo.service';
import { seoDescriptionNetwork } from '../../shared/common.utils';
2022-09-21 17:23:45 +02:00
import { AddressInformation } from '../../interfaces/node-api.interface';
2019-11-10 16:44:00 +08:00
class AddressStats implements ChainStats {
address: string;
scriptpubkey?: string;
funded_txo_count: number;
funded_txo_sum: number;
spent_txo_count: number;
spent_txo_sum: number;
tx_count: number;
constructor (stats: ChainStats, address: string, scriptpubkey?: string) {
Object.assign(this, stats);
this.address = address;
this.scriptpubkey = scriptpubkey;
}
public addTx(tx: Transaction): void {
for (const vin of tx.vin) {
if (vin.prevout?.scriptpubkey_address === this.address || (this.scriptpubkey === vin.prevout?.scriptpubkey)) {
this.spendTxo(vin.prevout.value);
}
}
for (const vout of tx.vout) {
if (vout.scriptpubkey_address === this.address || (this.scriptpubkey === vout.scriptpubkey)) {
this.fundTxo(vout.value);
}
}
this.tx_count++;
}
public removeTx(tx: Transaction): void {
for (const vin of tx.vin) {
if (vin.prevout?.scriptpubkey_address === this.address || (this.scriptpubkey === vin.prevout?.scriptpubkey)) {
this.unspendTxo(vin.prevout.value);
}
}
for (const vout of tx.vout) {
if (vout.scriptpubkey_address === this.address || (this.scriptpubkey === vout.scriptpubkey)) {
this.unfundTxo(vout.value);
}
}
this.tx_count--;
}
private fundTxo(value: number): void {
this.funded_txo_sum += value;
this.funded_txo_count++;
}
private unfundTxo(value: number): void {
this.funded_txo_sum -= value;
this.funded_txo_count--;
}
private spendTxo(value: number): void {
this.spent_txo_sum += value;
this.spent_txo_count++;
}
private unspendTxo(value: number): void {
this.spent_txo_sum -= value;
this.spent_txo_count--;
}
get balance(): number {
return this.funded_txo_sum - this.spent_txo_sum;
}
2024-06-10 23:04:37 +00:00
get volume(): number {
return this.funded_txo_sum + this.spent_txo_sum;
}
get utxos(): number {
return this.funded_txo_count - this.spent_txo_count;
}
}
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
2024-06-10 23:04:37 +00:00
isMobile: boolean;
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;
2021-10-12 10:54:14 -03:00
retryLoadMore = false;
2019-11-13 14:51:44 +08:00
error: any;
mainSubscription: Subscription;
mempoolTxSubscription: Subscription;
mempoolRemovedTxSubscription: Subscription;
blockTxSubscription: Subscription;
addressLoadingStatus$: Observable<number>;
addressInfo: null | AddressInformation = null;
2024-02-06 21:39:29 +00:00
fullyLoaded = false;
chainStats: AddressStats;
mempoolStats: AddressStats;
2024-06-10 23:04:37 +00:00
exampleChannel?: any;
exampleVin?: Vin;
exampleVout?: Vout;
now = Date.now() / 1000;
balancePeriod: 'all' | '1m' = 'all';
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,
public 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(): void {
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;
2024-02-06 21:39:29 +00:00
this.fullyLoaded = false;
2020-03-24 21:38:11 +07:00
this.address = null;
this.isLoadingTransactions = true;
this.transactions = null;
this.addressInfo = null;
2024-06-10 23:04:37 +00:00
this.exampleChannel = null;
this.exampleVin = null;
this.exampleVout = 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}|04[a-fA-F0-9]{128}|(02|03)[a-fA-F0-9]{64}$/.test(this.addressString)) {
this.addressString = this.addressString.toLowerCase();
}
this.seoService.setTitle($localize`:@@address.component.browser-title:Address: ${this.addressString}:INTERPOLATION:`);
this.seoService.setDescription($localize`:@@meta.description.bitcoin.address:See mempool transactions, confirmed transactions, balance, and more for ${this.stateService.network==='liquid'||this.stateService.network==='liquidtestnet'?'Liquid':'Bitcoin'}${seoDescriptionNetwork(this.stateService.network)} 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(
2023-07-22 17:51:45 +09:00
switchMap(() => (
this.addressString.match(/04[a-fA-F0-9]{128}|(02|03)[a-fA-F0-9]{64}/)
2023-07-22 17:51:45 +09:00
? this.electrsApiService.getPubKeyAddress$(this.addressString)
: this.electrsApiService.getAddress$(this.addressString)
).pipe(
2020-03-30 14:54:48 +07:00
catchError((err) => {
this.isLoadingAddress = false;
this.error = err;
this.seoService.logSoft404();
2020-03-30 14:54:48 +07:00
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' || this.stateService.network === 'liquidtestnet') && /^([a-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;
2023-07-22 17:51:45 +09:00
return address.is_pubkey
? this.electrsApiService.getScriptHashTransactions$((address.address.length === 66 ? '21' : '41') + address.address + 'ac')
2023-07-22 17:51:45 +09:00
: 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;
}
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).pipe(
catchError((err) => {
this.isLoadingAddress = false;
this.isLoadingTransactions = false;
this.error = err;
this.seoService.logSoft404();
console.log(err);
return of([]);
})
);
})
)
.subscribe((times: number[] | null) => {
if (!times) {
return;
}
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;
if (this.transactions.length === (this.mempoolStats.tx_count + this.chainStats.tx_count)) {
2024-05-13 17:16:28 +07:00
this.fullyLoaded = true;
}
this.isLoadingTransactions = false;
2024-06-10 23:04:37 +00:00
for (const tx of this.transactions) {
if (!this.exampleVin) {
this.exampleVin = tx.vin.find(v => v.prevout?.scriptpubkey_address === this.address.address);
}
if (!this.exampleVout) {
this.exampleVout = tx.vout.find(v => v.scriptpubkey_address === this.address.address);
}
if (this.exampleVin && this.exampleVout) {
break;
}
}
if (!this.showBalancePeriod()) {
this.setBalancePeriod('all');
} else {
this.setBalancePeriod('1m');
}
},
(error) => {
console.log(error);
this.error = error;
this.seoService.logSoft404();
this.isLoadingAddress = false;
});
2020-03-24 21:38:11 +07:00
this.mempoolTxSubscription = this.stateService.mempoolTransactions$
.subscribe(tx => {
this.addTransaction(tx);
this.mempoolStats.addTx(tx);
2020-03-24 21:38:11 +07:00
});
this.mempoolRemovedTxSubscription = this.stateService.mempoolRemovedTransactions$
.subscribe(tx => {
this.removeTransaction(tx);
this.mempoolStats.removeTx(tx);
});
this.blockTxSubscription = this.stateService.blockTransactions$
2020-03-24 21:38:11 +07:00
.subscribe((transaction) => {
const tx = this.transactions.find((t) => t.txid === transaction.txid);
if (tx) {
tx.status = transaction.status;
this.transactions = this.transactions.slice();
this.mempoolStats.removeTx(transaction);
2020-03-24 21:38:11 +07:00
this.audioService.playSound('magic');
} else {
if (this.addTransaction(transaction, false)) {
this.audioService.playSound('magic');
}
2020-03-24 21:38:11 +07:00
}
this.chainStats.addTx(transaction);
2020-03-24 21:38:11 +07:00
});
}
addTransaction(transaction: Transaction, playSound: boolean = true): boolean {
if (this.transactions.some((t) => t.txid === transaction.txid)) {
return false;
}
this.transactions.unshift(transaction);
this.transactions = this.transactions.slice();
if (playSound) {
if (transaction.vout.some((vout) => vout?.scriptpubkey_address === this.address.address)) {
this.audioService.playSound('cha-ching');
} else {
this.audioService.playSound('chime');
}
}
return true;
}
removeTransaction(transaction: Transaction): boolean {
const index = this.transactions.findIndex(((tx) => tx.txid === transaction.txid));
if (index === -1) {
return false;
}
this.transactions.splice(index, 1);
this.transactions = this.transactions.slice();
return true;
}
loadMore(): void {
2024-02-06 21:39:29 +00:00
if (this.isLoadingTransactions || this.fullyLoaded) {
return;
}
2019-11-13 14:51:44 +08:00
this.isLoadingTransactions = true;
2021-10-12 10:54:14 -03:00
this.retryLoadMore = false;
(this.address.is_pubkey
? this.electrsApiService.getScriptHashTransactions$((this.address.address.length === 66 ? '21' : '41') + this.address.address + 'ac', this.lastTransactionTxId)
: this.electrsApiService.getAddressTransactions$(this.address.address, this.lastTransactionTxId))
.subscribe((transactions: Transaction[]) => {
2024-02-06 21:39:29 +00:00
if (transactions && transactions.length) {
this.lastTransactionTxId = transactions[transactions.length - 1].txid;
this.transactions = this.transactions.concat(transactions);
} else {
2024-02-06 21:39:29 +00:00
this.fullyLoaded = true;
}
2019-11-13 14:51:44 +08:00
this.isLoadingTransactions = false;
},
(error) => {
this.isLoadingTransactions = false;
2021-10-12 10:54:14 -03:00
this.retryLoadMore = true;
2023-07-13 16:57:36 +09:00
// In the unlikely event of the txid wasn't found in the mempool anymore and we must reload the page.
if (error.status === 422) {
window.location.reload();
}
2019-11-13 14:51:44 +08:00
});
}
2020-02-23 19:16:50 +07:00
updateChainStats(): void {
this.chainStats = new AddressStats(this.address.chain_stats, this.address.address);
this.mempoolStats = new AddressStats(this.address.mempool_stats, this.address.address);
}
setBalancePeriod(period: 'all' | '1m'): boolean {
this.balancePeriod = period;
return false;
}
showBalancePeriod(): boolean {
return this.transactions?.length && (
!this.transactions[0].status?.confirmed
|| this.transactions[0].status.block_time > (this.now - (60 * 60 * 24 * 30))
);
}
2024-06-10 23:04:37 +00:00
@HostListener('window:resize', ['$event'])
onResize(): void {
this.isMobile = window.innerWidth < 768;
}
ngOnDestroy(): void {
this.mainSubscription.unsubscribe();
this.mempoolTxSubscription.unsubscribe();
this.mempoolRemovedTxSubscription.unsubscribe();
this.blockTxSubscription.unsubscribe();
this.websocketService.stopTrackingAddress();
2020-02-23 19:16:50 +07:00
}
2019-11-10 16:44:00 +08:00
}