4
4
"context"
5
5
"crypto/rand"
6
6
"crypto/sha256"
7
+ "encoding/json"
7
8
"errors"
8
9
"fmt"
9
10
"math"
@@ -355,6 +356,10 @@ func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
355
356
info .OutgoingChanSet = outgoingChanSet
356
357
}
357
358
359
+ if s .AssetSwapInfo != nil {
360
+ info .AssetSwapInfo = s .AssetSwapInfo
361
+ }
362
+
358
363
select {
359
364
case s .statusChan <- * info :
360
365
case <- ctx .Done ():
@@ -436,7 +441,7 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
436
441
case result := <- s .swapPaymentChan :
437
442
s .swapPaymentChan = nil
438
443
439
- err := s .handlePaymentResult (result , true )
444
+ err := s .handlePaymentResult (globalCtx , result , true )
440
445
if err != nil {
441
446
return err
442
447
}
@@ -452,7 +457,7 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
452
457
case result := <- s .prePaymentChan :
453
458
s .prePaymentChan = nil
454
459
455
- err := s .handlePaymentResult (result , false )
460
+ err := s .handlePaymentResult (globalCtx , result , false )
456
461
if err != nil {
457
462
return err
458
463
}
@@ -485,8 +490,8 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
485
490
// handlePaymentResult processes the result of a payment attempt. If the
486
491
// payment was successful and this is the main swap payment, the cost of the
487
492
// swap is updated.
488
- func (s * loopOutSwap ) handlePaymentResult (result paymentResult ,
489
- swapPayment bool ) error {
493
+ func (s * loopOutSwap ) handlePaymentResult (ctx context. Context ,
494
+ result paymentResult , swapPayment bool ) error {
490
495
491
496
switch {
492
497
// If our result has a non-nil error, our status will be nil. In this
@@ -513,6 +518,17 @@ func (s *loopOutSwap) handlePaymentResult(result paymentResult,
513
518
// the swap payment and the prepay.
514
519
s .cost .Offchain += result .status .Fee .ToSatoshis ()
515
520
521
+ // If this is an asset payment, we'll write the asset amounts
522
+ // to the swap.
523
+ if s .AssetSwapInfo != nil {
524
+ err := s .fillAssetOffchainPaymentResult (
525
+ ctx , result , swapPayment ,
526
+ )
527
+ if err != nil {
528
+ return err
529
+ }
530
+ }
531
+
516
532
return nil
517
533
518
534
case result .status .State == lnrpc .Payment_FAILED :
@@ -675,7 +691,7 @@ func (s *loopOutSwap) payInvoices(ctx context.Context) {
675
691
if s .AssetSwapInfo != nil {
676
692
assetPrepayRfq = s .AssetSwapInfo .PrepayRfqId
677
693
}
678
-
694
+
679
695
s .prePaymentChan = s .payInvoice (
680
696
ctx , s .PrepayInvoice , s .MaxPrepayRoutingFee ,
681
697
s .LoopOutContract .OutgoingChanSet ,
@@ -1033,7 +1049,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
1033
1049
case result := <- s .swapPaymentChan :
1034
1050
s .swapPaymentChan = nil
1035
1051
1036
- err := s .handlePaymentResult (result , true )
1052
+ err := s .handlePaymentResult (ctx , result , true )
1037
1053
if err != nil {
1038
1054
return nil , err
1039
1055
}
@@ -1055,7 +1071,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
1055
1071
case result := <- s .prePaymentChan :
1056
1072
s .prePaymentChan = nil
1057
1073
1058
- err := s .handlePaymentResult (result , false )
1074
+ err := s .handlePaymentResult (ctx , result , false )
1059
1075
if err != nil {
1060
1076
return nil , err
1061
1077
}
@@ -1455,3 +1471,31 @@ func (s *loopOutSwap) canSweep() bool {
1455
1471
1456
1472
return true
1457
1473
}
1474
+
1475
+ func (s * loopOutSwap ) fillAssetOffchainPaymentResult (ctx context.Context ,
1476
+ result paymentResult , isSwapInvoice bool ) error {
1477
+
1478
+ if len (result .status .Htlcs ) == 0 {
1479
+ return fmt .Errorf ("no htlcs in payment result" )
1480
+ }
1481
+
1482
+ // We only expect one htlc in the result.
1483
+ htlc := result .status .Htlcs [0 ]
1484
+
1485
+ var assetData rfqmsg.JsonHtlc
1486
+
1487
+ err := json .Unmarshal (htlc .Route .CustomChannelData , & assetData )
1488
+ if err != nil {
1489
+ return err
1490
+ }
1491
+
1492
+ assetSendAmt := btcutil .Amount (assetData .Balances [0 ].Amount )
1493
+ if isSwapInvoice {
1494
+ s .AssetSwapInfo .SwapPaidAmt = assetSendAmt
1495
+ log .Debugf ("Asset off-chain payment success: %v" , assetSendAmt )
1496
+ } else {
1497
+ s .AssetSwapInfo .PrepayPaidAmt = assetSendAmt
1498
+ }
1499
+
1500
+ return s .store .UpdateLoopOutAssetInfo (ctx , s .hash , s .AssetSwapInfo )
1501
+ }
0 commit comments