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