Skip to content

Commit

Permalink
feat: add non-checksummed address converter (with warning)
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Jul 22, 2021
1 parent 88f7dc3 commit 90cc548
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/ape/managers/converters.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from typing import Any, Dict, List, Type

from dataclassy import dataclass
from eth_utils import is_checksum_address, is_hex
from eth_utils import is_checksum_address, is_hex, is_hex_address, to_checksum_address
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 ape.utils import cached_property, notify

from .config import ConfigManager
from .networks import NetworkManager
Expand All @@ -25,15 +25,27 @@ def convert(self, value: str) -> bytes:
hex_converter = HexConverter(None, None) # type: ignore


class AddressConverter(ConverterAPI):
class AddressAPIConverter(ConverterAPI):
def is_convertible(self, value: Any) -> bool:
return isinstance(value, AddressAPI)

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


address_converter = AddressConverter(None, None) # type: ignore
address_api_converter = AddressAPIConverter(None, None) # type: ignore


class HexAddressConverter(ConverterAPI):
def is_convertible(self, value: str) -> bool:
return isinstance(value, str) and is_hex_address(value) and not is_checksum_address(value)

def convert(self, value: str) -> AddressType:
notify("WARNING", f"'{value}' is not in checksummed form")
return to_checksum_address(value)


hex_address_converter = HexAddressConverter(None, None) # type: ignore


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

0 comments on commit 90cc548

Please sign in to comment.