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

Change eth_getBalance and eth_getCode to use Method #1733

Merged
merged 3 commits into from Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions ens/utils.py
Expand Up @@ -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:
kclowes marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 1 addition & 0 deletions newsfragments/1733.misc.rst
@@ -0,0 +1 @@
Change eth_getBalance and eth_getCode to use Method class
12 changes: 12 additions & 0 deletions web3/_utils/validation.py
Expand Up @@ -34,6 +34,9 @@
valmap,
)

from ens.utils import (
is_valid_domain,
)
from web3._utils.abi import (
abi_to_signature,
filter_by_type,
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 9 additions & 21 deletions web3/eth.py
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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
Expand Down