Skip to content

Commit

Permalink
feat: make work on both
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 2, 2024
1 parent 5273714 commit eda83eb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/ape/utils/_web3_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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


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",
]
2 changes: 1 addition & 1 deletion src/ape_ethereum/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
TransactionNotFound,
)
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
Expand Down Expand Up @@ -57,6 +56,7 @@
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
from ape.utils.basemodel import ManagerAccessMixin
from ape.utils.misc import DEFAULT_MAX_RETRIES_TX, gas_estimation_error_message, to_int
from ape_ethereum._print import CONSOLE_ADDRESS, console_contract
Expand Down
2 changes: 1 addition & 1 deletion src/ape_node/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
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.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 (
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/geth/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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 (
Expand All @@ -26,6 +25,7 @@
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
Expand Down

0 comments on commit eda83eb

Please sign in to comment.