-
Notifications
You must be signed in to change notification settings - Fork 123
[4/?] Instant loop out: Add instant loop outs #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
112e612
reservation: add musig2 spend helpers
sputn1ck 89b5c00
reservation: update reservation state machine
sputn1ck 56ed6f7
instantout: add fsm and actions
sputn1ck ee0309f
instantout: add instantout store
sputn1ck b7c1e68
instantout: add instantout manager
sputn1ck 932a55a
swapserverrpc: add instantout service
sputn1ck 6c07f88
looprpc: add reservations to loop out
sputn1ck 7cafbe9
loopd: add instantout handling
sputn1ck 8c7c7cf
loop: add instantout cmd
sputn1ck f725f07
fsm: add instanout fsm parsing
sputn1ck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/lightninglabs/loop/instantout/reservation" | ||
| "github.com/lightninglabs/loop/looprpc" | ||
| "github.com/urfave/cli" | ||
| ) | ||
|
|
||
| var instantOutCommand = cli.Command{ | ||
| Name: "instantout", | ||
hieblmi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Usage: "perform an instant off-chain to on-chain swap (looping out)", | ||
| Description: ` | ||
| Attempts to instantly loop out into the backing lnd's wallet. The amount | ||
| will be chosen via the cli. | ||
| `, | ||
| Flags: []cli.Flag{ | ||
| cli.StringFlag{ | ||
hieblmi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Name: "channel", | ||
| Usage: "the comma-separated list of short " + | ||
| "channel IDs of the channels to loop out", | ||
| }, | ||
| }, | ||
| Action: instantOut, | ||
| } | ||
|
|
||
| func instantOut(ctx *cli.Context) error { | ||
| // Parse outgoing channel set. Don't string split if the flag is empty. | ||
| // Otherwise, strings.Split returns a slice of length one with an empty | ||
| // element. | ||
| var outgoingChanSet []uint64 | ||
| if ctx.IsSet("channel") { | ||
| chanStrings := strings.Split(ctx.String("channel"), ",") | ||
| for _, chanString := range chanStrings { | ||
| chanID, err := strconv.ParseUint(chanString, 10, 64) | ||
| if err != nil { | ||
| return fmt.Errorf("error parsing channel id "+ | ||
| "\"%v\"", chanString) | ||
| } | ||
| outgoingChanSet = append(outgoingChanSet, chanID) | ||
| } | ||
| } | ||
|
|
||
| // First set up the swap client itself. | ||
| client, cleanup, err := getClient(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer cleanup() | ||
|
|
||
| // Now we fetch all the confirmed reservations. | ||
| reservations, err := client.ListReservations( | ||
| context.Background(), &looprpc.ListReservationsRequest{}, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var ( | ||
| confirmedReservations []*looprpc.ClientReservation | ||
| totalAmt int64 | ||
| idx int | ||
| ) | ||
|
|
||
| for _, res := range reservations.Reservations { | ||
| if res.State != string(reservation.Confirmed) { | ||
| continue | ||
| } | ||
|
|
||
| confirmedReservations = append(confirmedReservations, res) | ||
| } | ||
|
|
||
| if len(confirmedReservations) == 0 { | ||
| fmt.Printf("No confirmed reservations found \n") | ||
| return nil | ||
| } | ||
|
|
||
| fmt.Printf("Available reservations: \n\n") | ||
| for _, res := range confirmedReservations { | ||
| idx++ | ||
| fmt.Printf("Reservation %v: %v \n", idx, res.Amount) | ||
| totalAmt += int64(res.Amount) | ||
| } | ||
|
|
||
| fmt.Println() | ||
| fmt.Printf("Max amount to instant out: %v\n", totalAmt) | ||
| fmt.Println() | ||
|
|
||
| fmt.Println("Select reservations for instantout (e.g. '1,2,3')") | ||
| fmt.Println("Type 'ALL' to use all available reservations.") | ||
|
|
||
| var answer string | ||
| fmt.Scanln(&answer) | ||
|
|
||
| // Parse | ||
| var selectedReservations [][]byte | ||
| switch answer { | ||
| case "ALL": | ||
| for _, res := range confirmedReservations { | ||
| selectedReservations = append( | ||
| selectedReservations, | ||
| res.ReservationId, | ||
| ) | ||
| } | ||
|
|
||
| case "": | ||
| return fmt.Errorf("no reservations selected") | ||
|
|
||
| default: | ||
| selectedIndexes := strings.Split(answer, ",") | ||
sputn1ck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| selectedIndexMap := make(map[int]struct{}) | ||
| for _, idxStr := range selectedIndexes { | ||
| idx, err := strconv.Atoi(idxStr) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if idx < 0 { | ||
| return fmt.Errorf("invalid index %v", idx) | ||
| } | ||
|
|
||
| if idx > len(confirmedReservations) { | ||
| return fmt.Errorf("invalid index %v", idx) | ||
| } | ||
| if _, ok := selectedIndexMap[idx]; ok { | ||
| return fmt.Errorf("duplicate index %v", idx) | ||
| } | ||
|
|
||
| selectedReservations = append( | ||
| selectedReservations, | ||
| confirmedReservations[idx-1].ReservationId, | ||
| ) | ||
|
|
||
| selectedIndexMap[idx] = struct{}{} | ||
| } | ||
| } | ||
|
|
||
| fmt.Println("Starting instant swap out") | ||
|
|
||
| // Now we can request the instant out swap. | ||
| instantOutRes, err := client.InstantOut( | ||
| context.Background(), | ||
| &looprpc.InstantOutRequest{ | ||
| ReservationIds: selectedReservations, | ||
| OutgoingChanSet: outgoingChanSet, | ||
| }, | ||
| ) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Printf("Instant out swap initiated with ID: %x, State: %v \n", | ||
| instantOutRes.InstantOutHash, instantOutRes.State) | ||
sputn1ck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if instantOutRes.SweepTxId != "" { | ||
| fmt.Printf("Sweepless sweep tx id: %v \n", | ||
| instantOutRes.SweepTxId) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be cool to have a cli command to also list available reservations. (can be a follow up).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have that
loop r lThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or do you mean list instantouts? (we should have that as well)