Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved account to BaseEth #2582

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
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/2580.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Moved `Eth.account` to `BaseEth.account` so that it could be use in `AsyncEth`
39 changes: 38 additions & 1 deletion tests/core/eth-module/test_accounts.py
Original file line number Diff line number Diff line change
@@ -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, w3):
if request.param == "instance":
return w3.eth.account
return BaseEth(w3).account
elif request.param == "class":
return Account
raise Exception("Unreachable!")
Expand Down Expand Up @@ -536,3 +550,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.privateKey)
2 changes: 1 addition & 1 deletion web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class BaseEth(Module):
_default_account: Union[ChecksumAddress, Empty] = empty
_default_block: BlockIdentifier = "latest"
gasPriceStrategy = None
account = Account()
defaultContractFactory: Any = None

_gas_price: Method[Callable[[], Wei]] = Method(
Expand Down Expand Up @@ -598,7 +599,6 @@ async def get_storage_at(


class Eth(BaseEth):
account = Account()
defaultContractFactory: Type[Union[Contract, ContractCaller]] = Contract

def namereg(self) -> NoReturn:
Expand Down