Skip to content

Commit

Permalink
module/eth: add Syncing() (#230)
Browse files Browse the repository at this point in the history
* module/eth: added `Syncing()`

* added doc

---------

Co-authored-by: lmittmann <lmittmann@users.noreply.github.com>
  • Loading branch information
lmittmann and lmittmann authored Feb 20, 2025
1 parent 40d0283 commit f922a0e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ List of supported RPC methods for [`w3.Client`](https://pkg.go.dev/github.com/lm
| `eth_getUncleByBlockNumberAndIndex` | `eth.UncleByBlockNumberAndIndex(number *big.Int, index uint).Returns(uncle **types.Header)`
| `eth_getUncleCountByBlockHash` | `eth.UncleCountByBlockHash(hash common.Hash).Returns(count *uint)`
| `eth_getUncleCountByBlockNumber` | `eth.UncleCountByBlockNumber(number *big.Int).Returns(count *uint)`
| `eth_syncing` | `eth.Syncing().Returns(syncing *bool)`

### [`debug`](https://pkg.go.dev/github.com/lmittmann/w3/module/debug)

Expand Down
9 changes: 9 additions & 0 deletions docs/pages/rpc-methods/eth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,12 @@ client.Call(
eth.UncleCountByBlockNumber(number).Returns(&count),
)
```

## `eth_syncing`
`Syncing` requests the syncing status of the node.
```go {3}
var syncing bool
client.Call(
eth.Syncing().Returns(&syncing),
)
```
30 changes: 30 additions & 0 deletions module/eth/syncing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package eth

import (
"github.com/lmittmann/w3/internal/module"
"github.com/lmittmann/w3/w3types"
)

// Syncing requests the syncing status of the node.
func Syncing() w3types.RPCCallerFactory[bool] {
return module.NewFactory(
"eth_syncing",
[]any{},
module.WithRetWrapper(syncingRetWrapper),
)
}

func syncingRetWrapper(ret *bool) any {
return (*syncingBool)(ret)
}

type syncingBool bool

func (s *syncingBool) UnmarshalJSON(b []byte) error {
if string(b) == "false" {
*s = false
} else {
*s = true
}
return nil
}
23 changes: 23 additions & 0 deletions module/eth/syncing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package eth_test

import (
"testing"

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

func TestSyncing(t *testing.T) {
rpctest.RunTestCases(t, []rpctest.TestCase[bool]{
{
Golden: "syncing__false",
Call: eth.Syncing(),
WantRet: false,
},
{
Golden: "syncing__true",
Call: eth.Syncing(),
WantRet: true,
},
})
}
2 changes: 2 additions & 0 deletions module/eth/testdata/syncing__false.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> {"jsonrpc":"2.0","id":1,"method":"eth_syncing","params":[]}
< {"jsonrpc":"2.0","id":1,"result":false}
2 changes: 2 additions & 0 deletions module/eth/testdata/syncing__true.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> {"jsonrpc":"2.0","id":1,"method":"eth_syncing","params":[]}
< {"jsonrpc":"2.0","id":1,"result":{"currentBlock":"0x3cf522","healedBytecodeBytes":"0x0","healedBytecodes":"0x0","healedTrienodes":"0x0","healingBytecode":"0x0","healingTrienodes":"0x0","highestBlock":"0x3e0e41","startingBlock":"0x3cbed5","syncedAccountBytes":"0x0","syncedAccounts":"0x0","syncedBytecodeBytes":"0x0","syncedBytecodes":"0x0","syncedStorage":"0x0","syncedStorageBytes":"0x0"}}

0 comments on commit f922a0e

Please sign in to comment.