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 diff --git a/web3/eth.py b/web3/eth.py index 1c611f5dd7..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, ) @@ -202,17 +205,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 +214,21 @@ 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=[address_resolver_munger], + ) + getStorageAt: Method[ Callable[..., HexBytes] ] = Method( @@ -239,16 +246,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=[address_resolver_munger] + ) def getBlock( self, block_identifier: BlockIdentifier, full_transactions: bool = False