Skip to content

Commit 59ec90c

Browse files
committed
loopd: multi addresses in GetStaticAddressSummary
1 parent 04d3c73 commit 59ec90c

File tree

1 file changed

+111
-46
lines changed

1 file changed

+111
-46
lines changed

loopd/swapclient_server.go

Lines changed: 111 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/lightninglabs/taproot-assets/rfqmath"
4040
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
4141
"github.com/lightningnetwork/lnd/lntypes"
42+
"github.com/lightningnetwork/lnd/lnwallet"
4243
"github.com/lightningnetwork/lnd/queue"
4344
"github.com/lightningnetwork/lnd/routing/route"
4445
"github.com/lightningnetwork/lnd/zpay32"
@@ -1986,83 +1987,147 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
19861987
_ *looprpc.StaticAddressSummaryRequest) (
19871988
*looprpc.StaticAddressSummaryResponse, error) {
19881989

1990+
summaries := make(map[string]*looprpc.StaticAddressSummary)
1991+
19891992
allDeposits, err := s.depositManager.GetAllDeposits(ctx)
19901993
if err != nil {
19911994
return nil, err
19921995
}
19931996

1994-
var (
1995-
totalNumDeposits = len(allDeposits)
1996-
valueUnconfirmed int64
1997-
valueDeposited int64
1998-
valueExpired int64
1999-
valueWithdrawn int64
2000-
valueLoopedIn int64
2001-
valueChannelsOpened int64
2002-
htlcTimeoutSwept int64
2003-
)
1997+
pkScriptDepositMap := make(map[string][]*deposit.Deposit)
1998+
for _, d := range allDeposits {
1999+
pkScript := string(d.AddressParams.PkScript)
2000+
pkScriptDepositMap[pkScript] = append(
2001+
pkScriptDepositMap[pkScript], d,
2002+
)
2003+
}
20042004

2005-
// Value unconfirmed.
2006-
utxos, err := s.staticAddressManager.ListUnspent(
2007-
ctx, 0, deposit.MinConfs-1,
2008-
)
2005+
network, err := s.network.ChainParams()
20092006
if err != nil {
20102007
return nil, err
20112008
}
2012-
for _, u := range utxos {
2013-
valueUnconfirmed += int64(u.Value)
2014-
}
2009+
for pkScript, deposits := range pkScriptDepositMap {
2010+
if len(deposits) == 0 {
2011+
continue
2012+
}
20152013

2016-
// Confirmed total values by category.
2017-
for _, d := range allDeposits {
2018-
value := int64(d.Value)
2019-
switch d.GetState() {
2020-
case deposit.Deposited:
2021-
valueDeposited += value
2014+
address, err := deposits[0].AddressParams.TaprootAddress(network)
2015+
if err != nil {
2016+
return nil, err
2017+
}
2018+
summary := &looprpc.StaticAddressSummary{
2019+
StaticAddress: address,
2020+
RelativeExpiryBlocks: uint64(deposits[0].AddressParams.Expiry),
2021+
TotalNumDeposits: uint32(len(deposits)),
2022+
}
2023+
2024+
for _, d := range deposits {
2025+
value := int64(d.Value)
2026+
switch d.GetState() {
2027+
case deposit.Deposited:
2028+
summary.ValueDepositedSatoshis += value
20222029

2023-
case deposit.Expired:
2024-
valueExpired += value
2030+
case deposit.Expired:
2031+
summary.ValueExpiredSatoshis += value
20252032

2026-
case deposit.Withdrawn:
2027-
valueWithdrawn += value
2033+
case deposit.Withdrawn:
2034+
summary.ValueWithdrawnSatoshis += value
20282035

2029-
case deposit.LoopedIn:
2030-
valueLoopedIn += value
2036+
case deposit.LoopedIn:
2037+
summary.ValueLoopedInSatoshis += value
20312038

2032-
case deposit.HtlcTimeoutSwept:
2033-
htlcTimeoutSwept += value
2039+
case deposit.HtlcTimeoutSwept:
2040+
summary.ValueHtlcTimeoutSweepsSatoshis += value
20342041

2035-
case deposit.ChannelPublished:
2036-
valueChannelsOpened += value
2042+
case deposit.ChannelPublished:
2043+
summary.ValueChannelsOpened += value
2044+
}
20372045
}
2046+
2047+
// Store per-address summary pointer directly.
2048+
summaries[pkScript] = summary
20382049
}
20392050

2040-
params, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
2051+
listUnspentMap := make(map[string][]*lnwallet.Utxo)
2052+
preDepositedUtxos, err := s.staticAddressManager.ListUnspent(
2053+
ctx, 0, deposit.MinConfs-1,
2054+
)
20412055
if err != nil {
20422056
return nil, err
20432057
}
20442058

2045-
network, err := s.network.ChainParams()
2059+
for _, utxo := range preDepositedUtxos {
2060+
pkScript := string(utxo.PkScript)
2061+
listUnspentMap[pkScript] = append(
2062+
listUnspentMap[pkScript], utxo,
2063+
)
2064+
}
2065+
2066+
for pkScript, utxos := range listUnspentMap {
2067+
notDepositedValue := int64(0)
2068+
for _, utxo := range utxos {
2069+
notDepositedValue += int64(utxo.Value)
2070+
}
2071+
2072+
if _, ok := summaries[pkScript]; ok {
2073+
summaries[pkScript].ValueUnconfirmedSatoshis =
2074+
notDepositedValue
2075+
} else {
2076+
params := s.staticAddressManager.GetParameters([]byte(pkScript))
2077+
staticAddress, err := params.TaprootAddress(network)
2078+
if err != nil {
2079+
return nil, err
2080+
}
2081+
summaries[pkScript] = &looprpc.StaticAddressSummary{
2082+
StaticAddress: staticAddress,
2083+
RelativeExpiryBlocks: uint64(params.Expiry),
2084+
ValueUnconfirmedSatoshis: notDepositedValue,
2085+
}
2086+
}
2087+
}
2088+
2089+
deprecatedParams, err := s.staticAddressManager.GetStaticAddressParameters(ctx)
20462090
if err != nil {
20472091
return nil, err
20482092
}
20492093

2050-
address, err := params.TaprootAddress(network)
2094+
deprecatedAddress, err := deprecatedParams.TaprootAddress(network)
20512095
if err != nil {
20522096
return nil, err
20532097
}
20542098

2099+
deprecatedSummary := &looprpc.StaticAddressSummary{
2100+
StaticAddress: deprecatedAddress,
2101+
RelativeExpiryBlocks: 0,
2102+
TotalNumDeposits: 0,
2103+
ValueDepositedSatoshis: 0,
2104+
ValueExpiredSatoshis: 0,
2105+
ValueWithdrawnSatoshis: 0,
2106+
ValueLoopedInSatoshis: 0,
2107+
ValueChannelsOpened: 0,
2108+
ValueHtlcTimeoutSweepsSatoshis: 0,
2109+
ValueUnconfirmedSatoshis: 0,
2110+
}
2111+
results := make([]*looprpc.StaticAddressSummary, 0, len(summaries))
2112+
for _, summary := range summaries {
2113+
if summary.StaticAddress == deprecatedAddress {
2114+
deprecatedSummary = summary
2115+
}
2116+
results = append(results, summary)
2117+
}
2118+
20552119
return &looprpc.StaticAddressSummaryResponse{
2056-
StaticAddress: address,
2057-
RelativeExpiryBlocks: uint64(params.Expiry),
2058-
TotalNumDeposits: uint32(totalNumDeposits),
2059-
ValueUnconfirmedSatoshis: valueUnconfirmed,
2060-
ValueDepositedSatoshis: valueDeposited,
2061-
ValueExpiredSatoshis: valueExpired,
2062-
ValueWithdrawnSatoshis: valueWithdrawn,
2063-
ValueLoopedInSatoshis: valueLoopedIn,
2064-
ValueChannelsOpened: valueChannelsOpened,
2065-
ValueHtlcTimeoutSweepsSatoshis: htlcTimeoutSwept,
2120+
StaticAddress: deprecatedSummary.StaticAddress,
2121+
RelativeExpiryBlocks: deprecatedSummary.RelativeExpiryBlocks,
2122+
TotalNumDeposits: deprecatedSummary.TotalNumDeposits,
2123+
ValueUnconfirmedSatoshis: deprecatedSummary.ValueUnconfirmedSatoshis,
2124+
ValueDepositedSatoshis: deprecatedSummary.ValueDepositedSatoshis,
2125+
ValueExpiredSatoshis: deprecatedSummary.ValueExpiredSatoshis,
2126+
ValueWithdrawnSatoshis: deprecatedSummary.ValueWithdrawnSatoshis,
2127+
ValueLoopedInSatoshis: deprecatedSummary.ValueLoopedInSatoshis,
2128+
ValueHtlcTimeoutSweepsSatoshis: deprecatedSummary.ValueHtlcTimeoutSweepsSatoshis,
2129+
ValueChannelsOpened: deprecatedSummary.ValueChannelsOpened,
2130+
PerAddressSummaries: results,
20662131
}, nil
20672132
}
20682133

0 commit comments

Comments
 (0)