Skip to content
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

Asyncify eth.chain_id #2251

Merged
merged 1 commit into from
Dec 14, 2021
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
3 changes: 2 additions & 1 deletion docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,10 @@ Supported Methods
Eth
***
- :meth:`web3.eth.block_number <web3.eth.Eth.block_number>`
- :meth:`web3.eth.chain_id <web3.eth.Eth.chain_id>`
- :meth:`web3.eth.coinbase <web3.eth.Eth.coinbase>`
- :meth:`web3.eth.gas_price <web3.eth.Eth.gas_price>`
- :meth:`web3.eth.hashrate <web3.eth.Eth.hashrate>`
- :meth:`web3.eth.max_priority_fee <web3.eth.Eth.max_priority_fee>`
- :meth:`web3.eth.call() <web3.eth.Eth.call>`
- :meth:`web3.eth.estimate_gas() <web3.eth.Eth.estimate_gas>`
Expand All @@ -414,7 +416,6 @@ Eth
- :meth:`web3.eth.get_transaction_count() <web3.eth.Eth.get_transaction_count>`
- :meth:`web3.eth.send_transaction() <web3.eth.Eth.send_transaction>`
- :meth:`web3.eth.send_raw_transaction() <web3.eth.Eth.send_raw_transaction>`
- :meth:`web3.eth.hashrate <web3.eth.Eth.hashrate>`

Net
***
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2251.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add async ``eth.chain_id`` method
9 changes: 9 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,15 @@ async def test_async_eth_hashrate(
assert is_integer(hashrate)
assert hashrate >= 0

@pytest.mark.asyncio
async def test_async_eth_chain_id(
self,
async_w3: "Web3"
) -> None:
chain_id = await async_w3.eth.chain_id # type: ignore
# chain id value from geth fixture genesis file
assert chain_id == 131277322940537


class EthModuleTest:
def test_eth_protocol_version(self, web3: "Web3") -> None:
Expand Down
14 changes: 9 additions & 5 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ def call_munger(
mungers=None,
)

_chain_id: Method[Callable[[], int]] = Method(
RPC.eth_chainId,
mungers=None,
)


class AsyncEth(BaseEth):
is_async = True
Expand Down Expand Up @@ -344,6 +349,10 @@ async def coinbase(self) -> ChecksumAddress:
async def hashrate(self) -> int:
return await self._get_hashrate() # type: ignore

@property
async def chain_id(self) -> int:
return await self._chain_id() # type: ignore

_get_balance: Method[Callable[..., Awaitable[Wei]]] = Method(
RPC.eth_getBalance,
mungers=[BaseEth.block_id_munger],
Expand Down Expand Up @@ -485,11 +494,6 @@ def blockNumber(self) -> BlockNumber:
)
return self.block_number

_chain_id: Method[Callable[[], int]] = Method(
RPC.eth_chainId,
mungers=None,
)

@property
def chain_id(self) -> int:
return self._chain_id()
Expand Down