@@ -402,7 +402,7 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
402402 case result := <- s .swapPaymentChan :
403403 s .swapPaymentChan = nil
404404
405- err := s .handlePaymentResult (result )
405+ err := s .handlePaymentResult (result , true )
406406 if err != nil {
407407 return err
408408 }
@@ -418,7 +418,7 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
418418 case result := <- s .prePaymentChan :
419419 s .prePaymentChan = nil
420420
421- err := s .handlePaymentResult (result )
421+ err := s .handlePaymentResult (result , false )
422422 if err != nil {
423423 return err
424424 }
@@ -448,17 +448,32 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
448448 return s .persistState (globalCtx )
449449}
450450
451- func (s * loopOutSwap ) handlePaymentResult (result paymentResult ) error {
451+ // handlePaymentResult processes the result of a payment attempt. If the
452+ // payment was successful and this is the main swap payment, the cost of the
453+ // swap is updated.
454+ func (s * loopOutSwap ) handlePaymentResult (result paymentResult ,
455+ swapPayment bool ) error {
456+
452457 switch {
453458 // If our result has a non-nil error, our status will be nil. In this
454459 // case the payment failed so we do not need to take any action.
455460 case result .err != nil :
456461 return nil
457462
458463 case result .status .State == lnrpc .Payment_SUCCEEDED :
459- s .cost .Server += result .status .Value .ToSatoshis () -
460- s .AmountRequested
461- s .cost .Offchain += result .status .Fee .ToSatoshis ()
464+ // Update the cost of the swap if this is the main swap payment.
465+ if swapPayment {
466+ // The client pays for the swap with the swap invoice,
467+ // so we can calculate the total cost of the swap by
468+ // subtracting the amount requested from the amount we
469+ // actually paid.
470+ s .cost .Server += result .status .Value .ToSatoshis () -
471+ s .AmountRequested
472+
473+ // On top of the swap cost we also pay for routing which
474+ // is reflected in the fee.
475+ s .cost .Offchain += result .status .Fee .ToSatoshis ()
476+ }
462477
463478 return nil
464479
@@ -917,7 +932,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
917932 case result := <- s .swapPaymentChan :
918933 s .swapPaymentChan = nil
919934
920- err := s .handlePaymentResult (result )
935+ err := s .handlePaymentResult (result , true )
921936 if err != nil {
922937 return nil , err
923938 }
@@ -939,7 +954,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
939954 case result := <- s .prePaymentChan :
940955 s .prePaymentChan = nil
941956
942- err := s .handlePaymentResult (result )
957+ err := s .handlePaymentResult (result , false )
943958 if err != nil {
944959 return nil , err
945960 }
0 commit comments