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';
|
2021-07-06 13:56:32 -03:00
|
|
|
import {
|
|
|
|
switchMap,
|
|
|
|
filter,
|
|
|
|
catchError,
|
|
|
|
retryWhen,
|
|
|
|
delay,
|
2021-07-24 19:26:29 -03:00
|
|
|
map
|
2021-07-06 13:56:32 -03:00
|
|
|
} from 'rxjs/operators';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { Transaction, Block } from '../../interfaces/electrs.interface';
|
2021-08-18 14:05:40 +03:00
|
|
|
import { of, merge, Subscription, Observable, Subject, timer, combineLatest, from } 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';
|
2021-03-22 18:04:50 +07:00
|
|
|
import { CpfpInfo } from 'src/app/interfaces/node-api.interface';
|
2021-08-17 20:20:25 +03:00
|
|
|
import { LiquidUnblinding } from './liquid-ublinding';
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-transaction',
|
|
|
|
templateUrl: './transaction.component.html',
|
2021-07-06 13:56:32 -03:00
|
|
|
styleUrls: ['./transaction.component.scss'],
|
2020-02-16 22:15:07 +07:00
|
|
|
})
|
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-23 04:07:31 +07:00
|
|
|
txInBlockIndex: number;
|
2020-02-16 22:15:07 +07:00
|
|
|
isLoadingTx = true;
|
|
|
|
error: any = undefined;
|
2021-07-06 13:56:32 -03:00
|
|
|
errorUnblinded: 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;
|
2021-04-27 02:13:48 +04:00
|
|
|
fetchCpfpSubscription: Subscription;
|
2020-06-08 18:55:53 +07:00
|
|
|
rbfTransaction: undefined | Transaction;
|
2021-03-22 18:04:50 +07:00
|
|
|
cpfpInfo: CpfpInfo | null;
|
|
|
|
showCpfpDetails = false;
|
2021-04-27 02:13:48 +04:00
|
|
|
fetchCpfp$ = new Subject<string>();
|
2021-07-24 19:26:29 -03:00
|
|
|
now = new Date().getTime();
|
|
|
|
timeAvg$: Observable<number>;
|
2021-08-17 20:20:25 +03:00
|
|
|
liquidUnblinding = new LiquidUnblinding();
|
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,
|
2021-07-06 13:56:32 -03:00
|
|
|
private seoService: SeoService
|
|
|
|
) {}
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
ngOnInit() {
|
2020-09-26 22:46:26 +07:00
|
|
|
this.websocketService.want(['blocks', 'mempool-blocks']);
|
2021-07-06 13:56:32 -03:00
|
|
|
this.stateService.networkChanged$.subscribe(
|
|
|
|
(network) => (this.network = network)
|
|
|
|
);
|
2020-05-09 20:37:50 +07:00
|
|
|
|
2021-07-24 19:26:29 -03:00
|
|
|
this.timeAvg$ = timer(0, 1000)
|
|
|
|
.pipe(
|
|
|
|
switchMap(() => combineLatest([
|
|
|
|
this.stateService.blocks$.pipe(map(([block]) => block)),
|
|
|
|
this.stateService.lastDifficultyAdjustment$
|
|
|
|
])),
|
|
|
|
map(([block, DATime]) => {
|
|
|
|
this.now = new Date().getTime();
|
|
|
|
const now = new Date().getTime() / 1000;
|
|
|
|
const diff = now - DATime;
|
|
|
|
const blocksInEpoch = block.height % 2016;
|
|
|
|
let difficultyChange = 0;
|
|
|
|
if (blocksInEpoch > 0) {
|
|
|
|
difficultyChange = (600 / (diff / blocksInEpoch ) - 1) * 100;
|
|
|
|
}
|
|
|
|
const timeAvgDiff = difficultyChange * 0.1;
|
|
|
|
|
|
|
|
let timeAvgMins = 10;
|
|
|
|
if (timeAvgDiff > 0 ){
|
|
|
|
timeAvgMins -= Math.abs(timeAvgDiff);
|
|
|
|
} else {
|
|
|
|
timeAvgMins += Math.abs(timeAvgDiff);
|
|
|
|
}
|
|
|
|
return timeAvgMins * 60 * 1000;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2021-05-21 17:06:53 +04:00
|
|
|
this.fetchCpfpSubscription = this.fetchCpfp$
|
|
|
|
.pipe(
|
2021-07-06 13:56:32 -03:00
|
|
|
switchMap((txId) =>
|
|
|
|
this.apiService
|
|
|
|
.getCpfpinfo$(txId)
|
|
|
|
.pipe(retryWhen((errors) => errors.pipe(delay(2000))))
|
|
|
|
)
|
2021-05-21 17:06:53 +04:00
|
|
|
)
|
|
|
|
.subscribe((cpfpInfo) => {
|
|
|
|
if (!this.tx) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-06 13:56:32 -03:00
|
|
|
const lowerFeeParents = cpfpInfo.ancestors.filter(
|
|
|
|
(parent) => parent.fee / (parent.weight / 4) < this.tx.feePerVsize
|
|
|
|
);
|
|
|
|
let totalWeight =
|
|
|
|
this.tx.weight +
|
|
|
|
lowerFeeParents.reduce((prev, val) => prev + val.weight, 0);
|
|
|
|
let totalFees =
|
|
|
|
this.tx.fee +
|
|
|
|
lowerFeeParents.reduce((prev, val) => prev + val.fee, 0);
|
2021-05-21 17:06:53 +04:00
|
|
|
|
|
|
|
if (cpfpInfo.bestDescendant) {
|
|
|
|
totalWeight += cpfpInfo.bestDescendant.weight;
|
|
|
|
totalFees += cpfpInfo.bestDescendant.fee;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tx.effectiveFeePerVsize = totalFees / (totalWeight / 4);
|
2021-07-06 13:56:32 -03:00
|
|
|
this.stateService.markBlock$.next({
|
|
|
|
txFeePerVSize: this.tx.effectiveFeePerVsize,
|
|
|
|
});
|
2021-05-21 17:06:53 +04:00
|
|
|
this.cpfpInfo = cpfpInfo;
|
|
|
|
});
|
|
|
|
|
2021-07-06 13:56:32 -03:00
|
|
|
this.subscription = this.route.paramMap
|
|
|
|
.pipe(
|
2021-08-17 20:20:25 +03:00
|
|
|
switchMap((params: ParamMap) => {
|
2021-07-06 13:56:32 -03:00
|
|
|
this.txId = params.get('id') || '';
|
|
|
|
this.seoService.setTitle(
|
|
|
|
$localize`:@@bisq.transaction.browser-title:Transaction: ${this.txId}:INTERPOLATION:`
|
2020-04-13 01:26:53 +07:00
|
|
|
);
|
2021-07-06 13:56:32 -03:00
|
|
|
this.resetTransaction();
|
|
|
|
return merge(
|
|
|
|
of(true),
|
|
|
|
this.stateService.connectionState$.pipe(
|
|
|
|
filter(
|
|
|
|
(state) => state === 2 && this.tx && !this.tx.status.confirmed
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
switchMap(() => {
|
|
|
|
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$
|
|
|
|
);
|
2021-08-18 14:05:40 +03:00
|
|
|
}),
|
|
|
|
switchMap((tx) => {
|
|
|
|
if (this.network === 'liquid') {
|
|
|
|
return from(this.liquidUnblinding.checkUnblindedTx(tx))
|
|
|
|
.pipe(
|
|
|
|
catchError((error) => {
|
|
|
|
this.errorUnblinded = error;
|
|
|
|
return of(tx);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return of(tx);
|
2021-07-06 13:56:32 -03:00
|
|
|
})
|
|
|
|
)
|
2021-08-18 14:05:40 +03:00
|
|
|
.subscribe((tx: Transaction) => {
|
2021-07-06 13:56:32 -03:00
|
|
|
if (!tx) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-18 03:34:17 +03:00
|
|
|
|
2021-07-06 13:56:32 -03:00
|
|
|
this.tx = tx;
|
|
|
|
if (tx.fee === undefined) {
|
|
|
|
this.tx.fee = 0;
|
|
|
|
}
|
|
|
|
this.tx.feePerVsize = tx.fee / (tx.weight / 4);
|
|
|
|
this.isLoadingTx = false;
|
|
|
|
this.error = undefined;
|
|
|
|
this.waitingForTransaction = false;
|
|
|
|
this.setMempoolBlocksSubscription();
|
2021-03-18 23:47:40 +07:00
|
|
|
|
2021-07-06 13:56:32 -03:00
|
|
|
if (!tx.status.confirmed) {
|
|
|
|
this.websocketService.startTrackTransaction(tx.txid);
|
|
|
|
|
|
|
|
if (tx.firstSeen) {
|
|
|
|
this.transactionTime = tx.firstSeen;
|
|
|
|
} else {
|
|
|
|
this.getTransactionTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.tx.status.confirmed) {
|
|
|
|
this.stateService.markBlock$.next({
|
|
|
|
blockHeight: tx.status.block_height,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (tx.cpfpChecked) {
|
|
|
|
this.stateService.markBlock$.next({
|
|
|
|
txFeePerVSize: tx.effectiveFeePerVsize,
|
|
|
|
});
|
|
|
|
this.cpfpInfo = {
|
|
|
|
ancestors: tx.ancestors,
|
|
|
|
bestDescendant: tx.bestDescendant,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.fetchCpfp$.next(this.tx.txid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.error = error;
|
|
|
|
this.isLoadingTx = false;
|
2021-03-18 23:47:40 +07:00
|
|
|
}
|
2021-07-06 13:56:32 -03:00
|
|
|
);
|
|
|
|
|
|
|
|
this.stateService.blocks$.subscribe(([block, txConfirmed]) => {
|
|
|
|
this.latestBlock = block;
|
|
|
|
|
|
|
|
if (txConfirmed && this.tx) {
|
|
|
|
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');
|
2020-03-22 17:44:36 +07:00
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
});
|
|
|
|
|
2021-07-06 13:56:32 -03: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() {
|
2021-07-06 13:56:32 -03:00
|
|
|
this.stateService.mempoolBlocks$.subscribe((mempoolBlocks) => {
|
|
|
|
if (!this.tx) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-23 04:07:31 +07:00
|
|
|
|
2021-07-06 13:56:32 -03:00
|
|
|
const txFeePerVSize =
|
|
|
|
this.tx.effectiveFeePerVsize || this.tx.fee / (this.tx.weight / 4);
|
2020-03-23 04:07:31 +07:00
|
|
|
|
2021-07-06 13:56:32 -03:00
|
|
|
for (const block of mempoolBlocks) {
|
|
|
|
for (let i = 0; i < block.feeRange.length - 1; i++) {
|
|
|
|
if (
|
|
|
|
txFeePerVSize <= block.feeRange[i + 1] &&
|
|
|
|
txFeePerVSize >= block.feeRange[i]
|
|
|
|
) {
|
|
|
|
this.txInBlockIndex = mempoolBlocks.indexOf(block);
|
2020-03-23 04:07:31 +07:00
|
|
|
}
|
|
|
|
}
|
2021-07-06 13:56:32 -03:00
|
|
|
}
|
|
|
|
});
|
2020-03-23 04:07:31 +07:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:09:07 +07:00
|
|
|
getTransactionTime() {
|
2021-07-06 13:56:32 -03:00
|
|
|
this.apiService
|
|
|
|
.getTransactionTimes$([this.tx.txid])
|
2020-02-28 01:09:07 +07:00
|
|
|
.subscribe((transactionTimes) => {
|
|
|
|
this.transactionTime = transactionTimes[0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-13 01:26:53 +07:00
|
|
|
resetTransaction() {
|
|
|
|
this.error = undefined;
|
|
|
|
this.tx = null;
|
|
|
|
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;
|
2021-03-22 18:04:50 +07:00
|
|
|
this.cpfpInfo = null;
|
|
|
|
this.showCpfpDetails = false;
|
2020-04-13 01:26:53 +07:00
|
|
|
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
|
|
|
|
2021-03-22 18:04:50 +07:00
|
|
|
roundToOneDecimal(cpfpTx: any): number {
|
|
|
|
return +(cpfpTx.fee / (cpfpTx.weight / 4)).toFixed(1);
|
|
|
|
}
|
|
|
|
|
2020-04-13 01:26:53 +07:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscription.unsubscribe();
|
2021-04-27 02:13:48 +04:00
|
|
|
this.fetchCpfpSubscription.unsubscribe();
|
2020-04-13 01:26:53 +07:00
|
|
|
this.leaveTransaction();
|
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|