Skip to content

Commit

Permalink
Merge pull request #9 from DanielSchiavini/0.1
Browse files Browse the repository at this point in the history
0.1 to master
  • Loading branch information
DanielSchiavini authored Aug 20, 2024
2 parents 2b01517 + 9f5464b commit 7d65e5f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion boa_zksync/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from boa.rpc import to_bytes

from boa_zksync.compiler_utils import get_compiler_output
from boa_zksync.types import ZksyncCompilerData


Expand Down Expand Up @@ -40,7 +41,7 @@ def compile_zksync(
with open(filename) as file:
source_code = file.read()

compile_output = output[filename.removeprefix("./")]
compile_output = get_compiler_output(output)
bytecode = to_bytes(compile_output.pop("bytecode"))
return ZksyncCompilerData(
contract_name, source_code, compiler_args, bytecode, **compile_output
Expand Down
15 changes: 15 additions & 0 deletions boa_zksync/compiler_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,18 @@ def detect_expr_type(source_code, contract):
except InvalidType:
pass
return None


def get_compiler_output(output):
# we need this helper method to get the correct key containing compiler output
# from the compiler. Assuming key names could change and also assuming that the
# number of keys could change, this method breaks if any of that happens:

excluded_keys = {"version", "zk_version", "__VYPER_MINIMAL_PROXY_CONTRACT"}
contract_keys = set(output.keys()) - excluded_keys

if len(contract_keys) != 1:
unexpected = ", ".join(sorted(contract_keys))
raise ValueError(f"Expected exactly one contract key, found {unexpected}")

return output[next(iter(contract_keys))]
37 changes: 37 additions & 0 deletions tests/test_get_compiler_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest

from boa_zksync.compiler_utils import get_compiler_output


def test_get_compiler_output():

output_dict = {"blabla": 123, "zk_version": 456, "version": 789}

assert get_compiler_output(output_dict) == 123


def test_get_compiler_output_too_many_keys():

output_dict = {
"blabla": 123,
"zk_version": 456,
"version": 789,
"new_compiler_output_key": 101112,
}

with pytest.raises(
ValueError,
match="Expected exactly one contract key, found blabla, new_compiler_output_key",
):
get_compiler_output(output_dict)


def test_get_compiler_output_unexpected_key():

output_dict = {"blabla": 123, "zk_versions": 456, "version": 789}

with pytest.raises(
ValueError,
match="Expected exactly one contract key, found blabla, zk_versions",
):
get_compiler_output(output_dict)

0 comments on commit 7d65e5f

Please sign in to comment.