You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
302 lines
15 KiB
TypeScript
302 lines
15 KiB
TypeScript
4 years ago
|
import { Component, OnInit, Inject, OnDestroy, ViewChild, AfterViewInit } from '@angular/core';
|
||
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||
|
import { Router } from '@angular/router';
|
||
|
import { DecimalPipe } from '@angular/common';
|
||
|
import { Subject } from 'rxjs';
|
||
|
import { takeUntil } from 'rxjs/operators';
|
||
4 years ago
|
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
|
||
3 years ago
|
import { MatStepper } from '@angular/material/stepper';
|
||
4 years ago
|
import { Store } from '@ngrx/store';
|
||
|
import { faInfoCircle } from '@fortawesome/free-solid-svg-icons';
|
||
|
|
||
3 years ago
|
import { opacityAnimation } from '../../../../animation/opacity-animation';
|
||
|
import { ScreenSizeEnum, LoopTypeEnum } from '../../../../services/consts-enums-functions';
|
||
|
import { LoopQuote, LoopStatus } from '../../../../models/loopModels';
|
||
|
import { LoopAlert } from '../../../../models/alertData';
|
||
|
import { LoopService } from '../../../../services/loop.service';
|
||
|
import { LoggerService } from '../../../../services/logger.service';
|
||
|
import { CommonService } from '../../../../services/common.service';
|
||
2 years ago
|
import { Channel, ChannelsSummary, LightningBalance } from '../../../../models/lndModels';
|
||
4 years ago
|
|
||
2 years ago
|
import { RTLState } from '../../../../../store/rtl.state';
|
||
|
import { channels } from '../../../../../lnd/store/lnd.selector';
|
||
|
import { ApiCallStatusPayload } from '../../../../models/apiCallsPayload';
|
||
4 years ago
|
|
||
|
@Component({
|
||
|
selector: 'rtl-loop-modal',
|
||
|
templateUrl: './loop-modal.component.html',
|
||
|
styleUrls: ['./loop-modal.component.scss'],
|
||
|
animations: [opacityAnimation]
|
||
|
})
|
||
|
export class LoopModalComponent implements OnInit, AfterViewInit, OnDestroy {
|
||
3 years ago
|
|
||
|
@ViewChild('stepper', { static: false }) stepper: MatStepper;
|
||
4 years ago
|
public faInfoCircle = faInfoCircle;
|
||
|
public quote: LoopQuote;
|
||
|
public channel: Channel;
|
||
|
public minQuote: LoopQuote;
|
||
|
public maxQuote: LoopQuote;
|
||
3 years ago
|
public LoopTypeEnum = LoopTypeEnum;
|
||
|
public direction = LoopTypeEnum.LOOP_OUT;
|
||
4 years ago
|
public loopDirectionCaption = 'Loop out';
|
||
2 years ago
|
public loopStatus: LoopStatus | null = null;
|
||
4 years ago
|
public inputFormLabel = 'Amount to loop out';
|
||
|
public quoteFormLabel = 'Confirm Quote';
|
||
|
public addressFormLabel = 'Withdrawal Address';
|
||
|
public prepayRoutingFee = 36;
|
||
|
public flgShowInfo = false;
|
||
|
public stepNumber = 1;
|
||
|
public screenSize = '';
|
||
|
public screenSizeEnum = ScreenSizeEnum;
|
||
|
public animationDirection = 'forward';
|
||
|
public flgEditable = true;
|
||
2 years ago
|
public localBalanceToCompare: number | null = null;
|
||
4 years ago
|
inputFormGroup: FormGroup;
|
||
|
quoteFormGroup: FormGroup;
|
||
|
addressFormGroup: FormGroup;
|
||
3 years ago
|
statusFormGroup: FormGroup;
|
||
3 years ago
|
private unSubs: Array<Subject<void>> = [new Subject(), new Subject(), new Subject(), new Subject(), new Subject(), new Subject(), new Subject()];
|
||
4 years ago
|
|
||
2 years ago
|
constructor(public dialogRef: MatDialogRef<LoopModalComponent>, @Inject(MAT_DIALOG_DATA) public data: LoopAlert, private store: Store<RTLState>, private loopService: LoopService, private formBuilder: FormBuilder, private decimalPipe: DecimalPipe, private logger: LoggerService, private router: Router, private commonService: CommonService) { }
|
||
4 years ago
|
|
||
|
ngOnInit() {
|
||
|
this.screenSize = this.commonService.getScreenSize();
|
||
|
this.channel = this.data.channel;
|
||
|
this.minQuote = this.data.minQuote ? this.data.minQuote : {};
|
||
|
this.maxQuote = this.data.maxQuote ? this.data.maxQuote : {};
|
||
2 years ago
|
this.direction = this.data.direction || LoopTypeEnum.LOOP_OUT;
|
||
3 years ago
|
this.loopDirectionCaption = this.direction === LoopTypeEnum.LOOP_IN ? 'Loop in' : 'Loop out';
|
||
3 years ago
|
this.inputFormLabel = 'Amount to ' + this.loopDirectionCaption;
|
||
4 years ago
|
this.inputFormGroup = this.formBuilder.group({
|
||
2 years ago
|
amount: [this.minQuote.amount, [Validators.required, Validators.min(this.minQuote.amount || 0), Validators.max(this.maxQuote.amount || 0)]],
|
||
4 years ago
|
sweepConfTarget: [6, [Validators.required, Validators.min(1)]],
|
||
3 years ago
|
routingFeePercent: [2, [Validators.required, Validators.min(0)]],
|
||
4 years ago
|
fast: [false, [Validators.required]]
|
||
|
});
|
||
|
this.quoteFormGroup = this.formBuilder.group({});
|
||
|
this.addressFormGroup = this.formBuilder.group({
|
||
|
addressType: ['local', [Validators.required]],
|
||
3 years ago
|
address: [{ value: '', disabled: true }]
|
||
4 years ago
|
});
|
||
|
this.statusFormGroup = this.formBuilder.group({});
|
||
|
this.onFormValueChanges();
|
||
2 years ago
|
this.store.select(channels).pipe(takeUntil(this.unSubs[6])).
|
||
|
subscribe((channelsSelector: { channels: Channel[], channelsSummary: ChannelsSummary, lightningBalance: LightningBalance, apiCallStatus: ApiCallStatusPayload }) => {
|
||
2 years ago
|
this.localBalanceToCompare = (this.channel && this.channel.local_balance) ? +this.channel.local_balance : (channelsSelector.lightningBalance && channelsSelector.lightningBalance.local) ? +channelsSelector.lightningBalance.local : null;
|
||
3 years ago
|
});
|
||
4 years ago
|
}
|
||
|
|
||
|
ngAfterViewInit() {
|
||
3 years ago
|
this.inputFormGroup.setErrors({ Invalid: true });
|
||
3 years ago
|
if (this.direction === LoopTypeEnum.LOOP_OUT) {
|
||
3 years ago
|
this.addressFormGroup.setErrors({ Invalid: true });
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
onFormValueChanges() {
|
||
3 years ago
|
this.inputFormGroup.valueChanges.pipe(takeUntil(this.unSubs[4])).subscribe((changedValues) => {
|
||
|
this.inputFormGroup.setErrors({ Invalid: true });
|
||
4 years ago
|
});
|
||
3 years ago
|
if (this.direction === LoopTypeEnum.LOOP_OUT) {
|
||
3 years ago
|
this.addressFormGroup.valueChanges.pipe(takeUntil(this.unSubs[5])).subscribe((changedValues) => {
|
||
|
this.addressFormGroup.setErrors({ Invalid: true });
|
||
4 years ago
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
onAddressTypeChange(event: any) {
|
||
|
if (event.value === 'external') {
|
||
|
this.addressFormGroup.controls.address.setValidators([Validators.required]);
|
||
|
this.addressFormGroup.controls.address.markAsTouched();
|
||
|
this.addressFormGroup.controls.address.enable();
|
||
|
} else {
|
||
|
this.addressFormGroup.controls.address.setValidators(null);
|
||
|
this.addressFormGroup.controls.address.markAsPristine();
|
||
|
this.addressFormGroup.controls.address.disable();
|
||
|
this.addressFormGroup.controls.address.setValue('');
|
||
|
}
|
||
3 years ago
|
this.addressFormGroup.setErrors({ Invalid: true });
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
onValidateAmount() {
|
||
2 years ago
|
if (this.localBalanceToCompare && this.inputFormGroup.controls.amount.value <= this.localBalanceToCompare) {
|
||
3 years ago
|
this.stepper.next();
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
onLoop(): boolean | void {
|
||
2 years ago
|
if (!this.inputFormGroup.controls.amount.value || (this.minQuote.amount && this.inputFormGroup.controls.amount.value < this.minQuote.amount) || (this.maxQuote.amount && this.inputFormGroup.controls.amount.value > this.maxQuote.amount) ||
|
||
3 years ago
|
!this.inputFormGroup.controls.sweepConfTarget.value || this.inputFormGroup.controls.sweepConfTarget.value < 2 ||
|
||
|
(this.direction === LoopTypeEnum.LOOP_OUT && (!this.inputFormGroup.controls.routingFeePercent.value || this.inputFormGroup.controls.routingFeePercent.value < 0)) ||
|
||
|
(this.direction === LoopTypeEnum.LOOP_OUT && this.addressFormGroup.controls.addressType.value === 'external' &&
|
||
|
(!this.addressFormGroup.controls.address.value || this.addressFormGroup.controls.address.value.trim() === ''))) {
|
||
|
return true;
|
||
|
}
|
||
4 years ago
|
this.flgEditable = false;
|
||
2 years ago
|
this.stepper.selected?.stepControl.setErrors(null);
|
||
4 years ago
|
this.stepper.next();
|
||
3 years ago
|
if (this.direction === LoopTypeEnum.LOOP_IN) {
|
||
2 years ago
|
this.loopService.loopIn(this.inputFormGroup.controls.amount.value, +(this.quote.swap_fee_sat || 0), +(this.quote.htlc_publish_fee_sat || 0), '', true).pipe(takeUntil(this.unSubs[0])).
|
||
3 years ago
|
subscribe({
|
||
|
next: (loopStatus: any) => {
|
||
|
this.loopStatus = loopStatus;
|
||
|
this.loopService.listSwaps();
|
||
|
this.flgEditable = true;
|
||
|
}, error: (err) => {
|
||
|
this.loopStatus = { error: err };
|
||
|
this.flgEditable = true;
|
||
|
this.logger.error(err);
|
||
|
}
|
||
|
});
|
||
4 years ago
|
} else {
|
||
3 years ago
|
const swapRoutingFee = Math.ceil(this.inputFormGroup.controls.amount.value * (this.inputFormGroup.controls.routingFeePercent.value / 100));
|
||
|
const destAddress = this.addressFormGroup.controls.addressType.value === 'external' ? this.addressFormGroup.controls.address.value : '';
|
||
|
const swapPublicationDeadline = this.inputFormGroup.controls.fast.value ? 0 : new Date().getTime() + (30 * 60000);
|
||
2 years ago
|
this.loopService.loopOut(this.inputFormGroup.controls.amount.value, (this.channel && this.channel.chan_id ? this.channel.chan_id : ''), this.inputFormGroup.controls.sweepConfTarget.value, swapRoutingFee, +(this.quote.htlc_sweep_fee_sat || 0), this.prepayRoutingFee, +(this.quote.prepay_amt_sat || 0), +(this.quote.swap_fee_sat || 0), swapPublicationDeadline, destAddress).pipe(takeUntil(this.unSubs[1])).
|
||
3 years ago
|
subscribe({
|
||
|
next: (loopStatus: any) => {
|
||
|
this.loopStatus = loopStatus;
|
||
|
this.loopService.listSwaps();
|
||
|
this.flgEditable = true;
|
||
|
}, error: (err) => {
|
||
|
this.loopStatus = { error: err };
|
||
|
this.flgEditable = true;
|
||
|
this.logger.error(err);
|
||
|
}
|
||
|
});
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
onEstimateQuote(): boolean | void {
|
||
2 years ago
|
if (!this.inputFormGroup.controls.amount.value || (this.minQuote.amount && this.inputFormGroup.controls.amount.value < this.minQuote.amount) || (this.maxQuote.amount && this.inputFormGroup.controls.amount.value > this.maxQuote.amount) || !this.inputFormGroup.controls.sweepConfTarget.value || this.inputFormGroup.controls.sweepConfTarget.value < 2) {
|
||
3 years ago
|
return true;
|
||
|
}
|
||
|
const swapPublicationDeadline = this.inputFormGroup.controls.fast.value ? 0 : new Date().getTime() + (30 * 60000);
|
||
|
if (this.direction === LoopTypeEnum.LOOP_IN) {
|
||
|
this.loopService.getLoopInQuote(this.inputFormGroup.controls.amount.value, this.inputFormGroup.controls.sweepConfTarget.value, swapPublicationDeadline).
|
||
|
pipe(takeUntil(this.unSubs[2])).
|
||
|
subscribe((response) => {
|
||
|
this.quote = response;
|
||
|
this.quote.off_chain_swap_routing_fee_percentage = this.inputFormGroup.controls.routingFeePercent.value ? this.inputFormGroup.controls.routingFeePercent.value : 2;
|
||
|
});
|
||
4 years ago
|
} else {
|
||
3 years ago
|
this.loopService.getLoopOutQuote(this.inputFormGroup.controls.amount.value, this.inputFormGroup.controls.sweepConfTarget.value, swapPublicationDeadline).
|
||
|
pipe(takeUntil(this.unSubs[3])).
|
||
|
subscribe((response) => {
|
||
|
this.quote = response;
|
||
|
this.quote.off_chain_swap_routing_fee_percentage = this.inputFormGroup.controls.routingFeePercent.value ? this.inputFormGroup.controls.routingFeePercent.value : 2;
|
||
|
});
|
||
4 years ago
|
}
|
||
2 years ago
|
this.stepper.selected?.stepControl.setErrors(null);
|
||
4 years ago
|
this.stepper.next();
|
||
4 years ago
|
}
|
||
|
|
||
|
stepSelectionChanged(event: any) {
|
||
|
switch (event.selectedIndex) {
|
||
|
case 0:
|
||
|
this.inputFormLabel = 'Amount to ' + this.loopDirectionCaption;
|
||
|
this.quoteFormLabel = 'Confirm Quote';
|
||
|
this.addressFormLabel = 'Withdrawal Address';
|
||
|
break;
|
||
3 years ago
|
|
||
4 years ago
|
case 1:
|
||
|
if (this.inputFormGroup.controls.amount.value || this.inputFormGroup.controls.sweepConfTarget.value) {
|
||
3 years ago
|
if (this.direction === LoopTypeEnum.LOOP_IN) {
|
||
4 years ago
|
this.inputFormLabel = this.loopDirectionCaption + ' Amount: ' + (this.decimalPipe.transform(this.inputFormGroup.controls.amount.value ? this.inputFormGroup.controls.amount.value : 0)) + ' Sats | Target Confirmation: ' + (this.inputFormGroup.controls.sweepConfTarget.value ? this.inputFormGroup.controls.sweepConfTarget.value : 6);
|
||
|
} else {
|
||
3 years ago
|
this.inputFormLabel = this.loopDirectionCaption + ' Amount: ' +
|
||
|
(this.decimalPipe.transform(this.inputFormGroup.controls.amount.value ? this.inputFormGroup.controls.amount.value : 0)) + ' Sats | Target Confirmation: ' +
|
||
|
(this.inputFormGroup.controls.sweepConfTarget.value ? this.inputFormGroup.controls.sweepConfTarget.value : 6) + ' | Percentage: ' +
|
||
|
(this.inputFormGroup.controls.routingFeePercent.value ? this.inputFormGroup.controls.routingFeePercent.value : '2') + ' | Fast: ' +
|
||
|
(this.inputFormGroup.controls.fast.value ? 'Enabled' : 'Disabled');
|
||
4 years ago
|
}
|
||
|
} else {
|
||
|
this.inputFormLabel = 'Amount to ' + this.loopDirectionCaption;
|
||
|
}
|
||
|
this.quoteFormLabel = 'Confirm Quote';
|
||
|
this.addressFormLabel = 'Withdrawal Address';
|
||
|
break;
|
||
|
|
||
|
case 2:
|
||
|
if (this.inputFormGroup.controls.amount.value || this.inputFormGroup.controls.sweepConfTarget.value) {
|
||
3 years ago
|
if (this.direction === LoopTypeEnum.LOOP_IN) {
|
||
4 years ago
|
this.inputFormLabel = this.loopDirectionCaption + ' Amount: ' + (this.decimalPipe.transform(this.inputFormGroup.controls.amount.value ? this.inputFormGroup.controls.amount.value : 0)) + ' Sats | Target Confirmation: ' + (this.inputFormGroup.controls.sweepConfTarget.value ? this.inputFormGroup.controls.sweepConfTarget.value : 6);
|
||
|
} else {
|
||
3 years ago
|
this.inputFormLabel = this.loopDirectionCaption + ' Amount: ' +
|
||
|
(this.decimalPipe.transform(this.inputFormGroup.controls.amount.value ? this.inputFormGroup.controls.amount.value : 0)) + ' Sats | Target Confirmation: ' +
|
||
|
(this.inputFormGroup.controls.sweepConfTarget.value ? this.inputFormGroup.controls.sweepConfTarget.value : 6) + ' | Fast: ' +
|
||
|
(this.inputFormGroup.controls.fast.value ? 'Enabled' : 'Disabled');
|
||
4 years ago
|
}
|
||
|
} else {
|
||
|
this.inputFormLabel = 'Amount to ' + this.loopDirectionCaption;
|
||
|
}
|
||
4 years ago
|
if (this.quote && this.quote.swap_fee_sat && (this.quote.htlc_sweep_fee_sat || this.quote.htlc_publish_fee_sat) && this.quote.prepay_amt_sat) {
|
||
|
this.quoteFormLabel = 'Quote confirmed | Estimated Fees: ' + this.decimalPipe.transform(+this.quote.swap_fee_sat + +(this.quote.htlc_sweep_fee_sat ? this.quote.htlc_sweep_fee_sat : this.quote.htlc_publish_fee_sat ? this.quote.htlc_publish_fee_sat : 0)) + ' Sats';
|
||
4 years ago
|
} else {
|
||
|
this.quoteFormLabel = 'Quote confirmed';
|
||
|
}
|
||
|
if (this.addressFormGroup.controls.addressType.value) {
|
||
|
this.addressFormLabel = 'Withdrawal Address | Type: ' + this.addressFormGroup.controls.addressType.value;
|
||
|
} else {
|
||
|
this.addressFormLabel = 'Withdrawal Address';
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
this.inputFormLabel = 'Amount to ' + this.loopDirectionCaption;
|
||
|
this.quoteFormLabel = 'Confirm Quote';
|
||
3 years ago
|
this.addressFormLabel = 'Withdrawal Address';
|
||
4 years ago
|
break;
|
||
|
}
|
||
3 years ago
|
if ((this.direction === LoopTypeEnum.LOOP_OUT && event.selectedIndex !== 1 && event.selectedIndex < event.previouslySelectedIndex) ||
|
||
|
(this.direction === LoopTypeEnum.LOOP_IN && event.selectedIndex < event.previouslySelectedIndex)) {
|
||
|
event.selectedStep.stepControl.setErrors({ Invalid: true });
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
goToLoop() {
|
||
|
this.dialogRef.close(true);
|
||
3 years ago
|
this.router.navigateByUrl('/services/loop');
|
||
4 years ago
|
}
|
||
|
|
||
|
onClose() {
|
||
|
this.dialogRef.close(true);
|
||
|
}
|
||
|
|
||
|
showInfo() {
|
||
|
this.flgShowInfo = true;
|
||
|
}
|
||
|
|
||
|
onReadMore() {
|
||
3 years ago
|
if (this.direction === LoopTypeEnum.LOOP_IN) {
|
||
4 years ago
|
window.open('https://blog.lightning.engineering/announcement/2019/06/25/loop-in.html', '_blank');
|
||
|
} else {
|
||
|
window.open('https://blog.lightning.engineering/technical/posts/2019/04/15/loop-out-in-depth.html', '_blank');
|
||
|
}
|
||
|
this.onClose();
|
||
|
}
|
||
|
|
||
|
onStepChanged(index: number) {
|
||
|
this.animationDirection = index < this.stepNumber ? 'backward' : 'forward';
|
||
|
this.stepNumber = index;
|
||
4 years ago
|
}
|
||
|
|
||
|
onRestart() {
|
||
|
this.stepper.reset();
|
||
|
this.flgEditable = true;
|
||
3 years ago
|
this.inputFormGroup.reset({ amount: this.minQuote.amount, sweepConfTarget: 6, routingFeePercent: 2, fast: false });
|
||
4 years ago
|
this.quoteFormGroup.reset();
|
||
|
this.statusFormGroup.reset();
|
||
3 years ago
|
this.addressFormGroup.reset({ addressType: 'local', address: '' });
|
||
4 years ago
|
this.addressFormGroup.controls.address.disable();
|
||
|
}
|
||
4 years ago
|
|
||
|
ngOnDestroy() {
|
||
3 years ago
|
this.unSubs.forEach((completeSub) => {
|
||
2 years ago
|
completeSub.next(<any>null);
|
||
4 years ago
|
completeSub.complete();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|