|
6 | 6 | "encoding/hex" |
7 | 7 | "errors" |
8 | 8 | "fmt" |
| 9 | + "github.com/lightninglabs/loop/fsm" |
9 | 10 | "reflect" |
10 | 11 | "sort" |
11 | 12 | "strings" |
@@ -1319,6 +1320,138 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context, |
1319 | 1320 | return &clientrpc.WithdrawDepositsResponse{}, err |
1320 | 1321 | } |
1321 | 1322 |
|
| 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 | + |
1322 | 1455 | func toServerOutpoints(outpoints []*clientrpc.OutPoint) ([]wire.OutPoint, |
1323 | 1456 | error) { |
1324 | 1457 |
|
|
0 commit comments