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';
|
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-23 19:16:50 +07:00
|
|
|
addedTransactions = 0;
|
2019-11-10 16:44:00 +08:00
|
|
|
|
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,
|
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
|
|
|
|
2019-11-13 14:51:44 +08:00
|
|
|
this.route.paramMap.pipe(
|
|
|
|
switchMap((params: ParamMap) => {
|
|
|
|
this.error = undefined;
|
|
|
|
this.isLoadingAddress = true;
|
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') || '';
|
|
|
|
return this.electrsApiService.getAddress$(this.addressString);
|
2019-11-13 14:51:44 +08:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.subscribe((address) => {
|
|
|
|
this.address = address;
|
2020-02-23 19:16:50 +07:00
|
|
|
this.websocketService.startTrackAddress(address.address);
|
2019-11-13 14:51:44 +08:00
|
|
|
this.isLoadingAddress = false;
|
|
|
|
this.getAddressTransactions(address.address);
|
|
|
|
},
|
|
|
|
(error) => {
|
2019-11-14 15:03:01 +08:00
|
|
|
console.log(error);
|
2019-11-13 14:51:44 +08:00
|
|
|
this.error = error;
|
|
|
|
this.isLoadingAddress = false;
|
|
|
|
});
|
2020-02-23 19:16:50 +07:00
|
|
|
|
|
|
|
this.stateService.mempoolTransactions$
|
|
|
|
.subscribe((transaction) => {
|
|
|
|
this.transactions.unshift(transaction);
|
|
|
|
this.addedTransactions++;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.stateService.blockTransactions$
|
|
|
|
.subscribe((transaction) => {
|
|
|
|
const tx = this.transactions.find((t) => t.txid === transaction.txid);
|
|
|
|
if (tx) {
|
|
|
|
tx.status = transaction.status;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.stateService.isOffline$
|
|
|
|
.subscribe((state) => {
|
|
|
|
if (!state && this.transactions && this.transactions.length) {
|
|
|
|
this.isLoadingTransactions = true;
|
|
|
|
this.getAddressTransactions(this.address.address);
|
|
|
|
}
|
|
|
|
});
|
2019-11-10 16:44:00 +08:00
|
|
|
}
|
|
|
|
|
2019-11-13 14:51:44 +08:00
|
|
|
getAddressTransactions(address: string) {
|
2020-02-16 22:15:07 +07:00
|
|
|
this.electrsApiService.getAddressTransactions$(address)
|
2019-11-13 14:51:44 +08:00
|
|
|
.subscribe((transactions: any) => {
|
|
|
|
this.transactions = transactions;
|
|
|
|
this.isLoadingTransactions = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadMore() {
|
|
|
|
this.isLoadingTransactions = true;
|
2020-02-16 22:15:07 +07:00
|
|
|
this.electrsApiService.getAddressTransactionsFromHash$(this.address.address, this.transactions[this.transactions.length - 1].txid)
|
2019-11-13 14:51:44 +08:00
|
|
|
.subscribe((transactions) => {
|
|
|
|
this.transactions = this.transactions.concat(transactions);
|
|
|
|
this.isLoadingTransactions = false;
|
|
|
|
});
|
|
|
|
}
|
2020-02-23 19:16:50 +07:00
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.websocketService.startTrackAddress('stop');
|
|
|
|
}
|
2019-11-10 16:44:00 +08:00
|
|
|
}
|