Skip to content

Commit

Permalink
makes ConstructorParameters instances callable; assorted renaming a…
Browse files Browse the repository at this point in the history
…nd reorg of constructor parameter utilities
  • Loading branch information
KPrasch committed Sep 19, 2023
1 parent 5ebb1ac commit 0e81248
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
31 changes: 18 additions & 13 deletions scripts/deploy_lynx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)

Expand All @@ -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
)
36 changes: 25 additions & 11 deletions scripts/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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:
Expand All @@ -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
Expand Down
File renamed without changes.

0 comments on commit 0e81248

Please sign in to comment.