Skip to content

Commit 872c6ff

Browse files
committed
all summary changes
1 parent 385f3ea commit 872c6ff

File tree

11 files changed

+1027
-211
lines changed

11 files changed

+1027
-211
lines changed

cmd/loop/staticaddr.go

Lines changed: 78 additions & 0 deletions
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. Utxos, Deposits,
191+
Withdrawls, loop-ins...
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 157 additions & 0 deletions
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,162 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
13951396
return &clientrpc.WithdrawDepositsResponse{}, err
13961397
}
13971398

1399+
func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
1400+
req *clientrpc.StaticAddressSummaryRequest) (*clientrpc.StaticAddressSummaryResponse,
1401+
error) {
1402+
1403+
allDeposits, err := s.depositManager.GetAllDeposits()
1404+
if err != nil {
1405+
return nil, err
1406+
}
1407+
1408+
return s.depositSummary(ctx, allDeposits, req.StateFilter)
1409+
}
1410+
1411+
func (s *swapClientServer) depositSummary(ctx context.Context,
1412+
deposits []*deposit.Deposit,
1413+
filter clientrpc.DepositState) (*clientrpc.StaticAddressSummaryResponse,
1414+
error) {
1415+
1416+
var (
1417+
totalNumDeposits = len(deposits)
1418+
valueUnconfirmed int64
1419+
valueDeposited int64
1420+
valueExpired int64
1421+
valueWithdrawn int64
1422+
)
1423+
1424+
// Value unconfirmed.
1425+
utxos, err := s.staticAddressManager.ListUnspent(
1426+
ctx, 0, deposit.MinConfs-1,
1427+
)
1428+
if err != nil {
1429+
return nil, err
1430+
}
1431+
for _, u := range utxos {
1432+
valueUnconfirmed += int64(u.Value)
1433+
}
1434+
1435+
for _, d := range deposits {
1436+
value := int64(d.Value)
1437+
switch d.GetState() {
1438+
case deposit.Deposited:
1439+
valueDeposited += value
1440+
1441+
case deposit.Expired:
1442+
valueExpired += value
1443+
1444+
case deposit.Withdrawn:
1445+
valueWithdrawn += value
1446+
}
1447+
}
1448+
1449+
clientDeposits := s.filterClientDeposits(deposits, filter)
1450+
1451+
params, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
1452+
if err != nil {
1453+
return nil, err
1454+
}
1455+
1456+
address, err := s.staticAddressManager.GetTaprootAddress(
1457+
params.ClientPubkey, params.ServerPubkey, int64(params.Expiry),
1458+
)
1459+
if err != nil {
1460+
return nil, err
1461+
}
1462+
1463+
return &clientrpc.StaticAddressSummaryResponse{
1464+
StaticAddress: address.String(),
1465+
TotalNumDeposits: uint32(totalNumDeposits),
1466+
ValueUnconfirmed: valueUnconfirmed,
1467+
ValueDeposited: valueDeposited,
1468+
ValueExpired: valueExpired,
1469+
ValueWithdrawn: valueWithdrawn,
1470+
FilteredDeposits: clientDeposits,
1471+
}, nil
1472+
}
1473+
1474+
func (s *swapClientServer) filterClientDeposits(deposits []*deposit.Deposit,
1475+
filterState clientrpc.DepositState) []*clientrpc.Deposit {
1476+
1477+
var clientDeposits []*clientrpc.Deposit
1478+
for _, d := range deposits {
1479+
if filterState != clientrpc.DepositState_UNKNOWN_STATE &&
1480+
d.GetState() != toServerState(filterState) {
1481+
1482+
continue
1483+
}
1484+
1485+
hash := d.Hash
1486+
outpoint := wire.NewOutPoint(&hash, d.Index).String()
1487+
clientDeposits = append(clientDeposits, &clientrpc.Deposit{
1488+
Id: d.ID[:],
1489+
State: toClientState(d.GetState()),
1490+
Outpoint: outpoint,
1491+
Value: int64(d.Value),
1492+
ConfirmationHeight: d.ConfirmationHeight,
1493+
})
1494+
}
1495+
1496+
return clientDeposits
1497+
}
1498+
1499+
func toClientState(state fsm.StateType) clientrpc.DepositState {
1500+
switch state {
1501+
case deposit.Deposited:
1502+
return clientrpc.DepositState_DEPOSITED
1503+
1504+
case deposit.Withdrawing:
1505+
return clientrpc.DepositState_WITHDRAWING
1506+
1507+
case deposit.Withdrawn:
1508+
return clientrpc.DepositState_WITHDRAWN
1509+
1510+
case deposit.PublishExpiredDeposit:
1511+
return clientrpc.DepositState_PUBLISH_EXPIRED
1512+
1513+
case deposit.WaitForExpirySweep:
1514+
return clientrpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
1515+
1516+
case deposit.Expired:
1517+
return clientrpc.DepositState_EXPIRED
1518+
1519+
case deposit.Failed:
1520+
return clientrpc.DepositState_FAILED_STATE
1521+
1522+
default:
1523+
return clientrpc.DepositState_UNKNOWN_STATE
1524+
}
1525+
}
1526+
1527+
func toServerState(state clientrpc.DepositState) fsm.StateType {
1528+
switch state {
1529+
case clientrpc.DepositState_DEPOSITED:
1530+
return deposit.Deposited
1531+
1532+
case clientrpc.DepositState_WITHDRAWING:
1533+
return deposit.Withdrawing
1534+
1535+
case clientrpc.DepositState_WITHDRAWN:
1536+
return deposit.Withdrawn
1537+
1538+
case clientrpc.DepositState_PUBLISH_EXPIRED:
1539+
return deposit.PublishExpiredDeposit
1540+
1541+
case clientrpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
1542+
return deposit.WaitForExpirySweep
1543+
1544+
case clientrpc.DepositState_EXPIRED:
1545+
return deposit.Expired
1546+
1547+
case clientrpc.DepositState_FAILED_STATE:
1548+
return deposit.Failed
1549+
1550+
default:
1551+
return fsm.EmptyState
1552+
}
1553+
}
1554+
13981555
func toServerOutpoints(outpoints []*clientrpc.OutPoint) ([]wire.OutPoint,
13991556
error) {
14001557

0 commit comments

Comments
 (0)