Skip to content

Commit d5ac415

Browse files
committed
loopd: static address summary
1 parent 6a82beb commit d5ac415

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed

cmd/loop/staticaddr.go

+78
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var staticAddressCommands = cli.Command{
2222
newStaticAddressCommand,
2323
listUnspentCommand,
2424
withdrawalCommand,
25+
summaryCommand,
2526
},
2627
}
2728

@@ -181,6 +182,83 @@ func withdraw(ctx *cli.Context) error {
181182
return nil
182183
}
183184

185+
var summaryCommand = cli.Command{
186+
Name: "summary",
187+
ShortName: "s",
188+
Usage: "Display a summary of static address related data.",
189+
Description: `
190+
Displays various static address related data like deposits, withdrawals,
191+
and statistics. The data can be filtered by state.
192+
`,
193+
Flags: []cli.Flag{
194+
cli.StringFlag{
195+
Name: "filter",
196+
Usage: "specify a filter to only display deposits in " +
197+
"the specified state. The state can be one " +
198+
"of [deposited|withdrawing|withdrawn|" +
199+
"publish_expired_deposit|" +
200+
"wait_for_expiry_sweep|expired|failed].",
201+
},
202+
},
203+
Action: summary,
204+
}
205+
206+
func summary(ctx *cli.Context) error {
207+
ctxb := context.Background()
208+
if ctx.NArg() > 0 {
209+
return cli.ShowCommandHelp(ctx, "summary")
210+
}
211+
212+
client, cleanup, err := getClient(ctx)
213+
if err != nil {
214+
return err
215+
}
216+
defer cleanup()
217+
218+
var filterState looprpc.DepositState
219+
switch ctx.String("filter") {
220+
case "":
221+
// If no filter is specified, we'll default to showing all.
222+
223+
case "deposited":
224+
filterState = looprpc.DepositState_DEPOSITED
225+
226+
case "withdrawing":
227+
filterState = looprpc.DepositState_WITHDRAWING
228+
229+
case "withdrawn":
230+
filterState = looprpc.DepositState_WITHDRAWN
231+
232+
case "publish_expired_deposit":
233+
filterState = looprpc.DepositState_PUBLISH_EXPIRED
234+
235+
case "wait_for_expiry_sweep":
236+
filterState = looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
237+
238+
case "expired":
239+
filterState = looprpc.DepositState_EXPIRED
240+
241+
case "failed":
242+
filterState = looprpc.DepositState_FAILED_STATE
243+
244+
default:
245+
filterState = looprpc.DepositState_UNKNOWN_STATE
246+
}
247+
248+
resp, err := client.GetStaticAddressSummary(
249+
ctxb, &looprpc.StaticAddressSummaryRequest{
250+
StateFilter: filterState,
251+
},
252+
)
253+
if err != nil {
254+
return err
255+
}
256+
257+
printRespJSON(resp)
258+
259+
return nil
260+
}
261+
184262
func utxosToOutpoints(utxos []string) ([]*looprpc.OutPoint, error) {
185263
outpoints := make([]*looprpc.OutPoint, 0, len(utxos))
186264
if len(utxos) == 0 {

loopd/perms/perms.go

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ var RequiredPermissions = map[string][]bakery.Op{
9494
Entity: "loop",
9595
Action: "in",
9696
}},
97+
"/looprpc.SwapClient/GetStaticAddressSummary": {{
98+
Entity: "swap",
99+
Action: "read",
100+
}, {
101+
Entity: "loop",
102+
Action: "in",
103+
}},
97104
"/looprpc.SwapClient/GetLsatTokens": {{
98105
Entity: "auth",
99106
Action: "read",

loopd/swapclient_server.go

+190
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightninglabs/aperture/l402"
2020
"github.com/lightninglabs/lndclient"
2121
"github.com/lightninglabs/loop"
22+
"github.com/lightninglabs/loop/fsm"
2223
"github.com/lightninglabs/loop/instantout"
2324
"github.com/lightninglabs/loop/instantout/reservation"
2425
"github.com/lightninglabs/loop/labels"
@@ -1395,6 +1396,195 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
13951396
return &clientrpc.WithdrawDepositsResponse{}, err
13961397
}
13971398

1399+
// GetStaticAddressSummary returns a summary static address related information.
1400+
// Amongst deposits and withdrawals and their total values it also includes a
1401+
// list of detailed deposit information filtered by their state.
1402+
func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
1403+
req *clientrpc.StaticAddressSummaryRequest) (
1404+
*clientrpc.StaticAddressSummaryResponse, error) {
1405+
1406+
if req.StateFilter != clientrpc.DepositState_UNKNOWN_STATE &&
1407+
len(req.Outpoints) > 0 {
1408+
1409+
return nil, fmt.Errorf("can either filter by state or " +
1410+
"outpoints")
1411+
}
1412+
1413+
allDeposits, err := s.depositManager.GetAllDeposits()
1414+
if err != nil {
1415+
return nil, err
1416+
}
1417+
1418+
return s.depositSummary(
1419+
ctx, allDeposits, req.StateFilter, req.Outpoints,
1420+
)
1421+
}
1422+
1423+
func (s *swapClientServer) depositSummary(ctx context.Context,
1424+
deposits []*deposit.Deposit, stateFilter clientrpc.DepositState,
1425+
outpointsFilter []string) (*clientrpc.StaticAddressSummaryResponse,
1426+
error) {
1427+
1428+
var (
1429+
totalNumDeposits = len(deposits)
1430+
valueUnconfirmed int64
1431+
valueDeposited int64
1432+
valueExpired int64
1433+
valueWithdrawn int64
1434+
)
1435+
1436+
// Value unconfirmed.
1437+
utxos, err := s.staticAddressManager.ListUnspent(
1438+
ctx, 0, deposit.MinConfs-1,
1439+
)
1440+
if err != nil {
1441+
return nil, err
1442+
}
1443+
for _, u := range utxos {
1444+
valueUnconfirmed += int64(u.Value)
1445+
}
1446+
1447+
// Confirmed total values by category.
1448+
for _, d := range deposits {
1449+
value := int64(d.Value)
1450+
switch d.GetState() {
1451+
case deposit.Deposited:
1452+
valueDeposited += value
1453+
1454+
case deposit.Expired:
1455+
valueExpired += value
1456+
1457+
case deposit.Withdrawn:
1458+
valueWithdrawn += value
1459+
}
1460+
}
1461+
1462+
// Deposits filtered by state or outpoints.
1463+
var clientDeposits []*clientrpc.Deposit
1464+
if stateFilter != clientrpc.DepositState_UNKNOWN_STATE {
1465+
f := func(d *deposit.Deposit) bool {
1466+
return d.GetState() == toServerState(stateFilter)
1467+
}
1468+
clientDeposits = filter(deposits, f)
1469+
} else if len(outpointsFilter) > 0 {
1470+
f := func(d *deposit.Deposit) bool {
1471+
for _, outpoint := range outpointsFilter {
1472+
if outpoint == d.OutPoint.String() {
1473+
return true
1474+
}
1475+
}
1476+
return false
1477+
}
1478+
clientDeposits = filter(deposits, f)
1479+
1480+
if len(outpointsFilter) != len(clientDeposits) {
1481+
return nil, fmt.Errorf("not all outpoints found in " +
1482+
"deposits")
1483+
}
1484+
}
1485+
1486+
params, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
1487+
if err != nil {
1488+
return nil, err
1489+
}
1490+
1491+
address, err := s.staticAddressManager.GetTaprootAddress(
1492+
params.ClientPubkey, params.ServerPubkey, int64(params.Expiry),
1493+
)
1494+
if err != nil {
1495+
return nil, err
1496+
}
1497+
1498+
return &clientrpc.StaticAddressSummaryResponse{
1499+
StaticAddress: address.String(),
1500+
TotalNumDeposits: uint32(totalNumDeposits),
1501+
ValueUnconfirmed: valueUnconfirmed,
1502+
ValueDeposited: valueDeposited,
1503+
ValueExpired: valueExpired,
1504+
ValueWithdrawn: valueWithdrawn,
1505+
FilteredDeposits: clientDeposits,
1506+
}, nil
1507+
}
1508+
1509+
type filterFunc func(deposits *deposit.Deposit) bool
1510+
1511+
func filter(deposits []*deposit.Deposit, f filterFunc) []*clientrpc.Deposit {
1512+
var clientDeposits []*clientrpc.Deposit
1513+
for _, d := range deposits {
1514+
if f(d) {
1515+
hash := d.Hash
1516+
outpoint := wire.NewOutPoint(&hash, d.Index).String()
1517+
deposit := &clientrpc.Deposit{
1518+
Id: d.ID[:],
1519+
State: toClientState(d.GetState()),
1520+
Outpoint: outpoint,
1521+
Value: int64(d.Value),
1522+
ConfirmationHeight: d.ConfirmationHeight,
1523+
}
1524+
1525+
clientDeposits = append(clientDeposits, deposit)
1526+
}
1527+
}
1528+
1529+
return clientDeposits
1530+
}
1531+
1532+
func toClientState(state fsm.StateType) clientrpc.DepositState {
1533+
switch state {
1534+
case deposit.Deposited:
1535+
return clientrpc.DepositState_DEPOSITED
1536+
1537+
case deposit.Withdrawing:
1538+
return clientrpc.DepositState_WITHDRAWING
1539+
1540+
case deposit.Withdrawn:
1541+
return clientrpc.DepositState_WITHDRAWN
1542+
1543+
case deposit.PublishExpiredDeposit:
1544+
return clientrpc.DepositState_PUBLISH_EXPIRED
1545+
1546+
case deposit.WaitForExpirySweep:
1547+
return clientrpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
1548+
1549+
case deposit.Expired:
1550+
return clientrpc.DepositState_EXPIRED
1551+
1552+
case deposit.Failed:
1553+
return clientrpc.DepositState_FAILED_STATE
1554+
1555+
default:
1556+
return clientrpc.DepositState_UNKNOWN_STATE
1557+
}
1558+
}
1559+
1560+
func toServerState(state clientrpc.DepositState) fsm.StateType {
1561+
switch state {
1562+
case clientrpc.DepositState_DEPOSITED:
1563+
return deposit.Deposited
1564+
1565+
case clientrpc.DepositState_WITHDRAWING:
1566+
return deposit.Withdrawing
1567+
1568+
case clientrpc.DepositState_WITHDRAWN:
1569+
return deposit.Withdrawn
1570+
1571+
case clientrpc.DepositState_PUBLISH_EXPIRED:
1572+
return deposit.PublishExpiredDeposit
1573+
1574+
case clientrpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
1575+
return deposit.WaitForExpirySweep
1576+
1577+
case clientrpc.DepositState_EXPIRED:
1578+
return deposit.Expired
1579+
1580+
case clientrpc.DepositState_FAILED_STATE:
1581+
return deposit.Failed
1582+
1583+
default:
1584+
return fsm.EmptyState
1585+
}
1586+
}
1587+
13981588
func toServerOutpoints(outpoints []*clientrpc.OutPoint) ([]wire.OutPoint,
13991589
error) {
14001590

0 commit comments

Comments
 (0)