Skip to content

Commit 2b1aa55

Browse files
committed
tbd
1 parent 6c0210c commit 2b1aa55

File tree

11 files changed

+1016
-237
lines changed

11 files changed

+1016
-237
lines changed

cmd/loop/staticaddr.go

Lines changed: 65 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

@@ -183,6 +184,70 @@ 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|withdrawn|expired].",
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 "deposited":
221+
filterState = looprpc.DepositState_DEPOSITED
222+
223+
case "withdrawn":
224+
filterState = looprpc.DepositState_WITHDRAWN
225+
226+
case "expired":
227+
filterState = looprpc.DepositState_EXPIRED
228+
229+
case "":
230+
filterState = looprpc.DepositState_EMPTY
231+
232+
default:
233+
return fmt.Errorf("unknown filter state: %v",
234+
ctx.String("filter"))
235+
236+
}
237+
resp, err := client.GetStaticAddressSummary(
238+
ctxb, &looprpc.StaticAddressSummaryRequest{
239+
StateFilter: filterState,
240+
},
241+
)
242+
if err != nil {
243+
return err
244+
}
245+
246+
printRespJSON(resp)
247+
248+
return nil
249+
}
250+
186251
func utxosToOutpoints(utxos []string) ([]*looprpc.OutPoint, error) {
187252
var outpoints []*looprpc.OutPoint
188253
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: 137 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"
@@ -1319,6 +1320,142 @@ 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, error) {
1338+
1339+
var (
1340+
totalNumSwaps = len(deposits)
1341+
valueUnconfirmed int64
1342+
valueDeposited int64
1343+
valueExpired int64
1344+
valueWithdrawn int64
1345+
valueLoopedIn int64
1346+
)
1347+
1348+
// Value unconfirmed.
1349+
utxos, err := s.staticAddressManager.ListUnspent(ctx, 0, deposit.MinConfs-1)
1350+
if err != nil {
1351+
return nil, err
1352+
}
1353+
for _, u := range utxos {
1354+
valueUnconfirmed += int64(u.Value)
1355+
}
1356+
1357+
for _, d := range deposits {
1358+
value := int64(d.Value)
1359+
switch d.State {
1360+
case deposit.Deposited:
1361+
valueDeposited += value
1362+
1363+
case deposit.SweptExpiredDeposit:
1364+
valueExpired += value
1365+
1366+
case deposit.Withdrawn:
1367+
valueWithdrawn += value
1368+
}
1369+
}
1370+
1371+
clientDeposits, err := s.filterClientDeposits(deposits, filter)
1372+
if err != nil {
1373+
return nil, err
1374+
}
1375+
1376+
params, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
1377+
if err != nil {
1378+
return nil, err
1379+
}
1380+
1381+
address, err := s.staticAddressManager.GetTaprootAddress(
1382+
params.ClientPubkey, params.ServerPubkey, int64(params.Expiry),
1383+
)
1384+
1385+
return &clientrpc.StaticAddressSummaryResponse{
1386+
StaticAddress: address.String(),
1387+
TotalNumSwaps: uint32(totalNumSwaps),
1388+
ValueUnconfirmed: valueUnconfirmed,
1389+
ValueDeposited: valueDeposited,
1390+
ValueExpired: valueExpired,
1391+
ValueWithdrawn: valueWithdrawn,
1392+
ValueLoopedIn: valueLoopedIn,
1393+
FilteredDeposits: clientDeposits,
1394+
}, nil
1395+
}
1396+
1397+
func (s *swapClientServer) filterClientDeposits(deposits []*deposit.Deposit,
1398+
filterState clientrpc.DepositState) ([]*clientrpc.Deposit, error) {
1399+
1400+
var clientDeposits []*clientrpc.Deposit
1401+
for _, d := range deposits {
1402+
if filterState != clientrpc.DepositState_EMPTY &&
1403+
d.State != toServerState(filterState) {
1404+
1405+
continue
1406+
}
1407+
1408+
outpoint := wire.NewOutPoint(&d.Hash, d.Index).String()
1409+
clientDeposits = append(clientDeposits, &clientrpc.Deposit{
1410+
Id: d.ID[:],
1411+
State: toClientState(d.State),
1412+
Outpoint: outpoint,
1413+
Value: int64(d.Value),
1414+
ConfirmationHeight: d.ConfirmationHeight,
1415+
})
1416+
}
1417+
1418+
return clientDeposits, nil
1419+
}
1420+
1421+
func toClientState(state fsm.StateType) clientrpc.DepositState {
1422+
switch state {
1423+
case deposit.Deposited:
1424+
return clientrpc.DepositState_DEPOSITED
1425+
1426+
case deposit.SweptExpiredDeposit:
1427+
return clientrpc.DepositState_EXPIRED
1428+
1429+
case deposit.Withdrawn:
1430+
return clientrpc.DepositState_WITHDRAWN
1431+
1432+
case deposit.Failed:
1433+
return clientrpc.DepositState_FAILED_STATE
1434+
1435+
default:
1436+
return clientrpc.DepositState_EMPTY
1437+
}
1438+
}
1439+
1440+
func toServerState(state clientrpc.DepositState) fsm.StateType {
1441+
switch state {
1442+
case clientrpc.DepositState_DEPOSITED:
1443+
return deposit.Deposited
1444+
1445+
case clientrpc.DepositState_EXPIRED:
1446+
return deposit.SweptExpiredDeposit
1447+
1448+
case clientrpc.DepositState_WITHDRAWN:
1449+
return deposit.Withdrawn
1450+
1451+
case clientrpc.DepositState_FAILED_STATE:
1452+
return deposit.Failed
1453+
1454+
default:
1455+
return fsm.EmptyState
1456+
}
1457+
}
1458+
13221459
func toServerOutpoints(outpoints []*clientrpc.OutPoint) ([]wire.OutPoint,
13231460
error) {
13241461

0 commit comments

Comments
 (0)