Skip to content

Commit

Permalink
swap: calculate required balance including fee
Browse files Browse the repository at this point in the history
During swap-out, conduct a pre-swap check,
including the payment of the fee invoice.
  • Loading branch information
YusukeShimizu committed Jul 18, 2024
1 parent c77a829 commit 769785d
Showing 1 changed file with 13 additions and 4 deletions.
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

0 comments on commit 769785d

Please sign in to comment.