Skip to content

Commit

Permalink
asyncify eth.accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
pacrob authored and pacrob committed Jan 4, 2022
1 parent e529a00 commit 4945c8d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ Supported Methods

Eth
***
- :meth:`web3.eth.accounts <web3.eth.Eth.accounts>`
- :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>`
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2284.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add async `eth.accounts` method
12 changes: 12 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 10 additions & 6 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4945c8d

Please sign in to comment.