|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/lightninglabs/loop/looprpc" |
| 10 | + "github.com/lightninglabs/loop/reservation" |
| 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 | + |
| 60 | + var confirmedReservations []*looprpc.ClientReservation |
| 61 | + |
| 62 | + var totalAmt int64 |
| 63 | + var idx int |
| 64 | + fmt.Printf("Available reservations: \n\n") |
| 65 | + for _, res := range reservations.Reservations { |
| 66 | + if res.State != string(reservation.Confirmed) { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + idx++ |
| 71 | + confirmedReservations = append(confirmedReservations, res) |
| 72 | + fmt.Printf("Reservation %v: %v \n", idx, res.Amount) |
| 73 | + totalAmt += int64(res.Amount) |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Println() |
| 77 | + fmt.Printf("Max amount to instant out: %v\n", totalAmt) |
| 78 | + fmt.Println() |
| 79 | + |
| 80 | + fmt.Printf("Select reservations for instantout (e.g. '1,2,3') \n") |
| 81 | + fmt.Printf("Type 'ALL' to use all available reservations. \n") |
| 82 | + |
| 83 | + var answer string |
| 84 | + fmt.Scanln(&answer) |
| 85 | + |
| 86 | + // Parse |
| 87 | + var selectedReservations [][]byte |
| 88 | + if answer == "ALL" { |
| 89 | + for _, res := range confirmedReservations { |
| 90 | + selectedReservations = append( |
| 91 | + selectedReservations, |
| 92 | + res.ReservationId, |
| 93 | + ) |
| 94 | + } |
| 95 | + } else { |
| 96 | + selectedIndexes := strings.Split(answer, ",") |
| 97 | + for _, idxStr := range selectedIndexes { |
| 98 | + idx, err := strconv.Atoi(idxStr) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + selectedReservations = append( |
| 104 | + selectedReservations, |
| 105 | + confirmedReservations[idx-1].ReservationId, |
| 106 | + ) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Printf("Starting instant swap out \n") |
| 111 | + |
| 112 | + // Now we can request the instant out swap. |
| 113 | + instantOutRes, err := client.InstantOut( |
| 114 | + context.Background(), |
| 115 | + &looprpc.InstantOutRequest{ |
| 116 | + ReservationIds: selectedReservations, |
| 117 | + OutgoingChanSet: outgoingChanSet, |
| 118 | + }, |
| 119 | + ) |
| 120 | + |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + fmt.Printf("Instant out swap initiated with ID: %x, State: %v \n", |
| 126 | + instantOutRes.InstantOutHash, instantOutRes.State) |
| 127 | + if instantOutRes.SweepTxId != "" { |
| 128 | + fmt.Printf("Sweepless sweep tx id: %v \n", |
| 129 | + instantOutRes.SweepTxId) |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments