Skip to content

Commit

Permalink
feat: specify opt runs in cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Jun 3, 2024
1 parent cf35d82 commit 8e4dd4a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion ape_solidity/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ class SolidityConfig(PluginConfig):
Compile with optimization. Defaults to ``True``.
"""

optimization_runs: int = DEFAULT_OPTIMIZATION_RUNS
"""
The number of runs specifies roughly how often each opcode of the
deployed code will be executed across the lifetime of the contract.
Lower values will optimize more for initial deployment cost, higher
values will optimize more for high-frequency usage.
"""

version: Optional[str] = None
"""
Hardcode a Solidity version to use. When not set,
Expand Down Expand Up @@ -379,7 +387,7 @@ def _get_settings_from_version_map(
settings: dict = {}
for solc_version, sources in version_map.items():
version_settings: dict[str, Union[Any, list[Any]]] = {
"optimizer": {"enabled": config.optimize, "runs": DEFAULT_OPTIMIZATION_RUNS},
"optimizer": {"enabled": config.optimize, "runs": config.optimization_runs},
"outputSelection": {
str(get_relative_path(p, pm.path)): {"*": OUTPUT_SELECTION, "": ["ast"]}
for p in sorted(sources)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ def test_get_version_map_full_project(project, compiler):

def test_get_compiler_settings(project, compiler):
path = project.sources.lookup("contracts/Imports.sol")

# Set this settings using an adhoc approach.
compiler.compiler_settings["optimization_runs"] = 190

actual = compiler.get_compiler_settings((path,), project=project)
# No reason (when alone) to not use
assert len(actual) == 1
Expand All @@ -344,7 +348,7 @@ def test_get_compiler_settings(project, compiler):
assert version > Version("0.8.12+commit.f00d7308")

settings = actual[version]
assert settings["optimizer"] == {"enabled": True, "runs": 200}
assert settings["optimizer"] == {"enabled": True, "runs": 190}

# NOTE: These should be sorted!
assert settings["remappings"] == [
Expand Down

0 comments on commit 8e4dd4a

Please sign in to comment.