Skip to content

Commit

Permalink
staticaddr: cmd listdeposits and listswaps
Browse files Browse the repository at this point in the history
  • Loading branch information
hieblmi committed Sep 17, 2024
1 parent c6e7a43 commit 8f153d7
Show file tree
Hide file tree
Showing 11 changed files with 1,531 additions and 563 deletions.
4 changes: 2 additions & 2 deletions cmd/loop/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func quoteIn(ctx *cli.Context) error {
func depositAmount(ctx context.Context, client looprpc.SwapClientClient,
depositOutpoints []string) (btcutil.Amount, error) {

addressSummary, err := client.GetStaticAddressSummary(
ctx, &looprpc.StaticAddressSummaryRequest{
addressSummary, err := client.ListStaticAddressDeposits(
ctx, &looprpc.ListStaticAddressDepositsRequest{
Outpoints: depositOutpoints,
},
)
Expand Down
105 changes: 97 additions & 8 deletions cmd/loop/staticaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightninglabs/loop/labels"
"github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/loop/staticaddr/deposit"
"github.com/lightninglabs/loop/swapserverrpc"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/urfave/cli"
Expand All @@ -24,6 +25,8 @@ var staticAddressCommands = cli.Command{
Subcommands: []cli.Command{
newStaticAddressCommand,
listUnspentCommand,
listDepositsCommand,
listStaticAddressSwapsCommand,
withdrawalCommand,
summaryCommand,
},
Expand Down Expand Up @@ -206,6 +209,36 @@ func withdraw(ctx *cli.Context) error {
return nil
}

var listDepositsCommand = cli.Command{
Name: "listdeposits",
Usage: "Display a summary of static address related information.",
Description: `
`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "filter",
Usage: "specify a filter to only display deposits in " +
"the specified state. Leaving out the filter " +
"returns all deposits.\nThe state can be one " +
"of the following: \n" +
"deposited\nwithdrawing\nwithdrawn\n" +
"looping_in\nlooped_in\n" +
"publish_expired_deposit\n" +
"sweep_htlc_timeout\nhtlc_timeout_swept\n" +
"wait_for_expiry_sweep\nexpired\nfailed\n.",
},
},
Action: listDeposits,
}

var listStaticAddressSwapsCommand = cli.Command{
Name: "listswaps",
Usage: "Display a summary of static address related information.",
Description: `
`,
Action: listStaticAddressSwaps,
}

var summaryCommand = cli.Command{
Name: "summary",
ShortName: "s",
Expand All @@ -231,10 +264,10 @@ var summaryCommand = cli.Command{
Action: summary,
}

func summary(ctx *cli.Context) error {
func listDeposits(ctx *cli.Context) error {
ctxb := context.Background()
if ctx.NArg() > 0 {
return cli.ShowCommandHelp(ctx, "summary")
return cli.ShowCommandHelp(ctx, "listdeposits")
}

client, cleanup, err := getClient(ctx)
Expand Down Expand Up @@ -285,8 +318,8 @@ func summary(ctx *cli.Context) error {
filterState = looprpc.DepositState_UNKNOWN_STATE
}

resp, err := client.GetStaticAddressSummary(
ctxb, &looprpc.StaticAddressSummaryRequest{
resp, err := client.ListStaticAddressDeposits(
ctxb, &looprpc.ListStaticAddressDepositsRequest{
StateFilter: filterState,
},
)
Expand All @@ -299,6 +332,54 @@ func summary(ctx *cli.Context) error {
return nil
}

func listStaticAddressSwaps(ctx *cli.Context) error {
ctxb := context.Background()
if ctx.NArg() > 0 {
return cli.ShowCommandHelp(ctx, "listswaps")
}

client, cleanup, err := getClient(ctx)
if err != nil {
return err
}
defer cleanup()

resp, err := client.ListStaticAddressSwaps(
ctxb, &looprpc.ListStaticAddressSwapsRequest{},
)
if err != nil {
return err
}

printRespJSON(resp)

return nil
}

func summary(ctx *cli.Context) error {
ctxb := context.Background()
if ctx.NArg() > 0 {
return cli.ShowCommandHelp(ctx, "summary")
}

client, cleanup, err := getClient(ctx)
if err != nil {
return err
}
defer cleanup()

resp, err := client.GetStaticAddressSummary(
ctxb, &looprpc.StaticAddressSummaryRequest{},
)
if err != nil {
return err
}

printRespJSON(resp)

return nil
}

func utxosToOutpoints(utxos []string) ([]*looprpc.OutPoint, error) {
outpoints := make([]*looprpc.OutPoint, 0, len(utxos))
if len(utxos) == 0 {
Expand Down Expand Up @@ -382,23 +463,31 @@ func staticAddressLoopIn(ctx *cli.Context) error {
}

// Get the amount we need to quote for.
summaryResp, err := client.GetStaticAddressSummary(
ctxb, &looprpc.StaticAddressSummaryRequest{
depositList, err := client.ListStaticAddressDeposits(
ctxb, &looprpc.ListStaticAddressDepositsRequest{
StateFilter: looprpc.DepositState_DEPOSITED,
},
)
if err != nil {
return err
}

if len(depositList.FilteredDeposits) == 0 {
errString := fmt.Sprintf("no confirmed deposits available, "+
"deposits need at least %v confirmations",
deposit.DefaultConfTarget)

return errors.New(errString)
}

var depositOutpoints []string
switch {
case isAllSelected == isUtxoSelected:
return errors.New("must select either all or some utxos")

case isAllSelected:
depositOutpoints = depositsToOutpoints(
summaryResp.FilteredDeposits,
depositList.FilteredDeposits,
)

case isUtxoSelected:
Expand Down Expand Up @@ -428,7 +517,7 @@ func staticAddressLoopIn(ctx *cli.Context) error {
// populate the quote request with the sum of selected deposits and
// prompt the user for acceptance.
quoteReq.Amt, err = sumDeposits(
depositOutpoints, summaryResp.FilteredDeposits,
depositOutpoints, depositList.FilteredDeposits,
)
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions loopd/perms/perms.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ var RequiredPermissions = map[string][]bakery.Op{
Entity: "loop",
Action: "in",
}},
"/looprpc.SwapClient/ListStaticAddressDeposits": {{
Entity: "swap",
Action: "read",
}, {
Entity: "loop",
Action: "in",
}},
"/looprpc.SwapClient/ListStaticAddressSwaps": {{
Entity: "swap",
Action: "read",
}, {
Entity: "loop",
Action: "in",
}},
"/looprpc.SwapClient/GetStaticAddressSummary": {{
Entity: "swap",
Action: "read",
Expand Down
Loading

0 comments on commit 8f153d7

Please sign in to comment.