Skip to content

Commit

Permalink
Implement eth_chainId endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Mar 21, 2019
1 parent ef728a8 commit 2ec2a58
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/web3.eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ The following properties are available on the ``web3.eth`` namespace.
'63'
.. py:attribute:: Eth.chainId
* Delegates to ``eth_chainId`` RPC Method

Returns a hex-encoded integer value for the currently configured "Chain Id" value introduced in `EIP-155 <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md>`_. Returns ``None`` if no Chain Id is available.

.. code-block:: python
>>> web3.eth.chainId
'0x3d'
Methods
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
def test_eth_protocolVersion(web3):
assert web3.eth.protocolVersion == '63'


def test_eth_chainId(web3):
assert web3.eth.chainId == '0x3d'
4 changes: 4 additions & 0 deletions tests/integration/go_ethereum/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def test_eth_estimateGas_with_block(self,
def test_eth_signTransaction(self, web3, unlocked_account):
super().test_eth_signTransaction(web3, unlocked_account, GETH_SIGNED_TX)

def test_eth_chainid(self, web3):
pytest.xfail('eth_chainId has not been implemented in geth')
super().test_eth_chainId(web3)


class GoEthereumVersionModuleTest(VersionModuleTest):
pass
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/test_ethereum_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
from eth_utils import (
is_checksum_address,
is_dict,
is_hex,
)

from web3 import Web3
from web3._utils.formatters import (
hex_to_integer,
)
from web3._utils.module_testing import (
EthModuleTest,
GoEthereumPersonalModuleTest,
Expand Down Expand Up @@ -291,6 +295,11 @@ def test_eth_estimateGas_with_block(self,
web3, unlocked_account_dual_type
)

def test_eth_chainId(self, web3):
chain_id = web3.eth.chainId
assert is_hex(chain_id)
assert hex_to_integer(chain_id) is 61


class TestEthereumTesterVersionModule(VersionModuleTest):
pass
Expand Down
4 changes: 4 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def test_eth_hashrate(self, web3):
assert is_integer(hashrate)
assert hashrate >= 0

def test_eth_chainId(self, web3):
chain_id = web3.eth.chainId
assert chain_id is None

def test_eth_gasPrice(self, web3):
gas_price = web3.eth.gasPrice
assert is_integer(gas_price)
Expand Down
4 changes: 4 additions & 0 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def accounts(self):
def blockNumber(self):
return self.web3.manager.request_blocking("eth_blockNumber", [])

@property
def chainId(self):
return self.web3.manager.request_blocking("eth_chainId", [])

def getBalance(self, account, block_identifier=None):
if block_identifier is None:
block_identifier = self.defaultBlock
Expand Down
1 change: 1 addition & 0 deletions web3/providers/eth_tester/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def personal_send_transaction(eth_tester, params):
),
'mining': static_return(False),
'hashrate': static_return(0),
'chainId': static_return('0x3d'),
'gasPrice': static_return(1),
'accounts': call_eth_tester('get_accounts'),
'blockNumber': compose(
Expand Down

0 comments on commit 2ec2a58

Please sign in to comment.