diff options
Diffstat (limited to 'ethclient')
-rw-r--r-- | ethclient/ethclient.go | 10 | ||||
-rw-r--r-- | ethclient/ethclient_test.go | 16 |
2 files changed, 26 insertions, 0 deletions
diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index aedf2814a..0a6f73ab5 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -61,6 +61,16 @@ func (ec *Client) Close() { // Blockchain Access +// ChainId retrieves the current chain ID for transaction replay protection. +func (ec *Client) ChainID(ctx context.Context) (*big.Int, error) { + var result hexutil.Big + err := ec.c.CallContext(ctx, &result, "eth_chainId") + if err != nil { + return nil, err + } + return (*big.Int)(&result), err +} + // BlockByHash returns the given full block. // // Note that loading full blocks requires two requests. Use HeaderByHash diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 2e464cd42..3576d4870 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -319,3 +319,19 @@ func TestTransactionInBlockInterrupted(t *testing.T) { t.Fatal("error should not be nil") } } + +func TestChainID(t *testing.T) { + backend, _ := newTestBackend(t) + client, _ := backend.Attach() + defer backend.Stop() + defer client.Close() + ec := NewClient(client) + + id, err := ec.ChainID(context.Background()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if id == nil || id.Cmp(params.AllEthashProtocolChanges.ChainID) != 0 { + t.Fatalf("ChainID returned wrong number: %+v", id) + } +} |