Skip to content

Commit

Permalink
fix: prepare as much as possible (without breaking changes) web3.py u…
Browse files Browse the repository at this point in the history
…pgrade (#2223)
  • Loading branch information
antazoey authored Aug 19, 2024
1 parent 3f85a75 commit 891d56b
Show file tree
Hide file tree
Showing 38 changed files with 126 additions and 110 deletions.
22 changes: 12 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,22 @@
"urllib3>=2.0.0,<3",
"watchdog>=3.0,<4",
# ** Dependencies maintained by Ethereum Foundation **
"eth-abi>=5.1.0,<6",
"eth-account>=0.11.2,<0.12",
"eth-typing>=3.5.2,<4",
"eth-utils>=2.3.1,<3",
"hexbytes", # Peer
# All version pins dependent on web3[tester]
"eth-abi",
"eth-account",
"eth-typing",
"eth-utils",
"hexbytes",
"py-geth>=5.0.0-beta.2,<6",
"trie>=3.0.0,<4", # Peer: stricter pin needed for uv support.
"trie>=3.0.1,<4", # Peer: stricter pin needed for uv support.
"web3[tester]>=6.17.2,<7",
# ** Dependencies maintained by ApeWorX **
"eip712>=0.2.7,<0.3",
"ethpm-types>=0.6.14,<0.7",
"eth_pydantic_types>=0.1.0,<0.2",
# Missing pins are dependent on ETH-prefixed dependencies.
"eip712",
"ethpm-types",
"eth_pydantic_types",
"evmchains>=0.0.10,<0.1",
"evm-trace>=0.2.0,<0.3",
"evm-trace",
],
entry_points={
"console_scripts": ["ape=ape._cli:cli"],
Expand Down
3 changes: 2 additions & 1 deletion src/ape/api/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from eth_account import Account
from eth_account.messages import encode_defunct
from eth_pydantic_types import HexBytes
from eth_utils import to_hex
from ethpm_types import ContractType

from ape.api.address import BaseAddress
Expand Down Expand Up @@ -346,7 +347,7 @@ def check_signature(
if isinstance(data, str):
data = encode_defunct(text=data)
elif isinstance(data, int):
data = encode_defunct(hexstr=HexBytes(data).hex())
data = encode_defunct(hexstr=to_hex(data))
elif isinstance(data, bytes) and (len(data) != 32 or recover_using_eip191):
data = encode_defunct(data)
elif isinstance(data, EIP712Message):
Expand Down
2 changes: 1 addition & 1 deletion src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING, Any, ClassVar, Optional, Union

from eth_account import Account as EthAccount
from eth_account._utils.legacy_transactions import (
from eth_account._utils.signing import (
encode_transaction,
serializable_unsigned_transaction_from_dict,
)
Expand Down
1 change: 1 addition & 0 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ def stream_request( # type: ignore[empty-body]
An iterator of items.
"""

# TODO: In 0.9, delete this method.
def get_storage_at(self, *args, **kwargs) -> HexBytes:
warnings.warn(
"'provider.get_storage_at()' is deprecated. Use 'provider.get_storage()'.",
Expand Down
8 changes: 4 additions & 4 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def trace(self) -> "TraceAPI":
:class:`~ape.exceptions.APINotImplementedError`: When using a provider
that does not support tracing.
"""
return self.provider.get_transaction_trace(self.txn_hash.hex())
return self.provider.get_transaction_trace(to_hex(self.txn_hash))

@abstractmethod
def serialize_transaction(self) -> bytes:
Expand Down Expand Up @@ -183,13 +183,13 @@ def __str__(self) -> str:
)
else:
data["data"] = (
"0x" + bytes(data["data"][:3]).hex() + "..." + bytes(data["data"][-3:]).hex()
to_hex(bytes(data["data"][:3])) + "..." + to_hex(bytes(data["data"][-3:]))
)
else:
if isinstance(data["data"], str):
data["data"] = "0x" + bytes(data["data"], encoding="utf8").hex()
data["data"] = to_hex(bytes(data["data"], encoding="utf8"))
else:
data["data"] = "0x" + bytes(data["data"]).hex()
data["data"] = to_hex(bytes(data["data"]))
params = "\n ".join(f"{k}: {v}" for k, v in data.items())
cls_name = getattr(type(self), "__name__", TransactionAPI.__name__)
return f"{cls_name}:\n {params}"
Expand Down
4 changes: 2 additions & 2 deletions src/ape/contracts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def decode_input(self, calldata: bytes) -> tuple[str, dict[str, Any]]:
matching_abis = []
rest_calldata = None
err = ContractDataError(
f"Unable to find matching method ABI for calldata '{calldata.hex()}'. "
f"Unable to find matching method ABI for calldata '{to_hex(calldata)}'. "
"Try prepending a method ID to the beginning of the calldata."
)
for abi in self.abis:
Expand Down Expand Up @@ -856,7 +856,7 @@ def decode_input(self, calldata: bytes) -> tuple[str, dict[str, Any]]:

if not method:
raise ContractDataError(
f"Unable to find method ABI from calldata '{calldata.hex()}'. "
f"Unable to find method ABI from calldata '{to_hex(calldata)}'. "
"Try prepending the method ID to the beginning of the calldata."
)

Expand Down
8 changes: 4 additions & 4 deletions src/ape/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from typing import TYPE_CHECKING, Any, Callable, Optional, Union, cast

import click
from eth_typing import Hash32
from eth_utils import humanize_hash
from eth_typing import Hash32, HexStr
from eth_utils import humanize_hash, to_hex
from ethpm_types import ContractType
from ethpm_types.abi import ConstructorABI, ErrorABI, MethodABI
from rich import print as rich_print
Expand Down Expand Up @@ -520,9 +520,9 @@ class BlockNotFoundError(ProviderError):

def __init__(self, block_id: "BlockID", reason: Optional[str] = None):
if isinstance(block_id, bytes):
block_id_str = block_id.hex()
block_id_str = to_hex(block_id)
else:
block_id_str = str(block_id)
block_id_str = HexStr(str(block_id))

message = (
"Missing latest block."
Expand Down
2 changes: 0 additions & 2 deletions src/ape/managers/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,6 @@ def cache_proxy_info(self, address: AddressType, proxy_info: ProxyInfoAPI):
proxy_info (:class:`~ape.api.networks.ProxyInfo`): The proxy info class
to cache.
"""

if self.get_proxy_info(address) and self._is_live_network:
return

Expand Down Expand Up @@ -860,7 +859,6 @@ def get_proxy_info(self, address: AddressType) -> Optional[ProxyInfoAPI]:
Returns:
Optional[:class:`~ape.api.networks.ProxyInfoAPI`]
"""

return self._local_proxies.get(address) or self._get_proxy_info_from_disk(address)

def get_creation_metadata(self, address: AddressType) -> Optional[ContractCreation]:
Expand Down
5 changes: 3 additions & 2 deletions src/ape/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Optional, Union, cast

from eth_typing import HexStr
from eth_utils import to_hex
from ethpm_types import ContractInstance as EthPMContractInstance
from ethpm_types import ContractType, PackageManifest, PackageMeta, Source
from ethpm_types.source import Compiler, ContractSource
Expand Down Expand Up @@ -1975,7 +1976,7 @@ def track(self, contract: ContractInstance):
f"at block_number={block_number} is unknown."
)

block_hash = block_hash_bytes.hex()
block_hash = to_hex(block_hash_bytes)
contract_type_str = (
f"{contract.contract_type.source_id}:{contract_name}"
if contract.contract_type.source_id
Expand All @@ -1992,7 +1993,7 @@ def track(self, contract: ContractInstance):
if not (block_0_hash := self.provider.get_block(0).hash):
raise ProjectError("Chain missing hash for block 0 (required for BIP-122 chain ID).")

bip122_chain_id = f"{block_0_hash.hex()[2:]}"
bip122_chain_id = f"{to_hex(block_0_hash)[2:]}"
deployments_folder = self.cache_folder / bip122_chain_id
deployments_folder.mkdir(exist_ok=True, parents=True)
destination = deployments_folder / f"{contract_name}.json"
Expand Down
3 changes: 2 additions & 1 deletion src/ape/pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Optional

import pytest
from eth_utils import to_hex

from ape.api.accounts import TestAccountAPI
from ape.api.transactions import ReceiptAPI
Expand Down Expand Up @@ -178,7 +179,7 @@ def capture_range(self, start_block: int, stop_block: int):

for txn in transactions:
try:
txn_hash = txn.txn_hash.hex()
txn_hash = to_hex(txn.txn_hash)
except Exception:
# Might have been from an impersonated account.
# Those txns need to be added separately, same as tracing calls.
Expand Down
2 changes: 1 addition & 1 deletion src/ape/types/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _bytes_to_human_str(bytes_value: bytes) -> Optional[str]:

try:
# Try as hex
return HexBytes(bytes_value).hex()
return to_hex(bytes_value)
except Exception:
pass

Expand Down
4 changes: 2 additions & 2 deletions src/ape/utils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from eth_account import Account
from eth_account.hdaccount import HDPath
from eth_account.hdaccount.mnemonic import Mnemonic
from eth_pydantic_types import HexBytes
from eth_utils import to_hex

DEFAULT_NUMBER_OF_TEST_ACCOUNTS = 10
DEFAULT_TEST_MNEMONIC = "test test test test test test test test test test test junk"
Expand Down Expand Up @@ -60,7 +60,7 @@ def generate_dev_accounts(
def _generate_dev_account(hd_path, index: int, seed: bytes) -> GeneratedDevAccount:
return GeneratedDevAccount(
address=Account.from_key(
private_key := HexBytes(HDPath(hd_path.format(index)).derive(seed)).hex()
private_key := to_hex(HDPath(hd_path.format(index)).derive(seed))
).address,
private_key=private_key,
)
4 changes: 2 additions & 2 deletions src/ape_accounts/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import click
from eth_account import Account as EthAccount
from eth_account.hdaccount import ETHEREUM_DEFAULT_PATH
from eth_utils import to_checksum_address
from eth_utils import to_checksum_address, to_hex

from ape.cli import ape_cli_context, existing_alias_argument, non_existing_alias_argument
from ape.logging import HIDDEN_MESSAGE
Expand Down Expand Up @@ -178,7 +178,7 @@ def export(cli_ctx, alias):
private_key = EthAccount.decrypt(account, password)
address = to_checksum_address(account["address"])
cli_ctx.logger.success(
f"Account {address} private key: {click.style(private_key.hex(), bold=True)}"
f"Account {address} private key: {click.style(to_hex(private_key), bold=True)}"
)


Expand Down
16 changes: 8 additions & 8 deletions src/ape_accounts/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from eth_account.signers.local import LocalAccount
from eth_keys import keys # type: ignore
from eth_pydantic_types import HexBytes
from eth_utils import to_bytes
from eth_utils import to_bytes, to_hex

from ape.api import AccountAPI, AccountContainerAPI, TransactionAPI
from ape.exceptions import AccountsError
Expand Down Expand Up @@ -63,7 +63,7 @@ class KeyfileAccount(AccountAPI):
keyfile_path: Path
locked: bool = True
__autosign: bool = False
__cached_key: Optional[HexBytes] = None
__cached_key: Optional[bytes] = None

@log_instead_of_fail(default="<KeyfileAccount>")
def __repr__(self) -> str:
Expand All @@ -85,7 +85,7 @@ def address(self) -> AddressType:
return self.network_manager.ethereum.decode_address(self.keyfile["address"])

@property
def __key(self) -> HexBytes:
def __key(self) -> bytes:
if self.__cached_key is not None:
if not self.locked:
logger.warning("Using cached key for %s", self.alias)
Expand Down Expand Up @@ -166,10 +166,10 @@ def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]

elif isinstance(msg, int):
display_msg = f"Signing raw integer: {msg}"
msg = encode_defunct(hexstr=HexBytes(msg).hex())
msg = encode_defunct(hexstr=to_hex(msg))

elif isinstance(msg, bytes):
display_msg = f"Signing raw bytes: '{msg.hex()}'"
display_msg = f"Signing raw bytes: '{to_hex(msg)}'"
msg = encode_defunct(primitive=msg)

elif isinstance(msg, EIP712Message):
Expand All @@ -187,13 +187,13 @@ def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]
if msg._verifyingContract_:
display_msg += f"\tContract: {msg._verifyingContract_}\n"
if msg._salt_:
display_msg += f"\tSalt: 0x{msg._salt_.hex()}\n"
display_msg += f"\tSalt: {to_hex(msg._salt_)}\n"

# Message Data
display_msg += "Message\n"
for field, value in msg._body_["message"].items():
if isinstance(value, bytes):
value = HexBytes(value).hex()
value = to_hex(value)
display_msg += f"\t{field}: {value}\n"

# Convert EIP712Message to SignableMessage for handling below
Expand Down Expand Up @@ -283,7 +283,7 @@ def _prompt_for_passphrase(self, message: Optional[str] = None, **kwargs) -> str
**kwargs,
)

def __decrypt_keyfile(self, passphrase: str) -> HexBytes:
def __decrypt_keyfile(self, passphrase: str) -> bytes:
try:
return EthAccount.decrypt(self.keyfile, passphrase)
except ValueError as err:
Expand Down
Empty file added src/ape_cache/py.typed
Empty file.
Empty file added src/ape_console/py.typed
Empty file.
12 changes: 6 additions & 6 deletions src/ape_ethereum/_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from typing import Any, cast

from eth_abi import decode
from eth_typing import ChecksumAddress, HexStr
from eth_utils import add_0x_prefix, decode_hex
from eth_typing import ChecksumAddress
from eth_utils import add_0x_prefix, decode_hex, to_hex
from ethpm_types import ContractType, MethodABI
from evm_trace import CallTreeNode
from hexbytes import HexBytes
Expand All @@ -43,7 +43,7 @@ def is_console_log(call: CallTreeNode) -> TypeGuard[CallTreeNode]:
"""Determine if a call is a standard console.log() call"""
return (
call.address == HexBytes(CONSOLE_ADDRESS)
and call.calldata[:4].hex() in console_contract.identifier_lookup
and to_hex(call.calldata[:4]) in console_contract.identifier_lookup
)


Expand Down Expand Up @@ -82,12 +82,12 @@ def vyper_print(calldata: str) -> tuple[Any]:
def extract_debug_logs(call: CallTreeNode) -> Iterable[tuple[Any]]:
"""Filter calls to console.log() and print() from a transactions call tree"""
if is_vyper_print(call) and call.calldata is not None:
yield vyper_print(add_0x_prefix(HexStr(call.calldata[4:].hex())))
yield vyper_print(add_0x_prefix(to_hex(call.calldata[4:])))

elif is_console_log(call) and call.calldata is not None:
method_abi = console_contract.identifier_lookup.get(call.calldata[:4].hex())
method_abi = console_contract.identifier_lookup.get(to_hex(call.calldata[:4]))
if isinstance(method_abi, MethodABI):
yield console_log(method_abi, call.calldata[4:].hex())
yield console_log(method_abi, to_hex(call.calldata[4:]))

elif call.calls is not None:
for sub_call in call.calls:
Expand Down
Loading

0 comments on commit 891d56b

Please sign in to comment.