2022-11-10 18:32:18 -06:00
|
|
|
import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-channel-close-box',
|
|
|
|
templateUrl: './channel-close-box.component.html',
|
|
|
|
styleUrls: ['./channel-close-box.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
})
|
|
|
|
export class ChannelCloseBoxComponent implements OnChanges {
|
|
|
|
@Input() channel: any;
|
2023-07-23 17:45:01 +09:00
|
|
|
@Input() left: any;
|
|
|
|
@Input() right: any;
|
2022-11-10 18:32:18 -06:00
|
|
|
|
|
|
|
showStartingBalance: boolean = false;
|
|
|
|
showClosingBalance: boolean = false;
|
|
|
|
minStartingBalance: number;
|
|
|
|
maxStartingBalance: number;
|
|
|
|
minClosingBalance: number;
|
|
|
|
maxClosingBalance: number;
|
|
|
|
|
2023-07-23 17:45:01 +09:00
|
|
|
startingBalanceStyle: {
|
|
|
|
left: string,
|
|
|
|
center: string,
|
|
|
|
right: string,
|
|
|
|
} = {
|
|
|
|
left: '',
|
|
|
|
center: '',
|
|
|
|
right: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
closingBalanceStyle: {
|
|
|
|
left: string,
|
|
|
|
center: string,
|
|
|
|
right: string,
|
|
|
|
} = {
|
|
|
|
left: '',
|
|
|
|
center: '',
|
|
|
|
right: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
hideStartingLeft: boolean = false;
|
|
|
|
hideStartingRight: boolean = false;
|
|
|
|
hideClosingLeft: boolean = false;
|
|
|
|
hideClosingRight: boolean = false;
|
|
|
|
|
2022-11-10 18:32:18 -06:00
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
2023-07-23 17:45:01 +09:00
|
|
|
let closingCapacity;
|
|
|
|
if (this.channel && this.left && this.right) {
|
|
|
|
this.showStartingBalance = (this.left.funding_balance || this.right.funding_balance) && this.channel.funding_ratio;
|
|
|
|
this.showClosingBalance = this.left.closing_balance || this.right.closing_balance;
|
2022-11-10 18:32:18 -06:00
|
|
|
|
|
|
|
if (this.channel.single_funded) {
|
2023-07-23 17:45:01 +09:00
|
|
|
if (this.left.funding_balance) {
|
2022-11-10 18:32:18 -06:00
|
|
|
this.minStartingBalance = this.channel.capacity;
|
|
|
|
this.maxStartingBalance = this.channel.capacity;
|
2023-07-23 17:45:01 +09:00
|
|
|
} else if (this.right.funding_balance) {
|
2022-11-10 18:32:18 -06:00
|
|
|
this.minStartingBalance = 0;
|
|
|
|
this.maxStartingBalance = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-23 17:45:01 +09:00
|
|
|
this.minStartingBalance = clampRound(0, this.channel.capacity, this.left.funding_balance * this.channel.funding_ratio);
|
|
|
|
this.maxStartingBalance = clampRound(0, this.channel.capacity, this.channel.capacity - (this.right.funding_balance * this.channel.funding_ratio));
|
2022-11-10 18:32:18 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 17:45:01 +09:00
|
|
|
closingCapacity = this.channel.capacity - this.channel.closing_fee;
|
|
|
|
this.minClosingBalance = clampRound(0, closingCapacity, this.left.closing_balance);
|
|
|
|
this.maxClosingBalance = clampRound(0, closingCapacity, closingCapacity - this.right.closing_balance);
|
2022-11-10 18:32:18 -06:00
|
|
|
|
|
|
|
// margin of error to account for 2 x 330 sat anchor outputs
|
|
|
|
if (Math.abs(this.minClosingBalance - this.maxClosingBalance) <= 660) {
|
|
|
|
this.maxClosingBalance = this.minClosingBalance;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.showStartingBalance = false;
|
|
|
|
this.showClosingBalance = false;
|
|
|
|
}
|
2023-07-23 17:45:01 +09:00
|
|
|
|
|
|
|
const startingMinPc = (this.minStartingBalance / this.channel.capacity) * 100;
|
|
|
|
const startingMaxPc = (this.maxStartingBalance / this.channel.capacity) * 100;
|
|
|
|
this.startingBalanceStyle = {
|
|
|
|
left: `left: 0%; right: ${100 - startingMinPc}%;`,
|
|
|
|
center: `left: ${startingMinPc}%; right: ${100 -startingMaxPc}%;`,
|
|
|
|
right: `left: ${startingMaxPc}%; right: 0%;`,
|
|
|
|
};
|
|
|
|
this.hideStartingLeft = startingMinPc < 15;
|
|
|
|
this.hideStartingRight = startingMaxPc > 85;
|
|
|
|
|
|
|
|
const closingMinPc = (this.minClosingBalance / closingCapacity) * 100;
|
|
|
|
const closingMaxPc = (this.maxClosingBalance / closingCapacity) * 100;
|
|
|
|
this.closingBalanceStyle = {
|
|
|
|
left: `left: 0%; right: ${100 - closingMinPc}%;`,
|
|
|
|
center: `left: ${closingMinPc}%; right: ${100 - closingMaxPc}%;`,
|
|
|
|
right: `left: ${closingMaxPc}%; right: 0%;`,
|
|
|
|
};
|
|
|
|
this.hideClosingLeft = closingMinPc < 15;
|
|
|
|
this.hideClosingRight = closingMaxPc > 85;
|
2022-11-10 18:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clampRound(min: number, max: number, value: number): number {
|
|
|
|
return Math.max(0, Math.min(max, Math.round(value)));
|
|
|
|
}
|