From 56be61c31d97e8dab88dcff059f03f5dcf4cb281 Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Mon, 3 Feb 2025 16:14:39 +0100 Subject: [PATCH] cmd: add new reservation clis --- cmd/loop/main.go | 2 + cmd/loop/reservations.go | 83 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/cmd/loop/main.go b/cmd/loop/main.go index 127634d2..dc57ddc1 100644 --- a/cmd/loop/main.go +++ b/cmd/loop/main.go @@ -41,6 +41,8 @@ var ( defaultSwapWaitTime = 30 * time.Minute + defaultRpcTimeout = 30 * time.Second + // maxMsgRecvSize is the largest message our client will receive. We // set this to 200MiB atm. maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200) diff --git a/cmd/loop/reservations.go b/cmd/loop/reservations.go index 122eda12..196610f7 100644 --- a/cmd/loop/reservations.go +++ b/cmd/loop/reservations.go @@ -2,13 +2,26 @@ package main import ( "context" + "errors" + "fmt" "github.com/lightninglabs/loop/looprpc" "github.com/urfave/cli/v3" ) -var reservationsCommands = &cli.Command{ +var ( + reservationAmountFlag = &cli.Uint64Flag{ + Name: "amt", + Usage: "the amount in satoshis for the reservation", + } + reservationExpiryFlag = &cli.UintFlag{ + Name: "expiry", + Usage: "the relative block height at which the reservation" + + " expires", + } +) +var reservationsCommands = &cli.Command{ Name: "reservations", Aliases: []string{"r"}, Usage: "manage reservations", @@ -20,6 +33,7 @@ var reservationsCommands = &cli.Command{ `, Commands: []*cli.Command{ listReservationsCommand, + newReservationCommand, }, } @@ -34,8 +48,75 @@ var ( `, Action: listReservations, } + + newReservationCommand = &cli.Command{ + Name: "new", + Aliases: []string{"n"}, + Usage: "create a new reservation", + Description: ` + Create a new reservation with the given value and expiry. + `, + Action: newReservation, + Flags: []cli.Flag{ + reservationAmountFlag, + reservationExpiryFlag, + }, + } ) +func newReservation(ctx context.Context, cmd *cli.Command) error { + client, cleanup, err := getClient(cmd) + if err != nil { + return err + } + defer cleanup() + + rpcCtx, cancel := context.WithTimeout(ctx, defaultRpcTimeout) + defer cancel() + + if !cmd.IsSet(reservationAmountFlag.Name) { + return errors.New("amt flag missing") + } + + if !cmd.IsSet(reservationExpiryFlag.Name) { + return errors.New("expiry flag missing") + } + + quoteReq, err := client.ReservationQuote( + rpcCtx, &looprpc.ReservationQuoteRequest{ + Amt: cmd.Uint64(reservationAmountFlag.Name), + Expiry: uint32(cmd.Uint(reservationExpiryFlag.Name)), + }, + ) + if err != nil { + return err + } + + fmt.Printf(satAmtFmt, "Reservation Cost: ", quoteReq.PrepayAmt) + + fmt.Printf("CONTINUE RESERVATION? (y/n): ") + + var answer string + fmt.Scanln(&answer) + if answer == "n" { + return nil + } + + reservationRes, err := client.ReservationRequest( + rpcCtx, &looprpc.ReservationRequestRequest{ + Amt: cmd.Uint64(reservationAmountFlag.Name), + Expiry: uint32(cmd.Uint(reservationExpiryFlag.Name)), + MaxPrepayAmt: quoteReq.PrepayAmt, + }, + ) + if err != nil { + return err + } + + printRespJSON(reservationRes) + return nil +} + func listReservations(ctx context.Context, cmd *cli.Command) error { client, cleanup, err := getClient(cmd) if err != nil {