|
| 1 | +import { useAppSelector } from './redux'; |
| 2 | +import { onChainBalanceSelector } from '../store/reselect/wallet'; |
| 3 | +import { blocktankInfoSelector } from '../store/reselect/blocktank'; |
| 4 | +import { blocktankChannelsSizeSelector } from '../store/reselect/lightning'; |
| 5 | +import { fiatToBitcoinUnit } from '../utils/conversion'; |
| 6 | + |
| 7 | +type TTransferValues = { |
| 8 | + maxClientBalance: number; |
| 9 | + defaultLspBalance: number; |
| 10 | + minLspBalance: number; |
| 11 | + maxLspBalance: number; |
| 12 | +}; |
| 13 | + |
| 14 | +const getDefaultLspBalance = ( |
| 15 | + clientBalance: number, |
| 16 | + maxLspBalance: number, |
| 17 | +): number => { |
| 18 | + const threshold1 = fiatToBitcoinUnit({ amount: 225, currency: 'EUR' }); |
| 19 | + const threshold2 = fiatToBitcoinUnit({ amount: 495, currency: 'EUR' }); |
| 20 | + const defaultLspBalance = fiatToBitcoinUnit({ amount: 450, currency: 'EUR' }); |
| 21 | + |
| 22 | + let lspBalance = defaultLspBalance - clientBalance; |
| 23 | + |
| 24 | + if (clientBalance > threshold1) { |
| 25 | + lspBalance = clientBalance; |
| 26 | + } |
| 27 | + |
| 28 | + if (clientBalance > threshold2) { |
| 29 | + lspBalance = maxLspBalance; |
| 30 | + } |
| 31 | + |
| 32 | + return Math.min(lspBalance, maxLspBalance); |
| 33 | +}; |
| 34 | + |
| 35 | +const getMinLspBalance = ( |
| 36 | + clientBalance: number, |
| 37 | + minChannelSize: number, |
| 38 | +): number => { |
| 39 | + // LSP balance must be at least 2% of the channel size for LDK to accept (reserve balance) |
| 40 | + const ldkMinimum = Math.round(clientBalance * 0.02); |
| 41 | + // Channel size must be at least minChannelSize |
| 42 | + const lspMinimum = Math.max(minChannelSize - clientBalance, 0); |
| 43 | + |
| 44 | + return Math.max(ldkMinimum, lspMinimum); |
| 45 | +}; |
| 46 | + |
| 47 | +const getMaxClientBalance = ( |
| 48 | + onchainBalance: number, |
| 49 | + maxChannelSize: number, |
| 50 | +): number => { |
| 51 | + // Remote balance must be at least 2% of the channel size for LDK to accept (reserve balance) |
| 52 | + const minRemoteBalance = Math.round(maxChannelSize * 0.02); |
| 53 | + // Cap client balance to 80% to leave buffer for fees |
| 54 | + const feeMaximum = Math.round(onchainBalance * 0.8); |
| 55 | + const ldkMaximum = maxChannelSize - minRemoteBalance; |
| 56 | + |
| 57 | + return Math.min(feeMaximum, ldkMaximum); |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Returns limits and default values for channel orders with the LSP |
| 62 | + * @param {number} clientBalance |
| 63 | + * @returns {TTransferValues} |
| 64 | + */ |
| 65 | +export const useTransfer = (clientBalance: number): TTransferValues => { |
| 66 | + const blocktankInfo = useAppSelector(blocktankInfoSelector); |
| 67 | + const onchainBalance = useAppSelector(onChainBalanceSelector); |
| 68 | + const channelsSize = useAppSelector(blocktankChannelsSizeSelector); |
| 69 | + |
| 70 | + const { minChannelSizeSat, maxChannelSizeSat } = blocktankInfo.options; |
| 71 | + |
| 72 | + // Because LSP limits constantly change depending on network fees |
| 73 | + // add a 2% buffer to avoid fluctuations while making the order |
| 74 | + const maxChannelSize1 = Math.round(maxChannelSizeSat * 0.98); |
| 75 | + // The maximum channel size the user can open including existing channels |
| 76 | + const maxChannelSize2 = Math.max(0, maxChannelSize1 - channelsSize); |
| 77 | + const maxChannelSize = Math.min(maxChannelSize1, maxChannelSize2); |
| 78 | + |
| 79 | + const minLspBalance = getMinLspBalance(clientBalance, minChannelSizeSat); |
| 80 | + const maxLspBalance = maxChannelSize - clientBalance; |
| 81 | + const defaultLspBalance = getDefaultLspBalance(clientBalance, maxLspBalance); |
| 82 | + const maxClientBalance = getMaxClientBalance(onchainBalance, maxChannelSize); |
| 83 | + |
| 84 | + return { |
| 85 | + defaultLspBalance, |
| 86 | + minLspBalance, |
| 87 | + maxLspBalance, |
| 88 | + maxClientBalance, |
| 89 | + }; |
| 90 | +}; |
0 commit comments