Skip to content

Commit 6672a00

Browse files
committed
tbd
1 parent 6c0210c commit 6672a00

File tree

11 files changed

+1061
-236
lines changed

11 files changed

+1061
-236
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

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

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

loopd/perms/perms.go

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

+158
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"
@@ -1319,6 +1320,163 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
13191320
return &clientrpc.WithdrawDepositsResponse{}, err
13201321
}
13211322

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

0 commit comments

Comments
 (0)