@@ -77,6 +77,10 @@ type loopOutSwap struct {
7777
7878 swapInvoicePaymentAddr [32 ]byte
7979
80+ // prepayAmount holds the amount of the prepay invoice. We use this
81+ // to calculate the total cost of the swap.
82+ prepayAmount btcutil.Amount
83+
8084 swapPaymentChan chan paymentResult
8185 prePaymentChan chan paymentResult
8286
@@ -466,16 +470,20 @@ func (s *loopOutSwap) handlePaymentResult(result paymentResult,
466470 if swapPayment {
467471 // The client pays for the swap with the swap invoice,
468472 // so we can calculate the total cost of the swap by
469- // subtracting the amount requested from the amount we
470- // actually paid.
471- s .cost .Server += result .status .Value .ToSatoshis () -
473+ // subtracting the amount requested from the total
474+ // amount that we actually paid (which is the sum of
475+ // the swap invoice amount and the prepay invoice
476+ // amount).
477+ s .cost .Server += s .prepayAmount +
478+ result .status .Value .ToSatoshis () -
472479 s .AmountRequested
473-
474- // On top of the swap cost we also pay for routing which
475- // is reflected in the fee.
476- s .cost .Offchain += result .status .Fee .ToSatoshis ()
477480 }
478481
482+ // On top of the swap cost we also pay for routing which
483+ // is reflected in the fee. We add the off-chain fee for both
484+ // the swap payment and the prepay.
485+ s .cost .Offchain += result .status .Fee .ToSatoshis ()
486+
479487 return nil
480488
481489 case result .status .State == lnrpc .Payment_FAILED :
@@ -489,6 +497,16 @@ func (s *loopOutSwap) handlePaymentResult(result paymentResult,
489497// executeSwap executes the swap, but returns as soon as the swap outcome is
490498// final. At that point, there may still be pending off-chain payment(s).
491499func (s * loopOutSwap ) executeSwap (globalCtx context.Context ) error {
500+ // Decode the prepay invoice so we can ensure that we account for the
501+ // prepay amount when calculating the final costs of the swap.
502+ _ , _ , _ , amt , err := swap .DecodeInvoice (
503+ s .lnd .ChainParams , s .PrepayInvoice ,
504+ )
505+ if err != nil {
506+ return err
507+ }
508+ s .prepayAmount = amt
509+
492510 // We always pay both invoices (again). This is currently the only way
493511 // to sort of resume payments.
494512 //
0 commit comments