Skip to content

Commit

Permalink
refactor: use metho
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 26, 2024
1 parent 8bf1e9f commit b71d57a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
16 changes: 10 additions & 6 deletions ape_infura/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,22 @@ def ws_uri(self) -> Optional[str]:
def connection_str(self) -> str:
return self.uri

@property
def _needs_poa_middleware(self) -> bool:
if self._web3 is None:
return False

def connect(self):
session = _get_session()
session.auth = ("", api_secret)

http_provider = HTTPProvider(self.uri, session=session)
self._web3 = Web3(http_provider)

if self._needs_poa_middleware:
self._web3.middleware_onion.inject(geth_poa_middleware, layer=0)

self._web3.eth.set_gas_price_strategy(rpc_gas_price_strategy)

@property
def _needs_poa_middleware(self) -> bool:
if self._web3 is None:
return False

# Any chain that *began* as PoA needs the middleware for pre-merge blocks
optimism = (10, 420)
polygon = (137, 80001, 80002)
Expand Down
5 changes: 1 addition & 4 deletions tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import pytest
import websocket # type: ignore
from ape import networks
from ape.utils import ZERO_ADDRESS
from web3.exceptions import ExtraDataLengthError
from web3.middleware import geth_poa_middleware

from ape_infura.provider import _WEBSOCKET_CAPABLE_ECOSYSTEMS, Infura
from ape_infura.provider import _WEBSOCKET_CAPABLE_ECOSYSTEMS, Infura, _get_session


def test_infura_http(provider):
Expand Down Expand Up @@ -111,8 +110,6 @@ def test_dynamic_poa_check(mocker):

def test_api_secret():
os.environ["WEB3_INFURA_PROJECT_SECRET"] = "123"
mainnet = networks.ethereum.mainnet
infura = Infura(name="infura", network=mainnet)
session = _get_session()
assert session.auth == ("", "123")
del os.environ["WEB3_INFURA_PROJECT_SECRET"]

0 comments on commit b71d57a

Please sign in to comment.