Skip to content

Commit

Permalink
refactor: use internal AddressType instead of ChecksumAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Jul 22, 2021
1 parent 5021bb3 commit a80a3d6
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 33 deletions.
15 changes: 7 additions & 8 deletions src/ape/api/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

from eth_account.datastructures import SignedMessage # type: ignore
from eth_account.messages import SignableMessage # type: ignore
from eth_typing import ChecksumAddress

from ape.types import ContractType
from ape.types import AddressType, ContractType
from ape.utils import cached_property

from .address import AddressAPI
Expand Down Expand Up @@ -68,14 +67,14 @@ def _convert(self) -> Callable:

def transfer(
self,
account: Union[str, ChecksumAddress, "AddressAPI"],
account: Union[str, AddressType, "AddressAPI"],
value: Union[str, int, None] = None,
data: Union[bytes, str, None] = None,
**kwargs,
) -> ReceiptAPI:
txn = self._transaction_class( # type: ignore
sender=self.address,
receiver=self._convert(account, ChecksumAddress),
receiver=self._convert(account, AddressType),
**kwargs,
)

Expand Down Expand Up @@ -129,7 +128,7 @@ def __len__(self) -> int:
def __iter__(self) -> Iterator[AccountAPI]:
...

def __getitem__(self, address: str) -> AccountAPI:
def __getitem__(self, address: AddressType) -> AccountAPI:
for account in self.__iter__():
if account.address == address:
return account
Expand All @@ -148,7 +147,7 @@ def append(self, account: AccountAPI):

self.__setitem__(account.address, account)

def __setitem__(self, address: str, account: AccountAPI):
def __setitem__(self, address: AddressType, account: AccountAPI):
raise NotImplementedError("Must define this method to use `container.append(acct)`")

def remove(self, account: AccountAPI):
Expand All @@ -163,10 +162,10 @@ def remove(self, account: AccountAPI):

self.__delitem__(account.address)

def __delitem__(self, address: str):
def __delitem__(self, address: AddressType):
raise NotImplementedError("Must define this method to use `container.remove(acct)`")

def __contains__(self, address: str) -> bool:
def __contains__(self, address: AddressType) -> bool:
try:
self.__getitem__(address)
return True
Expand Down
8 changes: 5 additions & 3 deletions src/ape/api/address.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List, Optional, Type

from ape.types import AddressType

from .base import abstractdataclass, abstractmethod
from .providers import ProviderAPI, ReceiptAPI, TransactionAPI

Expand All @@ -25,7 +27,7 @@ def _transaction_class(self) -> Type[TransactionAPI]:

@property
@abstractmethod
def address(self) -> str:
def address(self) -> AddressType:
...

def __dir__(self) -> List[str]:
Expand Down Expand Up @@ -69,8 +71,8 @@ def is_contract(self) -> bool:


class Address(AddressAPI):
_address: str
_address: AddressType

@property
def address(self) -> str:
def address(self) -> AddressType:
return self._address
22 changes: 11 additions & 11 deletions src/ape/api/contracts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from eth_utils import to_bytes

from ape.types import ABI, ContractType
from ape.types import ABI, AddressType, ContractType
from ape.utils import notify

from .address import Address, AddressAPI
Expand Down Expand Up @@ -48,7 +48,7 @@ def __call__(self, *args, **kwargs) -> ReceiptAPI:
@dataclass
class ContractCall:
abi: ABI
address: str
address: AddressType
provider: ProviderAPI

def __repr__(self) -> str:
Expand Down Expand Up @@ -83,7 +83,7 @@ def __call__(self, *args, **kwargs) -> Any:
@dataclass
class ContractCallHandler:
provider: ProviderAPI
address: str
address: AddressType
abis: List[ABI]

def __repr__(self) -> str:
Expand All @@ -109,7 +109,7 @@ def __call__(self, *args, **kwargs) -> Any:
@dataclass
class ContractTransaction:
abi: ABI
address: str
address: AddressType
provider: ProviderAPI

def __repr__(self) -> str:
Expand All @@ -136,7 +136,7 @@ def __call__(self, *args, **kwargs) -> ReceiptAPI:
@dataclass
class ContractTransactionHandler:
provider: ProviderAPI
address: str
address: AddressType
abis: List[ABI]

def __repr__(self) -> str:
Expand Down Expand Up @@ -174,14 +174,14 @@ class ContractEvent:


class ContractInstance(AddressAPI):
_address: str
_address: AddressType
_contract_type: ContractType

def __repr__(self) -> str:
return f"<{self._contract_type.contractName} {self.address}>"

@property
def address(self) -> str:
def address(self) -> AddressType:
return self._address

def __dir__(self) -> List[str]:
Expand Down Expand Up @@ -273,7 +273,7 @@ def __call__(self, *args, **kwargs) -> TransactionAPI:


