-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto-fixtures, and new deploy command (#46)
* feat: auto-fixtures, and new deploy command * fix: typecheck
- Loading branch information
1 parent
dc39b8b
commit 941e454
Showing
10 changed files
with
209 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from moccasin.logging import logger | ||
from moccasin.config import get_config, initialize_global_config | ||
from moccasin._sys_path_and_config_setup import ( | ||
_patch_sys_path, | ||
_setup_network_and_account_from_args, | ||
) | ||
from argparse import Namespace | ||
|
||
|
||
def main(args: Namespace) -> int: | ||
initialize_global_config() | ||
config_contracts = get_config().contracts_folder | ||
config_root = get_config().get_root() | ||
|
||
# Set up the environment (add necessary paths to sys.path, etc.) | ||
with _patch_sys_path([config_root, config_root / config_contracts]): | ||
_setup_network_and_account_from_args( | ||
network=args.network, | ||
url=args.url, | ||
fork=args.fork, | ||
account=args.account, | ||
private_key=args.private_key, | ||
password=args.password, | ||
password_file_path=args.password_file_path, | ||
) | ||
config = get_config() | ||
active_network = config.get_active_network() | ||
deployed_contract = active_network.get_or_deploy_contract( | ||
args.contract_name, force_deploy=True | ||
) | ||
logger.info( | ||
f"Deployed contract {args.contract_name} on {active_network.name} to {deployed_contract.address}" | ||
) | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from types import ModuleType | ||
from typing import Callable, Literal, cast | ||
from pytest import fixture | ||
from moccasin.config import get_config | ||
from boa.contracts.vyper.vyper_contract import VyperContract | ||
from boa.contracts.abi.abi_contract import ABIContract | ||
import inspect | ||
|
||
|
||
ScopeType = Literal["function", "class", "module", "package", "session"] | ||
|
||
|
||
def request_fixtures( | ||
fixture_requests: list[str | tuple[str, str]], scope: str = "module" | ||
): | ||
# Dear Charles, don't kill me. Idk how this works. | ||
current_frame = inspect.currentframe() | ||
if current_frame is None or current_frame.f_back is None: | ||
raise RuntimeError("Cannot determine caller module") | ||
caller_frame = current_frame.f_back | ||
|
||
caller_module = inspect.getmodule(caller_frame) | ||
if caller_module is None: | ||
raise RuntimeError("Cannot determine caller module") | ||
module: ModuleType = caller_module | ||
for fixture_request in fixture_requests: | ||
if isinstance(fixture_request, tuple): | ||
named_contract_name, fixture_name = fixture_request | ||
else: | ||
named_contract_name = fixture_request | ||
fixture_name = named_contract_name | ||
request_fixture(module, named_contract_name, fixture_name, scope) | ||
|
||
|
||
def request_fixture( | ||
module: ModuleType, | ||
named_contract_name: str, | ||
fixture_name: str, | ||
scope: str = "module", | ||
): | ||
active_network = get_config().get_active_network() | ||
named_contract = active_network.get_named_contract(named_contract_name) | ||
if named_contract is None: | ||
raise ValueError( | ||
f"No contract found for contract '{named_contract_name}' on network {active_network.name}" | ||
) | ||
if named_contract.deployer_script is None: | ||
raise ValueError( | ||
f"No deploy function found for '{named_contract_name}' on network {active_network.name}" | ||
) | ||
|
||
def deploy_func() -> VyperContract | ABIContract: | ||
return active_network.get_or_deploy_contract(named_contract_name) | ||
|
||
# Create the fixture function | ||
fixture_function = make_fixture(deploy_func, fixture_name, cast(ScopeType, scope)) | ||
|
||
# Add the fixture to the module's namespace | ||
setattr(module, fixture_name, fixture_function) | ||
|
||
|
||
def make_fixture( | ||
deploy_func: Callable[[], VyperContract | ABIContract], | ||
fixture_name: str, | ||
scope: Literal["function", "class", "module", "package", "session"], | ||
) -> Callable[[], VyperContract | ABIContract]: | ||
@fixture(scope=scope, name=fixture_name) | ||
def fixture_func(deploy_func=deploy_func): | ||
return deploy_func() | ||
|
||
return cast(Callable[[], VyperContract | ABIContract], fixture_func) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from pathlib import Path | ||
import subprocess | ||
import os | ||
from tests.conftest import COMPLEX_PROJECT_PATH | ||
|
||
|
||
# -------------------------------------------------------------- | ||
# WITHOUT ANVIL | ||
# -------------------------------------------------------------- | ||
def test_deploy_price_feed_pyevm(mox_path): | ||
current_dir = Path.cwd() | ||
try: | ||
os.chdir(COMPLEX_PROJECT_PATH) | ||
result = subprocess.run( | ||
[mox_path, "deploy", "price_feed"], | ||
check=True, | ||
capture_output=True, | ||
text=True, | ||
) | ||
finally: | ||
os.chdir(current_dir) | ||
assert "Deployed contract price_feed on pyevm to" in result.stderr | ||
|
||
|
||
# -------------------------------------------------------------- | ||
# WITH ANVIL | ||
# -------------------------------------------------------------- | ||
def test_deploy_price_feed_anvil(mox_path, anvil_process): | ||
current_dir = Path.cwd() | ||
try: | ||
os.chdir(COMPLEX_PROJECT_PATH) | ||
result = subprocess.run( | ||
[mox_path, "deploy", "price_feed", "--network", "anvil"], | ||
check=True, | ||
capture_output=True, | ||
text=True, | ||
) | ||
finally: | ||
os.chdir(current_dir) | ||
assert "Deployed contract price_feed on anvil to" in result.stderr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def test_using_fixture_one(price_feed): | ||
assert price_feed.address is not None | ||
|
||
|
||
def test_using_fixture_two(price_feed, eth_usd): | ||
assert price_feed.address is not None | ||
assert eth_usd.address is not None |