@@ -19,6 +19,7 @@ import (
1919 "github.com/lightninglabs/aperture/l402"
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"
@@ -1395,6 +1396,203 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
13951396 return & clientrpc.WithdrawDepositsResponse {}, err
13961397}
13971398
1399+ // GetStaticAddressSummary returns a summary static address related information.
1400+ // Amongst deposits and withdrawals and their total values it also includes a
1401+ // list of detailed deposit information filtered by their state.
1402+ func (s * swapClientServer ) GetStaticAddressSummary (ctx context.Context ,
1403+ req * clientrpc.StaticAddressSummaryRequest ) (
1404+ * clientrpc.StaticAddressSummaryResponse , error ) {
1405+
1406+ if req .StateFilter != clientrpc .DepositState_UNKNOWN_STATE &&
1407+ len (req .Outpoints ) > 0 {
1408+
1409+ return nil , fmt .Errorf ("can either filter by state or " +
1410+ "outpoints" )
1411+ }
1412+
1413+ allDeposits , err := s .depositManager .GetAllDeposits ()
1414+ if err != nil {
1415+ return nil , err
1416+ }
1417+
1418+ return s .depositSummary (
1419+ ctx , allDeposits , req .StateFilter , req .Outpoints ,
1420+ )
1421+ }
1422+
1423+ func (s * swapClientServer ) depositSummary (ctx context.Context ,
1424+ deposits []* deposit.Deposit , stateFilter clientrpc.DepositState ,
1425+ outpointsFilter []string ) (* clientrpc.StaticAddressSummaryResponse ,
1426+ error ) {
1427+
1428+ var (
1429+ totalNumDeposits = len (deposits )
1430+ valueUnconfirmed int64
1431+ valueDeposited int64
1432+ valueExpired int64
1433+ valueWithdrawn int64
1434+ )
1435+
1436+ // Value unconfirmed.
1437+ utxos , err := s .staticAddressManager .ListUnspent (
1438+ ctx , 0 , deposit .MinConfs - 1 ,
1439+ )
1440+ if err != nil {
1441+ return nil , err
1442+ }
1443+ for _ , u := range utxos {
1444+ valueUnconfirmed += int64 (u .Value )
1445+ }
1446+
1447+ // Confirmed total values by category.
1448+ for _ , d := range deposits {
1449+ value := int64 (d .Value )
1450+ switch d .GetState () {
1451+ case deposit .Deposited :
1452+ valueDeposited += value
1453+
1454+ case deposit .Expired :
1455+ valueExpired += value
1456+
1457+ case deposit .Withdrawn :
1458+ valueWithdrawn += value
1459+ }
1460+ }
1461+
1462+ // Deposits filtered by state or outpoints.
1463+ var clientDeposits []* clientrpc.Deposit
1464+ if len (outpointsFilter ) > 0 {
1465+ f := func (d * deposit.Deposit ) bool {
1466+ for _ , outpoint := range outpointsFilter {
1467+ if outpoint == d .OutPoint .String () {
1468+ return true
1469+ }
1470+ }
1471+ return false
1472+ }
1473+ clientDeposits = filter (deposits , f )
1474+
1475+ if len (outpointsFilter ) != len (clientDeposits ) {
1476+ return nil , fmt .Errorf ("not all outpoints found in " +
1477+ "deposits" )
1478+ }
1479+ } else {
1480+ f := func (d * deposit.Deposit ) bool {
1481+ if stateFilter == clientrpc .DepositState_UNKNOWN_STATE {
1482+ // Per default, we return deposits in all
1483+ // states.
1484+ return true
1485+ }
1486+
1487+ return d .GetState () == toServerState (stateFilter )
1488+ }
1489+ clientDeposits = filter (deposits , f )
1490+ }
1491+
1492+ params , err := s .staticAddressManager .GetStaticAddressParameters (ctx )
1493+ if err != nil {
1494+ return nil , err
1495+ }
1496+
1497+ address , err := s .staticAddressManager .GetTaprootAddress (
1498+ params .ClientPubkey , params .ServerPubkey , int64 (params .Expiry ),
1499+ )
1500+ if err != nil {
1501+ return nil , err
1502+ }
1503+
1504+ return & clientrpc.StaticAddressSummaryResponse {
1505+ StaticAddress : address .String (),
1506+ TotalNumDeposits : uint32 (totalNumDeposits ),
1507+ ValueUnconfirmed : valueUnconfirmed ,
1508+ ValueDeposited : valueDeposited ,
1509+ ValueExpired : valueExpired ,
1510+ ValueWithdrawn : valueWithdrawn ,
1511+ FilteredDeposits : clientDeposits ,
1512+ }, nil
1513+ }
1514+
1515+ type filterFunc func (deposits * deposit.Deposit ) bool
1516+
1517+ func filter (deposits []* deposit.Deposit , f filterFunc ) []* clientrpc.Deposit {
1518+ var clientDeposits []* clientrpc.Deposit
1519+ for _ , d := range deposits {
1520+ if ! f (d ) {
1521+ continue
1522+ }
1523+
1524+ hash := d .Hash
1525+ outpoint := wire .NewOutPoint (& hash , d .Index ).String ()
1526+ deposit := & clientrpc.Deposit {
1527+ Id : d .ID [:],
1528+ State : toClientState (d .GetState ()),
1529+ Outpoint : outpoint ,
1530+ Value : int64 (d .Value ),
1531+ ConfirmationHeight : d .ConfirmationHeight ,
1532+ }
1533+
1534+ clientDeposits = append (clientDeposits , deposit )
1535+ }
1536+
1537+ return clientDeposits
1538+ }
1539+
1540+ func toClientState (state fsm.StateType ) clientrpc.DepositState {
1541+ switch state {
1542+ case deposit .Deposited :
1543+ return clientrpc .DepositState_DEPOSITED
1544+
1545+ case deposit .Withdrawing :
1546+ return clientrpc .DepositState_WITHDRAWING
1547+
1548+ case deposit .Withdrawn :
1549+ return clientrpc .DepositState_WITHDRAWN
1550+
1551+ case deposit .PublishExpiredDeposit :
1552+ return clientrpc .DepositState_PUBLISH_EXPIRED
1553+
1554+ case deposit .WaitForExpirySweep :
1555+ return clientrpc .DepositState_WAIT_FOR_EXPIRY_SWEEP
1556+
1557+ case deposit .Expired :
1558+ return clientrpc .DepositState_EXPIRED
1559+
1560+ case deposit .Failed :
1561+ return clientrpc .DepositState_FAILED_STATE
1562+
1563+ default :
1564+ return clientrpc .DepositState_UNKNOWN_STATE
1565+ }
1566+ }
1567+
1568+ func toServerState (state clientrpc.DepositState ) fsm.StateType {
1569+ switch state {
1570+ case clientrpc .DepositState_DEPOSITED :
1571+ return deposit .Deposited
1572+
1573+ case clientrpc .DepositState_WITHDRAWING :
1574+ return deposit .Withdrawing
1575+
1576+ case clientrpc .DepositState_WITHDRAWN :
1577+ return deposit .Withdrawn
1578+
1579+ case clientrpc .DepositState_PUBLISH_EXPIRED :
1580+ return deposit .PublishExpiredDeposit
1581+
1582+ case clientrpc .DepositState_WAIT_FOR_EXPIRY_SWEEP :
1583+ return deposit .WaitForExpirySweep
1584+
1585+ case clientrpc .DepositState_EXPIRED :
1586+ return deposit .Expired
1587+
1588+ case clientrpc .DepositState_FAILED_STATE :
1589+ return deposit .Failed
1590+
1591+ default :
1592+ return fsm .EmptyState
1593+ }
1594+ }
1595+
13981596func toServerOutpoints (outpoints []* clientrpc.OutPoint ) ([]wire.OutPoint ,
13991597 error ) {
14001598
0 commit comments