Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
5 changes: 5 additions & 0 deletions tests/integration/go_ethereum/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def test_eth_submitHashrate(self, web3):
pytest.xfail('eth_submitHashrate depracated in 1.8.22 for ethash_submitHashRate')
super().test_eth_submitHashrate(web3)

def test_eth_chainId(self, web3):
if 'v1.7.2' in web3.clientVersion:
pytest.xfail('eth_chainId not implemented in geth 1.7.2')
super().test_eth_chainId(web3)


class GoEthereumVersionModuleTest(VersionModuleTest):
pass
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/parity/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def _check_web3_clientVersion(self, client_version):


class ParityEthModuleTest(EthModuleTest):
def test_eth_chainId(self, web3):
# Parity will return null if chainId is not available
chain_id = web3.eth.chainId
assert chain_id is None

def test_eth_getBlockByNumber_pending(self, web3):
pytest.xfail('Parity dropped "pending" option in 1.11.1')
super().test_eth_getBlockByNumber_pending(web3)
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
6 changes: 6 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
is_list_like,
is_same_address,
is_string,
to_int,
)
from hexbytes import (
HexBytes,
Expand Down Expand Up @@ -65,6 +66,11 @@ def test_eth_hashrate(self, web3):
assert is_integer(hashrate)
assert hashrate >= 0

def test_eth_chainId(self, web3):
chain_id = web3.eth.chainId
# chain id value from geth fixture genesis file
assert to_int(hexstr=chain_id) == 131277322940537

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