From 4945c8dd5254c95a9bdf836ab7fb96b9631018ec Mon Sep 17 00:00:00 2001 From: pacrob Date: Thu, 30 Dec 2021 13:06:57 -0700 Subject: [PATCH] asyncify eth.accounts --- docs/providers.rst | 1 + newsfragments/2284.feature.rst | 1 + web3/_utils/module_testing/eth_module.py | 12 ++++++++++++ web3/eth.py | 16 ++++++++++------ 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 newsfragments/2284.feature.rst diff --git a/docs/providers.rst b/docs/providers.rst index 741a79330c..8e9fff84a6 100644 --- a/docs/providers.rst +++ b/docs/providers.rst @@ -398,6 +398,7 @@ Supported Methods Eth *** +- :meth:`web3.eth.accounts ` - :meth:`web3.eth.block_number ` - :meth:`web3.eth.chain_id ` - :meth:`web3.eth.coinbase ` diff --git a/newsfragments/2284.feature.rst b/newsfragments/2284.feature.rst new file mode 100644 index 0000000000..7c42e65d20 --- /dev/null +++ b/newsfragments/2284.feature.rst @@ -0,0 +1 @@ +Add async `eth.accounts` 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 2a277fb9a4..321eebb595 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -833,6 +833,18 @@ async def test_async_eth_wait_for_transaction_receipt_with_log_entry( assert log_entry['transactionIndex'] == 0 assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log) + @pytest.mark.asyncio + async def test_async_eth_accounts(self, async_w3: "Web3") -> None: + accounts = await async_w3.eth.accounts # type: ignore + assert is_list_like(accounts) + assert len(accounts) != 0 + assert all(( + is_checksum_address(account) + for account + in accounts + )) + assert await async_w3.eth.coinbase in accounts # type: ignore + class EthModuleTest: def test_eth_protocol_version(self, web3: "Web3") -> None: diff --git a/web3/eth.py b/web3/eth.py index 083b532a11..b8bad742bf 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -263,6 +263,11 @@ def call_munger( else: return (transaction, block_identifier, state_override) + _get_accounts: Method[Callable[[], Tuple[ChecksumAddress]]] = Method( + RPC.eth_accounts, + mungers=None, + ) + _get_hashrate: Method[Callable[[], int]] = Method( RPC.eth_hashrate, mungers=None, @@ -287,6 +292,10 @@ def call_munger( class AsyncEth(BaseEth): is_async = True + @property + async def accounts(self) -> Tuple[ChecksumAddress]: + return await self._get_accounts() # type: ignore + @property async def block_number(self) -> BlockNumber: # types ignored b/c mypy conflict with BlockingEth properties @@ -514,14 +523,9 @@ def gasPrice(self) -> Wei: ) return self.gas_price - get_accounts: Method[Callable[[], Tuple[ChecksumAddress]]] = Method( - RPC.eth_accounts, - mungers=None, - ) - @property def accounts(self) -> Tuple[ChecksumAddress]: - return self.get_accounts() + return self._get_accounts() @property def block_number(self) -> BlockNumber: