Skip to content

Commit

Permalink
Removes the need for locals() context by accessing deployments via ap…
Browse files Browse the repository at this point in the history
…e contract containers
  • Loading branch information
KPrasch committed Sep 22, 2023
1 parent ee531ec commit 3d75c81
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
42 changes: 26 additions & 16 deletions scripts/deploy_lynx.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,47 @@ def main():
publish=PUBLISH
)

LynxRootApplication = deployer.deploy(
*params.get(project.LynxRootApplication, locals()), **params.get_kwargs()
root_application = deployer.deploy(
*params.get(project.LynxRootApplication), **params.get_kwargs()
)

LynxTACoChildApplication = deployer.deploy(
*params.get(project.LynxTACoChildApplication, locals()), **params.get_kwargs()
child_application = deployer.deploy(
*params.get(project.LynxTACoChildApplication), **params.get_kwargs()
)

LynxRootApplication.setChildApplication(
LynxTACoChildApplication.address,
root_application.setChildApplication(
child_application.address,
sender=deployer,
)

LynxRitualToken = deployer.deploy(
*params.get(project.LynxRitualToken, locals()), **params.get_kwargs()
ritual_token = deployer.deploy(
*params.get(project.LynxRitualToken), **params.get_kwargs()
)

# Lynx Coordinator
Coordinator = deployer.deploy(*params.get(project.Coordinator, locals()), **params.get_kwargs())
coordinator = deployer.deploy(
*params.get(project.Coordinator), **params.get_kwargs()
)

LynxTACoChildApplication.setCoordinator(Coordinator.address, sender=deployer)
child_application.setCoordinator(
coordinator.address,
sender=deployer
)

GlobalAllowList = deployer.deploy(
*params.get(project.GlobalAllowList, locals()), **params.get_kwargs()
global_allow_list = deployer.deploy(
*params.get(project.GlobalAllowList), **params.get_kwargs()
)

deployments = [LynxRootApplication, LynxTACoChildApplication, LynxRitualToken, Coordinator, GlobalAllowList]
deployments = [
root_application,
child_application,
ritual_token,
coordinator,
global_allow_list
]

registry_names = {
LynxRootApplication.contract_type.name: "TACoApplication",
LynxTACoChildApplication.contract_type.name: "TACoChildApplication",
root_application.contract_type.name: "TACoApplication",
child_application.contract_type.name: "TACoChildApplication",
}

output_filepath = registry_from_ape_deployments(
Expand Down
47 changes: 25 additions & 22 deletions scripts/deployment.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import json
import typing
from collections import OrderedDict, defaultdict
from collections import OrderedDict
from pathlib import Path
from typing import Any, List

from ape import project
from ape.api import AccountAPI
from ape.cli import get_user_selected_account
from ape.contracts.base import ContractContainer
from web3.auto.gethdev import w3

from scripts.constants import NULL_ADDRESS
from scripts.utils import check_etherscan_plugin, check_infura_plugin, check_registry_filepath
from web3.auto.gethdev import w3

VARIABLE_PREFIX = "$"

Expand Down Expand Up @@ -44,29 +45,38 @@ def _is_variable(param: Any) -> bool:
return result


def _resolve_param(value: Any, context: typing.Dict[str, Any]) -> Any:
def _resolve_param(value: Any) -> Any:
"""Resolves a single parameter value."""
if not _is_variable(value):
return value # literally a value
variable = value.strip(VARIABLE_PREFIX)
contract_instance = context[variable]
contract_container = getattr(project, variable)
contract_instances = contract_container.deployments
if not contract_instances:
return NULL_ADDRESS
if len(contract_instances) != 1:
raise ConstructorParameters.Invalid(
f"Variable {value} is ambiguous - "
f"expected exactly one contract instance, got {len(contract_instances)}"
)
contract_instance = contract_instances[0]
return contract_instance.address


def _resolve_list(value: List[Any], context: typing.Dict[str, Any]) -> List[Any]:
def _resolve_list(value: List[Any]) -> List[Any]:
"""Resolves a list of parameter values."""
params = [_resolve_param(v, context) for v in value]
params = [_resolve_param(v) for v in value]
return params


def _resolve_parameters(parameters: OrderedDict, context: typing.Dict[str, Any]) -> OrderedDict:
def _resolve_parameters(parameters: OrderedDict) -> OrderedDict:
"""Resolves a dictionary of parameter values for a single contract"""
resolved_params = OrderedDict()
for name, value in parameters.items():
if isinstance(value, list):
resolved_params[name] = _resolve_list(value, context)
resolved_params[name] = _resolve_list(value)
else:
resolved_params[name] = _resolve_param(value, context)
resolved_params[name] = _resolve_param(value)
return resolved_params


Expand Down Expand Up @@ -110,13 +120,10 @@ def _validate_constructor_abi_inputs(
# validate value type
value_to_validate = value
if _is_variable(value):
# at the moment only contract addresses are variables
# won't know address until deployment; use a placeholder
context = defaultdict(PlacehodlerContractInstance)
if isinstance(value, list):
value_to_validate = _resolve_list(value, context)
value_to_validate = _resolve_list(value)
else:
value_to_validate = _resolve_param(value, context)
value_to_validate = _resolve_param(value)
if not w3.is_encodable(abi_input.type, value_to_validate):
raise ConstructorParameters.Invalid(
f"Constructor param name '{name}' at position {position} has a value '{value}' "
Expand Down Expand Up @@ -162,10 +169,6 @@ def _confirm_resolution(resolved_params: OrderedDict, contract_name: str) -> Non
_confirm_deployment(contract_name)


class PlacehodlerContractInstance(typing.NamedTuple):
address: str = NULL_ADDRESS


class ConstructorParameters:
"""Represents the constructor parameters for a set of contracts."""

Expand All @@ -183,9 +186,9 @@ def from_file(cls, filepath: Path) -> "ConstructorParameters":
config = OrderedDict(json.load(params_file))
return cls(config)

def resolve(self, contract_name: str, context: typing.Dict[str, Any]) -> OrderedDict:
def resolve(self, contract_name: str) -> OrderedDict:
"""Resolves the constructor parameters for a single contract."""
result = _resolve_parameters(self.parameters[contract_name], context)
result = _resolve_parameters(self.parameters[contract_name])
return result


Expand All @@ -200,10 +203,10 @@ def get_kwargs(self) -> typing.Dict[str, Any]:
"""Returns the deployment kwargs."""
return {"publish": self.publish}

def get(self, container: ContractContainer, context: typing.Dict[str, Any]) -> List[Any]:
def get(self, container: ContractContainer) -> List[Any]:
"""Resolves the deployment parameters for a single contract."""
contract_name = container.contract_type.name
resolved_constructor_params = self.constructor_parameters.resolve(contract_name, context)
resolved_constructor_params = self.constructor_parameters.resolve(contract_name)
_confirm_resolution(resolved_constructor_params, contract_name)
deployment_params = [container, *resolved_constructor_params.values()]
return deployment_params

0 comments on commit 3d75c81

Please sign in to comment.