@@ -25,6 +25,7 @@ import (
25
25
"github.com/lightningnetwork/lnd/record"
26
26
"github.com/lightningnetwork/lnd/routing/route"
27
27
"github.com/urfave/cli"
28
+ "google.golang.org/grpc"
28
29
)
29
30
30
31
const (
@@ -319,6 +320,9 @@ func SendPayment(ctx *cli.Context) error {
319
320
return nil
320
321
}
321
322
323
+ conn := getClientConn (ctx , false )
324
+ defer conn .Close ()
325
+
322
326
args := ctx .Args ()
323
327
324
328
// If a payment request was provided, we can exit early since all of the
@@ -343,7 +347,9 @@ func SendPayment(ctx *cli.Context) error {
343
347
344
348
req .PaymentAddr = payAddr
345
349
346
- return SendPaymentRequest (ctx , req )
350
+ return SendPaymentRequest (
351
+ ctx , req , conn , conn , routerRPCSendPayment ,
352
+ )
347
353
}
348
354
349
355
var (
@@ -451,19 +457,29 @@ func SendPayment(ctx *cli.Context) error {
451
457
452
458
req .PaymentAddr = payAddr
453
459
454
- return SendPaymentRequest (ctx , req )
460
+ return SendPaymentRequest (ctx , req , conn , conn , routerRPCSendPayment )
455
461
}
456
462
457
- func SendPaymentRequest (ctx * cli.Context ,
458
- req * routerrpc.SendPaymentRequest ) error {
463
+ // SendPaymentFn is a function type that abstracts the SendPaymentV2 call of the
464
+ // router client.
465
+ type SendPaymentFn func (ctx context.Context , payConn grpc.ClientConnInterface ,
466
+ req * routerrpc.SendPaymentRequest ) (PaymentResultStream , error )
459
467
460
- ctxc := getContext ()
468
+ // routerRPCSendPayment is the default implementation of the SendPaymentFn type
469
+ // that uses the lnd routerrpc.SendPaymentV2 call.
470
+ func routerRPCSendPayment (ctx context.Context , payConn grpc.ClientConnInterface ,
471
+ req * routerrpc.SendPaymentRequest ) (PaymentResultStream , error ) {
461
472
462
- conn := getClientConn ( ctx , false )
463
- defer conn . Close ()
473
+ return routerrpc . NewRouterClient ( payConn ). SendPaymentV2 ( ctx , req )
474
+ }
464
475
465
- client := lnrpc .NewLightningClient (conn )
466
- routerClient := routerrpc .NewRouterClient (conn )
476
+ func SendPaymentRequest (ctx * cli.Context , req * routerrpc.SendPaymentRequest ,
477
+ lnConn , paymentConn grpc.ClientConnInterface ,
478
+ callSendPayment SendPaymentFn ) error {
479
+
480
+ ctxc := getContext ()
481
+
482
+ lnClient := lnrpc .NewLightningClient (lnConn )
467
483
468
484
outChan := ctx .Int64Slice ("outgoing_chan_id" )
469
485
if len (outChan ) != 0 {
@@ -543,7 +559,7 @@ func SendPaymentRequest(ctx *cli.Context,
543
559
if req .PaymentRequest != "" {
544
560
// Decode payment request to find out the amount.
545
561
decodeReq := & lnrpc.PayReqString {PayReq : req .PaymentRequest }
546
- decodeResp , err := client .DecodePayReq (ctxc , decodeReq )
562
+ decodeResp , err := lnClient .DecodePayReq (ctxc , decodeReq )
547
563
if err != nil {
548
564
return err
549
565
}
@@ -587,14 +603,12 @@ func SendPaymentRequest(ctx *cli.Context,
587
603
printJSON := ctx .Bool (jsonFlag .Name )
588
604
req .NoInflightUpdates = ! ctx .Bool (inflightUpdatesFlag .Name ) && printJSON
589
605
590
- stream , err := routerClient . SendPaymentV2 (ctxc , req )
606
+ stream , err := callSendPayment (ctxc , paymentConn , req )
591
607
if err != nil {
592
608
return err
593
609
}
594
610
595
- finalState , err := PrintLivePayment (
596
- ctxc , stream , client , printJSON ,
597
- )
611
+ finalState , err := PrintLivePayment (ctxc , stream , lnClient , printJSON )
598
612
if err != nil {
599
613
return err
600
614
}
@@ -656,20 +670,25 @@ func trackPayment(ctx *cli.Context) error {
656
670
return err
657
671
}
658
672
673
+ // PaymentResultStream is an interface that abstracts the Recv method of the
674
+ // SendPaymentV2 or TrackPaymentV2 client stream.
675
+ type PaymentResultStream interface {
676
+ Recv () (* lnrpc.Payment , error )
677
+ }
678
+
659
679
// PrintLivePayment receives payment updates from the given stream and either
660
680
// outputs them as json or as a more user-friendly formatted table. The table
661
681
// option uses terminal control codes to rewrite the output. This call
662
682
// terminates when the payment reaches a final state.
663
- func PrintLivePayment (ctxc context.Context ,
664
- stream routerrpc.Router_TrackPaymentV2Client ,
665
- client lnrpc.LightningClient , json bool ) (* lnrpc.Payment , error ) {
683
+ func PrintLivePayment (ctxc context.Context , stream PaymentResultStream ,
684
+ lnClient lnrpc.LightningClient , json bool ) (* lnrpc.Payment , error ) {
666
685
667
686
// Terminal escape codes aren't supported on Windows, fall back to json.
668
687
if ! json && runtime .GOOS == "windows" {
669
688
json = true
670
689
}
671
690
672
- aliases := newAliasCache (client )
691
+ aliases := newAliasCache (lnClient )
673
692
674
693
first := true
675
694
var lastLineCount int
@@ -691,17 +710,17 @@ func PrintLivePayment(ctxc context.Context,
691
710
// Write raw json to stdout.
692
711
printRespJSON (payment )
693
712
} else {
694
- table := formatPayment (ctxc , payment , aliases )
713
+ resultTable := formatPayment (ctxc , payment , aliases )
695
714
696
715
// Clear all previously written lines and print the
697
716
// updated table.
698
717
clearLines (lastLineCount )
699
- fmt .Print (table )
718
+ fmt .Print (resultTable )
700
719
701
720
// Store the number of lines written for the next update
702
721
// pass.
703
722
lastLineCount = 0
704
- for _ , b := range table {
723
+ for _ , b := range resultTable {
705
724
if b == '\n' {
706
725
lastLineCount ++
707
726
}
@@ -870,6 +889,9 @@ var payInvoiceCommand = cli.Command{
870
889
}
871
890
872
891
func payInvoice (ctx * cli.Context ) error {
892
+ conn := getClientConn (ctx , false )
893
+ defer conn .Close ()
894
+
873
895
args := ctx .Args ()
874
896
875
897
var payReq string
@@ -889,7 +911,7 @@ func payInvoice(ctx *cli.Context) error {
889
911
Amp : ctx .Bool (ampFlag .Name ),
890
912
}
891
913
892
- return SendPaymentRequest (ctx , req )
914
+ return SendPaymentRequest (ctx , req , conn , conn , routerRPCSendPayment )
893
915
}
894
916
895
917
var sendToRouteCommand = cli.Command {
0 commit comments