@@ -38,6 +38,7 @@ import (
3838 "github.com/lightninglabs/taproot-assets/rfqmath"
3939 "github.com/lightningnetwork/lnd/lnrpc/walletrpc"
4040 "github.com/lightningnetwork/lnd/lntypes"
41+ "github.com/lightningnetwork/lnd/lnwallet"
4142 "github.com/lightningnetwork/lnd/queue"
4243 "github.com/lightningnetwork/lnd/routing/route"
4344 "github.com/lightningnetwork/lnd/zpay32"
@@ -1914,78 +1915,142 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
19141915 _ * looprpc.StaticAddressSummaryRequest ) (
19151916 * looprpc.StaticAddressSummaryResponse , error ) {
19161917
1918+ summaries := make (map [string ]* looprpc.StaticAddressSummary )
1919+
19171920 allDeposits , err := s .depositManager .GetAllDeposits (ctx )
19181921 if err != nil {
19191922 return nil , err
19201923 }
19211924
1922- var (
1923- totalNumDeposits = len (allDeposits )
1924- valueUnconfirmed int64
1925- valueDeposited int64
1926- valueExpired int64
1927- valueWithdrawn int64
1928- valueLoopedIn int64
1929- htlcTimeoutSwept int64
1930- )
1925+ pkScriptDepositMap := make (map [string ][]* deposit.Deposit )
1926+ for _ , d := range allDeposits {
1927+ pkScript := string (d .AddressParams .PkScript )
1928+ pkScriptDepositMap [pkScript ] = append (
1929+ pkScriptDepositMap [pkScript ], d ,
1930+ )
1931+ }
19311932
1932- // Value unconfirmed.
1933- utxos , err := s .staticAddressManager .ListUnspent (
1934- ctx , 0 , deposit .MinConfs - 1 ,
1935- )
1933+ network , err := s .network .ChainParams ()
19361934 if err != nil {
19371935 return nil , err
19381936 }
1939- for _ , u := range utxos {
1940- valueUnconfirmed += int64 (u .Value )
1941- }
1937+ for pkScript , deposits := range pkScriptDepositMap {
1938+ if len (deposits ) == 0 {
1939+ continue
1940+ }
19421941
1943- // Confirmed total values by category.
1944- for _ , d := range allDeposits {
1945- value := int64 (d .Value )
1946- switch d .GetState () {
1947- case deposit .Deposited :
1948- valueDeposited += value
1942+ address , err := deposits [0 ].AddressParams .TaprootAddress (network )
1943+ if err != nil {
1944+ return nil , err
1945+ }
1946+ summary := & looprpc.StaticAddressSummary {
1947+ StaticAddress : address ,
1948+ RelativeExpiryBlocks : uint64 (deposits [0 ].AddressParams .Expiry ),
1949+ TotalNumDeposits : uint32 (len (deposits )),
1950+ }
1951+
1952+ for _ , d := range deposits {
1953+ value := int64 (d .Value )
1954+ switch d .GetState () {
1955+ case deposit .Deposited :
1956+ summary .ValueDepositedSatoshis += value
19491957
1950- case deposit .Expired :
1951- valueExpired += value
1958+ case deposit .Expired :
1959+ summary . ValueExpiredSatoshis += value
19521960
1953- case deposit .Withdrawn :
1954- valueWithdrawn += value
1961+ case deposit .Withdrawn :
1962+ summary . ValueWithdrawnSatoshis += value
19551963
1956- case deposit .LoopedIn :
1957- valueLoopedIn += value
1964+ case deposit .LoopedIn :
1965+ summary . ValueLoopedInSatoshis += value
19581966
1959- case deposit .HtlcTimeoutSwept :
1960- htlcTimeoutSwept += value
1967+ case deposit .HtlcTimeoutSwept :
1968+ summary .ValueHtlcTimeoutSweepsSatoshis += value
1969+ }
19611970 }
1971+
1972+ // Store per-address summary pointer directly.
1973+ summaries [pkScript ] = summary
19621974 }
19631975
1964- params , err := s .staticAddressManager .GetStaticAddressParameters (ctx )
1976+ listUnspentMap := make (map [string ][]* lnwallet.Utxo )
1977+ preDepositedUtxos , err := s .staticAddressManager .ListUnspent (
1978+ ctx , 0 , deposit .MinConfs - 1 ,
1979+ )
19651980 if err != nil {
19661981 return nil , err
19671982 }
19681983
1969- network , err := s .network .ChainParams ()
1984+ for _ , utxo := range preDepositedUtxos {
1985+ pkScript := string (utxo .PkScript )
1986+ listUnspentMap [pkScript ] = append (
1987+ listUnspentMap [pkScript ], utxo ,
1988+ )
1989+ }
1990+
1991+ for pkScript , utxos := range listUnspentMap {
1992+ notDepositedValue := int64 (0 )
1993+ for _ , utxo := range utxos {
1994+ notDepositedValue += int64 (utxo .Value )
1995+ }
1996+
1997+ if _ , ok := summaries [pkScript ]; ok {
1998+ summaries [pkScript ].ValueUnconfirmedSatoshis =
1999+ notDepositedValue
2000+ } else {
2001+ params := s .staticAddressManager .GetParameters ([]byte (pkScript ))
2002+ staticAddress , err := params .TaprootAddress (network )
2003+ if err != nil {
2004+ return nil , err
2005+ }
2006+ summaries [pkScript ] = & looprpc.StaticAddressSummary {
2007+ StaticAddress : staticAddress ,
2008+ RelativeExpiryBlocks : uint64 (params .Expiry ),
2009+ ValueUnconfirmedSatoshis : notDepositedValue ,
2010+ }
2011+ }
2012+ }
2013+
2014+ deprecatedParams , err := s .staticAddressManager .GetStaticAddressParameters (ctx )
19702015 if err != nil {
19712016 return nil , err
19722017 }
19732018
1974- address , err := params .TaprootAddress (network )
2019+ deprecatedAddress , err := deprecatedParams .TaprootAddress (network )
19752020 if err != nil {
19762021 return nil , err
19772022 }
19782023
2024+ deprecatedSummary := & looprpc.StaticAddressSummary {
2025+ StaticAddress : deprecatedAddress ,
2026+ RelativeExpiryBlocks : 0 ,
2027+ TotalNumDeposits : 0 ,
2028+ ValueDepositedSatoshis : 0 ,
2029+ ValueExpiredSatoshis : 0 ,
2030+ ValueWithdrawnSatoshis : 0 ,
2031+ ValueLoopedInSatoshis : 0 ,
2032+ ValueHtlcTimeoutSweepsSatoshis : 0 ,
2033+ ValueUnconfirmedSatoshis : 0 ,
2034+ }
2035+ results := make ([]* looprpc.StaticAddressSummary , 0 , len (summaries ))
2036+ for _ , summary := range summaries {
2037+ if summary .StaticAddress == deprecatedAddress {
2038+ deprecatedSummary = summary
2039+ }
2040+ results = append (results , summary )
2041+ }
2042+
19792043 return & looprpc.StaticAddressSummaryResponse {
1980- StaticAddress : address ,
1981- RelativeExpiryBlocks : uint64 (params .Expiry ),
1982- TotalNumDeposits : uint32 (totalNumDeposits ),
1983- ValueUnconfirmedSatoshis : valueUnconfirmed ,
1984- ValueDepositedSatoshis : valueDeposited ,
1985- ValueExpiredSatoshis : valueExpired ,
1986- ValueWithdrawnSatoshis : valueWithdrawn ,
1987- ValueLoopedInSatoshis : valueLoopedIn ,
1988- ValueHtlcTimeoutSweepsSatoshis : htlcTimeoutSwept ,
2044+ StaticAddress : deprecatedSummary .StaticAddress ,
2045+ RelativeExpiryBlocks : deprecatedSummary .RelativeExpiryBlocks ,
2046+ TotalNumDeposits : deprecatedSummary .TotalNumDeposits ,
2047+ ValueUnconfirmedSatoshis : deprecatedSummary .ValueUnconfirmedSatoshis ,
2048+ ValueDepositedSatoshis : deprecatedSummary .ValueDepositedSatoshis ,
2049+ ValueExpiredSatoshis : deprecatedSummary .ValueExpiredSatoshis ,
2050+ ValueWithdrawnSatoshis : deprecatedSummary .ValueWithdrawnSatoshis ,
2051+ ValueLoopedInSatoshis : deprecatedSummary .ValueLoopedInSatoshis ,
2052+ ValueHtlcTimeoutSweepsSatoshis : deprecatedSummary .ValueHtlcTimeoutSweepsSatoshis ,
2053+ PerAddressSummaries : results ,
19892054 }, nil
19902055}
19912056
0 commit comments