diff --git a/rpc/eth_api.go b/rpc/eth_api.go index 12a0a66e..22eef209 100644 --- a/rpc/eth_api.go +++ b/rpc/eth_api.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "math/big" + "strconv" "strings" "sync" @@ -67,6 +68,17 @@ func (e *PublicEthAPI) ProtocolVersion() hexutil.Uint { return hexutil.Uint(version.ProtocolVersion) } +// ChainId returns the chain's identifier in hex format +func (e *PublicEthAPI) ChainId() (hexutil.Uint, error) { // nolint + // parse the chainID from a integer string + intChainID, err := strconv.ParseUint(e.cliCtx.ChainID, 0, 64) + if err != nil { + return 0, fmt.Errorf("invalid chainID: %s, must be integer format", e.cliCtx.ChainID) + } + + return hexutil.Uint(intChainID), nil +} + // Syncing returns whether or not the current node is syncing with other peers. Returns false if not, or a struct // outlining the state of the sync if it is. func (e *PublicEthAPI) Syncing() (interface{}, error) { diff --git a/tests/rpc_test.go b/tests/rpc_test.go index fde00618..e3cf506d 100644 --- a/tests/rpc_test.go +++ b/tests/rpc_test.go @@ -130,6 +130,15 @@ func TestEth_protocolVersion(t *testing.T) { require.Equal(t, expectedRes, res, "expected: %s got: %s\n", expectedRes.String(), rpcRes.Result) } +func TestEth_chainId(t *testing.T) { + rpcRes := call(t, "eth_chainId", []string{}) + + var res hexutil.Uint + err := res.UnmarshalJSON(rpcRes.Result) + require.NoError(t, err) + require.NotEqual(t, "0x0", res.String()) +} + func TestEth_blockNumber(t *testing.T) { rpcRes := call(t, "eth_blockNumber", []string{})