@@ -25,6 +25,7 @@ import (
2525 "github.com/lightningnetwork/lnd/record"
2626 "github.com/lightningnetwork/lnd/routing/route"
2727 "github.com/urfave/cli"
28+ "google.golang.org/grpc"
2829)
2930
3031const (
@@ -319,6 +320,9 @@ func SendPayment(ctx *cli.Context) error {
319320 return nil
320321 }
321322
323+ conn := getClientConn (ctx , false )
324+ defer conn .Close ()
325+
322326 args := ctx .Args ()
323327
324328 // If a payment request was provided, we can exit early since all of the
@@ -343,7 +347,9 @@ func SendPayment(ctx *cli.Context) error {
343347
344348 req .PaymentAddr = payAddr
345349
346- return SendPaymentRequest (ctx , req )
350+ return SendPaymentRequest (
351+ ctx , req , conn , conn , routerRPCSendPayment ,
352+ )
347353 }
348354
349355 var (
@@ -451,19 +457,29 @@ func SendPayment(ctx *cli.Context) error {
451457
452458 req .PaymentAddr = payAddr
453459
454- return SendPaymentRequest (ctx , req )
460+ return SendPaymentRequest (ctx , req , conn , conn , routerRPCSendPayment )
455461}
456462
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 )
459467
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 ) {
461472
462- conn := getClientConn ( ctx , false )
463- defer conn . Close ()
473+ return routerrpc . NewRouterClient ( payConn ). SendPaymentV2 ( ctx , req )
474+ }
464475
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 )
467483
468484 outChan := ctx .Int64Slice ("outgoing_chan_id" )
469485 if len (outChan ) != 0 {
@@ -543,7 +559,7 @@ func SendPaymentRequest(ctx *cli.Context,
543559 if req .PaymentRequest != "" {
544560 // Decode payment request to find out the amount.
545561 decodeReq := & lnrpc.PayReqString {PayReq : req .PaymentRequest }
546- decodeResp , err := client .DecodePayReq (ctxc , decodeReq )
562+ decodeResp , err := lnClient .DecodePayReq (ctxc , decodeReq )
547563 if err != nil {
548564 return err
549565 }
@@ -587,14 +603,12 @@ func SendPaymentRequest(ctx *cli.Context,
587603 printJSON := ctx .Bool (jsonFlag .Name )
588604 req .NoInflightUpdates = ! ctx .Bool (inflightUpdatesFlag .Name ) && printJSON
589605
590- stream , err := routerClient . SendPaymentV2 (ctxc , req )
606+ stream , err := callSendPayment (ctxc , paymentConn , req )
591607 if err != nil {
592608 return err
593609 }
594610
595- finalState , err := PrintLivePayment (
596- ctxc , stream , client , printJSON ,
597- )
611+ finalState , err := PrintLivePayment (ctxc , stream , lnClient , printJSON )
598612 if err != nil {
599613 return err
600614 }
@@ -656,20 +670,25 @@ func trackPayment(ctx *cli.Context) error {
656670 return err
657671}
658672
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+
659679// PrintLivePayment receives payment updates from the given stream and either
660680// outputs them as json or as a more user-friendly formatted table. The table
661681// option uses terminal control codes to rewrite the output. This call
662682// 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 ) {
666685
667686 // Terminal escape codes aren't supported on Windows, fall back to json.
668687 if ! json && runtime .GOOS == "windows" {
669688 json = true
670689 }
671690
672- aliases := newAliasCache (client )
691+ aliases := newAliasCache (lnClient )
673692
674693 first := true
675694 var lastLineCount int
@@ -691,17 +710,17 @@ func PrintLivePayment(ctxc context.Context,
691710 // Write raw json to stdout.
692711 printRespJSON (payment )
693712 } else {
694- table := formatPayment (ctxc , payment , aliases )
713+ resultTable := formatPayment (ctxc , payment , aliases )
695714
696715 // Clear all previously written lines and print the
697716 // updated table.
698717 clearLines (lastLineCount )
699- fmt .Print (table )
718+ fmt .Print (resultTable )
700719
701720 // Store the number of lines written for the next update
702721 // pass.
703722 lastLineCount = 0
704- for _ , b := range table {
723+ for _ , b := range resultTable {
705724 if b == '\n' {
706725 lastLineCount ++
707726 }
@@ -870,6 +889,9 @@ var payInvoiceCommand = cli.Command{
870889}
871890
872891func payInvoice (ctx * cli.Context ) error {
892+ conn := getClientConn (ctx , false )
893+ defer conn .Close ()
894+
873895 args := ctx .Args ()
874896
875897 var payReq string
@@ -889,7 +911,7 @@ func payInvoice(ctx *cli.Context) error {
889911 Amp : ctx .Bool (ampFlag .Name ),
890912 }
891913
892- return SendPaymentRequest (ctx , req )
914+ return SendPaymentRequest (ctx , req , conn , conn , routerRPCSendPayment )
893915}
894916
895917var sendToRouteCommand = cli.Command {
0 commit comments