Skip to content

Commit

Permalink
feat: compiler code implementation (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 3, 2023
1 parent a56213a commit fd9c5d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ape_vyper/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,24 @@ def compile(

return contract_types

def compile_code(self, code: str, base_path: Optional[Path] = None, **kwargs) -> ContractType:
base_path = base_path or self.project_manager.contracts_folder
try:
result = vvm.compile_source(code, base_path=base_path)
except Exception as err:
raise VyperCompileError(str(err)) from err

output = result.get("<stdin>", {})
return ContractType(
abi=output["abi"],
deploymentBytecode={"bytecode": output["bytecode"]},
runtimeBytecode={"bytecode": output["bytecode_runtime"]},
**kwargs,
)

def get_optimization_pragma_map(
self, contract_filepaths: List[Path], base_path: Optional[Path] = None
self, contract_filepaths: List[Path]
) -> Dict[Union[str, bool], Set[Path]]:
base_path = base_path or self.config_manager.contracts_folder
optimization_pragma_map: Dict[Union[str, bool], Set[Path]] = {}
for path in contract_filepaths:
pragma = get_optimization_pragma(path) or True
Expand Down
11 changes: 11 additions & 0 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
from ape.exceptions import ContractLogicError
from ethpm_types import ContractType
from packaging.version import Version
from vvm import compile_source # type: ignore
from vvm.exceptions import VyperError # type: ignore
Expand Down Expand Up @@ -459,3 +460,13 @@ def test_compile_with_version_set(config, projects_path, compiler, mocker):
# Show it uses this version in the compiler.
project.load_contracts(use_cache=False)
assert str(spy.call_args[1]["vyper_version"]) == version_from_config


def test_compile_code(compiler, dev_revert_source):
code = dev_revert_source.read_text()
actual = compiler.compile_code(code, contractName="MyContract")
assert isinstance(actual, ContractType)
assert actual.name == "MyContract"
assert len(actual.abi) > 1
assert len(actual.deployment_bytecode.bytecode) > 1
assert len(actual.runtime_bytecode.bytecode) > 1

0 comments on commit fd9c5d2

Please sign in to comment.