Skip to content

Commit

Permalink
Create Transactor class (parent of Deployer) for transaction executio…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
derekpierre committed Sep 27, 2023
1 parent f403c5b commit 7688e60
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 67 deletions.
77 changes: 43 additions & 34 deletions deployment/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -308,21 +300,52 @@ 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

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
Expand Down Expand Up @@ -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)
51 changes: 22 additions & 29 deletions scripts/lynx/configure_staking.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
9 changes: 5 additions & 4 deletions scripts/lynx/confirm_operator_addresses.py
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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)

0 comments on commit 7688e60

Please sign in to comment.