diff --git a/.github/ISSUE_TEMPLATE/bug.md b/.github/ISSUE_TEMPLATE/bug.md index a60c9b7..91fc385 100644 --- a/.github/ISSUE_TEMPLATE/bug.md +++ b/.github/ISSUE_TEMPLATE/bug.md @@ -1,11 +1,12 @@ --- name: Bug report about: Report an error that you've encountered. -labels: 'bug' +labels: bug --- + ### Environment information -* `ape` and plugin versions: +- `ape` and plugin versions: ``` $ ape --version @@ -15,16 +16,16 @@ $ ape plugins list # ...copy and paste result of above command here... ``` -* Python Version: x.x.x -* OS: osx/linux/win +- Python Version: x.x.x +- OS: osx/linux/win ### What went wrong? Please include information like: -* what command you ran -* the code that caused the failure (see [this link](https://help.github.com/articles/basic-writing-and-formatting-syntax/) for help with formatting code) -* full output of the error you received +- what command you ran +- the code that caused the failure (see [this link](https://help.github.com/articles/basic-writing-and-formatting-syntax/) for help with formatting code) +- full output of the error you received ### How can it be fixed? diff --git a/.github/ISSUE_TEMPLATE/feature.md b/.github/ISSUE_TEMPLATE/feature.md index 742ce66..1b56056 100644 --- a/.github/ISSUE_TEMPLATE/feature.md +++ b/.github/ISSUE_TEMPLATE/feature.md @@ -1,15 +1,15 @@ --- name: Feature request about: Request a new feature, or an improvement to existing functionality. -labels: 'enhancement' +labels: enhancement --- ### Overview Provide a simple overview of what you wish to see added. Please include: -* What you are trying to do -* Why Ape's current functionality is inadequate to address your goal +- What you are trying to do +- Why Ape's current functionality is inadequate to address your goal ### Specification diff --git a/.github/ISSUE_TEMPLATE/work-item.md b/.github/ISSUE_TEMPLATE/work-item.md index 3aa53f5..1932055 100644 --- a/.github/ISSUE_TEMPLATE/work-item.md +++ b/.github/ISSUE_TEMPLATE/work-item.md @@ -1,14 +1,15 @@ --- name: Work item about: New work item for Ape team -labels: 'backlog' - +labels: backlog --- ### Elevator pitch: + ### Value: + ### Dependencies: + ### Design approach: + ### Task list: + -* [ ] Tasks go here -### Estimated completion date: +- [ ] Tasks go here +### Estimated completion date: ### Design review: + + Do not signoff unless: -- 1) agreed the tasks and design approach will achieve acceptance, and -- 2) the work can be completed by one person within the SLA. -Design reviewers should consider simpler approaches to achieve goals. + +- 1. agreed the tasks and design approach will achieve acceptance, and +- 2. the work can be completed by one person within the SLA. + Design reviewers should consider simpler approaches to achieve goals. (Please leave a comment to sign off) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 1d93a39..544cbc6 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,6 +1,7 @@ ### What I did + fixes: # ### How I did it diff --git a/.github/workflows/commitlint.yaml b/.github/workflows/commitlint.yaml index c376171..1d19118 100644 --- a/.github/workflows/commitlint.yaml +++ b/.github/workflows/commitlint.yaml @@ -9,12 +9,12 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: fetch-depth: 0 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: 3.8 diff --git a/.github/workflows/title.yaml b/.github/workflows/prtitle.yaml similarity index 86% rename from .github/workflows/title.yaml rename to .github/workflows/prtitle.yaml index d283678..e52d792 100644 --- a/.github/workflows/title.yaml +++ b/.github/workflows/prtitle.yaml @@ -12,10 +12,10 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: 3.8 diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index a1b9019..736a2f3 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -10,10 +10,10 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: 3.8 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6fa5461..7f5c5de 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -2,15 +2,22 @@ on: ["push", "pull_request"] name: Test +concurrency: + # Cancel older, in-progress jobs from the same PR, same workflow. + # use run_id if the job is triggered by a push to ensure + # push-triggered jobs to not get canceled. + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + jobs: linting: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: 3.8 @@ -22,20 +29,23 @@ jobs: - name: Run Black run: black --check . + - name: Run isort + run: isort --check-only . + - name: Run flake8 run: flake8 . - - name: Run isort - run: isort --check-only . + - name: Run mdformat + run: mdformat . --check type-check: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: 3.8 @@ -56,10 +66,10 @@ jobs: python-version: [3.8, 3.9, "3.10"] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} @@ -81,10 +91,10 @@ jobs: # fail-fast: true # # steps: -# - uses: actions/checkout@v2 +# - uses: actions/checkout@v3 # # - name: Setup Python -# uses: actions/setup-python@v2 +# uses: actions/setup-python@v4 # with: # python-version: 3.8 # diff --git a/.gitignore b/.gitignore index 44faecd..7881af6 100644 --- a/.gitignore +++ b/.gitignore @@ -119,3 +119,7 @@ version.py # Ape stuff .build/ + +**/.DS_Store +*.swp +*.swo diff --git a/.mdformat.toml b/.mdformat.toml new file mode 100644 index 0000000..01b2fb0 --- /dev/null +++ b/.mdformat.toml @@ -0,0 +1 @@ +number = true diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b40096a..8e37c4b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,22 +10,21 @@ repos: - id: isort - repo: https://github.com/psf/black - rev: 22.6.0 + rev: 22.12.0 hooks: - id: black name: black -- repo: https://gitlab.com/pycqa/flake8 - rev: 4.0.1 +- repo: https://github.com/pycqa/flake8 + rev: 5.0.4 hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.971 + rev: v0.991 hooks: - id: mypy - additional_dependencies: [types-PyYAML, types-requests] - + additional_dependencies: [types-requests, types-setuptools, pydantic] default_language_version: - python: python3.9 + python: python3 diff --git a/README.md b/README.md index f9d53b8..36d2959 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # Quick Start -Ecosystem Plugin for Tenderly support in Ape +Ecosystem plugin for Tenderly support in Ape. ## Dependencies -* [python3](https://www.python.org/downloads) version 3.8 or greater, python3-dev +- [python3](https://www.python.org/downloads) version 3.8 or greater, python3-dev ## Installation @@ -28,14 +28,16 @@ python3 setup.py install ## Quick Usage -This plugin works as a normal provider for forked mainnet networks only (e.g. Ethereum, Fantom, etc.) +This plugin works as a normal provider for forked mainnet networks only (e.g. Ethereum, Fantom, etc.). If you know your fork ID (from the Tenderly console) you can use that like this + ```sh export TENDERLY_FORK_ID=... ``` If you have an API service that automatically provisions tenderly forks, you can use it like this + ```sh export TENDERLY_FORK_SERVICE_URI=... ``` diff --git a/ape_tenderly/__init__.py b/ape_tenderly/__init__.py index 53fba40..d256273 100644 --- a/ape_tenderly/__init__.py +++ b/ape_tenderly/__init__.py @@ -1,6 +1,6 @@ from ape import plugins -from .providers import TenderlyProvider +from .provider import TenderlyProvider @plugins.register(plugins.ProviderPlugin) diff --git a/ape_tenderly/providers.py b/ape_tenderly/provider.py similarity index 69% rename from ape_tenderly/providers.py rename to ape_tenderly/provider.py index e8d7815..9f3577a 100644 --- a/ape_tenderly/providers.py +++ b/ape_tenderly/provider.py @@ -1,29 +1,32 @@ import os -import requests # type: ignore +import requests from ape.api import ProviderAPI, Web3Provider from ape.exceptions import ConfigError from ape.utils import cached_property from web3 import HTTPProvider, Web3 +TENDERLY_FORK_ID = "TENDERLY_FORK_ID" +TENDERLY_FORK_SERVICE_URI = "TENDERLY_FORK_SERVICE_URI" + class TenderlyProvider(Web3Provider, ProviderAPI): @cached_property def fork_id(self) -> str: - if "TENDERLY_FORK_ID" in os.environ: - return os.environ["TENDERLY_FORK_ID"] + if TENDERLY_FORK_ID in os.environ: + return os.environ[TENDERLY_FORK_ID] - elif "TENDERLY_FORK_SERVICE_URI" in os.environ: + elif TENDERLY_FORK_SERVICE_URI in os.environ: fork_network_name = self.network.name.replace("-fork", "") chain_id = self.network.ecosystem.get_network(fork_network_name).chain_id response = requests.post( - os.environ["TENDERLY_FORK_SERVICE_URI"], + os.environ[TENDERLY_FORK_SERVICE_URI], json={"network_id": str(chain_id)}, ) return response.json()["simulation_fork"]["id"] else: - raise ConfigError("No valid tenderly fork id found.") + raise ConfigError("No valid tenderly fork ID found.") @property def uri(self) -> str: diff --git a/pyproject.toml b/pyproject.toml index d5b470b..51dd9ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,6 +3,7 @@ requires = ["setuptools>=51.1.1", "wheel", "setuptools_scm[toml]>=5.0"] [tool.mypy] exclude = "build/" +plugins = ["pydantic.mypy"] [tool.setuptools_scm] write_to = "ape_tenderly/version.py" @@ -19,7 +20,6 @@ include = '\.pyi?$' [tool.pytest.ini_options] addopts = """ - -n auto -p no:ape_test --cov-branch --cov-report term diff --git a/setup.py b/setup.py index de7d775..ea9d593 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from setuptools import find_packages, setup # type: ignore +from setuptools import find_packages, setup extras_require = { "test": [ # `test` GitHub Action jobs uses this @@ -10,10 +10,15 @@ "hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer ], "lint": [ - "black>=22.6.0", # auto-formatter and linter - "mypy>=0.971", # Static type analyzer - "flake8>=4.0.1", # Style linter + "black>=22.12.0", # auto-formatter and linter + "mypy>=0.991", # Static type analyzer + "types-setuptools", # Needed due to mypy typeshed + "types-requests", # Needed due to mypy typeshed + "flake8>=5.0.4", # Style linter "isort>=5.10.1", # Import sorting linter + "mdformat>=0.7.16", # Auto-formatter for markdown + "mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown + "mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style headers in issue templates ], "release": [ # `release` GitHub Action job uses this "setuptools", # Installation tool @@ -22,7 +27,7 @@ ], "dev": [ "commitizen", # Manage commits and publishing releases - "pre-commit", # Ensure that linters are run prior to commiting + "pre-commit", # Ensure that linters are run prior to committing "pytest-watch", # `ptw` test watcher/runner "IPython", # Console for interacting "ipdb", # Debugger (Must use `export PYTHONBREAKPOINT=ipdb.set_trace`) @@ -53,7 +58,7 @@ url="https://github.com/ApeWorX/ape-tenderly", include_package_data=True, install_requires=[ - "eth-ape>=0.5.0,<0.6", + "eth-ape>=0.6.0,<0.7", "requests>=2.28.1,<3", ], python_requires=">=3.8,<4", diff --git a/tests/conftest.py b/tests/conftest.py index 84da31d..26ae556 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,10 +1,6 @@ -from pathlib import Path - import ape import pytest -from ape_tenderly.providers import TenderlyProvider - @pytest.fixture def networks(): @@ -22,15 +18,11 @@ def Contract(): @pytest.fixture -def mainnet_fork_provider(networks): - network_api = networks.ecosystems["ethereum"]["mainnet-fork"] - provider = TenderlyProvider( - name="tenderly", - network=network_api, - request_header={}, - data_folder=Path("."), - provider_settings={}, - ) - provider.connect() - yield provider - provider.disconnect() +def mainnet_fork(networks): + return networks.ethereum.mainnet_fork + + +@pytest.fixture +def mainnet_fork_provider(mainnet_fork): + with mainnet_fork.use_provider("tenderly") as provider: + yield provider diff --git a/tests/test_provider.py b/tests/test_provider.py index 325c745..a3136f4 100644 --- a/tests/test_provider.py +++ b/tests/test_provider.py @@ -1,3 +1,43 @@ -def test_reset_fork(mainnet_fork_provider): - assert mainnet_fork_provider.fork_id - assert mainnet_fork_provider.uri +import os + +import pytest +from ape.exceptions import ConfigError + +from ape_tenderly.provider import TENDERLY_FORK_ID + + +@pytest.fixture(autouse=True) +def connected_eth_mainnet_provider(mainnet_fork_provider): + """ + See README for info on how to set the environment variable. + Else, tests will not run. + """ + if TENDERLY_FORK_ID not in os.environ: + pytest.fail(f"{TENDERLY_FORK_ID} environment variable is required to run tests.") + + return mainnet_fork_provider + + +@pytest.fixture +def unset_fork_id(mainnet_fork_provider): + fork_id = os.environ.pop(TENDERLY_FORK_ID) + was_cached = "fork_id" in mainnet_fork_provider.__dict__ + if was_cached: + del mainnet_fork_provider.__dict__["fork_id"] + + yield + + os.environ[TENDERLY_FORK_ID] = fork_id + if was_cached: + mainnet_fork_provider.__dict__["fork_id"] = fork_id + + +def test_fork_id_and_uri(connected_eth_mainnet_provider): + provider = connected_eth_mainnet_provider + assert provider.fork_id + assert provider.uri + + +def test_fork_id_missing(mainnet_fork_provider, unset_fork_id): + with pytest.raises(ConfigError, match="No valid tenderly fork ID found."): + _ = mainnet_fork_provider.fork_id