@@ -294,91 +294,52 @@ class TransferViewModel: ObservableObject {
294294 selectedChannelIds = [ ]
295295 }
296296
297- // MARK: - Balance Calculation Functions
297+ // MARK: - Balance Calculation
298+
299+ /// Calculates channel liquidity options using bitkit-core
300+ func calculateTransferValues( clientBalanceSat: UInt64 , blocktankInfo: IBtInfo ? ) -> TransferValues {
301+ guard let blocktankInfo else {
302+ return TransferValues ( )
303+ }
298304
299- func getDefaultLspBalance( clientBalanceSat: UInt64 , maxLspBalance: UInt64 ) -> UInt64 {
300- // Get current rates
301305 guard let rates = currencyService. loadCachedRates ( ) ,
302306 let eurRate = currencyService. getCurrentRate ( for: " EUR " , from: rates)
303307 else {
304- Logger . error ( " Failed to get rates for getDefaultLspBalance " , context: " TransferViewModel " )
305- return 0
308+ Logger . error ( " Failed to get rates for calculateTransferValues " , context: " TransferViewModel " )
309+ return TransferValues ( )
306310 }
307311
308- // Calculate thresholds in sats
309- let threshold1 = currencyService. convertFiatToSats ( fiatValue: 225 , rate: eurRate)
310- let threshold2 = currencyService. convertFiatToSats ( fiatValue: 495 , rate: eurRate)
311- let defaultLspBalanceSats = currencyService. convertFiatToSats ( fiatValue: 450 , rate: eurRate)
312-
313- var lspBalance = Int64 ( defaultLspBalanceSats) - Int64( clientBalanceSat)
314-
315- // Ensure non-negative result
316- if lspBalance < 0 {
317- lspBalance = 0
318- }
312+ let satsPerEur = currencyService. convertFiatToSats ( fiatValue: 1 , rate: eurRate)
313+ let existingChannelsTotalSat = totalBtChannelsValueSats ( blocktankInfo: blocktankInfo)
319314
320- if clientBalanceSat > threshold1 {
321- lspBalance = Int64 ( clientBalanceSat)
322- }
315+ let params = ChannelLiquidityParams (
316+ clientBalanceSat: clientBalanceSat,
317+ existingChannelsTotalSat: existingChannelsTotalSat,
318+ minChannelSizeSat: blocktankInfo. options. minChannelSizeSat,
319+ maxChannelSizeSat: blocktankInfo. options. maxChannelSizeSat,
320+ satsPerEur: satsPerEur
321+ )
323322
324- if clientBalanceSat > threshold2 {
325- lspBalance = Int64 ( maxLspBalance)
326- }
323+ let options = BitkitCore . calculateChannelLiquidityOptions ( params: params)
327324
328- return min ( UInt64 ( lspBalance) , maxLspBalance)
325+ return TransferValues (
326+ defaultLspBalance: options. defaultLspBalanceSat,
327+ minLspBalance: options. minLspBalanceSat,
328+ maxLspBalance: options. maxLspBalanceSat,
329+ maxClientBalance: options. maxClientBalanceSat
330+ )
329331 }
330332
331- func getMinLspBalance( clientBalance: UInt64 , minChannelSize: UInt64 ) -> UInt64 {
332- // LSP balance must be at least 2.5% of the channel size for LDK to accept (reserve balance)
333- let ldkMinimum = UInt64 ( Double ( clientBalance) * 0.025 )
334- // Channel size must be at least minChannelSize
335- let lspMinimum = clientBalance < minChannelSize ? minChannelSize - clientBalance : 0
336-
337- return max ( ldkMinimum, lspMinimum)
333+ func updateTransferValues( clientBalanceSat: UInt64 , blocktankInfo: IBtInfo ? ) {
334+ transferValues = calculateTransferValues ( clientBalanceSat: clientBalanceSat, blocktankInfo: blocktankInfo)
338335 }
339336
337+ /// Calculates max client balance accounting for LDK reserve requirement
340338 func getMaxClientBalance( maxChannelSize: UInt64 ) -> UInt64 {
341- // Remote balance must be at least 2.5% of the channel size for LDK to accept (reserve balance)
342339 let minRemoteBalance = UInt64 ( Double ( maxChannelSize) * 0.025 )
343340 return maxChannelSize - minRemoteBalance
344341 }
345342
346- func updateTransferValues( clientBalanceSat: UInt64 , blocktankInfo: IBtInfo ? ) {
347- transferValues = calculateTransferValues ( clientBalanceSat: clientBalanceSat, blocktankInfo: blocktankInfo)
348- }
349-
350- func calculateTransferValues( clientBalanceSat: UInt64 , blocktankInfo: IBtInfo ? ) -> TransferValues {
351- guard let blocktankInfo else {
352- return TransferValues ( )
353- }
354-
355- // Calculate the total value of existing Blocktank channels
356- let channelsSize = totalBtChannelsValueSats ( blocktankInfo: blocktankInfo)
357-
358- let minChannelSizeSat = UInt64 ( blocktankInfo. options. minChannelSizeSat)
359- let maxChannelSizeSat = UInt64 ( blocktankInfo. options. maxChannelSizeSat)
360-
361- // Because LSP limits constantly change depending on network fees
362- // Add a 2% buffer to avoid fluctuations while making the order
363- let maxChannelSize1 = UInt64 ( Double ( maxChannelSizeSat) * 0.98 )
364-
365- // The maximum channel size the user can open including existing channels
366- let maxChannelSize2 = maxChannelSize1 > channelsSize ? maxChannelSize1 - channelsSize : 0
367- let maxChannelSize = min ( maxChannelSize1, maxChannelSize2)
368-
369- let minLspBalance = getMinLspBalance ( clientBalance: clientBalanceSat, minChannelSize: minChannelSizeSat)
370- let maxLspBalance = maxChannelSize > clientBalanceSat ? maxChannelSize - clientBalanceSat : 0
371- let defaultLspBalance = getDefaultLspBalance ( clientBalanceSat: clientBalanceSat, maxLspBalance: maxLspBalance)
372- let maxClientBalance = getMaxClientBalance ( maxChannelSize: maxChannelSize)
373-
374- return TransferValues (
375- defaultLspBalance: defaultLspBalance,
376- minLspBalance: minLspBalance,
377- maxLspBalance: maxLspBalance,
378- maxClientBalance: maxClientBalance
379- )
380- }
381-
382343 // MARK: - Manual Channel Opening
383344
384345 /// Opens a manual channel and tracks the transfer
0 commit comments