diff --git a/newsfragments/2207.feature.rst b/newsfragments/2207.feature.rst new file mode 100644 index 0000000000..78a6f8bbd5 --- /dev/null +++ b/newsfragments/2207.feature.rst @@ -0,0 +1 @@ +Add setter for the Eth.chain_id property diff --git a/tests/core/eth-module/test_eth_properties.py b/tests/core/eth-module/test_eth_properties.py index 9f89391ad6..596222b62a 100644 --- a/tests/core/eth-module/test_eth_properties.py +++ b/tests/core/eth-module/test_eth_properties.py @@ -2,7 +2,8 @@ def test_eth_protocol_version(w3): - assert w3.eth.protocol_version == '63' + with pytest.warns(DeprecationWarning): + assert w3.eth.protocol_version == '63' def test_eth_protocolVersion(w3): @@ -17,3 +18,13 @@ def test_eth_chain_id(w3): def test_eth_chainId(w3): with pytest.warns(DeprecationWarning): assert w3.eth.chainId == 61 + + +def test_set_chain_id(w3): + assert w3.eth.chain_id == 61 + + w3.eth.chain_id = 72 + assert w3.eth.chain_id == 72 + + w3.eth.chain_id = None + assert w3.eth.chain_id == 61 diff --git a/web3/eth.py b/web3/eth.py index 079f6bd1f5..ef1c134911 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -114,6 +114,7 @@ class BaseEth(Module): _default_account: Union[ChecksumAddress, Empty] = empty _default_block: BlockIdentifier = "latest" + _default_chain_id: Optional[int] = None gasPriceStrategy = None _gas_price: Method[Callable[[], Wei]] = Method( @@ -629,7 +630,14 @@ def blockNumber(self) -> BlockNumber: @property def chain_id(self) -> int: - return self._chain_id() + if self._default_chain_id is None: + return self._chain_id() + else: + return self._default_chain_id + + @chain_id.setter + def chain_id(self, value: int) -> None: + self._default_chain_id = value @property def chainId(self) -> int: