Skip to content

Commit

Permalink
module/txpool: added Status()
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Jul 19, 2023
1 parent a2acd31 commit 7e72eb2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
37 changes: 37 additions & 0 deletions module/txpool/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package txpool

import (
"encoding/json"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/lmittmann/w3/internal/module"
"github.com/lmittmann/w3/w3types"
)

// Status requests the txpool status.
func Status() w3types.CallerFactory[StatusResponse] {
return module.NewFactory[StatusResponse](
"txpool_status",
nil,
)
}

type StatusResponse struct {
Pending uint
Queued uint
}

func (s *StatusResponse) UnmarshalJSON(input []byte) error {
type statusResponse struct {
Pending hexutil.Uint `json:"pending"`
Queued hexutil.Uint `json:"queued"`
}

var dec statusResponse
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
s.Pending = uint(dec.Pending)
s.Queued = uint(dec.Queued)
return nil
}
20 changes: 20 additions & 0 deletions module/txpool/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package txpool_test

import (
"testing"

"github.com/lmittmann/w3/module/txpool"
"github.com/lmittmann/w3/rpctest"
)

func TestStatus(t *testing.T) {
tests := []rpctest.TestCase[txpool.StatusResponse]{
{
Golden: "status",
Call: txpool.Status(),
WantRet: txpool.StatusResponse{Pending: 10, Queued: 7},
},
}

rpctest.RunTestCases(t, tests)
}
2 changes: 2 additions & 0 deletions module/txpool/testdata/status.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> {"jsonrpc":"2.0","id":1,"method":"txpool_status"}
< {"jsonrpc":"2.0","id":1,"result":{"pending":"0xa","queued":"0x7"}}

0 comments on commit 7e72eb2

Please sign in to comment.