From ddac794be93a85ef5e92aa1ce48f031769a55476 Mon Sep 17 00:00:00 2001 From: pacrob Date: Thu, 20 Jan 2022 15:19:29 -0700 Subject: [PATCH] move default_account and default_block properties and setters to BaseEth so Eth and AsyncEth can access --- newsfragments/2315.feature.rst | 1 + web3/_utils/module_testing/eth_module.py | 76 +++++++++++++++++++++++ web3/eth.py | 78 +++++++++++------------- 3 files changed, 113 insertions(+), 42 deletions(-) create mode 100644 newsfragments/2315.feature.rst diff --git a/newsfragments/2315.feature.rst b/newsfragments/2315.feature.rst new file mode 100644 index 0000000000..1fa55c3ac3 --- /dev/null +++ b/newsfragments/2315.feature.rst @@ -0,0 +1 @@ +add Async access to `default_account` and `default_block` diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index 321eebb595..1804ba6bd5 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -35,6 +35,9 @@ HexBytes, ) +from web3._utils.empty import ( + empty, +) from web3._utils.ens import ( ens_addresses, ) @@ -53,6 +56,7 @@ ) from web3.types import ( # noqa: F401 BlockData, + BlockIdentifier, FilterParams, LogReceipt, Nonce, @@ -845,6 +849,43 @@ async def test_async_eth_accounts(self, async_w3: "Web3") -> None: )) assert await async_w3.eth.coinbase in accounts # type: ignore + @pytest.mark.asyncio + async def test_async_provider_default_account( + self, + async_w3: "Web3", + unlocked_account_dual_type: ChecksumAddress + ) -> None: + + # check defaults to empty + default_account = async_w3.eth.default_account + assert default_account is empty + + # check setter + async_w3.eth.default_account = unlocked_account_dual_type + default_account = async_w3.eth.default_account + assert default_account == unlocked_account_dual_type + + # reset to default + async_w3.eth.default_account = empty + + @pytest.mark.asyncio + async def test_async_provider_default_block( + self, + async_w3: "Web3", + ) -> None: + + # check defaults to 'latest' + default_block = async_w3.eth.default_block + assert default_block == 'latest' + + # check setter + async_w3.eth.default_block = BlockNumber(12345) + default_block = async_w3.eth.default_block + assert default_block == BlockNumber(12345) + + # reset to default + async_w3.eth.default_block = 'latest' + class EthModuleTest: def test_eth_protocol_version(self, web3: "Web3") -> None: @@ -2919,3 +2960,38 @@ def test_eth_get_raw_transaction_by_block_raises_error_block_identifier( ) ): web3.eth.get_raw_transaction_by_block(unknown_identifier, 0) # type: ignore + + def test_default_account( + self, + web3: "Web3", + unlocked_account_dual_type: ChecksumAddress + ) -> None: + + # check defaults to empty + default_account = web3.eth.default_account + assert default_account is empty + + # check setter + web3.eth.default_account = unlocked_account_dual_type + default_account = web3.eth.default_account + assert default_account == unlocked_account_dual_type + + # reset to default + web3.eth.default_account = empty + + async def test_default_block( + self, + web3: "Web3", + ) -> None: + + # check defaults to 'latest' + default_block = web3.eth.default_block + assert default_block == 'latest' + + # check setter + web3.eth.default_block = BlockNumber(12345) + default_block = web3.eth.default_block + assert default_block == BlockNumber(12345) + + # reset to default + web3.eth.default_block = 'latest' diff --git a/web3/eth.py b/web3/eth.py index 7287903119..00f6930ef7 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -117,7 +117,6 @@ class BaseEth(Module): mungers=None, ) - """ property default_block """ @property def default_block(self) -> BlockIdentifier: return self._default_block @@ -126,10 +125,46 @@ def default_block(self) -> BlockIdentifier: def default_block(self, value: BlockIdentifier) -> None: self._default_block = value + @property + def defaultBlock(self) -> BlockIdentifier: + warnings.warn( + 'defaultBlock is deprecated in favor of default_block', + category=DeprecationWarning, + ) + return self._default_block + + @defaultBlock.setter + def defaultBlock(self, value: BlockIdentifier) -> None: + warnings.warn( + 'defaultBlock is deprecated in favor of default_block', + category=DeprecationWarning, + ) + self._default_block = value + @property def default_account(self) -> Union[ChecksumAddress, Empty]: return self._default_account + @default_account.setter + def default_account(self, account: Union[ChecksumAddress, Empty]) -> None: + self._default_account = account + + @property + def defaultAccount(self) -> Union[ChecksumAddress, Empty]: + warnings.warn( + 'defaultAccount is deprecated in favor of default_account', + category=DeprecationWarning, + ) + return self._default_account + + @defaultAccount.setter + def defaultAccount(self, account: Union[ChecksumAddress, Empty]) -> None: + warnings.warn( + 'defaultAccount is deprecated in favor of default_account', + category=DeprecationWarning, + ) + self._default_account = account + def send_transaction_munger(self, transaction: TxParams) -> Tuple[TxParams]: if 'from' not in transaction and is_checksum_address(self.default_account): transaction = assoc(transaction, 'from', self.default_account) @@ -551,52 +586,11 @@ def chainId(self) -> int: ) return self.chain_id - """ property default_account """ - @property - def default_account(self) -> Union[ChecksumAddress, Empty]: - return self._default_account - - @default_account.setter - def default_account(self, account: Union[ChecksumAddress, Empty]) -> None: - self._default_account = account - - @property - def defaultAccount(self) -> Union[ChecksumAddress, Empty]: - warnings.warn( - 'defaultAccount is deprecated in favor of default_account', - category=DeprecationWarning, - ) - return self._default_account - - @defaultAccount.setter - def defaultAccount(self, account: Union[ChecksumAddress, Empty]) -> None: - warnings.warn( - 'defaultAccount is deprecated in favor of default_account', - category=DeprecationWarning, - ) - self._default_account = account - get_balance: Method[Callable[..., Wei]] = Method( RPC.eth_getBalance, mungers=[BaseEth.block_id_munger], ) - @property - def defaultBlock(self) -> BlockIdentifier: - warnings.warn( - 'defaultBlock is deprecated in favor of default_block', - category=DeprecationWarning, - ) - return self._default_block - - @defaultBlock.setter - def defaultBlock(self, value: BlockIdentifier) -> None: - warnings.warn( - 'defaultBlock is deprecated in favor of default_block', - category=DeprecationWarning, - ) - self._default_block = value - @property def max_priority_fee(self) -> Wei: return self._max_priority_fee()