Skip to content

Commit

Permalink
Add account to async Eth (backport of #2582)
Browse files Browse the repository at this point in the history
- Move ``account`` to ``BaseEth`` so it can be accessed by both ``Eth`` and ``AsyncEth``
  • Loading branch information
fselmo committed Jul 29, 2022
1 parent 22bfa48 commit e6b2f44
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/providers.rst
Expand Up @@ -411,6 +411,7 @@ Supported Methods

Eth
***
- :class:`web3.eth.account <eth_account.account.Account>`
- :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>`
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2590.feature.rst
@@ -0,0 +1 @@
Support for ``Account`` class access in ``AsyncEth`` via ``async_w3.eth.account``
39 changes: 38 additions & 1 deletion tests/core/eth-module/test_accounts.py
@@ -1,11 +1,18 @@
# coding=utf-8

import pytest
from unittest.mock import (
patch,
)

from eth_account.messages import (
encode_defunct,
)
from eth_account.signers.local import (
LocalAccount,
)
from eth_utils import (
is_bytes,
is_checksum_address,
to_bytes,
to_hex,
Expand All @@ -21,9 +28,16 @@
Account,
Web3,
)
from web3.eth import (
AsyncEth,
BaseEth,
)
from web3.providers.eth_tester import (
EthereumTesterProvider,
)
from web3.providers.eth_tester.main import (
AsyncEthereumTesterProvider,
)

# from https://github.com/ethereum/tests/blob/3930ca3a9a377107d5792b3e7202f79c688f1a67/BasicTests/txtest.json # noqa: 501
ETH_TEST_TRANSACTIONS = [
Expand Down Expand Up @@ -77,7 +91,7 @@ def web3js_password():
@pytest.fixture(params=['instance', 'class'])
def acct(request, web3):
if request.param == 'instance':
return web3.eth.account
return BaseEth(web3).account
elif request.param == 'class':
return Account
raise Exception('Unreachable!')
Expand Down Expand Up @@ -494,3 +508,26 @@ def test_eth_account_sign_and_send_EIP155_transaction_to_eth_tester(
assert actual_txn.r == r
assert actual_txn.s == s
assert actual_txn.v == v


# -- async -- #


@pytest.fixture()
def async_w3():
return Web3(AsyncEthereumTesterProvider(), modules={"eth": [AsyncEth]})


@patch("web3.eth.BaseEth.account", "wired via BaseEth")
def test_account_is_wired_via_base_eth_for_sync_and_async(w3, async_w3):
# this gives us some comfort that all the `w3` tests would apply for `async_w3` as
# well, since `Account` is static and not actually awaited
assert w3.eth.account == "wired via BaseEth"
assert async_w3.eth.account == "wired via BaseEth"


def test_async_eth_account_creates_account(async_w3):
account = async_w3.eth.account.create()
assert isinstance(account, LocalAccount)
assert is_checksum_address(account.address)
assert is_bytes(account.key)
2 changes: 1 addition & 1 deletion web3/eth.py
Expand Up @@ -112,6 +112,7 @@


class BaseEth(Module):
account = Account()
_default_account: Union[ChecksumAddress, Empty] = empty
_default_block: BlockIdentifier = "latest"
gasPriceStrategy = None
Expand Down Expand Up @@ -552,7 +553,6 @@ async def call(


class Eth(BaseEth):
account = Account()
defaultContractFactory: Type[Union[Contract, ConciseContract, ContractCaller]] = Contract # noqa: E704,E501
iban = Iban

Expand Down

0 comments on commit e6b2f44

Please sign in to comment.