Skip to content

Commit 7aa1e57

Browse files
committed
tbd
1 parent efeb3e6 commit 7aa1e57

File tree

11 files changed

+1026
-211
lines changed

11 files changed

+1026
-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"
@@ -1383,6 +1384,162 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
13831384
return &clientrpc.WithdrawDepositsResponse{}, err
13841385
}
13851386

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

0 commit comments

Comments
 (0)