Skip to content

Commit

Permalink
Process deployment configuration file with contract variables in it.
Browse files Browse the repository at this point in the history
Co-authored-by: Kieran Prasch <[email protected]>
  • Loading branch information
derekpierre and KPrasch committed Sep 19, 2023
1 parent 6e9cd69 commit bc094a3
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions utils/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
from pathlib import Path
from typing import Any, List

from ape.contracts.base import ContractContainer

VARIABLE_PREFIX = "$"


class DeploymentConfigError(ValueError):
pass


def read_deployment_config(config_filepath: Path) -> typing.OrderedDict[str, Any]:
with open(config_filepath, "r") as config_file:
config = json.load(config_file)
return OrderedDict(config)


def is_variable(param: Any):
return isinstance(param, str) and param.startswith(VARIABLE_PREFIX)

Expand All @@ -40,3 +36,49 @@ def validate_deployment_config(config: typing.OrderedDict[str, Any]):
_validate_param(param, available_contracts)
else:
_validate_param(value, available_contracts)


class ConstructorParams:
def __init__(self, constructor_values: OrderedDict):
self.params = constructor_values

def _resolve(self, value: Any, context: typing.Dict[str, Any]) -> Any:
if not is_variable(value):
return value

variable = value.strip(VARIABLE_PREFIX)
contract_instance = context[variable]
return contract_instance.address

def get_params(
self, container: ContractContainer, context: typing.Dict[str, Any], interactive: bool = True
) -> List[Any]:
contract_name = container.contract_type.name
contract_parameters = self.params[contract_name]
resolved_params = OrderedDict()
for name, value in contract_parameters:
if isinstance(value, list):
param_value_list = []
for item in value:
param_value_list.append(self._resolve(item, context))
resolved_params[name] = param_value_list
else:
resolved_params[name] = self._resolve(value, context)

if interactive:
print(f"Resolved constructor parameters for {contract_name}")
for name, resolved_value in resolved_params:
print(f"\t{name}={resolved_value}")
answer = input("Continue Y/N? ")
if answer.lower().strip() == "n":
print("Aborting deployment!")
exit(-1)

return list(resolved_params.values())

@classmethod
def from_file(cls, config_filepath: Path) -> "ConstructorParams":
with open(config_filepath, "r") as config_file:
config = OrderedDict(json.load(config_file))

return cls(config)

0 comments on commit bc094a3

Please sign in to comment.