@@ -402,7 +402,7 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
402
402
case result := <- s .swapPaymentChan :
403
403
s .swapPaymentChan = nil
404
404
405
- err := s .handlePaymentResult (result )
405
+ err := s .handlePaymentResult (result , true )
406
406
if err != nil {
407
407
return err
408
408
}
@@ -418,7 +418,7 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
418
418
case result := <- s .prePaymentChan :
419
419
s .prePaymentChan = nil
420
420
421
- err := s .handlePaymentResult (result )
421
+ err := s .handlePaymentResult (result , false )
422
422
if err != nil {
423
423
return err
424
424
}
@@ -448,17 +448,32 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
448
448
return s .persistState (globalCtx )
449
449
}
450
450
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
+
452
457
switch {
453
458
// If our result has a non-nil error, our status will be nil. In this
454
459
// case the payment failed so we do not need to take any action.
455
460
case result .err != nil :
456
461
return nil
457
462
458
463
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
+ }
462
477
463
478
return nil
464
479
@@ -917,7 +932,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
917
932
case result := <- s .swapPaymentChan :
918
933
s .swapPaymentChan = nil
919
934
920
- err := s .handlePaymentResult (result )
935
+ err := s .handlePaymentResult (result , true )
921
936
if err != nil {
922
937
return nil , err
923
938
}
@@ -939,7 +954,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
939
954
case result := <- s .prePaymentChan :
940
955
s .prePaymentChan = nil
941
956
942
- err := s .handlePaymentResult (result )
957
+ err := s .handlePaymentResult (result , false )
943
958
if err != nil {
944
959
return nil , err
945
960
}
0 commit comments