Skip to content

Commit eef5aa1

Browse files
authored
Merge pull request #517 from crytic/feat/via-ir-etherscan
platform: etherscan: add support for viaIR
2 parents 3f78565 + 457fd52 commit eef5aa1

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

crytic_compile/platform/etherscan.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,19 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
372372
working_dir: Optional[str] = None
373373
remappings: Optional[List[str]] = None
374374

375+
dict_source_code: Optional[Dict] = None
375376
try:
376377
# etherscan might return an object with two curly braces, {{ content }}
377378
dict_source_code = json.loads(source_code[1:-1])
379+
assert isinstance(dict_source_code, dict)
378380
filenames, working_dir, remappings = _handle_multiple_files(
379381
dict_source_code, addr, prefix, contract_name, export_dir
380382
)
381383
except JSONDecodeError:
382384
try:
383385
# or etherscan might return an object with single curly braces, { content }
384386
dict_source_code = json.loads(source_code)
387+
assert isinstance(dict_source_code, dict)
385388
filenames, working_dir, remappings = _handle_multiple_files(
386389
dict_source_code, addr, prefix, contract_name, export_dir
387390
)
@@ -390,6 +393,11 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
390393
_handle_single_file(source_code, addr, prefix, contract_name, export_dir)
391394
]
392395

396+
# viaIR is not exposed on the top level JSON offered by etherscan, so we need to inspect the settings
397+
via_ir_enabled: Optional[bool] = None
398+
if isinstance(dict_source_code, dict):
399+
via_ir_enabled = dict_source_code.get("settings", {}).get("viaIR", None)
400+
393401
compilation_unit = CompilationUnit(crytic_compile, contract_name)
394402

395403
compilation_unit.compiler_version = CompilerVersion(
@@ -413,6 +421,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
413421
working_dir=working_dir,
414422
remappings=remappings,
415423
evm_version=evm_version,
424+
via_ir=via_ir_enabled,
416425
)
417426

418427
def clean(self, **_kwargs: str) -> None:

crytic_compile/platform/solc_standard_json.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
LOGGER = logging.getLogger("CryticCompile")
2626

2727

28+
# pylint: disable=too-many-arguments
2829
def standalone_compile(
2930
filenames: List[str],
3031
compilation_unit: CompilationUnit,
3132
working_dir: Optional[str] = None,
3233
remappings: Optional[List[str]] = None,
3334
evm_version: Optional[str] = None,
35+
via_ir: Optional[bool] = None,
3436
) -> None:
3537
"""
3638
Boilerplate function to run the the standardjson. compilation_unit.compiler_version must be set before calling this function
@@ -48,6 +50,7 @@ def standalone_compile(
4850
working_dir (Optional[str]): working directory
4951
remappings (Optional[List[str]]): list of solc remaps to use
5052
evm_version (Optional[str]): EVM version to target. None for default
53+
via_ir (Optional[bool]): whether to enable the viaIR compilation flag. None for unset
5154
5255
Returns:
5356
@@ -70,6 +73,9 @@ def standalone_compile(
7073
if evm_version is not None:
7174
add_evm_version(standard_json_dict, evm_version)
7275

76+
if via_ir is not None:
77+
add_via_ir(standard_json_dict, via_ir)
78+
7379
add_optimization(
7480
standard_json_dict,
7581
compilation_unit.compiler_version.optimized,
@@ -278,6 +284,20 @@ def add_evm_version(json_dict: Dict, version: str) -> None:
278284
json_dict["settings"]["evmVersion"] = version
279285

280286

287+
def add_via_ir(json_dict: Dict, enabled: bool) -> None:
288+
"""
289+
Enable or disable the "viaIR" compilation flag.
290+
291+
Args:
292+
json_dict (Dict): solc standard json input
293+
enabled (bool): whether viaIR is enabled
294+
295+
Returns:
296+
297+
"""
298+
json_dict["settings"]["viaIR"] = enabled
299+
300+
281301
def parse_standard_json_output(
282302
targets_json: Dict, compilation_unit: CompilationUnit, solc_working_dir: Optional[str] = None
283303
) -> None:

scripts/ci_test_etherscan.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,16 @@ then
8686
exit 255
8787
fi
8888
echo "::endgroup::"
89+
90+
delay_etherscan
91+
92+
# via-ir test for crytic/crytic-compile#517
93+
echo "::group::Etherscan #7"
94+
crytic-compile 0x9AB6b21cDF116f611110b048987E58894786C244 --compile-remove-metadata --etherscan-apikey "$GITHUB_ETHERSCAN"
95+
96+
if [ $? -ne 0 ]
97+
then
98+
echo "Etherscan #7 test failed"
99+
exit 255
100+
fi
101+
echo "::endgroup::"

0 commit comments

Comments
 (0)