Skip to content

Commit 76c64cf

Browse files
committed
loop: add instantout cmd
1 parent 373cccc commit 76c64cf

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

cmd/loop/instantout.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/lightninglabs/loop/instantout/reservation"
10+
"github.com/lightninglabs/loop/looprpc"
11+
"github.com/urfave/cli"
12+
)
13+
14+
var instantOutCommand = cli.Command{
15+
Name: "instantout",
16+
Usage: "perform an instant off-chain to on-chain swap (looping out)",
17+
Description: `
18+
Attempts to instantly loop out into the backing lnd's wallet. The amount
19+
will be chosen via the cli.
20+
`,
21+
Flags: []cli.Flag{
22+
cli.StringFlag{
23+
Name: "channel",
24+
Usage: "the comma-separated list of short " +
25+
"channel IDs of the channels to loop out",
26+
},
27+
},
28+
Action: instantOut,
29+
}
30+
31+
func instantOut(ctx *cli.Context) error {
32+
// Parse outgoing channel set. Don't string split if the flag is empty.
33+
// Otherwise, strings.Split returns a slice of length one with an empty
34+
// element.
35+
var outgoingChanSet []uint64
36+
if ctx.IsSet("channel") {
37+
chanStrings := strings.Split(ctx.String("channel"), ",")
38+
for _, chanString := range chanStrings {
39+
chanID, err := strconv.ParseUint(chanString, 10, 64)
40+
if err != nil {
41+
return fmt.Errorf("error parsing channel id "+
42+
"\"%v\"", chanString)
43+
}
44+
outgoingChanSet = append(outgoingChanSet, chanID)
45+
}
46+
}
47+
48+
// First set up the swap client itself.
49+
client, cleanup, err := getClient(ctx)
50+
if err != nil {
51+
return err
52+
}
53+
defer cleanup()
54+
55+
// Now we fetch all the confirmed reservations.
56+
reservations, err := client.ListReservations(
57+
context.Background(), &looprpc.ListReservationsRequest{},
58+
)
59+
if err != nil {
60+
return err
61+
}
62+
63+
var confirmedReservations []*looprpc.ClientReservation
64+
65+
var totalAmt int64
66+
var idx int
67+
fmt.Printf("Available reservations: \n\n")
68+
for _, res := range reservations.Reservations {
69+
if res.State != string(reservation.Confirmed) {
70+
continue
71+
}
72+
73+
idx++
74+
confirmedReservations = append(confirmedReservations, res)
75+
fmt.Printf("Reservation %v: %v \n", idx, res.Amount)
76+
totalAmt += int64(res.Amount)
77+
}
78+
79+
fmt.Println()
80+
fmt.Printf("Max amount to instant out: %v\n", totalAmt)
81+
fmt.Println()
82+
83+
fmt.Printf("Select reservations for instantout (e.g. '1,2,3') \n")
84+
fmt.Printf("Type 'ALL' to use all available reservations. \n")
85+
86+
var answer string
87+
fmt.Scanln(&answer)
88+
89+
// Parse
90+
var selectedReservations [][]byte
91+
if answer == "ALL" {
92+
for _, res := range confirmedReservations {
93+
selectedReservations = append(
94+
selectedReservations,
95+
res.ReservationId,
96+
)
97+
}
98+
} else {
99+
selectedIndexes := strings.Split(answer, ",")
100+
for _, idxStr := range selectedIndexes {
101+
idx, err := strconv.Atoi(idxStr)
102+
if err != nil {
103+
return err
104+
}
105+
106+
selectedReservations = append(
107+
selectedReservations,
108+
confirmedReservations[idx-1].ReservationId,
109+
)
110+
}
111+
}
112+
113+
fmt.Printf("Starting instant swap out \n")
114+
115+
// Now we can request the instant out swap.
116+
instantOutRes, err := client.InstantOut(
117+
context.Background(),
118+
&looprpc.InstantOutRequest{
119+
ReservationIds: selectedReservations,
120+
OutgoingChanSet: outgoingChanSet,
121+
},
122+
)
123+
124+
if err != nil {
125+
return err
126+
}
127+
128+
fmt.Printf("Instant out swap initiated with ID: %x, State: %v \n",
129+
instantOutRes.InstantOutHash, instantOutRes.State)
130+
if instantOutRes.SweepTxId != "" {
131+
fmt.Printf("Sweepless sweep tx id: %v \n",
132+
instantOutRes.SweepTxId)
133+
}
134+
135+
return nil
136+
}

cmd/loop/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func main() {
148148
listSwapsCommand, swapInfoCommand, getLiquidityParamsCommand,
149149
setLiquidityRuleCommand, suggestSwapCommand, setParamsCommand,
150150
getInfoCommand, abandonSwapCommand, reservationsCommands,
151+
instantOutCommand,
151152
}
152153

153154
err := app.Run(os.Args)

0 commit comments

Comments
 (0)