Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swap: calculate required balance including fee #303

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lnd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

// since the max htlc limit is not always set reliably,
// the check is skipped if it is not set.
if maxHtlcAmtMsat == 0 {
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 13 additions & 4 deletions swap/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 0 additions & 8 deletions swap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading