From 769785df7f384adb41ebe041410db60f77fea794 Mon Sep 17 00:00:00 2001 From: bruwbird Date: Thu, 18 Jul 2024 16:39:39 +0900 Subject: [PATCH 1/3] swap: calculate required balance including fee During swap-out, conduct a pre-swap check, including the payment of the fee invoice. --- swap/actions.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/swap/actions.go b/swap/actions.go index a043ea96..c08e5f6b 100644 --- a/swap/actions.go +++ b/swap/actions.go @@ -589,20 +589,29 @@ func (r *PayFeeInvoiceAction) Execute(services *SwapServices, swap *SwapData) Ev } ll := services.lightning - // policy := services.policy + _, msatAmt, _, err := ll.DecodePayreq(swap.SwapOutAgreement.Payreq) if err != nil { return swap.HandleError(err) } + sp, err := ll.SpendableMsat(swap.SwapOutRequest.Scid) if err != nil { return swap.HandleError(err) } - if sp <= swap.SwapOutRequest.Amount*1000 { - return swap.HandleError(err) + // Calculate the total required balance + // This includes the swap amount (multiplied by 1000 to convert from sat to msat) + // plus the fee in millisatoshis + requiredBalance := swap.SwapOutRequest.Amount*1000 + msatAmt + + // Check if the spendable balance (sp) is sufficient + if sp <= requiredBalance { + return swap.HandleError(fmt.Errorf("not enough spendable msat: %d, expected: %d", sp, requiredBalance)) } - success, failureReason, err := ll.ProbePayment(swap.SwapOutRequest.Scid, swap.SwapOutRequest.Amount*1000) + + // Probe the payment to check if it's possible + success, failureReason, err := ll.ProbePayment(swap.SwapOutRequest.Scid, requiredBalance) if err != nil { return swap.HandleError(err) } From 144f9d15aa9bdb46689c62127b1ab735bf9ca706 Mon Sep 17 00:00:00 2001 From: bruwbird Date: Fri, 19 Jul 2024 10:27:03 +0900 Subject: [PATCH 2/3] swap: remove redundant prepayment probe The prepayment probe, which includes the expected payment amount for the fee invoice, is conducted at the start of the swapout. The prepayment probe at this stage is inaccurate and may cause unnecessary communication, so it will be removed. --- swap/service.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/swap/service.go b/swap/service.go index a5c55fdf..7f6ac99b 100644 --- a/swap/service.go +++ b/swap/service.go @@ -391,14 +391,6 @@ func (s *SwapService) SwapOut(peer string, chain string, channelId string, initi return nil, fmt.Errorf("exceeding spendable amount_msat: %d", sp) } - success, failureReason, err := s.swapServices.lightning.ProbePayment(channelId, amtSat*1000) - if err != nil { - return nil, err - } - if !success { - return nil, fmt.Errorf("the prepayment probe was unsuccessful: %s", failureReason) - } - swap := newSwapOutSenderFSM(s.swapServices, initiator, peer) err = s.lockSwap(swap.SwapId.String(), channelId, swap) if err != nil { From 76fa0cbd4ec8409acf087421f233058d370492b4 Mon Sep 17 00:00:00 2001 From: bruwbird Date: Fri, 19 Jul 2024 10:29:11 +0900 Subject: [PATCH 3/3] lnd: correct SpendableMsat and ReceivableMsat calc Fixes the calculation of the SpendableMsat and ReceivableMsat. --- lnd/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lnd/client.go b/lnd/client.go index b360d14e..18c50007 100644 --- a/lnd/client.go +++ b/lnd/client.go @@ -131,7 +131,7 @@ func (l *Client) SpendableMsat(scid string) (uint64, error) { return 0, err } spendable := (uint64(ch.GetLocalBalance()) - - ch.GetLocalConstraints().GetChanReserveSat()*1000) + ch.GetLocalConstraints().GetChanReserveSat()) * 1000 // since the max htlc limit is not always set reliably, // the check is skipped if it is not set. if maxHtlcAmtMsat == 0 { @@ -168,7 +168,7 @@ func (l *Client) ReceivableMsat(scid string) (uint64, error) { return 0, err } receivable := (uint64(ch.GetRemoteBalance()) - - ch.GetRemoteConstraints().GetChanReserveSat()*1000) + ch.GetRemoteConstraints().GetChanReserveSat()) * 1000 // since the max htlc limit is not always set reliably, // the check is skipped if it is not set. if maxHtlcAmtMsat == 0 {