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 Nov 5, 2024
1 parent d277bff commit 3bc6b25
Show file tree
Hide file tree
Showing 11 changed files with 1,596 additions and 514 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/staticaddr/loopin"
"github.com/lightninglabs/loop/swapserverrpc"
"github.com/lightningnetwork/lnd/routing/route"
Expand All @@ -24,6 +25,8 @@ var staticAddressCommands = cli.Command{
Subcommands: []cli.Command{
newStaticAddressCommand,
listUnspentCommand,
listDepositsCommand,
listStaticAddressSwapsCommand,
withdrawalCommand,
summaryCommand,
},
Expand Down Expand Up @@ -217,6 +220,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 @@ -242,10 +275,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 @@ -293,8 +326,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 @@ -307,6 +340,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 @@ -391,23 +472,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 @@ -437,7 +526,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 3bc6b25

Please sign in to comment.