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 d408bac commit 667ea07
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 10 additions & 7 deletions ape_infura/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,21 @@ def ws_uri(self) -> Optional[str]:
def connection_str(self) -> str:
return self.uri

def connect(self):
session = _get_session()
http_provider = HTTPProvider(self.uri, session=session)
self._web3 = _create_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

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

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

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

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


def test_infura_http(provider):
Expand Down Expand Up @@ -111,8 +111,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 667ea07

Please sign in to comment.