From 40a64c85a6fa19fee1ad6927f59f64c296df30fe Mon Sep 17 00:00:00 2001 From: Keri Date: Wed, 9 Sep 2020 15:47:00 -0600 Subject: [PATCH 1/3] Change eth_getBalance and eth_getCode to use Method --- ens/utils.py | 10 ++++++++++ web3/_utils/validation.py | 12 ++++++++++++ web3/eth.py | 30 +++++++++--------------------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/ens/utils.py b/ens/utils.py index 08eb211b94..c7891105c3 100644 --- a/ens/utils.py +++ b/ens/utils.py @@ -212,3 +212,13 @@ def assert_signer_in_modifier_kwargs(modifier_kwargs: Any) -> ChecksumAddress: def is_none_or_zero_address(addr: Union[Address, ChecksumAddress, HexAddress]) -> bool: return not addr or addr == EMPTY_ADDR_HEX + + +def is_valid_domain(domain: str) -> bool: + split_domain = domain.split('.') + if len(split_domain) == 1: + return False + for name in split_domain: + if not is_valid_name(name): + return False + return True diff --git a/web3/_utils/validation.py b/web3/_utils/validation.py index f16555745f..f2e1d51a4a 100644 --- a/web3/_utils/validation.py +++ b/web3/_utils/validation.py @@ -34,6 +34,9 @@ valmap, ) +from ens.utils import ( + is_valid_domain, +) from web3._utils.abi import ( abi_to_signature, filter_by_type, @@ -152,10 +155,19 @@ def validate_abi_value(abi_type: TypeStr, value: Any) -> None: ) +def is_non_address_string(value: str) -> bool: + return (is_string(value) and not is_bytes(value) and not + is_checksum_address(value) and not is_hex_address(value)) + + def validate_address(value: Any) -> None: """ Helper function for validating an address """ + if is_non_address_string(value): + if not is_valid_domain(value): + raise InvalidAddress("Address needs to be a full domain name including a TLD", value) + return if is_bytes(value): if not is_binary_address(value): raise InvalidAddress("Address must be 20 bytes when input type is bytes", value) diff --git a/web3/eth.py b/web3/eth.py index 1c611f5dd7..6216efe05b 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -202,17 +202,6 @@ def blockNumber(self) -> BlockNumber: def chainId(self) -> int: return self.web3.manager.request_blocking(RPC.eth_chainId, []) - def getBalance( - self, account: Union[Address, ChecksumAddress, ENS], - block_identifier: Optional[BlockIdentifier] = None - ) -> Wei: - if block_identifier is None: - block_identifier = self.defaultBlock - return self.web3.manager.request_blocking( - RPC.eth_getBalance, - [account, block_identifier], - ) - def block_identifier_munger( self, *args: Any, @@ -222,6 +211,11 @@ def block_identifier_munger( block_identifier = self.defaultBlock return [*args, block_identifier] + getBalance: Method[Callable[..., Wei]] = Method( + RPC.eth_getBalance, + mungers=[block_identifier_munger] + ) + getStorageAt: Method[ Callable[..., HexBytes] ] = Method( @@ -239,16 +233,10 @@ def block_identifier_munger( mungers=[block_identifier_munger], ) - def getCode( - self, account: Union[Address, ChecksumAddress, ENS], - block_identifier: Optional[BlockIdentifier] = None - ) -> HexBytes: - if block_identifier is None: - block_identifier = self.defaultBlock - return self.web3.manager.request_blocking( - RPC.eth_getCode, - [account, block_identifier], - ) + getCode: Method[Callable[..., HexBytes]] = Method( + RPC.eth_getCode, + mungers=[block_identifier_munger] + ) def getBlock( self, block_identifier: BlockIdentifier, full_transactions: bool = False From a604046d2fc90191850eb37ff141a34c3f2b31be Mon Sep 17 00:00:00 2001 From: Keri Date: Wed, 9 Sep 2020 16:11:36 -0600 Subject: [PATCH 2/3] Add newsfragment for eth_getCode and eth_getBalance methods --- newsfragments/1733.misc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/1733.misc.rst diff --git a/newsfragments/1733.misc.rst b/newsfragments/1733.misc.rst new file mode 100644 index 0000000000..9fcce89895 --- /dev/null +++ b/newsfragments/1733.misc.rst @@ -0,0 +1 @@ +Change eth_getBalance and eth_getCode to use Method class From fbcc75c2e71f71701fe6b85c6493f698b06adea3 Mon Sep 17 00:00:00 2001 From: Keri Date: Thu, 10 Sep 2020 12:27:56 -0600 Subject: [PATCH 3/3] Add address resolver to Eth class --- ens/utils.py | 10 ---------- web3/_utils/validation.py | 12 ------------ web3/eth.py | 17 +++++++++++++++-- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/ens/utils.py b/ens/utils.py index c7891105c3..08eb211b94 100644 --- a/ens/utils.py +++ b/ens/utils.py @@ -212,13 +212,3 @@ def assert_signer_in_modifier_kwargs(modifier_kwargs: Any) -> ChecksumAddress: def is_none_or_zero_address(addr: Union[Address, ChecksumAddress, HexAddress]) -> bool: return not addr or addr == EMPTY_ADDR_HEX - - -def is_valid_domain(domain: str) -> bool: - split_domain = domain.split('.') - if len(split_domain) == 1: - return False - for name in split_domain: - if not is_valid_name(name): - return False - return True diff --git a/web3/_utils/validation.py b/web3/_utils/validation.py index f2e1d51a4a..f16555745f 100644 --- a/web3/_utils/validation.py +++ b/web3/_utils/validation.py @@ -34,9 +34,6 @@ valmap, ) -from ens.utils import ( - is_valid_domain, -) from web3._utils.abi import ( abi_to_signature, filter_by_type, @@ -155,19 +152,10 @@ def validate_abi_value(abi_type: TypeStr, value: Any) -> None: ) -def is_non_address_string(value: str) -> bool: - return (is_string(value) and not is_bytes(value) and not - is_checksum_address(value) and not is_hex_address(value)) - - def validate_address(value: Any) -> None: """ Helper function for validating an address """ - if is_non_address_string(value): - if not is_valid_domain(value): - raise InvalidAddress("Address needs to be a full domain name including a TLD", value) - return if is_bytes(value): if not is_binary_address(value): raise InvalidAddress("Address must be 20 bytes when input type is bytes", value) diff --git a/web3/eth.py b/web3/eth.py index 6216efe05b..9e108dea6a 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -52,6 +52,9 @@ LogFilter, TransactionFilter, ) +from web3._utils.normalizers import ( + abi_ens_resolver, +) from web3._utils.rpc_abi import ( RPC, ) @@ -211,9 +214,19 @@ def block_identifier_munger( block_identifier = self.defaultBlock return [*args, block_identifier] + def address_resolver_munger( + self, + account: Union[Address, ChecksumAddress, ENS], + block_identifier: Optional[BlockIdentifier]=None + ) -> Tuple[Union[Address, ChecksumAddress, ENS], BlockIdentifier]: + resolved_addr = abi_ens_resolver(self.web3, 'address', account)[1] + if block_identifier is None: + block_identifier = self.defaultBlock + return (resolved_addr, block_identifier) + getBalance: Method[Callable[..., Wei]] = Method( RPC.eth_getBalance, - mungers=[block_identifier_munger] + mungers=[address_resolver_munger], ) getStorageAt: Method[ @@ -235,7 +248,7 @@ def block_identifier_munger( getCode: Method[Callable[..., HexBytes]] = Method( RPC.eth_getCode, - mungers=[block_identifier_munger] + mungers=[address_resolver_munger] ) def getBlock(