Skip to content

Commit

Permalink
fix: update user params
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoreira-valory committed Aug 20, 2024
1 parent 4ce058e commit 8a8e675
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
9 changes: 9 additions & 0 deletions operate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand Down
37 changes: 27 additions & 10 deletions operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions operate/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8a8e675

Please sign in to comment.