diff options
author | Jim McDonald <Jim@mcdee.net> | 2017-08-01 16:59:46 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-08-01 16:59:46 +0800 |
commit | bc0e6a5e68904b2a1251240b85dd97dc8fa07e11 (patch) | |
tree | 557a626364c294bb3b1847014d14ec9e851f0fe6 /ethclient | |
parent | 60c858a5291da6757ca6798178f4e67f77dd4122 (diff) | |
download | go-tangerine-bc0e6a5e68904b2a1251240b85dd97dc8fa07e11.tar go-tangerine-bc0e6a5e68904b2a1251240b85dd97dc8fa07e11.tar.gz go-tangerine-bc0e6a5e68904b2a1251240b85dd97dc8fa07e11.tar.bz2 go-tangerine-bc0e6a5e68904b2a1251240b85dd97dc8fa07e11.tar.lz go-tangerine-bc0e6a5e68904b2a1251240b85dd97dc8fa07e11.tar.xz go-tangerine-bc0e6a5e68904b2a1251240b85dd97dc8fa07e11.tar.zst go-tangerine-bc0e6a5e68904b2a1251240b85dd97dc8fa07e11.zip |
ethclient: add NetworkID method (#14791)
There is currently no simple way to obtain the network ID from a Client.
This adds a NetworkID method that wraps the net_version JSON-RPC call.
Diffstat (limited to 'ethclient')
-rw-r--r-- | ethclient/ethclient.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 02df03fff..48639d949 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -256,6 +256,19 @@ func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) // State Access +// NetworkID returns the network ID (also known as the chain ID) for this chain. +func (ec *Client) NetworkID(ctx context.Context) (*big.Int, error) { + version := new(big.Int) + var ver string + if err := ec.c.CallContext(ctx, &ver, "net_version"); err != nil { + return nil, err + } + if _, ok := version.SetString(ver, 10); !ok { + return nil, fmt.Errorf("invalid net_version result %q", ver) + } + return version, nil +} + // BalanceAt returns the wei balance of the given account. // The block number can be nil, in which case the balance is taken from the latest known block. func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) { |