Skip to content

Commit

Permalink
fix(lightning): Fix Custom Channel Open Flow Calculations
Browse files Browse the repository at this point in the history
Updates receiving package calculations.
  • Loading branch information
coreyphillips committed Aug 31, 2023
1 parent 0eeed2f commit 0cd61f5
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/screens/Lightning/CustomSetup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const PACKAGES_SPENDING: Omit<TPackage, 'satoshis'>[] = [
{
id: 'big',
img: require('../../assets/illustrations/coin-stack-3.png'),
fiatAmount: 480,
fiatAmount: 450,
},
];

Expand All @@ -90,7 +90,7 @@ const PACKAGES_RECEIVING: Omit<TPackage, 'satoshis'>[] = [
{
id: 'big',
img: require('../../assets/illustrations/coin-stack-3.png'),
fiatAmount: 989,
fiatAmount: 999,
},
];

Expand Down Expand Up @@ -179,34 +179,39 @@ const CustomSetup = ({
let maxReceiving = maxChannelSizeSat;
let minReceiving = spendingAmount! ?? 0;
let availReceivingPackages: TPackage[] = [];
PACKAGES_RECEIVING.reverse().forEach((p) => {
PACKAGES_RECEIVING.forEach((p) => {
const maxChannelSizeFiat = getFiatDisplayValues({
// Ensure the total channel size (sending & receiving) remains below the maxChannelSizeSat.
satoshis: maxReceiving,
});

const delta = Math.abs(maxReceiving - minReceiving);
let packageFiatAmount = p.fiatAmount;

// Ensure the fiatAmount is within the range of minReceiving and maxChannelSizeFiat.fiatValue
if (packageFiatAmount >= maxChannelSizeFiat.fiatValue) {
packageFiatAmount = maxChannelSizeFiat.fiatValue;
} else if (packageFiatAmount < minReceiving) {
packageFiatAmount = minReceiving;
}

const convertedAmount = convertCurrency({
amount:
p.fiatAmount < maxChannelSizeFiat.fiatValue!
? maxChannelSizeFiat.fiatValue!
: p.fiatAmount,
amount: packageFiatAmount,
from: 'USD',
to: selectedCurrency,
});

const satoshis = convertToSats(convertedAmount.fiatValue, EUnit.fiat);
// Ensure the conversion still puts us below the max_chan_receiving
const satoshisCapped = Math.min(satoshis, maxReceiving);

maxReceiving = Number((maxReceiving - delta / 2).toFixed(0));
if (maxReceiving < minReceiving) {
maxReceiving = Number((minReceiving + delta).toFixed(0));
}
availReceivingPackages.push({
...p,
fiatAmount: convertedAmount.fiatValue,
satoshis: satoshisCapped,
});
});
setReceivingPackages(availReceivingPackages.reverse());
availReceivingPackages.sort((a, b) => a.satoshis - b.satoshis);
setReceivingPackages(availReceivingPackages);
}, [maxChannelSizeSat, onchainFiatBalance, selectedCurrency, spendingAmount]);

// set initial spending/receiving amount
Expand Down

0 comments on commit 0cd61f5

Please sign in to comment.