diff --git a/docs/providers.rst b/docs/providers.rst index e5f538364b..fc7d62fa4f 100644 --- a/docs/providers.rst +++ b/docs/providers.rst @@ -399,8 +399,10 @@ Supported Methods Eth *** - :meth:`web3.eth.block_number ` +- :meth:`web3.eth.chain_id ` - :meth:`web3.eth.coinbase ` - :meth:`web3.eth.gas_price ` +- :meth:`web3.eth.hashrate ` - :meth:`web3.eth.max_priority_fee ` - :meth:`web3.eth.call() ` - :meth:`web3.eth.estimate_gas() ` @@ -414,7 +416,6 @@ Eth - :meth:`web3.eth.get_transaction_count() ` - :meth:`web3.eth.send_transaction() ` - :meth:`web3.eth.send_raw_transaction() ` -- :meth:`web3.eth.hashrate ` Net *** diff --git a/newsfragments/2251.feature.rst b/newsfragments/2251.feature.rst new file mode 100644 index 0000000000..47961935bb --- /dev/null +++ b/newsfragments/2251.feature.rst @@ -0,0 +1 @@ +Add async ``eth.chain_id`` method \ No newline at end of file diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index 7d1c5ca4a8..d76055b639 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -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: diff --git a/web3/eth.py b/web3/eth.py index 1708c0d19c..0c2ddb7bf8 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -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 @@ -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], @@ -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()