Skip to content

Commit

Permalink
Merge pull request #797 from eth-brownie/feat-vyper-lib
Browse files Browse the repository at this point in the history
Use vyper library for latest version
  • Loading branch information
iamdefinitelyahuman authored Oct 7, 2020
2 parents f605a53 + 2ba0bf3 commit 212332c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
70 changes: 49 additions & 21 deletions brownie/project/compiler/vyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from typing import Dict, List, Optional, Tuple, Union

import vvm
import vyper
from semantic_version import Version
from vyper.cli import vyper_json
from vyper.exceptions import VyperException

from brownie.exceptions import CompilerError, IncompatibleVyperVersion
from brownie.project import sources
Expand All @@ -21,20 +24,26 @@
vvm_logger.addHandler(sh)

AVAILABLE_VYPER_VERSIONS = None
_active_version = Version(vyper.__version__)


def get_version() -> Version:
return vvm.get_vyper_version()
return _active_version


def set_vyper_version(version: str) -> str:
def set_vyper_version(version: Union[str, Version]) -> str:
"""Sets the vyper version. If not available it will be installed."""
try:
vvm.set_vyper_version(version, silent=True)
except vvm.exceptions.VyperNotInstalled:
install_vyper(version)
vvm.set_vyper_version(version, silent=True)
return str(vvm.get_vyper_version())
global _active_version
if isinstance(version, str):
version = Version(version)
if version != Version(vyper.__version__):
try:
vvm.set_vyper_version(version, silent=True)
except vvm.exceptions.VyperNotInstalled:
install_vyper(version)
vvm.set_vyper_version(version, silent=True)
_active_version = version
return str(_active_version)


def get_abi(contract_source: str, name: str) -> Dict:
Expand All @@ -43,19 +52,31 @@ def get_abi(contract_source: str, name: str) -> Dict:
This function is deprecated in favor of `brownie.project.compiler.get_abi`
"""
compiled = vvm.compile_standard(
{
"language": "Vyper",
"sources": {name: {"content": contract_source}},
"settings": {"outputSelection": {"*": {"*": ["abi"]}}},
}
)
input_json = {
"language": "Vyper",
"sources": {name: {"content": contract_source}},
"settings": {"outputSelection": {"*": {"*": ["abi"]}}},
}
if _active_version == Version(vyper.__version__):
try:
compiled = vyper_json.compile_json(input_json)
except VyperException as exc:
raise exc.with_traceback(None)
else:
try:
compiled = vvm.compile_standard(input_json, vyper_version=_active_version)
except vvm.exceptions.VyperError as exc:
raise CompilerError(exc, "vyper")

return {name: compiled["contracts"][name][name]["abi"]}


def _get_vyper_version_list() -> Tuple[List, List]:
global AVAILABLE_VYPER_VERSIONS
installed_versions = vvm.get_installed_vyper_versions()
lib_version = Version(vyper.__version__)
if lib_version not in installed_versions:
installed_versions.append(lib_version)
if AVAILABLE_VYPER_VERSIONS is None:
try:
AVAILABLE_VYPER_VERSIONS = vvm.get_installable_vyper_versions()
Expand Down Expand Up @@ -200,17 +221,24 @@ def compile_from_input_json(
Returns: standard compiler output json
"""

version = get_version()
if not silent:
print("Compiling contracts...")
print(f" Vyper version: {get_version()}")
if get_version() < Version("0.1.0-beta.17"):
print(f" Vyper version: {version}")
if version < Version("0.1.0-beta.17"):
outputs = input_json["settings"]["outputSelection"]["*"]["*"]
outputs.remove("userdoc")
outputs.remove("devdoc")
try:
return vvm.compile_standard(input_json, base_path=allow_paths)
except vvm.exceptions.VyperError as exc:
raise CompilerError(exc, "vyper")
if version == Version(vyper.__version__):
try:
return vyper_json.compile_json(input_json, root_path=allow_paths)
except VyperException as exc:
raise exc.with_traceback(None)
else:
try:
return vvm.compile_standard(input_json, base_path=allow_paths, vyper_version=version)
except vvm.exceptions.VyperError as exc:
raise CompilerError(exc, "vyper")


def _get_unique_build_json(
Expand Down
10 changes: 8 additions & 2 deletions brownie/utils/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pygments.formatters import get_formatter_by_name
from pygments.lexers import PythonLexer
from pygments_lexer_solidity import SolidityLexer
from vyper.exceptions import VyperException

from brownie._config import CONFIG

Expand Down Expand Up @@ -148,9 +149,14 @@ def format_tb(
if code:
tb[i] += f"\n{code}"

from brownie.exceptions import CompilerError

msg = str(exc)
if isinstance(exc, VyperException):
# apply syntax highlight and remove traceback on vyper exceptions
msg = self.highlight(msg)
if not CONFIG.argv["tb"]:
tb.clear()

from brownie.exceptions import CompilerError

if isinstance(exc, CompilerError):
# apply syntax highlighting on solc exceptions
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ requests>=2.23.0,<3.0.0
rlp==1.2.0
semantic-version==2.8.5
tqdm==4.48.2
vvm>=0.0.2,<0.1.0
vvm>=0.1.0,<0.2.0
vyper>=0.2.5,<0.30
web3==5.11.1
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ force_grid_wrap = 0
include_trailing_comma = True
known_standard_library = tkinter
known_first_party = brownie
known_third_party = _pytest,black,ens,eth_abi,eth_account,eth_event,eth_hash,eth_keys,eth_utils,ethpm,hexbytes,hypothesis,mythx_models,prompt_toolkit,psutil,py,pygments,pygments_lexer_solidity,pytest,pythx,requests,rlp,semantic_version,setuptools,solcast,solcx,tqdm,vvm,web3,xdist,yaml
known_third_party = _pytest,black,ens,eth_abi,eth_account,eth_event,eth_hash,eth_keys,eth_utils,ethpm,hexbytes,hypothesis,mythx_models,prompt_toolkit,psutil,py,pygments,pygments_lexer_solidity,pytest,pythx,requests,rlp,semantic_version,setuptools,solcast,solcx,tqdm,vvm,vyper,web3,xdist,yaml
line_length = 100
multi_line_output = 3
use_parentheses = True
Expand Down

0 comments on commit 212332c

Please sign in to comment.