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';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { ElectrsApiService } from '../../services/electrs-api.service';
|
2019-11-13 14:51:44 +08:00
|
|
|
import { switchMap } from 'rxjs/operators';
|
2020-02-16 22:15:07 +07:00
|
|
|
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';
|
2020-02-26 04:29:57 +07:00
|
|
|
import { AudioService } from 'src/app/services/audio.service';
|
2020-02-28 01:09:07 +07:00
|
|
|
import { ApiService } from 'src/app/services/api.service';
|
2020-02-28 03:51:59 +07:00
|
|
|
import { of } from 'rxjs';
|
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 {
|
2020-02-16 22:15:07 +07:00
|
|
|
address: Address;
|
|
|
|
addressString: string;
|
2019-11-13 14:51:44 +08:00
|
|
|
isLoadingAddress = true;
|
2020-02-16 22:15:07 +07:00
|
|
|
transactions: Transaction[];
|
2019-11-13 14:51:44 +08:00
|
|
|
isLoadingTransactions = true;
|
|
|
|
error: any;
|
2020-02-26 04:29:57 +07:00
|
|
|
|
2020-02-28 03:51:59 +07:00
|
|
|
totalConfirmedTxCount = 0;
|
|
|
|
loadedConfirmedTxCount = 0;
|
2020-02-26 04:29:57 +07:00
|
|
|
txCount = 0;
|
|
|
|
receieved = 0;
|
|
|
|
sent = 0;
|
2019-11-10 16:44:00 +08:00
|
|
|
|
2020-02-28 03:51:59 +07:00
|
|
|
private tempTransactions: Transaction[];
|
|
|
|
private timeTxIndexes: number[];
|
|
|
|
private lastTransactionTxId: string;
|
|
|
|
|
2019-11-13 14:51:44 +08:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
2020-02-16 22:15:07 +07:00
|
|
|
private electrsApiService: ElectrsApiService,
|
2020-02-23 19:16:50 +07:00
|
|
|
private websocketService: WebsocketService,
|
|
|
|
private stateService: StateService,
|
2020-02-26 04:29:57 +07:00
|
|
|
private audioService: AudioService,
|
2020-02-28 01:09:07 +07:00
|
|
|
private apiService: ApiService,
|
2019-11-13 14:51:44 +08:00
|
|
|
) { }
|
2019-11-10 16:44:00 +08:00
|
|
|
|
|
|
|
ngOnInit() {
|
2020-02-24 22:51:27 +07:00
|
|
|
this.websocketService.want(['blocks', 'stats', 'mempool-blocks']);
|
2020-02-23 19:16:50 +07:00
|
|
|
|
2020-02-26 23:21:16 +07:00
|
|
|
this.route.paramMap
|
|
|
|
.subscribe((params: ParamMap) => {
|
2019-11-13 14:51:44 +08:00
|
|
|
this.error = undefined;
|
|
|
|
this.isLoadingAddress = true;
|
2020-02-28 04:16:15 +07:00
|
|
|
this.loadedConfirmedTxCount = 0;
|
2020-03-02 17:29:00 +07:00
|
|
|
this.address = null;
|
2020-02-16 22:15:07 +07:00
|
|
|
this.isLoadingTransactions = true;
|
|
|
|
this.transactions = null;
|
2020-02-24 03:42:29 +07:00
|
|
|
document.body.scrollTo(0, 0);
|
2020-02-16 22:15:07 +07:00
|
|
|
this.addressString = params.get('id') || '';
|
2020-02-26 23:21:16 +07:00
|
|
|
this.loadAddress(this.addressString);
|
|
|
|
});
|
2020-02-23 19:16:50 +07:00
|
|
|
|
|
|
|
this.stateService.mempoolTransactions$
|
|
|
|
.subscribe((transaction) => {
|
2020-02-26 21:11:43 +07:00
|
|
|
if (this.transactions.some((t) => t.txid === transaction.txid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:16:50 +07:00
|
|
|
this.transactions.unshift(transaction);
|
2020-02-26 04:29:57 +07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
2020-02-23 19:16:50 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
this.stateService.blockTransactions$
|
|
|
|
.subscribe((transaction) => {
|
|
|
|
const tx = this.transactions.find((t) => t.txid === transaction.txid);
|
|
|
|
if (tx) {
|
|
|
|
tx.status = transaction.status;
|
2020-02-26 04:29:57 +07:00
|
|
|
this.transactions = this.transactions.slice();
|
|
|
|
this.audioService.playSound('magic');
|
2020-02-23 19:16:50 +07:00
|
|
|
}
|
2020-02-28 03:51:59 +07:00
|
|
|
this.totalConfirmedTxCount++;
|
|
|
|
this.loadedConfirmedTxCount++;
|
2020-02-23 19:16:50 +07:00
|
|
|
});
|
|
|
|
|
2020-03-09 17:53:54 +07:00
|
|
|
this.stateService.connectionState$
|
2020-02-23 19:16:50 +07:00
|
|
|
.subscribe((state) => {
|
2020-03-09 17:53:54 +07:00
|
|
|
if (state === 2 && this.transactions && this.transactions.length) {
|
2020-02-26 23:21:16 +07:00
|
|
|
this.loadAddress(this.addressString);
|
2020-02-23 19:16:50 +07:00
|
|
|
}
|
|
|
|
});
|
2019-11-10 16:44:00 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 23:21:16 +07:00
|
|
|
loadAddress(addressStr?: string) {
|
|
|
|
this.electrsApiService.getAddress$(addressStr)
|
2020-02-28 01:09:07 +07:00
|
|
|
.pipe(
|
|
|
|
switchMap((address) => {
|
|
|
|
this.address = address;
|
|
|
|
this.updateChainStats();
|
|
|
|
this.websocketService.startTrackAddress(address.address);
|
|
|
|
this.isLoadingAddress = false;
|
|
|
|
this.isLoadingTransactions = true;
|
|
|
|
return this.electrsApiService.getAddressTransactions$(address.address);
|
|
|
|
}),
|
|
|
|
switchMap((transactions) => {
|
|
|
|
this.tempTransactions = transactions;
|
2020-02-28 04:16:15 +07:00
|
|
|
this.lastTransactionTxId = transactions[transactions.length - 1].txid;
|
|
|
|
this.loadedConfirmedTxCount += transactions.filter((tx) => tx.status.confirmed).length;
|
2020-02-28 03:51:59 +07:00
|
|
|
|
|
|
|
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([]);
|
|
|
|
}
|
2020-02-28 01:09:07 +07:00
|
|
|
return this.apiService.getTransactionTimes$(fetchTxs);
|
|
|
|
})
|
|
|
|
)
|
2020-02-28 03:51:59 +07:00
|
|
|
.subscribe((times: number[]) => {
|
2020-02-28 01:09:07 +07:00
|
|
|
times.forEach((time, index) => {
|
2020-02-28 03:51:59 +07:00
|
|
|
this.tempTransactions[this.timeTxIndexes[index]].firstSeen = time;
|
2020-02-28 01:09:07 +07:00
|
|
|
});
|
|
|
|
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;
|
2020-02-26 23:21:16 +07:00
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
console.log(error);
|
|
|
|
this.error = error;
|
|
|
|
this.isLoadingAddress = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:51:44 +08:00
|
|
|
loadMore() {
|
2020-03-01 03:32:12 +07:00
|
|
|
if (this.isLoadingTransactions || !this.totalConfirmedTxCount || this.loadedConfirmedTxCount >= this.totalConfirmedTxCount) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-13 14:51:44 +08:00
|
|
|
this.isLoadingTransactions = true;
|
2020-02-28 03:51:59 +07:00
|
|
|
this.electrsApiService.getAddressTransactionsFromHash$(this.address.address, this.lastTransactionTxId)
|
2020-02-28 04:16:15 +07:00
|
|
|
.subscribe((transactions: Transaction[]) => {
|
2020-03-01 18:09:42 +07:00
|
|
|
this.lastTransactionTxId = transactions[transactions.length - 1].txid;
|
2020-02-28 04:16:15 +07:00
|
|
|
this.loadedConfirmedTxCount += transactions.length;
|
|
|
|
this.transactions = this.transactions.concat(transactions);
|
2019-11-13 14:51:44 +08:00
|
|
|
this.isLoadingTransactions = false;
|
|
|
|
});
|
|
|
|
}
|
2020-02-23 19:16:50 +07:00
|
|
|
|
2020-02-28 04:16:15 +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.websocketService.startTrackAddress('stop');
|
|
|
|
}
|
2019-11-10 16:44:00 +08:00
|
|
|
}
|