Skip to content

Commit

Permalink
add async eth.chain_id and test
Browse files Browse the repository at this point in the history
  • Loading branch information
pacrob committed Dec 14, 2021
1 parent 0268d79 commit d442b76
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ Eth
- :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>`
- :meth:`web3.eth.chain_id <web3.eth.Eth.chain_id>`

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

0 comments on commit d442b76

Please sign in to comment.