Skip to content

Commit

Permalink
test: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Jul 12, 2024
1 parent 6c5cfc6 commit f9cd454
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 35 deletions.
21 changes: 0 additions & 21 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,3 @@ jobs:
- name: Run Tests
run: pytest -m "not fuzzing" -n 0 -s --cov

# NOTE: uncomment this block after you've marked tests with @pytest.mark.fuzzing
# fuzzing:
# runs-on: ubuntu-latest
#
# strategy:
# fail-fast: true
#
# steps:
# - uses: actions/checkout@v4
#
# - name: Setup Python
# uses: actions/setup-python@v5
# with:
# python-version: "3.10"
#
# - name: Install Dependencies
# run: pip install .[test]
#
# - name: Run Tests
# run: pytest -m "fuzzing" --no-cov -s
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

Ape Utils is a CLI tool designed to interact with Ethereum smart contracts, specifically focusing on calling view functions. The tool allows you to call a view function from a given function signature and address directly from the command line.

## Demo

![demo](media/demo.gif)

## Features

- **Call View Functions:** Invoke view functions on Ethereum smart contracts using their function signature and address.
Expand Down Expand Up @@ -79,10 +83,6 @@ ape_utils decode --signature "call_this_view_function(uint256 arg1)" "0x1e4f420d
![working](media/working.png)
## Demo
![demo](media/demo.gif)
## Development
Please see the [contributing guide](CONTRIBUTING.md) to learn more how to contribute to this project.
Expand Down
2 changes: 1 addition & 1 deletion demo.tape
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Sleep 2s
Type "ape_utils call --function-sig 'call_this_view_function(uint256)(string)' --address '0x80E097a70cacA11EB71B6401FB12D48A1A61Ef54' --args 6147190 --network :sepolia:infura"
Enter 1
Sleep 4s
Type "ape_utils abi_encode --signature 'call_this_view_function(uint256 arg1, string addr)' 1234 '0xdeadbeef'"
Type "ape_utils abi_encode --signature 'call_this_view_function(uint256 arg1, string addr)' 1234 '0x00000000000000000000000000000000000000000000000000000000deadbeef'"
Enter 1
Sleep 2s
Type "ape_utils abi_decode --signature 'call_this_view_function(uint256 arg1, string addr)' '0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000a3078646561646265656600000000000000000000000000000000000000000000'"
Expand Down
18 changes: 11 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,13 @@ exclude_lines = [
[tool.hatch.envs.default]
installer = "uv"
dependencies = [
"eth-ape",
"eth-ape", # will use ape 0.8.5 for now because of py-geth version issue
"ethpm_types",
"multicall",
"rich-click", # https://github.com/ewels/rich-click
# "click",
# "rich",
]
post-install-commands = ["pre-commit install"]

[tool.hatch.envs.test]
dependencies = ["pytest", "pytest-xdist", "pytest-cov", "hypothesis"]

[tool.hatch.envs.lint]
# If you define environments with dependencies that only slightly differ from their
# inherited environments, you can use the extra-dependencies option to avoid redeclaring the
Expand All @@ -191,10 +186,12 @@ typing = [
"mypy --install-types --non-interactive src/{args}"
]
lint = [
"echo \"VERSION: `ruff --version`\"",
"echo \"ruff VERSION: `ruff --version`\"",
"ruff format .",
"ruff check . --fix",
"mypy src/ape_utils/",
"echo \"mdformat VERSION: `mdformat --version`\"",
"mdformat ."
]
lint-check = [
"echo \"VERSION: `ruff --version`\"",
Expand All @@ -203,10 +200,17 @@ lint-check = [
"mypy src/ape_utils/"
]

[tool.hatch.envs.test]
extra-dependencies = ["pytest", "pytest-xdist", "pytest-cov", "hypothesis"]

[tool.hatch.envs.test.scripts]
test = "pytest"
test-cov-xml = "pytest --cov-report=xml"

# Replace tox & nox
[[tool.hatch.envs.test.matrix]]
python = ["3.9", "3.10", "3.11", "3.12"]
# python = ["3.9"]

[build-system]
requires = ["hatchling", "hatch-regex-commit"]
Expand Down
5 changes: 3 additions & 2 deletions src/ape_utils/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from ape.cli import ConnectedProviderCommand, network_option
from ape_node.provider import Node
from rich.console import Console
from rich.logging import RichHandler

# from rich.logging import RichHandler
from rich.pretty import pprint
from rich.traceback import install

Expand All @@ -22,7 +23,7 @@
install()
console = Console()
FORMAT = "%(message)s"
logging.basicConfig(level="INFO", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()])
# logging.basicConfig(level="WARN", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()])

log = logging.getLogger("rich")

Expand Down
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from click.testing import CliRunner
from ape_utils._cli import cli as _cli
import pytest
import ape


@pytest.fixture(scope="session")
def networks():
return ape.networks

@pytest.fixture(scope="session", autouse=True)
def provider(networks):
with networks.ethereum.local.use_provider("test") as provider:
yield provider

@pytest.fixture(scope="session")
def runner():
return CliRunner()


@pytest.fixture(scope="session")
def cli():
return _cli
9 changes: 9 additions & 0 deletions tests/test_call.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# import pytest
from click.testing import CliRunner
import rich_click as rclick


def test_call_a_view_contract(runner: CliRunner, cli: rclick.RichGroup) -> None:
result = runner.invoke(cli, ["call", "--function-sig", "call_this_view_function(uint256 arg1)", "--address","0x80E097a70CACA11EB71B6401FB12D48A1A61Ef54", "--args", "6147190", "--network", ":sepolia"])
# print(result)
assert result.exit_code == 0
59 changes: 59 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from click.testing import CliRunner
import rich_click as rclick
import re


regex_pat = re.compile("Encoded Calldata:\s+.*3030303030303030303064656164626565662700000000000000000000000000000000000000000000000000000000")

def test_abi_encode_function_signature(runner: CliRunner, cli: rclick.RichGroup) -> None:
result = runner.invoke(cli,
['abi_encode',
'--signature',
"'call_this_view_function(uint256 arg1, string addr)'",
'1234',
"'0x00000000000000000000000000000000000000000000000000000000deadbeef'"]
)

assert result.exit_code == 0
#* These gibberish bcz of fancy coloured outputs
assert regex_pat.findall(result.output) is not None
# assert "32m3030303030303030303064656164626565662700000000000000000000000000000000000000000000000000000000" in result.output


def test_abi_decode_function_signature(runner: CliRunner, cli: rclick.RichGroup) -> None:
result = runner.invoke(cli,
['abi_decode',
'--signature',
"'call_this_view_function(uint256 arg1, string addr)'",
'0x00000000000000000000000000000000000000000000000000000000000004d20000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000a3078646561646265656600000000000000000000000000000000000000000000']
)

assert result.exit_code == 0
#* Bcz of fancy coloured outputs it's very hard to match the whole string
assert "0xdeadbeef" in result.output




def test_encode_function_signature(runner: CliRunner, cli: rclick.RichGroup) -> None:
result = runner.invoke(cli,
['encode',
'--signature',
"'call_this_view_function(uint256 arg1)'",
'1234']
)

assert result.exit_code == 0
assert "0xb732f4ca00000000000000000000000000000000000000000000000000000000000004d2" in result.output


def test_decode_function_signature(runner: CliRunner, cli: rclick.RichGroup) -> None:
result = runner.invoke(cli,
['decode',
'--signature',
"'call_this_view_function(uint256 arg1)'",
'0x1e4f420d00000000000000000000000000000000000000000000000000000000000004d2']
)

assert result.exit_code == 0
assert "1234" in result.output

0 comments on commit f9cd454

Please sign in to comment.