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
1 change: 1 addition & 0 deletions newsfragments/2207.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add setter for the Eth.chain_id property
42 changes: 41 additions & 1 deletion tests/core/eth-module/test_eth_properties.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import pytest

from web3 import Web3
from web3.eth import (
AsyncEth,
)
from web3.providers.eth_tester.main import (
AsyncEthereumTesterProvider,
)


@pytest.fixture
def async_w3():
return Web3(
AsyncEthereumTesterProvider(),
middlewares=[],
modules={
'eth': (AsyncEth,),
})


def test_eth_protocol_version(web3):
assert web3.eth.protocol_version == '63'
with pytest.warns(DeprecationWarning):
assert web3.eth.protocol_version == '63'


def test_eth_protocolVersion(web3):
Expand All @@ -17,3 +36,24 @@ def test_eth_chain_id(web3):
def test_eth_chainId(web3):
with pytest.warns(DeprecationWarning):
assert web3.eth.chainId == 61


def test_set_chain_id(web3):
assert web3.eth.chain_id == 61

web3.eth.chain_id = 72
assert web3.eth.chain_id == 72

web3.eth.chain_id = None
assert web3.eth.chain_id == 61


@pytest.mark.asyncio
async def test_async_set_chain_id(async_w3):
assert await async_w3.eth.chain_id == 61

async_w3.eth.chain_id = 72
assert await async_w3.eth.chain_id == 72

async_w3.eth.chain_id = None
assert await async_w3.eth.chain_id == 61
19 changes: 17 additions & 2 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -357,7 +358,14 @@ async def block_number(self) -> BlockNumber:

@property
async def chain_id(self) -> int:
return await self._chain_id() # type: ignore
if self._default_chain_id is None:
return await self._chain_id() # type: ignore
else:
return self._default_chain_id

@chain_id.setter
def chain_id(self, value: int) -> None:
self._default_chain_id = value

@property
async def coinbase(self) -> ChecksumAddress:
Expand Down Expand Up @@ -629,7 +637,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:
Expand Down