1- // Copyright (c) 2018-2024 , The Decred developers
1+ // Copyright (c) 2018-2025 , The Decred developers
22// Copyright (c) 2017, The dcrdata developers
33// See LICENSE for details.
44
55package explorer
66
77import (
8- "context"
98 "encoding/hex"
109 "encoding/json"
1110 "errors"
@@ -1389,62 +1388,38 @@ type TreasuryInfo struct {
13891388 Path string
13901389 Limit , Offset int64 // ?n=Limit&start=Offset
13911390 TxnType string // ?txntype=TxnType
1392-
1393- // TODO: tadd and tspend can be unconfirmed. tspend for a very long time.
1394- // NumUnconfirmed is the number of unconfirmed txns
1395- // NumUnconfirmed int64
1396- // UnconfirmedTxns []*dbtypes.TreasuryTx
1397-
13981391 // Transactions on the current page
13991392 Transactions []* dbtypes.TreasuryTx
14001393 NumTransactions int64 // len(Transactions) but int64 for dumb template
14011394
14021395 Balance * dbtypes.TreasuryBalance
14031396 ConvertedBalance * exchanges.Conversion
14041397 TypeCount int64
1398+
1399+ // tadd and tspend can be unconfirmed. tspend for a very long time.
1400+ Mempool * TreasuryMempoolInfo
1401+ }
1402+
1403+ // TreasuryMempoolInfo holds the treasury-related mempool transactions that are
1404+ // not yet confirmed in a block. It is used to display treasury mempool
1405+ // information on the treasury page.
1406+ type TreasuryMempoolInfo struct {
1407+ NumTSpends int
1408+ NumTAdds int
1409+ TSpends []types.MempoolTx
1410+ TAdds []types.MempoolTx
14051411}
14061412
14071413// TreasuryPage is the page handler for the "/treasury" path
14081414func (exp * explorerUI ) TreasuryPage (w http.ResponseWriter , r * http.Request ) {
1409- ctx := context .WithValue (r .Context (), ctxAddress , exp .pageData .HomeInfo .DevAddress )
1410- r = r .WithContext (ctx )
1411- if queryVals := r .URL .Query (); queryVals .Get ("txntype" ) == "" {
1412- queryVals .Set ("txntype" , "tspend" )
1413- r .URL .RawQuery = queryVals .Encode ()
1414- }
1415-
1416- limitN := defaultAddressRows
1417- if nParam := r .URL .Query ().Get ("n" ); nParam != "" {
1418- val , err := strconv .ParseUint (nParam , 10 , 64 )
1419- if err != nil {
1420- exp .StatusPage (w , defaultErrorCode , "invalid n value" , "" , ExpStatusError )
1421- return
1422- }
1423- if int64 (val ) > MaxTreasuryRows {
1424- log .Warnf ("TreasuryPage: requested up to %d address rows, " +
1425- "limiting to %d" , limitN , MaxTreasuryRows )
1426- limitN = MaxTreasuryRows
1427- } else {
1428- limitN = int64 (val )
1429- }
1430- }
1431-
1432- // Number of txns to skip (OFFSET in database query). For UX reasons, the
1433- // "start" URL query parameter is used.
1434- var offset int64
1435- if startParam := r .URL .Query ().Get ("start" ); startParam != "" {
1436- val , err := strconv .ParseUint (startParam , 10 , 64 )
1437- if err != nil {
1438- exp .StatusPage (w , defaultErrorCode , "invalid start value" , "" , ExpStatusError )
1439- return
1440- }
1441- offset = int64 (val )
1415+ // Grab the URL query parameters
1416+ txType , txTypeStr , limitN , offset , err := parseTreasuryParams (r )
1417+ if err != nil {
1418+ log .Errorf ("TreasuryPage request error: %v" , err )
1419+ http .Error (w , http .StatusText (http .StatusBadRequest ), http .StatusBadRequest )
1420+ return
14421421 }
14431422
1444- // Transaction types to show.
1445- txTypeStr := r .URL .Query ().Get ("txntype" )
1446- txType := parseTreasuryTransactionType (txTypeStr )
1447-
14481423 txns , err := exp .dataSource .TreasuryTxns (limitN , offset , txType )
14491424 if exp .timeoutErrorPage (w , err , "TreasuryTxns" ) {
14501425 return
@@ -1458,6 +1433,7 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14581433 exp .pageData .RUnlock ()
14591434
14601435 typeCount := treasuryTypeCount (treasuryBalance , txType )
1436+ inv := exp .MempoolInventory ()
14611437
14621438 treasuryData := & TreasuryInfo {
14631439 Net : exp .ChainParams .Net .String (),
@@ -1470,6 +1446,12 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14701446 Transactions : txns ,
14711447 Balance : treasuryBalance ,
14721448 TypeCount : typeCount ,
1449+ Mempool : & TreasuryMempoolInfo {
1450+ NumTSpends : inv .NumTSpends ,
1451+ NumTAdds : inv .NumTAdds ,
1452+ TSpends : inv .TSpends ,
1453+ TAdds : inv .TAdds ,
1454+ },
14731455 }
14741456
14751457 xcBot := exp .xcBot
@@ -1478,7 +1460,7 @@ func (exp *explorerUI) TreasuryPage(w http.ResponseWriter, r *http.Request) {
14781460 }
14791461
14801462 // Execute the HTML template.
1481- linkTemplate := fmt .Sprintf ("/treasury?start=%%d&n=%d&txntype=%v " , limitN , txType )
1463+ linkTemplate := fmt .Sprintf ("/treasury?start=%%d&n=%d&txntype=%s " , limitN , txTypeStr )
14821464 pageData := struct {
14831465 * CommonPageData
14841466 Data * TreasuryInfo
@@ -1679,7 +1661,7 @@ func (exp *explorerUI) AddressTable(w http.ResponseWriter, r *http.Request) {
16791661// TreasuryTable is the handler for the "/treasurytable" path.
16801662func (exp * explorerUI ) TreasuryTable (w http.ResponseWriter , r * http.Request ) {
16811663 // Grab the URL query parameters
1682- txType , limitN , offset , err := parseTreasuryParams (r )
1664+ txType , txTypeStr , limitN , offset , err := parseTreasuryParams (r )
16831665 if err != nil {
16841666 log .Errorf ("TreasuryTable request error: %v" , err )
16851667 http .Error (w , http .StatusText (http .StatusBadRequest ), http .StatusBadRequest )
@@ -1698,7 +1680,7 @@ func (exp *explorerUI) TreasuryTable(w http.ResponseWriter, r *http.Request) {
16981680 bal := exp .pageData .HomeInfo .TreasuryBalance
16991681 exp .pageData .RUnlock ()
17001682
1701- linkTemplate := "/treasury" + "?start=%d&n=" + strconv .FormatInt (limitN , 10 ) + "&txntype=" + fmt .Sprintf ("%v " , txType )
1683+ linkTemplate := "/treasury" + "?start=%d&n=" + strconv .FormatInt (limitN , 10 ) + "&txntype=" + fmt .Sprintf ("%s " , txTypeStr )
17021684
17031685 response := struct {
17041686 TxnCount int64 `json:"tx_count"`
@@ -1791,9 +1773,9 @@ func parseTreasuryTransactionType(txnTypeStr string) (txType stake.TxType) {
17911773
17921774// parseTreasuryParams parses the tx filters for the treasury page. Used by both
17931775// TreasuryPage and TreasuryTable.
1794- func parseTreasuryParams (r * http.Request ) (txType stake.TxType , limitN , offsetAddrOuts int64 , err error ) {
1795- tType , limitN , offsetAddrOuts , err : = parsePaginationParams (r )
1796- txType = parseTreasuryTransactionType (tType )
1776+ func parseTreasuryParams (r * http.Request ) (txType stake.TxType , txTypeStr string , limitN , offsetAddrOuts int64 , err error ) {
1777+ txTypeStr , limitN , offsetAddrOuts , err = parsePaginationParams (r )
1778+ txType = parseTreasuryTransactionType (txTypeStr )
17971779 return
17981780}
17991781
@@ -1805,7 +1787,6 @@ func parsePaginationParams(r *http.Request) (txnType string, limitN, offset int6
18051787 limitN = defaultAddressRows
18061788
18071789 if nParam := r .URL .Query ().Get ("n" ); nParam != "" {
1808-
18091790 var val uint64
18101791 val , err = strconv .ParseUint (nParam , 10 , 64 )
18111792 if err != nil {
@@ -1834,9 +1815,8 @@ func parsePaginationParams(r *http.Request) (txnType string, limitN, offset int6
18341815 }
18351816
18361817 // Transaction types to show.
1837- txnType = r .URL .Query ().Get ("txntype" )
1838- if txnType == "" {
1839- txnType = "all"
1818+ if txnType = r .URL .Query ().Get ("txntype" ); txnType == "" {
1819+ txnType = "tspend" // Default to tspend
18401820 }
18411821
18421822 return
0 commit comments