diff --git a/ape_foundry/provider.py b/ape_foundry/provider.py index 3a4d50d..a57b961 100644 --- a/ape_foundry/provider.py +++ b/ape_foundry/provider.py @@ -333,7 +333,13 @@ def _set_web3(self): return self._web3 = Web3(HTTPProvider(self.uri, request_kwargs={"timeout": self.timeout})) - if not self._web3.is_connected(): + + try: + is_connected = self._web3.is_connected() + except Exception: + is_connected = False + + if not is_connected: self._web3 = None return @@ -397,7 +403,12 @@ def _start(self): else: self._host = f"http://127.0.0.1:{DEFAULT_PORT}" - self.start() + if "127.0.0.1" in self._host or "localhost" in self._host: + # Start local process + self.start() + + else: + raise FoundryProviderError(f"Failed to connect to Anvil node at '{self._clean_uri}'.") def disconnect(self): self._web3 = None diff --git a/tests/conftest.py b/tests/conftest.py index ec303de..99fdc5d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import json +import os import tempfile from contextlib import contextmanager from pathlib import Path @@ -257,3 +258,12 @@ def contract_a(owner, connected_provider, get_contract_type): ContractContainer(get_contract_type("contract_a")), contract_b.address, contract_c.address ) return contract_a + + +@pytest.fixture +def no_anvil_bin(monkeypatch): + original_path = os.environ.get("PATH") + modified_path = ":".join(path for path in original_path.split(":") if "anvil" not in path) + monkeypatch.setenv("PATH", modified_path) + yield + monkeypatch.setenv("PATH", original_path) diff --git a/tests/test_provider.py b/tests/test_provider.py index 9e8f50e..7166615 100644 --- a/tests/test_provider.py +++ b/tests/test_provider.py @@ -10,6 +10,7 @@ from evm_trace import CallType from hexbytes import HexBytes +from ape_foundry import FoundryProviderError from ape_foundry.provider import FOUNDRY_CHAIN_ID TEST_WALLET_ADDRESS = "0xD9b7fdb3FC0A0Aa3A507dCf0976bc23D49a9C7A3" @@ -293,3 +294,14 @@ def test_block_time(temp_config, networks, connected_provider): cmd = provider.build_command() assert "--block-time" in cmd assert "10" in cmd + + +def test_remote_host(temp_config, networks, no_anvil_bin): + data = {"foundry": {"host": "https://example.com"}} + with temp_config(data): + with pytest.raises( + FoundryProviderError, + match=r"Failed to connect to Anvil node at 'https://example.com'\.", + ): + with networks.ethereum.local.use_provider("foundry"): + assert True