Skip to content

Commit 7750c43

Browse files
committed
tbd
1 parent db6f5d7 commit 7750c43

File tree

10 files changed

+1008
-204
lines changed

10 files changed

+1008
-204
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"
@@ -1327,6 +1328,161 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
13271328
return &clientrpc.WithdrawDepositsResponse{}, err
13281329
}
13291330

1331+
func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
1332+
req *clientrpc.StaticAddressSummaryRequest) (*clientrpc.StaticAddressSummaryResponse,
1333+
error) {
1334+
1335+
allDeposits, err := s.depositManager.GetAllDeposits()
1336+
if err != nil {
1337+
return nil, err
1338+
}
1339+
1340+
return s.depositSummary(ctx, allDeposits, req.StateFilter)
1341+
}
1342+
1343+
func (s *swapClientServer) depositSummary(ctx context.Context,
1344+
deposits []*deposit.Deposit,
1345+
filter clientrpc.DepositState) (*clientrpc.StaticAddressSummaryResponse,
1346+
error) {
1347+
1348+
var (
1349+
totalNumDeposits = len(deposits)
1350+
valueUnconfirmed int64
1351+
valueDeposited int64
1352+
valueExpired int64
1353+
valueWithdrawn int64
1354+
)
1355+
1356+
// Value unconfirmed.
1357+
utxos, err := s.staticAddressManager.ListUnspent(
1358+
ctx, 0, deposit.MinConfs-1,
1359+
)
1360+
if err != nil {
1361+
return nil, err
1362+
}
1363+
for _, u := range utxos {
1364+
valueUnconfirmed += int64(u.Value)
1365+
}
1366+
1367+
for _, d := range deposits {
1368+
value := int64(d.Value)
1369+
switch d.State {
1370+
case deposit.Deposited:
1371+
valueDeposited += value
1372+
1373+
case deposit.Expired:
1374+
valueExpired += value
1375+
1376+
case deposit.Withdrawn:
1377+
valueWithdrawn += value
1378+
}
1379+
}
1380+
1381+
clientDeposits, err := s.filterClientDeposits(deposits, filter)
1382+
if err != nil {
1383+
return nil, err
1384+
}
1385+
1386+
params, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
1387+
if err != nil {
1388+
return nil, err
1389+
}
1390+
1391+
address, err := s.staticAddressManager.GetTaprootAddress(
1392+
params.ClientPubkey, params.ServerPubkey, int64(params.Expiry),
1393+
)
1394+
1395+
return &clientrpc.StaticAddressSummaryResponse{
1396+
StaticAddress: address.String(),
1397+
TotalNumDeposits: uint32(totalNumDeposits),
1398+
ValueUnconfirmed: valueUnconfirmed,
1399+
ValueDeposited: valueDeposited,
1400+
ValueExpired: valueExpired,
1401+
ValueWithdrawn: valueWithdrawn,
1402+
FilteredDeposits: clientDeposits,
1403+
}, nil
1404+
}
1405+
1406+
func (s *swapClientServer) filterClientDeposits(deposits []*deposit.Deposit,
1407+
filterState clientrpc.DepositState) ([]*clientrpc.Deposit, error) {
1408+
1409+
var clientDeposits []*clientrpc.Deposit
1410+
for _, d := range deposits {
1411+
if filterState != clientrpc.DepositState_UNKNOWN_STATE &&
1412+
d.State != toServerState(filterState) {
1413+
1414+
continue
1415+
}
1416+
1417+
outpoint := wire.NewOutPoint(&d.Hash, d.Index).String()
1418+
clientDeposits = append(clientDeposits, &clientrpc.Deposit{
1419+
Id: d.ID[:],
1420+
State: toClientState(d.State),
1421+
Outpoint: outpoint,
1422+
Value: int64(d.Value),
1423+
ConfirmationHeight: d.ConfirmationHeight,
1424+
})
1425+
}
1426+
1427+
return clientDeposits, nil
1428+
}
1429+
1430+
func toClientState(state fsm.StateType) clientrpc.DepositState {
1431+
switch state {
1432+
case deposit.Deposited:
1433+
return clientrpc.DepositState_DEPOSITED
1434+
1435+
case deposit.Withdrawing:
1436+
return clientrpc.DepositState_WITHDRAWING
1437+
1438+
case deposit.Withdrawn:
1439+
return clientrpc.DepositState_WITHDRAWN
1440+
1441+
case deposit.PublishExpiredDeposit:
1442+
return clientrpc.DepositState_PUBLISH_EXPIRED
1443+
1444+
case deposit.WaitForExpirySweep:
1445+
return clientrpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
1446+
1447+
case deposit.Expired:
1448+
return clientrpc.DepositState_EXPIRED
1449+
1450+
case deposit.Failed:
1451+
return clientrpc.DepositState_FAILED_STATE
1452+
1453+
default:
1454+
return clientrpc.DepositState_UNKNOWN_STATE
1455+
}
1456+
}
1457+
1458+
func toServerState(state clientrpc.DepositState) fsm.StateType {
1459+
switch state {
1460+
case clientrpc.DepositState_DEPOSITED:
1461+
return deposit.Deposited
1462+
1463+
case clientrpc.DepositState_WITHDRAWING:
1464+
return deposit.Withdrawing
1465+
1466+
case clientrpc.DepositState_WITHDRAWN:
1467+
return deposit.Withdrawn
1468+
1469+
case clientrpc.DepositState_PUBLISH_EXPIRED:
1470+
return deposit.PublishExpiredDeposit
1471+
1472+
case clientrpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
1473+
return deposit.WaitForExpirySweep
1474+
1475+
case clientrpc.DepositState_EXPIRED:
1476+
return deposit.Expired
1477+
1478+
case clientrpc.DepositState_FAILED_STATE:
1479+
return deposit.Failed
1480+
1481+
default:
1482+
return fsm.EmptyState
1483+
}
1484+
}
1485+
13301486
func toServerOutpoints(outpoints []*clientrpc.OutPoint) ([]wire.OutPoint,
13311487
error) {
13321488

0 commit comments

Comments
 (0)