Skip to content

Commit

Permalink
Merge pull request #8947 from guggero/reuse-cli-payment-code
Browse files Browse the repository at this point in the history
cmd/commands: refactor SendPaymentRequest for re-use
  • Loading branch information
guggero committed Jul 30, 2024
2 parents f09d404 + e3f5b74 commit 1b353b0
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions cmd/commands/cmd_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/lightningnetwork/lnd/record"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/urfave/cli"
"google.golang.org/grpc"
)

const (
Expand Down Expand Up @@ -319,6 +320,9 @@ func SendPayment(ctx *cli.Context) error {
return nil
}

conn := getClientConn(ctx, false)
defer conn.Close()

args := ctx.Args()

// If a payment request was provided, we can exit early since all of the
Expand All @@ -343,7 +347,9 @@ func SendPayment(ctx *cli.Context) error {

req.PaymentAddr = payAddr

return SendPaymentRequest(ctx, req)
return SendPaymentRequest(
ctx, req, conn, conn, routerRPCSendPayment,
)
}

var (
Expand Down Expand Up @@ -451,19 +457,29 @@ func SendPayment(ctx *cli.Context) error {

req.PaymentAddr = payAddr

return SendPaymentRequest(ctx, req)
return SendPaymentRequest(ctx, req, conn, conn, routerRPCSendPayment)
}

func SendPaymentRequest(ctx *cli.Context,
req *routerrpc.SendPaymentRequest) error {
// SendPaymentFn is a function type that abstracts the SendPaymentV2 call of the
// router client.
type SendPaymentFn func(ctx context.Context, payConn grpc.ClientConnInterface,
req *routerrpc.SendPaymentRequest) (PaymentResultStream, error)

ctxc := getContext()
// routerRPCSendPayment is the default implementation of the SendPaymentFn type
// that uses the lnd routerrpc.SendPaymentV2 call.
func routerRPCSendPayment(ctx context.Context, payConn grpc.ClientConnInterface,
req *routerrpc.SendPaymentRequest) (PaymentResultStream, error) {

conn := getClientConn(ctx, false)
defer conn.Close()
return routerrpc.NewRouterClient(payConn).SendPaymentV2(ctx, req)
}

client := lnrpc.NewLightningClient(conn)
routerClient := routerrpc.NewRouterClient(conn)
func SendPaymentRequest(ctx *cli.Context, req *routerrpc.SendPaymentRequest,
lnConn, paymentConn grpc.ClientConnInterface,
callSendPayment SendPaymentFn) error {

ctxc := getContext()

lnClient := lnrpc.NewLightningClient(lnConn)

outChan := ctx.Int64Slice("outgoing_chan_id")
if len(outChan) != 0 {
Expand Down Expand Up @@ -543,7 +559,7 @@ func SendPaymentRequest(ctx *cli.Context,
if req.PaymentRequest != "" {
// Decode payment request to find out the amount.
decodeReq := &lnrpc.PayReqString{PayReq: req.PaymentRequest}
decodeResp, err := client.DecodePayReq(ctxc, decodeReq)
decodeResp, err := lnClient.DecodePayReq(ctxc, decodeReq)
if err != nil {
return err
}
Expand Down Expand Up @@ -587,14 +603,12 @@ func SendPaymentRequest(ctx *cli.Context,
printJSON := ctx.Bool(jsonFlag.Name)
req.NoInflightUpdates = !ctx.Bool(inflightUpdatesFlag.Name) && printJSON

stream, err := routerClient.SendPaymentV2(ctxc, req)
stream, err := callSendPayment(ctxc, paymentConn, req)
if err != nil {
return err
}

finalState, err := PrintLivePayment(
ctxc, stream, client, printJSON,
)
finalState, err := PrintLivePayment(ctxc, stream, lnClient, printJSON)
if err != nil {
return err
}
Expand Down Expand Up @@ -656,20 +670,25 @@ func trackPayment(ctx *cli.Context) error {
return err
}

// PaymentResultStream is an interface that abstracts the Recv method of the
// SendPaymentV2 or TrackPaymentV2 client stream.
type PaymentResultStream interface {
Recv() (*lnrpc.Payment, error)
}

// PrintLivePayment receives payment updates from the given stream and either
// outputs them as json or as a more user-friendly formatted table. The table
// option uses terminal control codes to rewrite the output. This call
// terminates when the payment reaches a final state.
func PrintLivePayment(ctxc context.Context,
stream routerrpc.Router_TrackPaymentV2Client,
client lnrpc.LightningClient, json bool) (*lnrpc.Payment, error) {
func PrintLivePayment(ctxc context.Context, stream PaymentResultStream,
lnClient lnrpc.LightningClient, json bool) (*lnrpc.Payment, error) {

// Terminal escape codes aren't supported on Windows, fall back to json.
if !json && runtime.GOOS == "windows" {
json = true
}

aliases := newAliasCache(client)
aliases := newAliasCache(lnClient)

first := true
var lastLineCount int
Expand All @@ -691,17 +710,17 @@ func PrintLivePayment(ctxc context.Context,
// Write raw json to stdout.
printRespJSON(payment)
} else {
table := formatPayment(ctxc, payment, aliases)
resultTable := formatPayment(ctxc, payment, aliases)

// Clear all previously written lines and print the
// updated table.
clearLines(lastLineCount)
fmt.Print(table)
fmt.Print(resultTable)

// Store the number of lines written for the next update
// pass.
lastLineCount = 0
for _, b := range table {
for _, b := range resultTable {
if b == '\n' {
lastLineCount++
}
Expand Down Expand Up @@ -870,6 +889,9 @@ var payInvoiceCommand = cli.Command{
}

func payInvoice(ctx *cli.Context) error {
conn := getClientConn(ctx, false)
defer conn.Close()

args := ctx.Args()

var payReq string
Expand All @@ -889,7 +911,7 @@ func payInvoice(ctx *cli.Context) error {
Amp: ctx.Bool(ampFlag.Name),
}

return SendPaymentRequest(ctx, req)
return SendPaymentRequest(ctx, req, conn, conn, routerRPCSendPayment)
}

var sendToRouteCommand = cli.Command{
Expand Down

0 comments on commit 1b353b0

Please sign in to comment.