Skip to content

Commit ca662c8

Browse files
authored
Add async chain_id setter (#2376)
* Add async chain_id setter * Add newsfragment for async chain_id
1 parent 9ba6fec commit ca662c8

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

newsfragments/2376.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add async default_chain_id and chain_id setter

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/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)