diff --git a/README.md b/README.md index 0c791f5..0773130 100644 --- a/README.md +++ b/README.md @@ -58,5 +58,6 @@ To connect to Infura from a Python script, use the `networks` top-level manager: from ape import networks with networks.parse_network_choice("ethereum:mainnet:infura") as provider: - ... + # Also, access the websocket URI: + print(provider.ws_uri) ``` diff --git a/tests/conftest.py b/tests/conftest.py index 01742a9..ca8f003 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,8 @@ import ape import pytest +from ape_infura import NETWORKS + @pytest.fixture def accounts(): @@ -15,3 +17,11 @@ def Contract(): @pytest.fixture def networks(): return ape.networks + + +@pytest.fixture(params=[(name, net) for name, values in NETWORKS.items() for net in values]) +def provider(networks, request): + ecosystem_cls = networks.get_ecosystem(request.param[0]) + network_cls = ecosystem_cls.get_network(request.param[1]) + with network_cls.use_provider("infura") as provider: + yield provider diff --git a/tests/test_provider.py b/tests/test_provider.py index b292cbd..7223201 100644 --- a/tests/test_provider.py +++ b/tests/test_provider.py @@ -1,34 +1,28 @@ import pytest -from ape import networks +import websocket # type: ignore from ape.utils import ZERO_ADDRESS from ape_infura.provider import Infura -@pytest.mark.parametrize( - "ecosystem,network", - [ - ("ethereum", "mainnet"), - ("ethereum", "goerli"), - ("ethereum", "sepolia"), - ("arbitrum", "mainnet"), - ("arbitrum", "goerli"), - ("optimism", "mainnet"), - ("optimism", "goerli"), - ("polygon", "mainnet"), - ("polygon", "mumbai"), - ("linea", "mainnet"), - ("linea", "goerli"), - ], -) -def test_infura(ecosystem, network): - ecosystem_cls = networks.get_ecosystem(ecosystem) - network_cls = ecosystem_cls.get_network(network) - with network_cls.use_provider("infura") as provider: - assert isinstance(provider, Infura) - assert provider.http_uri.startswith("https") - assert provider.ws_uri.startswith("wss") - assert provider.get_balance(ZERO_ADDRESS) > 0 - assert provider.get_block(0) - ecosystem_uri = "" if ecosystem == "ethereum" else f"{ecosystem}-" - assert f"https://{ecosystem_uri}{network}.infura.io/v3/" in provider.uri +def test_infura_http(provider): + ecosystem = provider.network.ecosystem.name + network = provider.network.name + assert isinstance(provider, Infura) + assert provider.http_uri.startswith("https") + assert provider.get_balance(ZERO_ADDRESS) > 0 + assert provider.get_block(0) + ecosystem_uri = "" if ecosystem == "ethereum" else f"{ecosystem}-" + assert f"https://{ecosystem_uri}{network}.infura.io/v3/" in provider.uri + + +def test_infura_ws(provider): + assert provider.ws_uri.startswith("wss") + + try: + ws = websocket.WebSocket() + ws.connect(provider.ws_uri) + ws.close() + + except Exception as err: + pytest.fail(f"Websocket URI not accessible. Reason: {err}")