From 0e81248bb06f94256e82252a2a6ee35c81f37cf2 Mon Sep 17 00:00:00 2001 From: Kieran Prasch Date: Tue, 19 Sep 2023 23:10:44 +0200 Subject: [PATCH] makes `ConstructorParameters` instances callable; assorted renaming and reorg of constructor parameter utilities --- scripts/deploy_lynx.py | 31 +++++++++------- scripts/deployment.py | 36 +++++++++++++------ .../lynx-alpha-13.json} | 0 3 files changed, 43 insertions(+), 24 deletions(-) rename scripts/{configs/lynx_config.json => params/lynx-alpha-13.json} (100%) diff --git a/scripts/deploy_lynx.py b/scripts/deploy_lynx.py index dcc92329..4e83bc06 100644 --- a/scripts/deploy_lynx.py +++ b/scripts/deploy_lynx.py @@ -2,13 +2,12 @@ from pathlib import Path from ape import project -from ape.cli import get_user_selected_account -from scripts.deployment import DeploymentConfig + +from scripts.deployment import prepare_deployment from scripts.registry import registry_from_ape_deployments -from scripts.utils import check_etherscan_plugin PUBLISH = False -DEPLOYMENT_CONFIG_FILEPATH = Path(__file__).parent / "configs" / "lynx_config.json" +DEPLOYMENT_CONFIG_FILEPATH = Path(__file__).parent / "params" / "lynx-alpha-13.json" DEPLOYMENT_REGISTRY_FILEPATH = ( Path(__file__).parent.parent / "artifacts" / "lynx_testnet_registry.json" ) # TODO: make unique @@ -32,17 +31,17 @@ def main(): 'Coordinator' deployed to: 0x4077ad1CFA834aEd68765dB0Cf3d14701a970a9a """ - check_etherscan_plugin() - deployer = get_user_selected_account() - config = DeploymentConfig.from_file(DEPLOYMENT_CONFIG_FILEPATH) + deployer, params = prepare_deployment( + params_filepath=DEPLOYMENT_CONFIG_FILEPATH + ) LynxRootApplication = deployer.deploy( - *config.get_deployment_params(project.LynxRootApplication, locals()), + *params(project.LynxRootApplication, locals()), publish=PUBLISH, ) LynxTACoChildApplication = deployer.deploy( - *config.get_deployment_params(project.LynxTACoChildApplication, locals()), + *params(project.LynxTACoChildApplication, locals()), publish=PUBLISH, ) @@ -53,20 +52,26 @@ def main(): ) LynxRitualToken = deployer.deploy( - *config.get_deployment_params(project.LynxRitualToken, locals()), + *params(project.LynxRitualToken, locals()), publish=PUBLISH, ) # Lynx Coordinator Coordinator = deployer.deploy( - *config.get_deployment_params(project.Coordinator, locals()), + *params(project.Coordinator, locals()), publish=PUBLISH, ) LynxTACoChildApplication.setCoordinator(Coordinator.address, sender=deployer) - deployments = [LynxRootApplication, LynxTACoChildApplication, LynxRitualToken, Coordinator] + deployments = [ + LynxRootApplication, + LynxTACoChildApplication, + LynxRitualToken, + Coordinator + ] registry_from_ape_deployments( - deployments=deployments, output_filepath=DEPLOYMENT_REGISTRY_FILEPATH + deployments=deployments, + output_filepath=DEPLOYMENT_REGISTRY_FILEPATH ) diff --git a/scripts/deployment.py b/scripts/deployment.py index f1c9e940..ddb6035b 100644 --- a/scripts/deployment.py +++ b/scripts/deployment.py @@ -4,21 +4,28 @@ from pathlib import Path from typing import Any, List +from ape.api import AccountAPI +from ape.cli import get_user_selected_account from ape.contracts.base import ContractContainer +from scripts.utils import check_etherscan_plugin + VARIABLE_PREFIX = "$" -class DeploymentConfigError(ValueError): - pass +def prepare_deployment(params_filepath: Path) -> typing.Tuple[AccountAPI, 'ConstructorParameters']: + check_etherscan_plugin() + deployer = get_user_selected_account() + params = ConstructorParameters.from_file(params_filepath) + return deployer, params -def is_variable(param: Any) -> bool: +def _is_variable(param: Any) -> bool: return isinstance(param, str) and param.startswith(VARIABLE_PREFIX) def _resolve_param(value: Any, context: typing.Dict[str, Any]) -> Any: - if not is_variable(value): + if not _is_variable(value): return value variable = value.strip(VARIABLE_PREFIX) contract_instance = context[variable] @@ -40,11 +47,11 @@ def _resolve_parameters(parameters: OrderedDict, context: typing.Dict[str, Any]) def _validate_param(param: Any, contracts: List[str]) -> None: - if not is_variable(param): + if not _is_variable(param): return variable = param.strip(VARIABLE_PREFIX) if variable not in contracts: - raise DeploymentConfigError(f"Variable {param} is not resolvable") + raise ConstructorParameters.Invalid(f"Variable {param} is not resolvable") def validate_deployment_config(config: typing.OrderedDict[str, Any]) -> None: @@ -64,27 +71,34 @@ def _confirm_resolution(resolved_params: OrderedDict, contract_name: str) -> Non print(f"(i) No constructor parameters for {contract_name}; proceeding") return - print(f"Resolved constructor parameters for {contract_name}") + print(f"Constructor parameters for {contract_name}") for name, resolved_value in resolved_params.items(): print(f"\t{name}={resolved_value}") - answer = input("Continue Y/N? ") + answer = input("Deploy Y/N? ") if answer.lower().strip() == "n": print("Aborting deployment!") exit(-1) -class DeploymentConfig: +class ConstructorParameters: + + class Invalid(Exception): + """raised when the constructor parameters are invalid""" + def __init__(self, contracts_configuration: OrderedDict): validate_deployment_config(contracts_configuration) self.contracts_configuration = contracts_configuration + def __call__(self, *args, **kwargs): + return self.__resolve_constructor_parameters(*args, **kwargs) + @classmethod - def from_file(cls, config_filepath: Path) -> "DeploymentConfig": + def from_file(cls, config_filepath: Path) -> "ConstructorParameters": with open(config_filepath, "r") as config_file: config = OrderedDict(json.load(config_file)) return cls(config) - def get_deployment_params( + def __resolve_constructor_parameters( self, container: ContractContainer, context: typing.Dict[str, Any], interactive: bool = True ) -> List[Any]: contract_name = container.contract_type.name diff --git a/scripts/configs/lynx_config.json b/scripts/params/lynx-alpha-13.json similarity index 100% rename from scripts/configs/lynx_config.json rename to scripts/params/lynx-alpha-13.json