github.com/lmittmann/w3@v0.20.0/module/txpool/content.go (about) 1 package txpool 2 3 import ( 4 "encoding/json" 5 "sort" 6 7 "github.com/ethereum/go-ethereum/common" 8 "github.com/ethereum/go-ethereum/core/types" 9 "github.com/lmittmann/w3/internal/module" 10 "github.com/lmittmann/w3/w3types" 11 ) 12 13 // Content requests the pending and queued transactions in the transaction pool. 14 func Content() w3types.RPCCallerFactory[*ContentResponse] { 15 return module.NewFactory[*ContentResponse]( 16 "txpool_content", 17 nil, 18 ) 19 } 20 21 // ContentFrom requests the pending and queued transactions in the transaction pool 22 // from the given address. 23 func ContentFrom(addr common.Address) w3types.RPCCallerFactory[*ContentFromResponse] { 24 return module.NewFactory[*ContentFromResponse]( 25 "txpool_contentFrom", 26 []any{addr}, 27 ) 28 } 29 30 type ContentResponse struct { 31 Pending map[common.Address][]*types.Transaction 32 Queued map[common.Address][]*types.Transaction 33 } 34 35 func (c *ContentResponse) UnmarshalJSON(data []byte) error { 36 type contentResponse struct { 37 Pending map[common.Address]map[uint64]*types.Transaction `json:"pending"` 38 Queued map[common.Address]map[uint64]*types.Transaction `json:"queued"` 39 } 40 41 var dec contentResponse 42 if err := json.Unmarshal(data, &dec); err != nil { 43 return err 44 } 45 46 c.Pending = make(map[common.Address][]*types.Transaction, len(dec.Pending)) 47 for addr, nonceTx := range dec.Pending { 48 txs := make(types.TxByNonce, 0, len(nonceTx)) 49 for _, tx := range nonceTx { 50 txs = append(txs, tx) 51 } 52 sort.Sort(txs) 53 c.Pending[addr] = txs 54 } 55 56 c.Queued = make(map[common.Address][]*types.Transaction, len(dec.Queued)) 57 for addr, nonceTx := range dec.Queued { 58 txs := make(types.TxByNonce, 0, len(nonceTx)) 59 for _, tx := range nonceTx { 60 txs = append(txs, tx) 61 } 62 sort.Sort(txs) 63 c.Queued[addr] = txs 64 } 65 66 return nil 67 } 68 69 type ContentFromResponse struct { 70 Pending []*types.Transaction 71 Queued []*types.Transaction 72 } 73 74 func (cf *ContentFromResponse) UnmarshalJSON(data []byte) error { 75 type contentFromResponse struct { 76 Pending map[uint64]*types.Transaction `json:"pending"` 77 Queued map[uint64]*types.Transaction `json:"queued"` 78 } 79 80 var dec contentFromResponse 81 if err := json.Unmarshal(data, &dec); err != nil { 82 return err 83 } 84 85 txs := make(types.TxByNonce, 0, len(dec.Pending)) 86 for _, tx := range dec.Pending { 87 txs = append(txs, tx) 88 } 89 sort.Sort(txs) 90 cf.Pending = txs 91 92 txs = make(types.TxByNonce, 0, len(dec.Queued)) 93 for _, tx := range dec.Queued { 94 txs = append(txs, tx) 95 } 96 sort.Sort(txs) 97 cf.Queued = txs 98 99 return nil 100 }