Skip to content

Commit

Permalink
feat: upgrade to ape 0.7 (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 19, 2023
1 parent 4b3c397 commit 289aa7e
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 224 deletions.
1 change: 0 additions & 1 deletion .mdformat.toml

This file was deleted.

4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 23.11.0
rev: 23.12.0
hooks:
- id: black
name: black
Expand All @@ -21,7 +21,7 @@ repos:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.0
rev: v1.7.1
hooks:
- id: mypy
additional_dependencies: [types-requests, types-setuptools, pydantic, types-pkg-resources]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Use the `add_library()` method from the `ape-solidity` compiler class to add the
A typical flow is:

1. Deploy the library.
2. Call `add_library()` using the Solidity compiler plugin, which will also re-compile contracts that need the library.
3. Deploy and use contracts that require the library.
1. Call `add_library()` using the Solidity compiler plugin, which will also re-compile contracts that need the library.
1. Deploy and use contracts that require the library.

For example:

Expand All @@ -111,7 +111,7 @@ def contract(accounts, project, compilers):
# Deploy the library.
account = accounts[0]
library = project.Set.deploy(sender=account)
# Add the library to Solidity (re-compiles contracts that use the library).
compilers.solidity.add_library(library)
Expand Down
49 changes: 9 additions & 40 deletions ape_solidity/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from pathlib import Path
from typing import Dict, List, Optional, Sequence, Set, Union

from ape._pydantic_compat import BaseModel, validator
from ape.exceptions import CompilerError
from ape.logging import logger
from ape.utils import pragma_str_to_specifier_set
from packaging.specifiers import SpecifierSet
from packaging.version import InvalidVersion
from packaging.version import Version
from packaging.version import Version as _Version
from pydantic import BaseModel, field_validator
from solcx.install import get_executable
from solcx.wrapper import get_solc_version as get_solc_version_from_binary

Expand All @@ -36,7 +36,8 @@ class ImportRemapping(BaseModel):
entry: str
packages_cache: Path

@validator("entry")
@field_validator("entry", mode="before")
@classmethod
def validate_entry(cls, value):
if len((value or "").split("=")) != 2:
raise IncorrectMappingFormatError()
Expand Down Expand Up @@ -93,6 +94,8 @@ def package_id(self) -> Path:

class ImportRemappingBuilder:
def __init__(self, contracts_cache: Path):
# import_map maps import keys like `@openzeppelin/contracts`
# to str paths in the contracts' .cache folder.
self.import_map: Dict[str, str] = {}
self.dependencies_added: Set[Path] = set()
self.contracts_cache = contracts_cache
Expand Down Expand Up @@ -145,7 +148,7 @@ def get_pragma_spec_from_path(source_file_path: Union[Path, str]) -> Optional[Sp
source_file_path (Union[Path, str]): Solidity source file path.
Returns:
``packaging.specifiers.SpecifierSet``
``Optional[packaging.specifiers.SpecifierSet]``
"""
path = Path(source_file_path)
if not path.is_file():
Expand All @@ -163,41 +166,7 @@ def get_pragma_spec_from_str(source_str: str) -> Optional[SpecifierSet]:
):
return None # Try compiling with latest

# The following logic handles the case where the user puts a space
# between the operator and the version number in the pragma string,
# such as `solidity >= 0.4.19 < 0.7.0`.
pragma_parts = pragma_match.groups()[0].split()

def _to_spec(item: str) -> str:
item = item.replace("^", "~=")
if item and item[0].isnumeric():
return f"=={item}"
elif item and len(item) >= 2 and item[0] == "=" and item[1] != "=":
return f"={item}"

return item

pragma_parts_fixed = []
builder = ""
for sub_part in pragma_parts:
if not any(c.isnumeric() for c in sub_part):
# Handle pragma with spaces between constraint and values
# like `>= 0.6.0`.
builder += sub_part
continue
elif builder:
spec = _to_spec(f"{builder}{sub_part}")
builder = ""
else:
spec = _to_spec(sub_part)

pragma_parts_fixed.append(spec)

try:
return SpecifierSet(",".join(pragma_parts_fixed))
except ValueError as err:
logger.error(str(err))
return None
return pragma_str_to_specifier_set(pragma_match.groups()[0])


def load_dict(data: Union[str, dict]) -> Dict:
Expand All @@ -215,7 +184,7 @@ def add_commit_hash(version: Union[str, Version]) -> Version:
return get_solc_version_from_binary(solc, with_commit_hash=True)


def verify_contract_filepaths(contract_filepaths: List[Path]) -> Set[Path]:
def verify_contract_filepaths(contract_filepaths: Sequence[Path]) -> Set[Path]:
invalid_files = [p.name for p in contract_filepaths if p.suffix != Extension.SOL.value]
if not invalid_files:
return set(contract_filepaths)
Expand Down
Loading

0 comments on commit 289aa7e

Please sign in to comment.