Skip to content

Commit 952c1d4

Browse files
committed
Add async chain_id setter
1 parent 81427e6 commit 952c1d4

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

tests/core/eth-module/test_eth_properties.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import pytest
22

3+
from web3 import Web3
4+
from web3.eth import (
5+
AsyncEth,
6+
)
7+
from web3.providers.eth_tester.main import (
8+
AsyncEthereumTesterProvider,
9+
)
10+
11+
12+
@pytest.fixture
13+
def async_w3():
14+
return Web3(
15+
AsyncEthereumTesterProvider(),
16+
middlewares=[],
17+
modules={
18+
'eth': (AsyncEth,),
19+
})
20+
321

422
def test_eth_protocol_version(w3):
523
with pytest.warns(DeprecationWarning):
@@ -28,3 +46,14 @@ def test_set_chain_id(w3):
2846

2947
w3.eth.chain_id = None
3048
assert w3.eth.chain_id == 61
49+
50+
51+
@pytest.mark.asyncio
52+
async def test_async_set_chain_id(async_w3):
53+
assert await async_w3.eth.chain_id == 61
54+
55+
async_w3.eth.chain_id = 72
56+
assert await async_w3.eth.chain_id == 72
57+
58+
async_w3.eth.chain_id = None
59+
assert await async_w3.eth.chain_id == 61

web3/_utils/module_testing/web3_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def test_solidityKeccak(
179179
self, w3: "Web3", types: Sequence[TypeStr], values: Sequence[Any], expected: HexBytes
180180
) -> None:
181181
if isinstance(expected, type) and issubclass(expected, Exception):
182-
with pytest.raises(expected): # type: ignore
182+
with pytest.raises(expected):
183183
w3.solidityKeccak(types, values)
184184
return
185185

web3/eth.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,14 @@ async def block_number(self) -> BlockNumber:
358358

359359
@property
360360
async def chain_id(self) -> int:
361-
return await self._chain_id() # type: ignore
361+
if self._default_chain_id is None:
362+
return await self._chain_id() # type: ignore
363+
else:
364+
return self._default_chain_id
365+
366+
@chain_id.setter
367+
def chain_id(self, value: int) -> None:
368+
self._default_chain_id = value
362369

363370
@property
364371
async def coinbase(self) -> ChecksumAddress:

0 commit comments

Comments
 (0)