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';
|
2020-04-13 02:06:10 +07:00
|
|
|
import { switchMap, filter, take, catchError, flatMap } from 'rxjs/operators';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { Transaction, Block } from '../../interfaces/electrs.interface';
|
2020-04-13 02:06:10 +07:00
|
|
|
import { of, merge, Subscription, Observable } from 'rxjs';
|
2020-02-16 22:15:07 +07:00
|
|
|
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-03-24 00:52:08 +07:00
|
|
|
import { SeoService } from 'src/app/services/seo.service';
|
2020-05-28 18:39:45 +07:00
|
|
|
import { calcSegwitFeeGains } from 'src/app/bitcoin.utils';
|
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-05-09 20:37:50 +07:00
|
|
|
network = '';
|
2020-02-16 22:15:07 +07:00
|
|
|
tx: Transaction;
|
|
|
|
txId: string;
|
2020-03-16 02:01:03 +07:00
|
|
|
feeRating: number;
|
|
|
|
overpaidTimes: number;
|
2020-03-16 02:41:13 +07:00
|
|
|
medianFeeNeeded: number;
|
2020-03-23 04:07:31 +07:00
|
|
|
txInBlockIndex: number;
|
2020-02-16 22:15:07 +07:00
|
|
|
isLoadingTx = true;
|
|
|
|
error: any = undefined;
|
2020-04-13 01:26:53 +07:00
|
|
|
waitingForTransaction = false;
|
2020-02-16 22:15:07 +07:00
|
|
|
latestBlock: Block;
|
2020-02-28 01:09:07 +07:00
|
|
|
transactionTime = -1;
|
2020-04-12 03:03:51 +07:00
|
|
|
subscription: Subscription;
|
2020-05-28 18:39:45 +07:00
|
|
|
segwitGains = {
|
|
|
|
realizedGains: 0,
|
|
|
|
potentialBech32Gains: 0,
|
|
|
|
potentialP2shGains: 0,
|
|
|
|
};
|
|
|
|
isRbfTransaction: boolean;
|
2020-06-08 18:55:53 +07:00
|
|
|
rbfTransaction: undefined | Transaction;
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
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-03-24 00:52:08 +07:00
|
|
|
private seoService: SeoService,
|
2020-02-16 22:15:07 +07:00
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-05-09 20:37:50 +07:00
|
|
|
this.stateService.networkChanged$.subscribe((network) => this.network = network);
|
|
|
|
|
2020-04-12 03:03:51 +07:00
|
|
|
this.subscription = this.route.paramMap.pipe(
|
2020-02-16 22:15:07 +07:00
|
|
|
switchMap((params: ParamMap) => {
|
|
|
|
this.txId = params.get('id') || '';
|
2020-04-04 21:47:05 +07:00
|
|
|
this.seoService.setTitle('Transaction: ' + this.txId, true);
|
2020-04-13 01:26:53 +07:00
|
|
|
this.resetTransaction();
|
2020-03-24 21:01:29 +07:00
|
|
|
return merge(
|
|
|
|
of(true),
|
2020-04-13 01:26:53 +07:00
|
|
|
this.stateService.connectionState$.pipe(
|
|
|
|
filter((state) => state === 2 && this.tx && !this.tx.status.confirmed)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}),
|
2020-04-13 02:43:43 +07:00
|
|
|
switchMap(() => {
|
2020-04-13 01:26:53 +07:00
|
|
|
let transactionObservable$: Observable<Transaction>;
|
|
|
|
if (history.state.data) {
|
|
|
|
transactionObservable$ = of(history.state.data);
|
|
|
|
} else {
|
|
|
|
transactionObservable$ = this.electrsApiService.getTransaction$(this.txId).pipe(
|
|
|
|
catchError(this.handleLoadElectrsTransactionError.bind(this))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return merge(
|
|
|
|
transactionObservable$,
|
|
|
|
this.stateService.mempoolTransactions$
|
2020-03-24 21:01:29 +07:00
|
|
|
);
|
2020-02-16 22:15:07 +07:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.subscribe((tx: Transaction) => {
|
2020-04-13 01:26:53 +07:00
|
|
|
if (!tx) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
this.tx = tx;
|
|
|
|
this.isLoadingTx = false;
|
2020-04-13 01:26:53 +07:00
|
|
|
this.error = undefined;
|
|
|
|
this.waitingForTransaction = false;
|
2020-03-23 04:07:31 +07:00
|
|
|
this.setMempoolBlocksSubscription();
|
2020-05-28 18:39:45 +07:00
|
|
|
this.segwitGains = calcSegwitFeeGains(tx);
|
|
|
|
this.isRbfTransaction = tx.vin.some((v) => v.sequence < 0xfffffffe);
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
if (!tx.status.confirmed) {
|
2020-02-19 23:50:23 +07:00
|
|
|
this.websocketService.startTrackTransaction(tx.txid);
|
2020-04-13 01:26:53 +07:00
|
|
|
|
|
|
|
if (tx.firstSeen) {
|
|
|
|
this.transactionTime = tx.firstSeen;
|
|
|
|
} else {
|
|
|
|
this.getTransactionTime();
|
|
|
|
}
|
2020-03-16 02:01:03 +07:00
|
|
|
} else {
|
|
|
|
this.findBlockAndSetFeeRating();
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
2020-03-22 17:44:36 +07:00
|
|
|
if (this.tx.status.confirmed) {
|
|
|
|
this.stateService.markBlock$.next({ blockHeight: tx.status.block_height });
|
|
|
|
} else {
|
|
|
|
this.stateService.markBlock$.next({ txFeePerVSize: tx.fee / (tx.weight / 4) });
|
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.error = error;
|
|
|
|
this.isLoadingTx = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.stateService.blocks$
|
2020-06-10 23:52:14 +07:00
|
|
|
.subscribe(([block, txConfirmed]) => {
|
|
|
|
this.latestBlock = block;
|
2020-02-16 22:15:07 +07:00
|
|
|
|
2020-06-10 23:52:14 +07:00
|
|
|
if (txConfirmed) {
|
|
|
|
this.tx.status = {
|
|
|
|
confirmed: true,
|
|
|
|
block_height: block.height,
|
|
|
|
block_hash: block.id,
|
|
|
|
block_time: block.timestamp,
|
|
|
|
};
|
|
|
|
this.stateService.markBlock$.next({ blockHeight: block.height });
|
|
|
|
this.audioService.playSound('magic');
|
|
|
|
this.findBlockAndSetFeeRating();
|
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
});
|
2020-06-08 18:55:53 +07:00
|
|
|
|
|
|
|
this.stateService.txReplaced$
|
|
|
|
.subscribe((rbfTransaction) => this.rbfTransaction = rbfTransaction);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
2020-02-19 23:50:23 +07:00
|
|
|
|
2020-04-13 01:26:53 +07:00
|
|
|
handleLoadElectrsTransactionError(error: any): Observable<any> {
|
|
|
|
if (error.status === 404 && /^[a-fA-F0-9]{64}$/.test(this.txId)) {
|
2020-04-13 02:06:10 +07:00
|
|
|
this.websocketService.startMultiTrackTransaction(this.txId);
|
2020-04-13 01:26:53 +07:00
|
|
|
this.waitingForTransaction = true;
|
|
|
|
}
|
|
|
|
this.error = error;
|
|
|
|
this.isLoadingTx = false;
|
|
|
|
return of(false);
|
|
|
|
}
|
|
|
|
|
2020-03-23 04:07:31 +07:00
|
|
|
setMempoolBlocksSubscription() {
|
|
|
|
this.stateService.mempoolBlocks$
|
|
|
|
.subscribe((mempoolBlocks) => {
|
|
|
|
if (!this.tx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const txFeePerVSize = this.tx.fee / (this.tx.weight / 4);
|
|
|
|
|
|
|
|
for (const block of mempoolBlocks) {
|
|
|
|
for (let i = 0; i < block.feeRange.length - 1; i++) {
|
2020-03-27 17:56:42 +07:00
|
|
|
if (txFeePerVSize <= block.feeRange[i + 1] && txFeePerVSize >= block.feeRange[i]) {
|
2020-03-23 04:07:31 +07:00
|
|
|
this.txInBlockIndex = mempoolBlocks.indexOf(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:09:07 +07:00
|
|
|
getTransactionTime() {
|
|
|
|
this.apiService.getTransactionTimes$([this.tx.txid])
|
|
|
|
.subscribe((transactionTimes) => {
|
|
|
|
this.transactionTime = transactionTimes[0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-16 02:01:03 +07:00
|
|
|
findBlockAndSetFeeRating() {
|
|
|
|
this.stateService.blocks$
|
2020-03-23 04:07:31 +07:00
|
|
|
.pipe(
|
2020-06-10 23:52:14 +07:00
|
|
|
filter(([block]) => block.height === this.tx.status.block_height),
|
2020-03-23 04:07:31 +07:00
|
|
|
take(1)
|
|
|
|
)
|
2020-06-10 23:52:14 +07:00
|
|
|
.subscribe(([block]) => {
|
2020-03-16 02:01:03 +07:00
|
|
|
const feePervByte = this.tx.fee / (this.tx.weight / 4);
|
2020-03-27 01:29:55 +07:00
|
|
|
this.medianFeeNeeded = Math.round(block.feeRange[Math.round(block.feeRange.length * 0.5)]);
|
2020-03-16 02:01:03 +07:00
|
|
|
|
2020-03-16 02:25:14 +07:00
|
|
|
// Block not filled
|
|
|
|
if (block.weight < 4000000 * 0.95) {
|
2020-03-16 02:41:13 +07:00
|
|
|
this.medianFeeNeeded = 1;
|
2020-03-16 02:25:14 +07:00
|
|
|
}
|
|
|
|
|
2020-03-16 02:41:13 +07:00
|
|
|
this.overpaidTimes = Math.round(feePervByte / this.medianFeeNeeded);
|
2020-03-16 02:25:14 +07:00
|
|
|
|
2020-03-28 16:38:28 +07:00
|
|
|
if (feePervByte <= this.medianFeeNeeded || this.overpaidTimes < 2) {
|
2020-03-16 02:01:03 +07:00
|
|
|
this.feeRating = 1;
|
|
|
|
} else {
|
|
|
|
this.feeRating = 2;
|
|
|
|
if (this.overpaidTimes > 10) {
|
|
|
|
this.feeRating = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-13 01:26:53 +07:00
|
|
|
resetTransaction() {
|
|
|
|
this.error = undefined;
|
|
|
|
this.tx = null;
|
|
|
|
this.feeRating = undefined;
|
|
|
|
this.waitingForTransaction = false;
|
|
|
|
this.isLoadingTx = true;
|
2020-06-08 18:55:53 +07:00
|
|
|
this.rbfTransaction = undefined;
|
2020-04-13 01:26:53 +07:00
|
|
|
this.transactionTime = -1;
|
|
|
|
document.body.scrollTo(0, 0);
|
2020-04-12 03:03:51 +07:00
|
|
|
this.leaveTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
leaveTransaction() {
|
|
|
|
this.websocketService.stopTrackingTransaction();
|
2020-03-22 17:44:36 +07:00
|
|
|
this.stateService.markBlock$.next({});
|
2020-02-19 23:50:23 +07:00
|
|
|
}
|
2020-04-13 01:26:53 +07:00
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
this.leaveTransaction();
|
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|