2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-10-31 09:20:27 +00:00
RTL/src/app/cln/on-chain/on-chain-send-modal/on-chain-send-modal.component.ts

341 lines
16 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, OnDestroy, ViewChild, Inject } from '@angular/core';
2022-11-24 02:20:23 +00:00
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { DecimalPipe } from '@angular/common';
import { Subject, combineLatest } from 'rxjs';
import { takeUntil, filter, take } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { Actions } from '@ngrx/effects';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';
2021-08-28 21:03:18 +00:00
import { MatStepper } from '@angular/material/stepper';
import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons';
2021-12-29 23:08:41 +00:00
import * as sha256 from 'sha256';
2021-12-29 23:08:41 +00:00
import { SelNodeChild, RTLConfiguration } from '../../../shared/models/RTLconfig';
import { CLNOnChainSendFunds } from '../../../shared/models/alertData';
import { GetInfo, Balance, OnChain, UTXO, LocalRemoteBalance } from '../../../shared/models/clnModels';
import { CURRENCY_UNITS, CurrencyUnitEnum, CURRENCY_UNIT_FORMATS, ADDRESS_TYPES, FEE_RATE_TYPES, APICallStatusEnum, CLNActions, ScreenSizeEnum } from '../../../shared/services/consts-enums-functions';
import { CommonService } from '../../../shared/services/common.service';
import { LoggerService } from '../../../shared/services/logger.service';
import { RTLEffects } from '../../../store/rtl.effects';
2021-12-29 23:08:41 +00:00
import { RTLState } from '../../../store/rtl.state';
import { isAuthorized, openSnackBar } from '../../../store/rtl.actions';
import { setChannelTransaction } from '../../store/cln.actions';
2021-12-29 23:08:41 +00:00
import { rootAppConfig, rootSelectedNode } from '../../../store/rtl.selector';
import { clnNodeInformation, utxoBalances } from '../../store/cln.selector';
2021-12-29 23:08:41 +00:00
import { ApiCallStatusPayload } from '../../../shared/models/apiCallsPayload';
@Component({
selector: 'rtl-cln-on-chain-send-modal',
templateUrl: './on-chain-send-modal.component.html',
styleUrls: ['./on-chain-send-modal.component.scss']
})
export class CLNOnChainSendModalComponent implements OnInit, OnDestroy {
2021-08-28 21:03:18 +00:00
@ViewChild('form', { static: true }) form: any;
@ViewChild('formSweepAll', { static: false }) formSweepAll: any;
@ViewChild('stepper', { static: false }) stepper: MatStepper;
public faExclamationTriangle = faExclamationTriangle;
public sweepAll = false;
public selNode: SelNodeChild | null = {};
public appConfig: RTLConfiguration;
public addressTypes = [];
public utxos: UTXO[] = [];
public selUTXOs: UTXO[] = [];
public flgUseAllBalance = false;
public totalSelectedUTXOAmount: number | null = null;
public selectedAddress = ADDRESS_TYPES[1];
public blockchainBalance: Balance = {};
public information: GetInfo = {};
public newAddress = '';
public transaction: OnChain | any = {};
public feeRateTypes = FEE_RATE_TYPES;
2021-12-29 23:08:41 +00:00
public selFeeRate = '';
public customFeeRate = null;
public flgMinConf = false;
2021-12-29 23:08:41 +00:00
public minConfValue = null;
public sendFundError = '';
public fiatConversion = false;
public amountUnits = CURRENCY_UNITS;
public selAmountUnit = CURRENCY_UNITS[0];
public currConvertorRate = {};
public unitConversionValue = 0;
public currencyUnitFormats = CURRENCY_UNIT_FORMATS;
public advancedTitle = 'Advanced Options';
public flgValidated = false;
public flgEditable = true;
public passwordFormLabel = 'Authenticate with your RTL password';
public sendFundFormLabel = 'Sweep funds';
public confirmFormLabel = 'Confirm sweep';
2021-08-28 21:03:18 +00:00
public amountError = 'Amount is Required.';
2022-11-24 02:20:23 +00:00
passwordFormGroup: UntypedFormGroup;
sendFundFormGroup: UntypedFormGroup;
confirmFormGroup: UntypedFormGroup;
2021-12-29 23:08:41 +00:00
public screenSize = '';
public screenSizeEnum = ScreenSizeEnum;
private unSubs: Array<Subject<void>> = [new Subject(), new Subject(), new Subject(), new Subject(), new Subject(), new Subject(), new Subject(), new Subject()];
2022-10-31 21:24:41 +00:00
constructor(
public dialogRef: MatDialogRef<CLNOnChainSendModalComponent>,
@Inject(MAT_DIALOG_DATA) public data: CLNOnChainSendFunds,
private logger: LoggerService,
private store: Store<RTLState>,
private commonService: CommonService,
private decimalPipe: DecimalPipe,
private actions: Actions,
2022-11-24 02:20:23 +00:00
private formBuilder: UntypedFormBuilder,
2022-10-31 21:24:41 +00:00
private rtlEffects: RTLEffects,
private snackBar: MatSnackBar) {
2021-12-29 23:08:41 +00:00
this.screenSize = this.commonService.getScreenSize();
}
ngOnInit() {
this.sweepAll = this.data.sweepAll;
this.passwordFormGroup = this.formBuilder.group({
hiddenPassword: ['', [Validators.required]],
password: ['', [Validators.required]]
});
this.sendFundFormGroup = this.formBuilder.group({
transactionAddress: ['', Validators.required],
2021-12-29 23:08:41 +00:00
selFeeRate: [null],
customFeeRate: [null],
flgMinConf: [false],
2021-12-29 23:08:41 +00:00
minConfValue: [{ value: null, disabled: true }]
});
2021-08-28 21:03:18 +00:00
this.confirmFormGroup = this.formBuilder.group({});
2021-12-29 23:08:41 +00:00
this.sendFundFormGroup.controls.flgMinConf.valueChanges.pipe(takeUntil(this.unSubs[0])).subscribe((flg) => {
if (flg) {
2021-12-29 23:08:41 +00:00
this.sendFundFormGroup.controls.selFeeRate.disable();
this.sendFundFormGroup.controls.selFeeRate.setValue(null);
this.sendFundFormGroup.controls.minConfValue.reset();
this.sendFundFormGroup.controls.minConfValue.enable();
this.sendFundFormGroup.controls.minConfValue.setValidators([Validators.required]);
this.sendFundFormGroup.controls.minConfValue.setValue(null);
} else {
2021-12-29 23:08:41 +00:00
this.sendFundFormGroup.controls.selFeeRate.enable();
this.sendFundFormGroup.controls.selFeeRate.setValue(null);
this.sendFundFormGroup.controls.minConfValue.setValue(null);
this.sendFundFormGroup.controls.minConfValue.disable();
this.sendFundFormGroup.controls.minConfValue.setValidators(null);
this.sendFundFormGroup.controls.minConfValue.setErrors(null);
}
});
2021-12-29 23:08:41 +00:00
this.sendFundFormGroup.controls.selFeeRate.valueChanges.pipe(takeUntil(this.unSubs[1])).subscribe((feeRate) => {
this.sendFundFormGroup.controls.customFeeRate.setValue(null);
this.sendFundFormGroup.controls.customFeeRate.reset();
if (feeRate === 'customperkb' && !this.sendFundFormGroup.controls.flgMinConf.value) {
this.sendFundFormGroup.controls.customFeeRate.setValidators([Validators.required]);
} else {
this.sendFundFormGroup.controls.customFeeRate.setValidators(null);
}
});
combineLatest([this.store.select(rootSelectedNode), this.store.select(rootAppConfig)]).pipe(takeUntil(this.unSubs[1])).
subscribe(([selNode, appConfig]) => {
this.fiatConversion = selNode.settings.fiatConversion;
this.amountUnits = selNode.settings.currencyUnits;
this.appConfig = appConfig;
});
this.store.select(clnNodeInformation).pipe(takeUntil(this.unSubs[2])).
2021-12-29 23:08:41 +00:00
subscribe((nodeInfo: GetInfo) => {
this.information = nodeInfo;
});
this.store.select(utxoBalances).pipe(takeUntil(this.unSubs[3])).
subscribe((utxoBalancesSeletor: { utxos: UTXO[], balance: Balance, localRemoteBalance: LocalRemoteBalance, apiCallStatus: ApiCallStatusPayload }) => {
this.utxos = this.commonService.sortAscByKey(utxoBalancesSeletor.utxos?.filter((utxo) => utxo.status === 'confirmed'), 'value');
this.logger.info(utxoBalancesSeletor);
2021-08-28 21:03:18 +00:00
});
this.actions.pipe(
2021-12-29 23:08:41 +00:00
takeUntil(this.unSubs[4]),
filter((action) => action.type === CLNActions.UPDATE_API_CALL_STATUS_CLN || action.type === CLNActions.SET_CHANNEL_TRANSACTION_RES_CLN)).
2021-12-29 23:08:41 +00:00
subscribe((action: any) => {
if (action.type === CLNActions.SET_CHANNEL_TRANSACTION_RES_CLN) {
2021-12-29 23:08:41 +00:00
this.store.dispatch(openSnackBar({ payload: 'Fund Sent Successfully!' }));
2021-08-28 21:03:18 +00:00
this.dialogRef.close();
}
if (action.type === CLNActions.UPDATE_API_CALL_STATUS_CLN && action.payload.status === APICallStatusEnum.ERROR && action.payload.action === 'SetChannelTransaction') {
2021-08-28 21:03:18 +00:00
this.sendFundError = action.payload.message;
}
});
}
2021-08-28 21:03:18 +00:00
onAuthenticate(): boolean | void {
if (!this.passwordFormGroup.controls.password.value) {
return true;
}
this.flgValidated = false;
2021-12-29 23:08:41 +00:00
this.store.dispatch(isAuthorized({ payload: sha256(this.passwordFormGroup.controls.password.value).toString() }));
2021-08-28 21:03:18 +00:00
this.rtlEffects.isAuthorizedRes.
pipe(take(1)).
subscribe((authRes) => {
if (authRes !== 'ERROR') {
this.passwordFormGroup.controls.hiddenPassword.setValue(this.passwordFormGroup.controls.password.value);
this.stepper.next();
} else {
this.dialogRef.close();
this.snackBar.open('Unauthorized User. Logging out from RTL.');
}
});
}
2021-08-28 21:03:18 +00:00
onSendFunds(): boolean | void {
this.sendFundError = '';
if (this.flgUseAllBalance) {
this.transaction.satoshi = 'all';
}
if (this.selUTXOs.length && this.selUTXOs.length > 0) {
this.transaction.utxos = [];
this.selUTXOs.forEach((utxo: UTXO) => this.transaction.utxos?.push(utxo.txid + ':' + utxo.output));
}
if (this.sweepAll) {
2021-12-29 23:08:41 +00:00
if (
(!this.sendFundFormGroup.controls.transactionAddress.value || this.sendFundFormGroup.controls.transactionAddress.value === '') ||
(this.sendFundFormGroup.controls.flgMinConf.value && (!this.sendFundFormGroup.controls.minConfValue.value || this.sendFundFormGroup.controls.minConfValue.value <= 0)) ||
(this.selFeeRate === 'customperkb' && !this.flgMinConf && !this.customFeeRate)
) {
return true;
}
this.transaction.satoshi = 'all';
this.transaction.destination = this.sendFundFormGroup.controls.transactionAddress.value;
if (this.sendFundFormGroup.controls.flgMinConf.value) {
delete this.transaction.feerate;
2021-12-29 23:08:41 +00:00
this.transaction.minconf = this.sendFundFormGroup.controls.flgMinConf.value ? this.sendFundFormGroup.controls.minConfValue.value : null;
} else {
delete this.transaction.minconf;
this.transaction.feerate = (this.sendFundFormGroup.controls.selFeeRate.value === 'customperkb' &&
2022-10-31 21:24:41 +00:00
!this.sendFundFormGroup.controls.flgMinConf.value && this.sendFundFormGroup.controls.customFeeRate.value) ?
((this.sendFundFormGroup.controls.customFeeRate.value * 1000) + 'perkb') : this.sendFundFormGroup.controls.selFeeRate.value;
}
delete this.transaction.utxos;
2021-12-29 23:08:41 +00:00
this.store.dispatch(setChannelTransaction({ payload: this.transaction }));
} else {
2021-12-29 23:08:41 +00:00
this.transaction.minconf = this.flgMinConf ? this.minConfValue : null;
this.transaction['feerate'] = (this.selFeeRate === 'customperkb' && !this.flgMinConf && this.customFeeRate) ? (this.customFeeRate * 1000) + 'perkb' : this.selFeeRate !== '' ? this.selFeeRate : null;
if ((!this.transaction.destination || this.transaction.destination === '') ||
((!this.transaction.satoshi || +this.transaction.satoshi <= 0)) ||
2021-12-29 23:08:41 +00:00
(this.flgMinConf && (!this.transaction.minconf || this.transaction.minconf <= 0)) ||
(this.selFeeRate === 'customperkb' && !this.flgMinConf && !this.customFeeRate)) {
return true;
}
if (this.transaction.satoshi && this.transaction.satoshi !== 'all' && this.selAmountUnit !== CurrencyUnitEnum.SATS) {
this.commonService.convertCurrency(+this.transaction.satoshi, this.selAmountUnit === this.amountUnits[2] ? CurrencyUnitEnum.OTHER : this.selAmountUnit, CurrencyUnitEnum.SATS, this.amountUnits[2], this.fiatConversion).
2021-12-29 23:08:41 +00:00
pipe(takeUntil(this.unSubs[5])).
2021-08-28 21:03:18 +00:00
subscribe({
next: (data) => {
this.transaction.satoshi = data[CurrencyUnitEnum.SATS];
2021-08-28 21:03:18 +00:00
this.selAmountUnit = CurrencyUnitEnum.SATS;
2021-12-29 23:08:41 +00:00
this.store.dispatch(setChannelTransaction({ payload: this.transaction }));
2021-08-28 21:03:18 +00:00
}, error: (err) => {
this.transaction.satoshi = null;
2021-08-28 21:03:18 +00:00
this.selAmountUnit = CurrencyUnitEnum.SATS;
this.amountError = 'Conversion Error: ' + err;
}
});
} else {
2021-12-29 23:08:41 +00:00
this.store.dispatch(setChannelTransaction({ payload: this.transaction }));
}
}
}
resetData() {
2021-08-28 21:03:18 +00:00
this.sendFundError = '';
this.transaction = {};
this.flgMinConf = false;
this.totalSelectedUTXOAmount = null;
this.selUTXOs = [];
this.flgUseAllBalance = false;
this.selAmountUnit = CURRENCY_UNITS[0];
}
stepSelectionChanged(event: any) {
this.sendFundError = '';
switch (event.selectedIndex) {
case 0:
this.passwordFormLabel = 'Authenticate with your RTL password';
2021-08-28 21:03:18 +00:00
this.sendFundFormLabel = 'Sweep funds';
break;
2021-08-28 21:03:18 +00:00
case 1:
this.passwordFormLabel = 'User authenticated successfully';
2021-08-28 21:03:18 +00:00
this.sendFundFormLabel = 'Sweep funds';
break;
case 2:
this.passwordFormLabel = 'User authenticated successfully';
2021-08-28 21:03:18 +00:00
this.sendFundFormLabel = 'Sweep funds | Address: ' + this.sendFundFormGroup.controls.transactionAddress.value +
2021-12-29 23:08:41 +00:00
(this.sendFundFormGroup.controls.flgMinConf.value ? (' | Min Confirmation Blocks: ' + this.sendFundFormGroup.controls.minConfValue.value) : (this.sendFundFormGroup.controls.selFeeRate.value ? (' | Fee Rate: ' +
this.feeRateTypes.find((frType) => frType.feeRateId === this.sendFundFormGroup.controls.selFeeRate.value)?.feeRateType) : ''));
break;
default:
this.passwordFormLabel = 'Authenticate with your RTL password';
2021-08-28 21:03:18 +00:00
this.sendFundFormLabel = 'Sweep funds';
break;
}
if (event.selectedIndex < event.previouslySelectedIndex) {
if (event.selectedIndex === 0) {
this.passwordFormGroup.controls.hiddenPassword.setValue('');
}
2021-08-28 21:03:18 +00:00
}
}
onUTXOSelectionChange(event: any) {
if (this.selUTXOs.length && this.selUTXOs.length > 0) {
this.totalSelectedUTXOAmount = this.selUTXOs?.reduce((total, curr) => (total + ((curr.amount_msat || 0) / 1000)), 0);
2021-08-28 21:03:18 +00:00
if (this.flgUseAllBalance) {
this.onUTXOAllBalanceChange();
}
} else {
this.totalSelectedUTXOAmount = null;
this.transaction.satoshi = null;
this.flgUseAllBalance = false;
}
}
onUTXOAllBalanceChange() {
if (this.flgUseAllBalance) {
this.transaction.satoshi = this.totalSelectedUTXOAmount;
this.selAmountUnit = CURRENCY_UNITS[0];
} else {
this.transaction.satoshi = null;
}
}
onAmountUnitChange(event: any) {
2021-08-28 21:03:18 +00:00
const self = this;
const prevSelectedUnit = (this.selAmountUnit === this.amountUnits[2]) ? CurrencyUnitEnum.OTHER : this.selAmountUnit;
2020-01-04 01:06:36 +00:00
let currSelectedUnit = event.value === this.amountUnits[2] ? CurrencyUnitEnum.OTHER : event.value;
if (this.transaction.satoshi && this.selAmountUnit !== event.value) {
this.commonService.convertCurrency(+this.transaction.satoshi, prevSelectedUnit, currSelectedUnit, this.amountUnits[2], this.fiatConversion).
2021-12-29 23:08:41 +00:00
pipe(takeUntil(this.unSubs[6])).
2021-08-28 21:03:18 +00:00
subscribe({
next: (data) => {
this.selAmountUnit = event.value;
self.transaction.satoshi = self.decimalPipe.transform(data[currSelectedUnit], self.currencyUnitFormats[currSelectedUnit])?.replace(/,/g, '');
2021-08-28 21:03:18 +00:00
}, error: (err) => {
self.transaction.satoshi = null;
2021-08-28 21:03:18 +00:00
this.amountError = 'Conversion Error: ' + err;
this.selAmountUnit = prevSelectedUnit;
currSelectedUnit = prevSelectedUnit;
}
});
}
2021-08-28 21:03:18 +00:00
}
onAdvancedPanelToggle(isClosed: boolean) {
if (isClosed) {
this.advancedTitle = (this.selUTXOs.length && this.selUTXOs.length > 0) ? 'Advanced Options | Selected UTXOs: ' + this.selUTXOs.length + ' | Selected UTXO Amount: ' + this.decimalPipe.transform(this.totalSelectedUTXOAmount) + ' Sats' : 'Advanced Options';
} else {
this.advancedTitle = 'Advanced Options';
}
}
ngOnDestroy() {
2021-08-28 21:03:18 +00:00
this.unSubs.forEach((completeSub) => {
completeSub.next(<any>null);
completeSub.complete();
});
}
}