Skip to content

Commit

Permalink
feat: set --optimism flag for optimism-based networks (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Aug 2, 2024
1 parent 1a09913 commit e5b2c0d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ repos:
rev: 7.1.0
hooks:
- id: flake8
additional_dependencies: [flake8-breakpoint, flake8-print, flake8-pydantic]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.0
Expand Down
30 changes: 29 additions & 1 deletion ape_foundry/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@
from yarl import URL

from ape_foundry.constants import EVM_VERSION_BY_NETWORK
from ape_foundry.exceptions import (
FoundryNotInstalledError,
FoundryProviderError,
FoundrySubprocessError,
)

from .exceptions import FoundryNotInstalledError, FoundryProviderError, FoundrySubprocessError
try:
from ape_optimism import Optimism # type: ignore
except ImportError:
Optimism = None # type: ignore

EPHEMERAL_PORTS_START = 49152
EPHEMERAL_PORTS_END = 60999
Expand Down Expand Up @@ -104,6 +112,13 @@ class FoundryNetworkConfig(PluginConfig):
rather than only when a new transaction is submitted.
"""

use_optimism: Optional[bool] = None
"""
Configure the node to run with the `--optimism` flag.
NOTE: When using Optimism-based networks (including Base),
this flag is automatically added.
"""

model_config = SettingsConfigDict(extra="allow")

@field_validator("fork", mode="before")
Expand Down Expand Up @@ -162,6 +177,14 @@ def _clean_uri(self) -> str:
# Likely isn't a real URI.
return self.uri

@property
def use_optimism(self) -> bool:
return self.settings.use_optimism or (
self.settings.use_optimism is not False
and Optimism is not None
and isinstance(self.network.ecosystem, Optimism)
)

@property
def _port(self) -> Optional[int]:
return URL(self.uri).port
Expand Down Expand Up @@ -457,6 +480,11 @@ def build_command(self) -> list[str]:
if evm_version := self.evm_version:
cmd.extend(("--hardfork", evm_version))

# Optimism-based networks are different; Anvil provides a flag to make
# testing more like the real network(s).
if self.use_optimism:
cmd.append("--optimism")

return cmd

def set_balance(self, account: AddressType, amount: Union[int, float, str, bytes]):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exclude =
venv*
docs
build
ignore = E704,W503,PYD002
per-file-ignores =
# The traces have to be formatted this way for the tests.
tests/expected_traces.py: E501
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer
"ape-alchemy", # For running fork tests
"ape-polygon", # For running polygon fork tests
"ape-optimism", # For Optimism integration tests
],
"lint": [
"black>=24.4.2,<25", # Auto-formatter and linter
Expand All @@ -21,6 +22,7 @@
"flake8>=7.1.0,<8", # Style linter
"flake8-breakpoint>=1.1.0,<2", # Detect breakpoints left in code
"flake8-print>=5.0.0,<6", # Detect print statements left in code
"flake8-pydantic", # For detecting issues with Pydantic models
"isort>=5.13.2,<6", # Import sorting linter
"mdformat>=0.7.17", # Auto-formatter for markdown
"mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown
Expand Down
9 changes: 9 additions & 0 deletions tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,12 @@ def test_evm_version(project, local_network, host):
cmd = provider.build_command()
assert "--hardfork" in cmd
assert "shanghai" in cmd


def test_optimism(networks):
with networks.optimism.local.use_provider(
"foundry", provider_settings={"port": 9545}
) as provider:
assert provider.is_connected
cmd = provider.build_command()
assert "--optimism" in cmd

0 comments on commit e5b2c0d

Please sign in to comment.