Skip to content

Commit

Permalink
Merge pull request #47 from madlabman/main
Browse files Browse the repository at this point in the history
build: make installable via poetry / pipx
  • Loading branch information
TheDZhon authored Jun 5, 2024
2 parents c368bad + e89ce97 commit da194e4
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 73 deletions.
87 changes: 50 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,10 @@

Diff your Ethereum smart contracts code from GitHub against Blockchain explorer verified source code.

## Prerequisites
## Install

This project was developed using these dependencies with their exact versions listed below:

- Python 3.12
- Poetry 1.8

Other versions may work as well but were not tested at all.

## Setup

1. Install Poetry

Use the following command to install poetry:

```shell
pip install --user poetry~=1.8
```

alternatively, you could proceed with `pipx`:

```shell
pipx install poetry~=1.8
```

2. Activate poetry virtual environment,

```shell
poetry shell
```

3. Install Python dependencies

```shell
poetry install
```bash
pipx install git+https://github.com/lidofinance/diffyscan
```

## Usage
Expand All @@ -61,7 +30,7 @@ export GITHUB_API_TOKEN=<your-github-token>
Start script with one of the examples provided

```bash
python3 main.py config_samples/lido_dao_sepolia_config.json
diffyscan config_samples/lido_dao_sepolia_config.json
```

Alternatively, create a new config file named `config.json`,
Expand Down Expand Up @@ -92,15 +61,59 @@ Alternatively, create a new config file named `config.json`,
Start the script

```bash
python3 main.py
dyffyscan
```

> Note: Brownie verification tooling might rewrite the imports in the source submission. It transforms relative paths to imported contracts into flat paths ('./folder/contract.sol' -> 'contract.sol'), which makes Diffyscan unable to find a contract for verification.
For contracts whose sources were verified by brownie tooling:

```bash
python3 main.py --support-brownie
diffyscan --support-brownie
```

ℹ️ See more config examples inside the [config_samples](./config_samples/) dir.

## Development setup

### Prerequisites

This project was developed using these dependencies with their exact versions listed below:

- Python 3.12
- Poetry 1.8

Other versions may work as well but were not tested at all.

### Setup

1. Install Poetry

Use the following command to install poetry:

```bash
pip install --user poetry~=1.8
```

alternatively, you could proceed with `pipx`:

```bash
pipx install poetry~=1.8
```

2. Activate poetry virtual environment,

```bash
poetry shell
```

3. Install [poetry-dynamic-versioning](https://github.com/mtkennerly/poetry-dynamic-versioning?tab=readme-ov-file#installation)

- In most cases: `poetry self add "poetry-dynamic-versioning[plugin]"`
- If you installed Poetry with Pipx: `pipx inject poetry "poetry-dynamic-versioning[plugin]"`

4. Install Python dependencies

```bash
poetry install
```
26 changes: 17 additions & 9 deletions main.py → diffyscan/diffyscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import argparse
import os

from utils.common import load_config, load_env
from utils.constants import DIFFS_DIR, START_TIME, DEFAULT_CONFIG_PATH
from utils.explorer import get_contract_from_explorer
from utils.github import get_file_from_github, get_file_from_github_recursive, resolve_dep
from utils.helpers import create_dirs
from utils.logger import logger
from .utils.common import load_config, load_env
from .utils.constants import DIFFS_DIR, START_TIME, DEFAULT_CONFIG_PATH
from .utils.explorer import get_contract_from_explorer
from .utils.github import get_file_from_github, get_file_from_github_recursive, resolve_dep
from .utils.helpers import create_dirs
from .utils.logger import logger


GITHUB_API_TOKEN = load_env("GITHUB_API_TOKEN", masked=True)
__version__ = "0.0.0"


g_skip_user_input: bool = False

Expand Down Expand Up @@ -83,7 +84,7 @@ def run_diff(config, name, address, explorer_api_token, github_api_token, recurs
github_file = get_file_from_github_recursive(github_api_token, repo, path_to_file, dep_name)
else:
github_file = get_file_from_github(github_api_token, repo, path_to_file, dep_name)

if not github_file:
github_file = "<!-- No file content -->"
file_found = False
Expand Down Expand Up @@ -126,18 +127,21 @@ def run_diff(config, name, address, explorer_api_token, github_api_token, recurs
def process_config(path: str, recursive_parsing: bool):
logger.info(f"Loading config {path}...")
config = load_config(path)

github_api_token = load_env("GITHUB_API_TOKEN", masked=True)
explorer_token = None
if "explorer_token_env_var" in config:
explorer_token = load_env(config["explorer_token_env_var"], masked=True, required=False)

contracts = config["contracts"]
logger.info(f"Running diff for contracts from config {contracts}...")
for address, name in config["contracts"].items():
run_diff(config, name, address, explorer_token, GITHUB_API_TOKEN, recursive_parsing)
run_diff(config, name, address, explorer_token, github_api_token, recursive_parsing)


def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--version", "-V", action="store_true", help="Display version information")
parser.add_argument("path", nargs="?", default=None, help="Path to config or directory with configs")
parser.add_argument("--yes", "-y", help="If set don't ask for input before validating each contract", action="store_true")
parser.add_argument(
Expand All @@ -154,6 +158,10 @@ def main():
args = parse_arguments()
g_skip_user_input = args.yes

if args.version:
print(f"Diffyscan {__version__}")
return

logger.info("Welcome to Diffyscan!")
logger.divider()

Expand Down
6 changes: 3 additions & 3 deletions utils/common.py → diffyscan/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import requests

from utils.logger import logger
from utils.types import Config
from .logger import logger
from .types import Config


def load_env(variable_name, required=True, masked=False):
Expand All @@ -32,7 +32,7 @@ def load_config(path: str) -> Config:


def fetch(url, headers={}):
logger.log(f"fetch: {url}")
logger.log(f"Fetch: {url}")
response = requests.get(url, headers=headers)

if response.status_code == 404:
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions utils/explorer.py → diffyscan/utils/explorer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
import sys

from utils.common import fetch
from utils.logger import logger
from .common import fetch
from .logger import logger


def _errorNoSourceCodeAndExit(address):
Expand Down
5 changes: 3 additions & 2 deletions utils/github.py → diffyscan/utils/github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
from utils.common import fetch, parse_repo_link
from utils.logger import logger

from .common import fetch, parse_repo_link
from .logger import logger


def get_file_from_github(github_api_token, dependency_repo, path_to_file, dep_name):
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions utils/logger.py → diffyscan/utils/logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import termtables
from utils.constants import LOGS_PATH
from utils.helpers import create_dirs

from .constants import LOGS_PATH
from .helpers import create_dirs

CYAN = "\033[96m"
PURPLE = "\033[95m"
Expand Down Expand Up @@ -107,7 +108,7 @@ def color_row(self, row):
hlcolor = GREEN

file_found = row[2]
diffs_found = row[3] != None and row[3] > 0
diffs_found = row[3] is not None and row[3] > 0

if not file_found:
hlcolor = RED
Expand Down
8 changes: 8 additions & 0 deletions diffyscan/utils/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import TypedDict


class Config(TypedDict):
contracts: dict[str, str]
network: str
github_repo: str
dependencies: dict[str, str]
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
[tool.poetry]
name = "explorer-github-diff-checker"
version = "0.2.0"
name = "diffyscan"
version = "0.0.0"
description = "Diff your Ethereum smart contracts code from GitHub against Blockchain explorer verified source code."
authors = ["Azat Serikov <[email protected]>"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/lidofinance/diffyscan"

keywords = ["ethereum", "diff", "sources"]
package-mode = false

package-mode = true
packages = [{ include = "diffyscan"}]
exclude = ["config_samples"]

[tool.poetry.scripts]
diffyscan = "diffyscan.diffyscan:main"

[tool.poetry.dependencies]
python = ">=3.10,<4"
requests = "^2.32.2"
termtables = "^0.2.4"

[tool.poetry.dev-dependencies]

[tool.poetry.group.dev.dependencies]
black = "^24.4.2"

[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
style = "semver"
metadata = false

[tool.poetry-dynamic-versioning.substitution]
files = ["diffyscan/diffyscan.py"]

[build-system]
requires = ["poetry-core>=1.9.0"]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
build-backend = "poetry_dynamic_versioning.backend"
8 changes: 0 additions & 8 deletions utils/types.py

This file was deleted.

0 comments on commit da194e4

Please sign in to comment.