From 7688e60e1b6e2ae377e9df52ea277c357c179bc9 Mon Sep 17 00:00:00 2001 From: derekpierre Date: Wed, 27 Sep 2023 11:51:21 -0400 Subject: [PATCH] Create Transactor class (parent of Deployer) for transaction execution and includes validation and annotated information. Use Transactor in scripts which only perform transactions based on already deployed contracts i.e. no fresh deployments. Update configure_staking script to use MockPolygonChild entry from child registry. --- deployment/params.py | 77 ++++++++++++---------- scripts/lynx/configure_staking.py | 51 +++++++------- scripts/lynx/confirm_operator_addresses.py | 9 +-- 3 files changed, 70 insertions(+), 67 deletions(-) diff --git a/deployment/params.py b/deployment/params.py index 4f57fe2b..17931a35 100644 --- a/deployment/params.py +++ b/deployment/params.py @@ -7,20 +7,9 @@ from ape import chain, project from ape.api import AccountAPI, ReceiptAPI from ape.cli import get_user_selected_account -from ape.contracts.base import ( - ContractContainer, - ContractInstance, - ContractTransactionHandler -) +from ape.contracts.base import ContractContainer, ContractInstance, ContractTransactionHandler from ape.utils import ZERO_ADDRESS -from eth_typing import ChecksumAddress -from ethpm_types import MethodABI -from web3.auto.gethdev import w3 - -from deployment.confirm import ( - _confirm_resolution, - _continue -) +from deployment.confirm import _confirm_resolution, _continue from deployment.constants import ( BYTES_PREFIX, DEPLOYER_INDICATOR, @@ -29,6 +18,9 @@ SPECIAL_VARIABLE_DELIMITER, VARIABLE_PREFIX, ) +from eth_typing import ChecksumAddress +from ethpm_types import MethodABI +from web3.auto.gethdev import w3 def _is_variable(param: Any) -> bool: @@ -308,10 +300,42 @@ def resolve(self, contract_name: str) -> OrderedDict: return result -class Deployer: +class Transactor: """ - Represents ape an ape deployment account plus - deployment parameters for a set of contracts. + Represents an ape account plus validated/annotated transaction execution. + """ + + def __init__(self, account: typing.Optional[AccountAPI] = None): + if account is None: + self._account = get_user_selected_account() + + def get_account(self) -> AccountAPI: + """Returns the transactor account.""" + return self._account + + def transact(self, method: ContractTransactionHandler, *args) -> ReceiptAPI: + abi = _get_function_abi(method=method, args=args) + named_args = _validate_transaction_args(args=args, abi=abi) + base_message = ( + f"\nTransacting {method.contract.contract_type.name}" + f"[{method.contract.address[:10]}].{method}" + ) + if named_args: + pretty_args = "\n\t".join(f"{k}={v}" for k, v in named_args.items()) + message = f"{base_message} with arguments:\n\t{pretty_args}" + else: + message = f"{base_message} with no arguments" + print(message) + _continue() + + result = method(*args, sender=self._account) + return result + + +class Deployer(Transactor): + """ + Represents an ape account plus + deployment parameters for a set of contracts, plus validated/annotated execution. """ __DEPLOYER_ACCOUNT: AccountAPI = None @@ -319,10 +343,9 @@ class Deployer: def __init__( self, params_path: Path, publish: bool, account: typing.Optional[AccountAPI] = None ): + super().__init__(account) self.constructor_parameters = ConstructorParameters.from_file(params_path) - if account is None: - account = get_user_selected_account() - self._set_deployer(account) + self._set_deployer(self._account) self.publish = publish @classmethod @@ -353,24 +376,10 @@ def deploy(self, container: ContractContainer) -> ContractInstance: instance = deployer_account.deploy(*args, **kwargs) return instance - def transact(self, method: ContractTransactionHandler, *args) -> ReceiptAPI: - abi = _get_function_abi(method=method, args=args) - named_args = _validate_transaction_args(args=args, abi=abi) - base_message = f"Transacting {method.contract.contract_type.name}[{method.contract.address[:10]}].{method}" - if named_args: - pretty_args = "\n\t".join(f"{k}={v}" for k, v in named_args.items()) - message = f"{base_message} with arguments:\n\t{pretty_args}" - else: - message = f"{base_message} with no arguments" - print(message) - _continue() - result = method(*args, sender=self.get_account()) - return result - @staticmethod def proxy(contract: ContractContainer, proxy_contract: ContractInstance) -> ContractInstance: print( - f"Wrapping {contract.contract_type.name} in " + f"\nWrapping {contract.contract_type.name} in " f"at {proxy_contract.contract_type.name}:{proxy_contract.address}." ) return contract.at(proxy_contract.address) diff --git a/scripts/lynx/configure_staking.py b/scripts/lynx/configure_staking.py index 0cecd9e7..34f7ff92 100644 --- a/scripts/lynx/configure_staking.py +++ b/scripts/lynx/configure_staking.py @@ -1,14 +1,14 @@ from ape import networks, project -from ape.api import AccountAPI -from ape.cli import get_user_selected_account from deployment.constants import ARTIFACTS_DIR, LYNX_NODES +from deployment.params import Transactor from deployment.registry import contracts_from_registry +from deployment.utils import check_plugins ROOT_REGISTRY_FILEPATH = ARTIFACTS_DIR / "lynx" / "lynx-alpha-13-root-registry.json" CHILD_REGISTRY_FILEPATH = ARTIFACTS_DIR / "lynx" / "lynx-alpha-13-child-registry.json" -def configure_goerli_root(deployer_account: AccountAPI) -> int: +def configure_goerli_root(transactor: Transactor) -> int: """Configures ThresholdStaking and TACoApplication on Goerli.""" deployments = contracts_from_registry(filepath=ROOT_REGISTRY_FILEPATH) @@ -21,57 +21,50 @@ def configure_goerli_root(deployer_account: AccountAPI) -> int: min_stake_size = taco_application_contract.minimumAuthorization() for staking_provider, operator in LYNX_NODES.items(): # staking - print(f"Setting roles for staking provider {staking_provider} on Goerli") - threshold_staking_contract.setRoles( + transactor.transact( + threshold_staking_contract.setRoles, staking_provider, - deployer_account.address, + transactor.get_account().address, staking_provider, staking_provider, - sender=deployer_account, ) - print( - f"Authorizing increased in stake for staking provider {staking_provider} on Goerli" - ) - threshold_staking_contract.authorizationIncreased( - staking_provider, 0, min_stake_size, sender=deployer_account + transactor.transact( + threshold_staking_contract.authorizationIncreased, + staking_provider, + 0, + min_stake_size, ) # bonding - print(f"Bonding operator {operator} for {staking_provider} on Goerli") - taco_application_contract.bondOperator( - staking_provider, operator, sender=deployer_account - ) + transactor.transact(taco_application_contract.bondOperator, staking_provider, operator) return min_stake_size -def configure_mumbai_root(deployer_account: AccountAPI, stake_size: int): +def configure_mumbai_root(transactor: Transactor, stake_size: int): """Configures MockTACoApplication on Mumbai.""" deployments = contracts_from_registry(filepath=CHILD_REGISTRY_FILEPATH) # Set up lynx stakes on Mumbai poly_network = networks.polygon.mumbai with poly_network.use_provider("infura"): - mock_taco_application_contract = deployments[ - project.LynxMockTACoApplication.contract_type.name - ] + mock_taco_application_contract = deployments[project.MockPolygonChild.contract_type.name] for staking_provider, operator in LYNX_NODES.items(): # staking - print(f"Setting stake for staking provider {staking_provider} on Mumbai") - mock_taco_application_contract.updateAuthorization( - staking_provider, stake_size, sender=deployer_account + transactor.transact( + mock_taco_application_contract.updateAuthorization, staking_provider, stake_size ) # bonding - print(f"Bonding operator {operator} for {staking_provider} on Mumbai") - mock_taco_application_contract.updateOperator( - staking_provider, operator, sender=deployer_account + transactor.transact( + mock_taco_application_contract.updateOperator, staking_provider, operator ) def main(): - deployer_account = get_user_selected_account() - stake_size = configure_goerli_root(deployer_account) - configure_mumbai_root(deployer_account, stake_size) + check_plugins() + transactor = Transactor() + stake_size = configure_goerli_root(transactor) + configure_mumbai_root(transactor, stake_size) diff --git a/scripts/lynx/confirm_operator_addresses.py b/scripts/lynx/confirm_operator_addresses.py index 97c6ab33..667409f7 100644 --- a/scripts/lynx/confirm_operator_addresses.py +++ b/scripts/lynx/confirm_operator_addresses.py @@ -1,7 +1,8 @@ from ape import project -from ape.cli import get_user_selected_account from deployment.constants import ARTIFACTS_DIR, LYNX_NODES +from deployment.params import Transactor from deployment.registry import contracts_from_registry +from deployment.utils import check_plugins ROOT_REGISTRY_FILEPATH = ARTIFACTS_DIR / "lynx" / "lynx-alpha-13-root-registry.json" @@ -12,9 +13,9 @@ def main(): ape run lynx confirm_operator_addresses --network ethereum:goerli:infura """ - - deployer_account = get_user_selected_account() + check_plugins() + transactor = Transactor() deployments = contracts_from_registry(filepath=ROOT_REGISTRY_FILEPATH) mock_polygon_root = deployments[project.MockPolygonRoot.contract_type.name] for _, operator in LYNX_NODES.items(): - mock_polygon_root.confirmOperatorAddress(operator, sender=deployer_account) + transactor.transact(mock_polygon_root.confirmOperatorAddress, operator)