Skip to content

Commit

Permalink
Merge pull request #303 from ElementsProject/pre-swapcheck-fee
Browse files Browse the repository at this point in the history
swap: calculate required balance including fee
  • Loading branch information
grubles authored Jul 26, 2024
2 parents c77a829 + 76fa0cb commit e825b73
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
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
// 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

0 comments on commit e825b73

Please sign in to comment.