2020-02-19 23:50:23 +07:00
|
|
|
import { Component, OnInit, OnDestroy } from '@angular/core';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { ElectrsApiService } from '../../services/electrs-api.service';
|
|
|
|
import { ActivatedRoute, ParamMap } from '@angular/router';
|
|
|
|
import { switchMap } from 'rxjs/operators';
|
|
|
|
import { Transaction, Block } from '../../interfaces/electrs.interface';
|
|
|
|
import { of } from 'rxjs';
|
|
|
|
import { StateService } from '../../services/state.service';
|
|
|
|
import { WebsocketService } from '../../services/websocket.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-16 22:15:07 +07:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-transaction',
|
|
|
|
templateUrl: './transaction.component.html',
|
|
|
|
styleUrls: ['./transaction.component.scss']
|
|
|
|
})
|
2020-02-19 23:50:23 +07:00
|
|
|
export class TransactionComponent implements OnInit, OnDestroy {
|
2020-02-16 22:15:07 +07:00
|
|
|
tx: Transaction;
|
|
|
|
txId: string;
|
|
|
|
isLoadingTx = true;
|
|
|
|
error: any = undefined;
|
|
|
|
latestBlock: Block;
|
2020-02-28 01:09:07 +07:00
|
|
|
transactionTime = -1;
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
rightPosition = 0;
|
|
|
|
blockDepth = 0;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private electrsApiService: ElectrsApiService,
|
|
|
|
private stateService: StateService,
|
|
|
|
private websocketService: WebsocketService,
|
2020-02-26 04:29:57 +07:00
|
|
|
private audioService: AudioService,
|
2020-02-28 01:09:07 +07:00
|
|
|
private apiService: ApiService,
|
2020-02-16 22:15:07 +07:00
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-02-24 22:51:27 +07:00
|
|
|
this.websocketService.want(['blocks', 'stats', 'mempool-blocks']);
|
2020-02-17 20:39:20 +07:00
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
this.route.paramMap.pipe(
|
|
|
|
switchMap((params: ParamMap) => {
|
|
|
|
this.txId = params.get('id') || '';
|
|
|
|
this.error = undefined;
|
|
|
|
this.isLoadingTx = true;
|
2020-03-10 15:33:58 +07:00
|
|
|
this.transactionTime = -1;
|
2020-02-24 03:42:29 +07:00
|
|
|
document.body.scrollTo(0, 0);
|
2020-02-16 22:15:07 +07:00
|
|
|
if (history.state.data) {
|
|
|
|
return of(history.state.data);
|
|
|
|
} else {
|
|
|
|
return this.electrsApiService.getTransaction$(this.txId);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.subscribe((tx: Transaction) => {
|
|
|
|
this.tx = tx;
|
|
|
|
this.isLoadingTx = false;
|
|
|
|
|
|
|
|
if (!tx.status.confirmed) {
|
2020-02-19 23:50:23 +07:00
|
|
|
this.websocketService.startTrackTransaction(tx.txid);
|
2020-03-10 15:33:58 +07:00
|
|
|
this.getTransactionTime();
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.error = error;
|
|
|
|
this.isLoadingTx = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.stateService.blocks$
|
|
|
|
.subscribe((block) => this.latestBlock = block);
|
|
|
|
|
2020-02-23 19:16:50 +07:00
|
|
|
this.stateService.txConfirmed$
|
2020-02-16 22:15:07 +07:00
|
|
|
.subscribe((block) => {
|
|
|
|
this.tx.status = {
|
|
|
|
confirmed: true,
|
|
|
|
block_height: block.height,
|
|
|
|
block_hash: block.id,
|
|
|
|
block_time: block.timestamp,
|
|
|
|
};
|
2020-02-26 04:29:57 +07:00
|
|
|
this.audioService.playSound('magic');
|
2020-02-16 22:15:07 +07:00
|
|
|
});
|
|
|
|
}
|
2020-02-19 23:50:23 +07:00
|
|
|
|
2020-02-28 01:09:07 +07:00
|
|
|
getTransactionTime() {
|
|
|
|
this.apiService.getTransactionTimes$([this.tx.txid])
|
|
|
|
.subscribe((transactionTimes) => {
|
|
|
|
this.transactionTime = transactionTimes[0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:50:23 +07:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.websocketService.startTrackTransaction('stop');
|
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|