diff --git a/operate/cli.py b/operate/cli.py index 1eac6a3b6..f4632b70c 100644 --- a/operate/cli.py +++ b/operate/cli.py @@ -504,6 +504,14 @@ async def _create_services(request: Request) -> JSONResponse: if operate.password is None: return USER_NOT_LOGGED_IN_ERROR template = await request.json() + + print("!!!!!!!!!!!!!!!!") + from icecream import ic + + ic(template) + print(template) + import sys + manager = operate.service_manager() if len(manager.json) > 0: old_hash = manager.json[0]["hash"] @@ -528,6 +536,7 @@ async def _create_services(request: Request) -> JSONResponse: ) if template.get("deploy", False): + def _fn() -> None: manager.deploy_service_onchain_from_safe(hash=service.hash) # manager.stake_service_on_chain_from_safe(hash=service.hash) # Done inside deploy_service_onchain diff --git a/operate/services/manage.py b/operate/services/manage.py index 730ec5c8d..6acf0b462 100644 --- a/operate/services/manage.py +++ b/operate/services/manage.py @@ -48,10 +48,7 @@ OnChainUserParams, Service, ) -from operate.types import ( - ServiceTemplate, - LedgerConfig -) +from operate.types import LedgerConfig, ServiceTemplate from operate.utils.gnosis import NULL_ADDRESS from operate.wallet.master import MasterWalletManager @@ -159,7 +156,12 @@ def load_or_create( """ path = self.path / hash if path.exists(): - return Service.load(path=path) + service = Service.load(path=path) + + if service_template is not None: + service.update_user_params_from_template(service_template=service_template) + + return service if service_template is None: raise ValueError( @@ -878,8 +880,8 @@ def stake_service_on_chain_from_safe(self, hash: str, chain_id: str) -> None: ledger_config = chain_config.ledger_config chain_data = chain_config.chain_data user_params = chain_data.user_params - target_staking_program_id = user_params.staking_program_id - target_staking_contract = STAKING[ledger_config.chain][target_staking_program_id] + target_staking_program = user_params.staking_program_id + target_staking_contract = STAKING[ledger_config.chain][target_staking_program] sftxb = self.get_eth_safe_tx_builder(ledger_config=ledger_config) # TODO fixme @@ -925,13 +927,27 @@ def stake_service_on_chain_from_safe(self, hash: str, chain_id: str) -> None: service_id=chain_config.chain_data.token, staking_contract=target_staking_contract, ) + self.logger.info("Checking conditions to stake.") + + staking_rewards_available = sftxb.staking_rewards_available(target_staking_contract) + staking_slots_available = sftxb.staking_slots_available(target_staking_contract) + on_chain_state = self._get_on_chain_state(chain_config=chain_config) + current_staking_program = self._get_current_staking_program(chain_data, ledger_config, sftxb) + + self.logger.info(f"use_staking={chain_config.chain_data.user_params.use_staking}") + self.logger.info(f"{staking_state=}") + self.logger.info(f"{staking_rewards_available=}") + self.logger.info(f"{staking_slots_available=}") + self.logger.info(f"{on_chain_state=}") + self.logger.info(f"{current_staking_program=}") + self.logger.info(f"{target_staking_program=}") if ( chain_config.chain_data.user_params.use_staking and staking_state == StakingState.UNSTAKED - and sftxb.staking_rewards_available(target_staking_contract) - and sftxb.staking_slots_available(target_staking_contract) - and self._get_on_chain_state(chain_config=chain_config) == OnChainState.DEPLOYED + and staking_rewards_available + and staking_slots_available + and on_chain_state == OnChainState.DEPLOYED ): self.logger.info(f"Approving staking: {chain_config.chain_data.token}") sftxb.new_tx().add( @@ -955,6 +971,7 @@ def stake_service_on_chain_from_safe(self, hash: str, chain_id: str) -> None: service.store() current_staking_program = self._get_current_staking_program(chain_data, ledger_config, sftxb) + self.logger.info(f"{target_staking_program=}") self.logger.info(f"{current_staking_program=}") def unstake_service_on_chain(self, hash: str) -> None: diff --git a/operate/services/service.py b/operate/services/service.py index 6a476866b..ea6c563ca 100644 --- a/operate/services/service.py +++ b/operate/services/service.py @@ -777,6 +777,14 @@ def new( service.store() return service + def update_user_params_from_template(self, service_template: ServiceTemplate): + """Update user params from template.""" + for chain, config in service_template["configurations"].items(): + for chain, config in service_template["configurations"].items(): + self.chain_configs[chain].chain_data.user_params = OnChainUserParams.from_json(config) + + self.store() + def delete(self) -> None: """Delete a service.""" parent_directory = self.path.parent