Skip to content

Commit

Permalink
fix: issues demanding anvil bin [APE-1190] (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Jul 18, 2023
1 parent 77e291f commit d0c0861
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
15 changes: 13 additions & 2 deletions ape_foundry/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import tempfile
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -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)
12 changes: 12 additions & 0 deletions tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

0 comments on commit d0c0861

Please sign in to comment.