Skip to content

Commit

Permalink
remove import path remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdefinitelyahuman committed Nov 8, 2019
1 parent b42f383 commit 36f0ec5
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 86 deletions.
13 changes: 2 additions & 11 deletions brownie/project/compiler.py
Expand Up @@ -15,7 +15,7 @@

from brownie.exceptions import CompilerError, IncompatibleSolcVersion, PragmaError

from . import ethpm, sources
from . import sources

solcx_logger = logging.getLogger("solcx")
solcx_logger.setLevel(10)
Expand Down Expand Up @@ -82,9 +82,6 @@ def compile_and_format(
if not contract_sources:
return {}

ethpm_sources, remappings = ethpm.resolve_ethpm_imports(contract_sources)
contract_sources.update(ethpm_sources)

if solc_version is not None:
path_versions = {solc_version: list(contract_sources)}
else:
Expand All @@ -96,9 +93,7 @@ def compile_and_format(
compiler_data = {"minify_source": minify, "version": solcx.get_solc_version_string()}
to_compile = dict((k, v) for k, v in contract_sources.items() if k in path_list)

input_json = generate_input_json(
to_compile, optimize, runs, evm_version, minify, remappings
)
input_json = generate_input_json(to_compile, optimize, runs, evm_version, minify)
output_json = compile_from_input_json(input_json, silent)
build_json.update(generate_build_json(input_json, output_json, compiler_data, silent))
return build_json
Expand Down Expand Up @@ -186,7 +181,6 @@ def generate_input_json(
runs: int = 200,
evm_version: Union[int, str, None] = None,
minify: bool = False,
remappings: Optional[List] = None,
) -> Dict:
"""Formats contracts to the standard solc input json.
Expand All @@ -196,7 +190,6 @@ def generate_input_json(
runs: optimizer runs
evm_version: evm version to compile for
minify: should source code be minified?
remappings: list of path remappings
Returns: dict
"""
Expand All @@ -206,8 +199,6 @@ def generate_input_json(
input_json["settings"]["optimizer"]["enabled"] = optimize
input_json["settings"]["optimizer"]["runs"] = runs if optimize else 0
input_json["settings"]["evmVersion"] = evm_version
if remappings is not None:
input_json["settings"]["remappings"] = remappings
input_json["sources"] = dict(
(k, {"content": sources.minify(v)[0] if minify else v}) for k, v in contract_sources.items()
)
Expand Down
57 changes: 2 additions & 55 deletions brownie/project/ethpm.py
Expand Up @@ -3,23 +3,21 @@
import json
import re
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from typing import Dict, List, Optional
from urllib.parse import urlparse

from abi2solc import generate_interface
from ethpm.package import resolve_uri_contents

from brownie._config import CONFIG
from brownie.convert import to_address
from brownie.exceptions import InvalidErc1319Import, InvalidManifest
from brownie.exceptions import InvalidManifest
from brownie.network.web3 import _resolve_address, web3

from . import compiler

URI_REGEX = (
r"""^(?:erc1319://|)([^/:\s]*):(?:[0-9]+)/([a-z][a-z0-9_-]{0,255})@[^\s:/'";]*?/([^\s:'";]*)$"""
)
IMPORT_REGEX = r"""(?:^|;)\s*import\s*(?:{[a-zA-Z][-a-zA-Z0-9_]{0,255}}\s*from|)\s*("|')((erc1319://[^\s:/'";]*:[0-9]+/[a-z][a-z0-9_-]{0,255}@[^\s:/'";]*?)/([^\s:'";]*))(?=\1\s*;)""" # NOQA: E501


def get_manifest(uri: str) -> Dict:
Expand Down Expand Up @@ -172,57 +170,6 @@ def get_deployment_addresses(
]


def resolve_ethpm_imports(contract_sources: Dict[str, str]) -> Tuple[Dict[str, str], List]:

"""
Parses a dict of contract sources for ethPM import statements.
Args:
contract_sources: A dict in the form of {'path': "source code"}
Returns: Dict of imported contract sources, List of path remappings
"""

ethpm_sources = {}
remappings = {}
for path in list(contract_sources):
for match in re.finditer(IMPORT_REGEX, contract_sources[path]):
import_str, uri, key_path = match.group(2, 3, 4)
manifest = get_manifest(uri)

type_, target = key_path.split("/", maxsplit=1)
if type_ not in ("contract_types", "sources"):
raise InvalidErc1319Import(
f"{import_str} - Path must begin with sources or contract_types, not '{type_}'"
)
if type_ == "contract_types":
if not target.endswith("/abi"):
raise InvalidErc1319Import(
f"{import_str} - Path must be in the form of "
"/contract_types/[CONTRACT_NAME]/abi"
)
contract_name = target[:-4]
ethpm_sources[import_str] = generate_interface(
manifest["contract_types"][contract_name], contract_name
)
continue
target = f"erc1319/{target}"
source_paths = set(
x
for i in manifest["contract_types"].values()
for x in i["all_source_paths"]
if i["source_path"] == target
)
if not source_paths:
raise InvalidErc1319Import(f"{import_str} - Manifest does not contain {target}")

for source_path in source_paths:
ethpm_sources[source_path] = manifest["sources"][source_path]

remappings[import_str] = target
return ethpm_sources, [f"{k}={v}" for k, v in remappings.items()]


def _get_pm(): # type: ignore
return web3._mainnet.pm

Expand Down
19 changes: 1 addition & 18 deletions docs/ethpm.rst
Expand Up @@ -73,24 +73,7 @@ If the package does not include deployment information for the currently active
Integrating a Package into your Project
=======================================

Brownie uses `path remappings <https://solidity.readthedocs.io/en/latest/layout-of-source-files.html#paths>`_ to allow you to import ethPM URIs in Solidity contracts:

.. code-block:: solidity
pragma solidity ^0.5.7;
import "erc1319://erc20.snakecharmers.eth:1/dai-dai@1.0.0/sources/DSToken.sol";
Contract DSExpanded is DSToken {
...
URIs given in import statements must include a path that follows the package version. in the example above, ``/sources/DSToken.sol``. This corresponds to the location of the source file within the ethPM manifest.

You can also import an ABI from ``/contract_types/[CONTRACT_NAME]/abi``, which Brownie converts to an `interface <https://solidity.readthedocs.io/en/latest/contracts.html#interfaces>`_:

.. code-block:: solidity
import "erc1319://erc20.snakecharmers.eth:1/dai-dai@1.0.0/contract_types/DSToken/abi";
** TODO **

Publishing an ethPM Package
===========================
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
@@ -1,4 +1,3 @@
abi2solc>=0.1.0,<0.2.0
colorama>=0.4.1;platform_system=='Windows'
cytoolz>=0.9.0,<1.0.0;implementation_name=='cpython'
docopt==0.6.2
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -21,7 +21,7 @@ ignore = E203,W503
force_grid_wrap = 0
include_trailing_comma = True
known_first_party = brownie
known_third_party = _pytest,abi2solc,docopt,ens,eth_abi,eth_event,eth_hash,eth_keys,eth_utils,ethpm,hexbytes,mythx_models,psutil,pytest,pythx,requests,semantic_version,setuptools,solcast,solcx,web3
known_third_party = _pytest,docopt,ens,eth_abi,eth_event,eth_hash,eth_keys,eth_utils,ethpm,hexbytes,mythx_models,psutil,pytest,pythx,requests,semantic_version,setuptools,solcast,solcx,web3
line_length = 100
multi_line_output = 3
use_parentheses = True
Expand Down

0 comments on commit 36f0ec5

Please sign in to comment.