44 "context"
55 "crypto/rand"
66 "crypto/sha256"
7+ "encoding/json"
78 "errors"
89 "fmt"
910 "math"
@@ -355,6 +356,10 @@ func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
355356 info .OutgoingChanSet = outgoingChanSet
356357 }
357358
359+ if s .AssetSwapInfo != nil {
360+ info .AssetSwapInfo = s .AssetSwapInfo
361+ }
362+
358363 select {
359364 case s .statusChan <- * info :
360365 case <- ctx .Done ():
@@ -436,7 +441,7 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
436441 case result := <- s .swapPaymentChan :
437442 s .swapPaymentChan = nil
438443
439- err := s .handlePaymentResult (result , true )
444+ err := s .handlePaymentResult (globalCtx , result , true )
440445 if err != nil {
441446 return err
442447 }
@@ -452,7 +457,7 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
452457 case result := <- s .prePaymentChan :
453458 s .prePaymentChan = nil
454459
455- err := s .handlePaymentResult (result , false )
460+ err := s .handlePaymentResult (globalCtx , result , false )
456461 if err != nil {
457462 return err
458463 }
@@ -485,8 +490,8 @@ func (s *loopOutSwap) executeAndFinalize(globalCtx context.Context) error {
485490// handlePaymentResult processes the result of a payment attempt. If the
486491// payment was successful and this is the main swap payment, the cost of the
487492// 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 {
490495
491496 switch {
492497 // 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,
513518 // the swap payment and the prepay.
514519 s .cost .Offchain += result .status .Fee .ToSatoshis ()
515520
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+
516532 return nil
517533
518534 case result .status .State == lnrpc .Payment_FAILED :
@@ -675,7 +691,7 @@ func (s *loopOutSwap) payInvoices(ctx context.Context) {
675691 if s .AssetSwapInfo != nil {
676692 assetPrepayRfq = s .AssetSwapInfo .PrepayRfqId
677693 }
678-
694+
679695 s .prePaymentChan = s .payInvoice (
680696 ctx , s .PrepayInvoice , s .MaxPrepayRoutingFee ,
681697 s .LoopOutContract .OutgoingChanSet ,
@@ -1031,7 +1047,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
10311047 case result := <- s .swapPaymentChan :
10321048 s .swapPaymentChan = nil
10331049
1034- err := s .handlePaymentResult (result , true )
1050+ err := s .handlePaymentResult (ctx , result , true )
10351051 if err != nil {
10361052 return nil , err
10371053 }
@@ -1053,7 +1069,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
10531069 case result := <- s .prePaymentChan :
10541070 s .prePaymentChan = nil
10551071
1056- err := s .handlePaymentResult (result , false )
1072+ err := s .handlePaymentResult (ctx , result , false )
10571073 if err != nil {
10581074 return nil , err
10591075 }
@@ -1453,3 +1469,31 @@ func (s *loopOutSwap) canSweep() bool {
14531469
14541470 return true
14551471}
1472+
1473+ func (s * loopOutSwap ) fillAssetOffchainPaymentResult (ctx context.Context ,
1474+ result paymentResult , isSwapInvoice bool ) error {
1475+
1476+ if len (result .status .Htlcs ) == 0 {
1477+ return fmt .Errorf ("no htlcs in payment result" )
1478+ }
1479+
1480+ // We only expect one htlc in the result.
1481+ htlc := result .status .Htlcs [0 ]
1482+
1483+ var assetData rfqmsg.JsonHtlc
1484+
1485+ err := json .Unmarshal (htlc .Route .CustomChannelData , & assetData )
1486+ if err != nil {
1487+ return err
1488+ }
1489+
1490+ assetSendAmt := btcutil .Amount (assetData .Balances [0 ].Amount )
1491+ if isSwapInvoice {
1492+ s .AssetSwapInfo .SwapPaidAmt = assetSendAmt
1493+ log .Debugf ("Asset off-chain payment success: %v" , assetSendAmt )
1494+ } else {
1495+ s .AssetSwapInfo .PrepayPaidAmt = assetSendAmt
1496+ }
1497+
1498+ return s .store .UpdateLoopOutAssetInfo (ctx , s .hash , s .AssetSwapInfo )
1499+ }
0 commit comments