Skip to content

Commit b3f8669

Browse files
committed
tbd
1 parent f3a52c7 commit b3f8669

File tree

10 files changed

+1013
-209
lines changed

10 files changed

+1013
-209
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

@@ -184,6 +185,83 @@ func withdraw(ctx *cli.Context) error {
184185
return nil
185186
}
186187

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

loopd/perms/perms.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ var RequiredPermissions = map[string][]bakery.Op{
9090
Entity: "loop",
9191
Action: "in",
9292
}},
93+
"/looprpc.SwapClient/GetStaticAddressSummary": {{
94+
Entity: "swap",
95+
Action: "read",
96+
}, {
97+
Entity: "loop",
98+
Action: "in",
99+
}},
93100
"/looprpc.SwapClient/GetLsatTokens": {{
94101
Entity: "auth",
95102
Action: "read",

loopd/swapclient_server.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightninglabs/aperture/lsat"
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"
@@ -1368,6 +1369,161 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
13681369
return &clientrpc.WithdrawDepositsResponse{}, err
13691370
}
13701371

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

0 commit comments

Comments
 (0)