Skip to content

Commit a9b6c87

Browse files
committed
cmd: add new reservation clis
1 parent 9d8b1a4 commit a9b6c87

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

Diff for: cmd/loop/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ var (
3838

3939
defaultSwapWaitTime = 30 * time.Minute
4040

41+
defaultRpcTimeout = 30 * time.Second
42+
4143
// maxMsgRecvSize is the largest message our client will receive. We
4244
// set this to 200MiB atm.
4345
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)

Diff for: cmd/loop/reservations.go

+84-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ package main
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57

68
"github.com/lightninglabs/loop/looprpc"
79
"github.com/urfave/cli"
810
)
911

10-
var reservationsCommands = cli.Command{
12+
var (
13+
reservationAmountFlag = cli.Uint64Flag{
14+
Name: "amt",
15+
Usage: "the amount in satoshis for the reservation",
16+
}
17+
reservationExpiryFlag = cli.UintFlag{
18+
Name: "expiry",
19+
Usage: "the relative block height at which the reservation" +
20+
" expires",
21+
}
22+
)
1123

24+
var reservationsCommands = cli.Command{
1225
Name: "reservations",
1326
ShortName: "r",
1427
Usage: "manage reservations",
@@ -20,6 +33,7 @@ var reservationsCommands = cli.Command{
2033
`,
2134
Subcommands: []cli.Command{
2235
listReservationsCommand,
36+
newReservationCommand,
2337
},
2438
}
2539

@@ -34,8 +48,77 @@ var (
3448
`,
3549
Action: listReservations,
3650
}
51+
52+
newReservationCommand = cli.Command{
53+
Name: "new",
54+
ShortName: "n",
55+
Usage: "create a new reservation",
56+
Description: `
57+
Create a new reservation with the given value and expiry.
58+
`,
59+
Action: newReservation,
60+
Flags: []cli.Flag{
61+
reservationAmountFlag,
62+
reservationExpiryFlag,
63+
},
64+
}
3765
)
3866

67+
func newReservation(ctx *cli.Context) error {
68+
client, cleanup, err := getClient(ctx)
69+
if err != nil {
70+
return err
71+
}
72+
defer cleanup()
73+
74+
ctxt, cancel := context.WithTimeout(
75+
context.Background(), defaultRpcTimeout,
76+
)
77+
defer cancel()
78+
79+
if !ctx.IsSet(reservationAmountFlag.Name) {
80+
return errors.New("amt flag missing")
81+
}
82+
83+
if !ctx.IsSet(reservationExpiryFlag.Name) {
84+
return errors.New("expiry flag missing")
85+
}
86+
87+
quoteReq, err := client.ReservationQuote(
88+
ctxt, &looprpc.ReservationQuoteRequest{
89+
Amt: ctx.Uint64(reservationAmountFlag.Name),
90+
Expiry: uint32(ctx.Uint(reservationExpiryFlag.Name)),
91+
},
92+
)
93+
if err != nil {
94+
return err
95+
}
96+
97+
fmt.Printf(satAmtFmt, "Reservation Cost: ", quoteReq.PrepayAmt)
98+
99+
fmt.Printf("CONTINUE RESERVATION? (y/n): ")
100+
101+
var answer string
102+
fmt.Scanln(&answer)
103+
if answer == "n" {
104+
return nil
105+
}
106+
107+
reservationRes, err := client.ReservationRequest(
108+
ctxt, &looprpc.ReservationRequestRequest{
109+
Amt: ctx.Uint64(reservationAmountFlag.Name),
110+
Expiry: uint32(ctx.Uint(reservationExpiryFlag.Name)),
111+
MaxPrepayAmt: quoteReq.PrepayAmt,
112+
},
113+
)
114+
if err != nil {
115+
return err
116+
}
117+
118+
printRespJSON(reservationRes)
119+
return nil
120+
}
121+
39122
func listReservations(ctx *cli.Context) error {
40123
client, cleanup, err := getClient(ctx)
41124
if err != nil {

0 commit comments

Comments
 (0)