From 97e30f859d58a139045339de9d33ca353219eba0 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Fri, 1 Nov 2024 16:31:04 -0500 Subject: [PATCH] feat: upgrade web3 --- setup.py | 12 +++++------ src/ape/utils/_web3_compat.py | 28 -------------------------- src/ape_accounts/accounts.py | 3 +-- src/ape_ethereum/provider.py | 6 +++--- src/ape_node/provider.py | 2 +- src/ape_test/accounts.py | 3 +-- tests/functional/geth/test_provider.py | 2 +- 7 files changed, 13 insertions(+), 43 deletions(-) delete mode 100644 src/ape/utils/_web3_compat.py diff --git a/setup.py b/setup.py index 1022c5f157..c59895aa4b 100644 --- a/setup.py +++ b/setup.py @@ -120,13 +120,13 @@ "watchdog>=3.0,<4", # ** Dependencies maintained by Ethereum Foundation ** "eth-abi>=5.1.0,<6", - "eth-account>=0.11.3,<0.14", - "eth-typing>=3.5.2,<6", - "eth-utils>=2.1.0,<6", - "hexbytes>=0.3.1,<2", - "py-geth>=3.14.0,<6", + "eth-account>=0.13.4,<0.14", + "eth-typing>=5.0.1,<6", + "eth-utils>=5.1.0,<6", + "hexbytes>=1.2.1,<2", + "py-geth>=5.1.0,<6", "trie>=3.0.1,<4", # Peer: stricter pin needed for uv support. - "web3[tester]>=6.20.1,<8", + "web3[tester]>=7.6.0,<8", # ** Dependencies maintained by ApeWorX ** "eip712>=0.2.10,<0.3", "ethpm-types>=0.6.19,<0.7", diff --git a/src/ape/utils/_web3_compat.py b/src/ape/utils/_web3_compat.py deleted file mode 100644 index d6d3b498a8..0000000000 --- a/src/ape/utils/_web3_compat.py +++ /dev/null @@ -1,28 +0,0 @@ -from eth_account import Account as EthAccount - -try: - # Web3 v7 - from web3.middleware import ExtraDataToPOAMiddleware # type: ignore -except ImportError: - from web3.middleware import geth_poa_middleware as ExtraDataToPOAMiddleware # type: ignore - -try: - from web3.providers import WebsocketProviderV2 as WebsocketProvider # type: ignore -except ImportError: - from web3.providers import WebSocketProvider as WebsocketProvider # type: ignore - - -def sign_hash(msghash, private_key): - try: - # Web3 v7 - return EthAccount.unsafe_sign_hash(msghash, private_key) # type: ignore - except AttributeError: - # Web3 v6 - return EthAccount.signHash(msghash, private_key) # type: ignore - - -__all__ = [ - "ExtraDataToPOAMiddleware", - "sign_hash", - "WebsocketProvider", -] diff --git a/src/ape_accounts/accounts.py b/src/ape_accounts/accounts.py index cec087b505..47d4949e9b 100644 --- a/src/ape_accounts/accounts.py +++ b/src/ape_accounts/accounts.py @@ -18,7 +18,6 @@ from ape.exceptions import AccountsError from ape.logging import logger from ape.types.signatures import MessageSignature, SignableMessage, TransactionSignature -from ape.utils._web3_compat import sign_hash from ape.utils.basemodel import ManagerAccessMixin from ape.utils.misc import log_instead_of_fail from ape.utils.validators import _validate_account_alias, _validate_account_passphrase @@ -256,7 +255,7 @@ def sign_raw_msghash(self, msghash: HexBytes) -> Optional[MessageSignature]: # Also, we have already warned the user about the safety. with warnings.catch_warnings(): warnings.simplefilter("ignore") - signed_msg = sign_hash(msghash, self.__key) + signed_msg = EthAccount.unsafe_sign_hash(msghash, self.__key) return MessageSignature( v=signed_msg.v, diff --git a/src/ape_ethereum/provider.py b/src/ape_ethereum/provider.py index bd14077935..6279c813f7 100644 --- a/src/ape_ethereum/provider.py +++ b/src/ape_ethereum/provider.py @@ -19,7 +19,7 @@ from evmchains import PUBLIC_CHAIN_META, get_random_rpc from pydantic.dataclasses import dataclass from requests import HTTPError -from web3 import HTTPProvider, IPCProvider, Web3 +from web3 import HTTPProvider, IPCProvider, Web3, WebSocketProvider from web3 import __version__ as web3_version from web3.exceptions import ContractLogicError as Web3ContractLogicError from web3.exceptions import ( @@ -35,6 +35,7 @@ Web3RPCError = ValueError # type: ignore from web3.gas_strategies.rpc import rpc_gas_price_strategy +from web3.middleware import ExtraDataToPOAMiddleware from web3.middleware.validation import MAX_EXTRADATA_LENGTH from web3.providers import AutoProvider from web3.providers.auto import load_provider_from_environment @@ -63,7 +64,6 @@ from ape.types.events import ContractLog, LogFilter from ape.types.gas import AutoGasLimit from ape.types.trace import SourceTraceback -from ape.utils._web3_compat import ExtraDataToPOAMiddleware, WebsocketProvider from ape.utils.basemodel import ManagerAccessMixin from ape.utils.misc import DEFAULT_MAX_RETRIES_TX, gas_estimation_error_message, to_int from ape.utils.rpc import request_with_retry @@ -1633,7 +1633,7 @@ def _create_web3( providers.append(lambda: HTTPProvider(endpoint_uri=http, request_kwargs=request_kwargs)) if ws := ws_uri: - providers.append(lambda: WebsocketProvider(endpoint_uri=ws)) + providers.append(lambda: WebSocketProvider(endpoint_uri=ws)) provider = AutoProvider(potential_providers=providers) return Web3(provider) diff --git a/src/ape_node/provider.py b/src/ape_node/provider.py index 9ee2bb356c..2360bb2c67 100644 --- a/src/ape_node/provider.py +++ b/src/ape_node/provider.py @@ -13,12 +13,12 @@ from pydantic import field_validator from pydantic_settings import SettingsConfigDict from requests.exceptions import ConnectionError +from web3.middleware import ExtraDataToPOAMiddleware from ape.api.config import PluginConfig from ape.api.providers import SubprocessProvider, TestProviderAPI from ape.exceptions import VirtualMachineError from ape.logging import LogLevel, logger -from ape.utils._web3_compat import ExtraDataToPOAMiddleware from ape.utils.misc import ZERO_ADDRESS, log_instead_of_fail, raises_not_implemented from ape.utils.process import JoinableQueue, spawn from ape.utils.testing import ( diff --git a/src/ape_test/accounts.py b/src/ape_test/accounts.py index a0abea03a5..c2490d34bc 100644 --- a/src/ape_test/accounts.py +++ b/src/ape_test/accounts.py @@ -13,7 +13,6 @@ from ape.api.accounts import TestAccountAPI, TestAccountContainerAPI from ape.exceptions import ProviderNotConnectedError, SignatureError from ape.types.signatures import MessageSignature, TransactionSignature -from ape.utils._web3_compat import sign_hash from ape.utils.testing import ( DEFAULT_NUMBER_OF_TEST_ACCOUNTS, DEFAULT_TEST_HD_PATH, @@ -167,7 +166,7 @@ def sign_transaction( def sign_raw_msghash(self, msghash: HexBytes) -> MessageSignature: with warnings.catch_warnings(): warnings.simplefilter("ignore") - signed_msg = sign_hash(msghash, self.private_key) + signed_msg = EthAccount.unsafe_sign_hash(msghash, self.private_key) return MessageSignature( v=signed_msg.v, diff --git a/tests/functional/geth/test_provider.py b/tests/functional/geth/test_provider.py index 25d5234e87..d419de351c 100644 --- a/tests/functional/geth/test_provider.py +++ b/tests/functional/geth/test_provider.py @@ -11,6 +11,7 @@ from web3 import AutoProvider, Web3 from web3.exceptions import ContractLogicError as Web3ContractLogicError from web3.exceptions import ExtraDataLengthError +from web3.middleware import ExtraDataToPOAMiddleware from web3.providers import HTTPProvider from ape.exceptions import ( @@ -25,7 +26,6 @@ VirtualMachineError, ) from ape.utils import to_int -from ape.utils._web3_compat import ExtraDataToPOAMiddleware from ape_ethereum.ecosystem import Block from ape_ethereum.provider import DEFAULT_SETTINGS, EthereumNodeProvider from ape_ethereum.trace import TraceApproach