Skip to content

Add async chain_id setter #2376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2022
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/2376.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add async default_chain_id and chain_id setter
29 changes: 29 additions & 0 deletions tests/core/eth-module/test_eth_properties.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
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(w3):
with pytest.warns(DeprecationWarning):
Expand Down Expand Up @@ -28,3 +46,14 @@ def test_set_chain_id(w3):

w3.eth.chain_id = None
assert w3.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
9 changes: 8 additions & 1 deletion web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,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