def _Contract(
address: str,
address: Union[str, AddressAPI, AddressType],
networks: "NetworkManager",
converters: "ConversionManager",
contract_type: Optional[ContractType] = None,
Expand Down Expand Up @@ -303,7 +303,7 @@ def _Contract(
# 3) from explorer
if contract_type:
return ContractInstance( # type: ignore
_address=address,
_address=converters.convert(address, AddressType),
_provider=networks.active_provider,
_contract_type=contract_type,
)
Expand All @@ -312,6 +312,6 @@ def _Contract(
# We don't have a contract type from any source, provide raw address instead
notify("WARNING", f"No contract type found for {address}")
return Address( # type: ignore
_address=address,
_address=converters.convert(address, AddressType),
_provider=networks.active_provider,
)
6 changes: 4 additions & 2 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pluggy import PluginManager # type: ignore

from ape.types import ABI
from ape.types import ABI, AddressType
from ape.utils import cached_property

from .base import abstractdataclass, abstractmethod
Expand Down Expand Up @@ -106,7 +106,9 @@ def encode_deployment(
...

@abstractmethod
def encode_transaction(self, address: str, abi: ABI, *args, **kwargs) -> "TransactionAPI":
def encode_transaction(
self, address: AddressType, abi: ABI, *args, **kwargs
) -> "TransactionAPI":
...

@abstractmethod
Expand Down
7 changes: 5 additions & 2 deletions src/ape/managers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pluggy import PluginManager # type: ignore

from ape.api.accounts import AccountAPI, AccountContainerAPI
from ape.types import AddressType
from ape.utils import cached_property, singledispatchmethod

from .config import ConfigManager
Expand Down Expand Up @@ -81,7 +82,9 @@ def __getitem_int(self, account_id: int) -> AccountAPI:
raise IndexError(f"No account at index `{account_id}`.")

@__getitem__.register
def __getitem_str(self, account_id: str) -> AccountAPI:
def __getitem_str(self, account_str: str) -> AccountAPI:
account_id = self.converters.convert(account_str, AddressType)

for container in self.containers.values():
if account_id in container:
account = container[account_id]
Expand All @@ -91,5 +94,5 @@ def __getitem_str(self, account_id: str) -> AccountAPI:

raise IndexError(f"No account with address `{account_id}`.")

def __contains__(self, address: str) -> bool:
def __contains__(self, address: AddressType) -> bool:
return any(address in container for container in self.containers.values())
8 changes: 4 additions & 4 deletions src/ape/managers/converters.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Any, Dict, List, Type

from dataclassy import dataclass
from eth_typing import ChecksumAddress
from eth_utils import is_checksum_address, is_hex
from hexbytes import HexBytes

from ape.api import AddressAPI, ConverterAPI
from ape.plugins import PluginManager
from ape.types import AddressType
from ape.utils import cached_property

from .config import ConfigManager
Expand All @@ -29,7 +29,7 @@ class AddressConverter(ConverterAPI):
def is_convertible(self, value: Any) -> bool:
return isinstance(value, AddressAPI)

def convert(self, value: AddressAPI) -> ChecksumAddress:
def convert(self, value: AddressAPI) -> AddressType:
return value.address


Expand All @@ -45,7 +45,7 @@ class ConversionManager:
@cached_property
def _converters(self) -> Dict[Type, List[ConverterAPI]]:
converters: Dict[Type, List[ConverterAPI]] = {
ChecksumAddress: [address_converter],
AddressType: [address_converter],
bytes: [hex_converter],
int: [],
}
Expand All @@ -61,7 +61,7 @@ def _converters(self) -> Dict[Type, List[ConverterAPI]]:
return converters

def is_type(self, value: Any, type: Type) -> bool:
if type is ChecksumAddress:
if type is AddressType:
return is_checksum_address(value)

else:
Expand Down
3 changes: 3 additions & 0 deletions src/ape/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from eth_typing import ChecksumAddress as AddressType

from .contract import ABI, Bytecode, Checksum, Compiler, ContractType, Source
from .manifest import PackageManifest, PackageMeta

__all__ = [
"ABI",
"AddressType",
"Bytecode",
"Checksum",
"Compiler",
Expand Down
3 changes: 2 additions & 1 deletion src/ape_accounts/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from ape.api import AccountAPI, AccountContainerAPI, TransactionAPI
from ape.convert import to_address
from ape.types import AddressType


class AccountContainer(AccountContainerAPI):
Expand Down Expand Up @@ -46,7 +47,7 @@ def keyfile(self) -> dict:
return json.loads(self._keyfile.read_text())

@property
def address(self) -> str:
def address(self) -> AddressType:
return to_address(self.keyfile["address"])

@property
Expand Down
10 changes: 8 additions & 2 deletions src/ape_ethereum/ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from hexbytes import HexBytes

from ape.api import ContractLog, EcosystemAPI, ReceiptAPI, TransactionAPI, TransactionStatusEnum
from ape.types import ABI
from ape.types import ABI, AddressType

NETWORKS = {
# chain_id, network_id
Expand Down Expand Up @@ -101,7 +101,13 @@ def encode_deployment(

return txn

def encode_transaction(self, address: str, abi: ABI, *args, **kwargs) -> Transaction:
def encode_transaction(
self,
address: AddressType,
abi: ABI,
*args,
**kwargs,
) -> Transaction:
txn = Transaction(receiver=address, **kwargs) # type: ignore

# Add method ID
Expand Down

0 comments on commit a80a3d6

Please sign in to comment.