Skip to content

Commit 81427e6

Browse files
authored
Add chain_id setter (#2207)
1 parent bdf0d93 commit 81427e6

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

newsfragments/2207.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add setter for the Eth.chain_id property

tests/core/eth-module/test_eth_properties.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33

44
def test_eth_protocol_version(w3):
5-
assert w3.eth.protocol_version == '63'
5+
with pytest.warns(DeprecationWarning):
6+
assert w3.eth.protocol_version == '63'
67

78

89
def test_eth_protocolVersion(w3):
@@ -17,3 +18,13 @@ def test_eth_chain_id(w3):
1718
def test_eth_chainId(w3):
1819
with pytest.warns(DeprecationWarning):
1920
assert w3.eth.chainId == 61
21+
22+
23+
def test_set_chain_id(w3):
24+
assert w3.eth.chain_id == 61
25+
26+
w3.eth.chain_id = 72
27+
assert w3.eth.chain_id == 72
28+
29+
w3.eth.chain_id = None
30+
assert w3.eth.chain_id == 61

web3/eth.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
class BaseEth(Module):
115115
_default_account: Union[ChecksumAddress, Empty] = empty
116116
_default_block: BlockIdentifier = "latest"
117+
_default_chain_id: Optional[int] = None
117118
gasPriceStrategy = None
118119

119120
_gas_price: Method[Callable[[], Wei]] = Method(
@@ -629,7 +630,14 @@ def blockNumber(self) -> BlockNumber:
629630

630631
@property
631632
def chain_id(self) -> int:
632-
return self._chain_id()
633+
if self._default_chain_id is None:
634+
return self._chain_id()
635+
else:
636+
return self._default_chain_id
637+
638+
@chain_id.setter
639+
def chain_id(self, value: int) -> None:
640+
self._default_chain_id = value
633641

634642
@property
635643
def chainId(self) -> int:

0 commit comments

Comments
 (0)