Skip to content

Commit de23774

Browse files
committed
tbd
1 parent 4e6752f commit de23774

File tree

9 files changed

+1001
-233
lines changed

9 files changed

+1001
-233
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: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"errors"
88
"fmt"
9+
"github.com/lightninglabs/loop/fsm"
910
"reflect"
1011
"sort"
1112
"strings"
@@ -1319,6 +1320,138 @@ 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+
address, err := s.staticAddressManager.NewAddress(ctx)
1377+
if err != nil {
1378+
return nil, err
1379+
}
1380+
1381+
return &clientrpc.StaticAddressSummaryResponse{
1382+
StaticAddress: address.String(),
1383+
TotalNumSwaps: uint32(totalNumSwaps),
1384+
ValueUnconfirmed: valueUnconfirmed,
1385+
ValueDeposited: valueDeposited,
1386+
ValueExpired: valueExpired,
1387+
ValueWithdrawn: valueWithdrawn,
1388+
ValueLoopedIn: valueLoopedIn,
1389+
FilteredDeposits: clientDeposits,
1390+
}, nil
1391+
}
1392+
1393+
func (s *swapClientServer) filterClientDeposits(deposits []*deposit.Deposit,
1394+
filterState clientrpc.DepositState) ([]*clientrpc.Deposit, error) {
1395+
1396+
var clientDeposits []*clientrpc.Deposit
1397+
for _, d := range deposits {
1398+
if filterState != clientrpc.DepositState_EMPTY &&
1399+
d.State != toServerState(filterState) {
1400+
1401+
continue
1402+
}
1403+
1404+
outpoint := wire.NewOutPoint(&d.Hash, d.Index).String()
1405+
clientDeposits = append(clientDeposits, &clientrpc.Deposit{
1406+
Id: d.ID[:],
1407+
State: toClientState(d.State),
1408+
Outpoint: outpoint,
1409+
Value: int64(d.Value),
1410+
ConfirmationHeight: d.ConfirmationHeight,
1411+
})
1412+
}
1413+
1414+
return clientDeposits, nil
1415+
}
1416+
1417+
func toClientState(state fsm.StateType) clientrpc.DepositState {
1418+
switch state {
1419+
case deposit.Deposited:
1420+
return clientrpc.DepositState_DEPOSITED
1421+
1422+
case deposit.SweptExpiredDeposit:
1423+
return clientrpc.DepositState_EXPIRED
1424+
1425+
case deposit.Withdrawn:
1426+
return clientrpc.DepositState_WITHDRAWN
1427+
1428+
case deposit.Failed:
1429+
return clientrpc.DepositState_FAILED_STATE
1430+
1431+
default:
1432+
return clientrpc.DepositState_EMPTY
1433+
}
1434+
}
1435+
1436+
func toServerState(state clientrpc.DepositState) fsm.StateType {
1437+
switch state {
1438+
case clientrpc.DepositState_DEPOSITED:
1439+
return deposit.Deposited
1440+
1441+
case clientrpc.DepositState_EXPIRED:
1442+
return deposit.SweptExpiredDeposit
1443+
1444+
case clientrpc.DepositState_WITHDRAWN:
1445+
return deposit.Withdrawn
1446+
1447+
case clientrpc.DepositState_FAILED_STATE:
1448+
return deposit.Failed
1449+
1450+
default:
1451+
return fsm.EmptyState
1452+
}
1453+
}
1454+
13221455
func toServerOutpoints(outpoints []*clientrpc.OutPoint) ([]wire.OutPoint,
13231456
error) {
13241457

0 commit comments

Comments
 (0)