Skip to content

Commit

Permalink
Merge pull request #170 from valory-xyz/feat/update-agent
Browse files Browse the repository at this point in the history
Update agent hash
  • Loading branch information
truemiller authored Jun 14, 2024
2 parents f9332cd + 8ff071f commit bccc507
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
node-version: "20.11"
- name: Setup
run: |
npm install
yarn
# Uninstall brew
sudo bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
- name: Run Test
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ frontend/build
# python
.tox/
.operate/
.operate*/
__pycache__/
data/
backend/temp/
Expand Down
2 changes: 1 addition & 1 deletion electron/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { BrewScript } = require("./scripts")
* - use "" (nothing as a suffix) for latest release candidate, for example "0.1.0rc26"
* - use "alpha" for alpha release, for example "0.1.0rc26-alpha"
*/
const OlasMiddlewareVersion = '0.1.0rc40';
const OlasMiddlewareVersion = '0.1.0rc46';
const OperateDirectory = `${os.homedir()}/.operate`;
const VenvDir = `${OperateDirectory}/venv`;
const TempDir = `${OperateDirectory}/temp`;
Expand Down
2 changes: 1 addition & 1 deletion frontend/constants/serviceTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ServiceTemplate } from '@/client';
export const SERVICE_TEMPLATES: ServiceTemplate[] = [
{
name: 'Trader Agent',
hash: 'bafybeiha6dxygx2ntgjxhs6zzymgqk3s5biy3ozeqw6zuhr6yxgjlebfmq',
hash: 'bafybeihv3nyh43rim6kcxdscdgsiojhbqfwndhwcrtxhi3gkxbnasdc7ui',
description: 'Trader agent for omen prediction markets',
image:
'https://operate.olas.network/_next/image?url=%2Fimages%2Fprediction-agent.png&w=3840&q=75',
Expand Down
155 changes: 106 additions & 49 deletions operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ def deploy_service_onchain( # pylint: disable=too-many-statements
service.chain_data.on_chain_state = OnChainState.MINTED
service.store()

info = ocm.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state == OnChainState.MINTED:
self.logger.info("Activating service")
ocm.activate(
Expand All @@ -255,6 +258,9 @@ def deploy_service_onchain( # pylint: disable=too-many-statements
service.chain_data.on_chain_state = OnChainState.ACTIVATED
service.store()

info = ocm.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state == OnChainState.ACTIVATED:
self.logger.info("Registering service")
ocm.register(
Expand All @@ -271,6 +277,9 @@ def deploy_service_onchain( # pylint: disable=too-many-statements
service.keys = keys
service.store()

info = ocm.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state == OnChainState.REGISTERED:
self.logger.info("Deploying service")
ocm.deploy(
Expand Down Expand Up @@ -399,6 +408,9 @@ def deploy_service_onchain_from_safe( # pylint: disable=too-many-statements,too
service.chain_data.on_chain_state = OnChainState.MINTED
service.store()

info = sftxb.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state == OnChainState.MINTED:
cost_of_bond = user_params.cost_of_bond
if user_params.use_staking:
Expand Down Expand Up @@ -448,6 +460,9 @@ def deploy_service_onchain_from_safe( # pylint: disable=too-many-statements,too
service.chain_data.on_chain_state = OnChainState.ACTIVATED
service.store()

info = sftxb.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state == OnChainState.ACTIVATED:
cost_of_bond = user_params.cost_of_bond
if user_params.use_staking:
Expand Down Expand Up @@ -502,6 +517,9 @@ def deploy_service_onchain_from_safe( # pylint: disable=too-many-statements,too
service.keys = keys
service.store()

info = sftxb.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state == OnChainState.REGISTERED:
self.logger.info("Deploying service")
sftxb.new_tx().add(
Expand Down Expand Up @@ -532,12 +550,15 @@ def terminate_service_on_chain(self, hash: str) -> None:
:param hash: Service hash
"""
service = self.create_or_load(hash=hash)
ocm = self.get_on_chain_manager(service=service)
info = ocm.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state != OnChainState.DEPLOYED:
self.logger.info("Cannot terminate service")
return

self.logger.info("Terminating service")
ocm = self.get_on_chain_manager(service=service)
ocm.terminate(
service_id=service.chain_data.token,
token=(
Expand All @@ -556,12 +577,15 @@ def terminate_service_on_chain_from_safe(self, hash: str) -> None:
:param hash: Service hash
"""
service = self.create_or_load(hash=hash)
sftxb = self.get_eth_safe_tx_builder(service=service)
info = sftxb.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state != OnChainState.DEPLOYED:
self.logger.info("Cannot terminate service")
return

self.logger.info("Terminating service")
sftxb = self.get_eth_safe_tx_builder(service=service)
sftxb.new_tx().add(
sftxb.get_terminate_data(
service_id=service.chain_data.token,
Expand All @@ -577,12 +601,15 @@ def unbond_service_on_chain(self, hash: str) -> None:
:param hash: Service hash
"""
service = self.create_or_load(hash=hash)
ocm = self.get_on_chain_manager(service=service)
info = ocm.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state != OnChainState.TERMINATED:
self.logger.info("Cannot unbond service")
return

self.logger.info("Unbonding service")
ocm = self.get_on_chain_manager(service=service)
ocm.unbond(
service_id=service.chain_data.token,
token=(
Expand All @@ -601,12 +628,15 @@ def unbond_service_on_chain_from_safe(self, hash: str) -> None:
:param hash: Service hash
"""
service = self.create_or_load(hash=hash)
sftxb = self.get_eth_safe_tx_builder(service=service)
info = sftxb.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state != OnChainState.TERMINATED:
self.logger.info("Cannot unbond service")
return

self.logger.info("Unbonding service")
sftxb = self.get_eth_safe_tx_builder(service=service)
sftxb.new_tx().add(
sftxb.get_unbond_data(
service_id=service.chain_data.token,
Expand All @@ -626,11 +656,14 @@ def stake_service_on_chain(self, hash: str) -> None:
self.logger.info("Cannot stake service, `use_staking` is set to false")
return

ocm = self.get_on_chain_manager(service=service)
info = ocm.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state != OnChainState.DEPLOYED:
self.logger.info("Cannot stake service, it's not in deployed state")
return

ocm = self.get_on_chain_manager(service=service)
state = ocm.staking_status(
service_id=service.chain_data.token,
staking_contract=STAKING[service.ledger_config.chain],
Expand All @@ -642,6 +675,12 @@ def stake_service_on_chain(self, hash: str) -> None:
service.store()
return

if state == StakingState.EVICTED:
self.logger.info(f"{service.chain_data.token} has been evicted")
service.chain_data.staked = True
service.store()
self.unstake_service_on_chain(hash=hash)

self.logger.info(f"Staking service: {service.chain_data.token}")
ocm.stake(
service_id=service.chain_data.token,
Expand All @@ -662,11 +701,14 @@ def stake_service_on_chain_from_safe(self, hash: str) -> None:
self.logger.info("Cannot stake service, `use_staking` is set to false")
return

sftxb = self.get_eth_safe_tx_builder(service=service)
info = sftxb.info(token_id=service.chain_data.token)
service.chain_data.on_chain_state = OnChainState(info["service_state"])

if service.chain_data.on_chain_state != OnChainState.DEPLOYED:
self.logger.info("Cannot stake service, it's not in deployed state")
return

sftxb = self.get_eth_safe_tx_builder(service=service)
state = sftxb.staking_status(
service_id=service.chain_data.token,
staking_contract=STAKING[service.ledger_config.chain],
Expand All @@ -678,6 +720,12 @@ def stake_service_on_chain_from_safe(self, hash: str) -> None:
service.store()
return

if state == StakingState.EVICTED:
self.logger.info(f"{service.chain_data.token} has been evicted")
service.chain_data.staked = True
service.store()
self.unstake_service_on_chain_from_safe(hash=hash)

self.logger.info(f"Approving staking: {service.chain_data.token}")
sftxb.new_tx().add(
sftxb.get_staking_approval_data(
Expand Down Expand Up @@ -715,7 +763,10 @@ def unstake_service_on_chain(self, hash: str) -> None:
service_id=service.chain_data.token,
staking_contract=STAKING[service.ledger_config.chain],
)
if state != StakingState.STAKED:
self.logger.info(
f"Staking status for service {service.chain_data.token}: {state}"
)
if state not in {StakingState.STAKED, StakingState.EVICTED}:
self.logger.info("Cannot unstake service, it's not staked")
service.chain_data.staked = False
service.store()
Expand Down Expand Up @@ -745,8 +796,10 @@ def unstake_service_on_chain_from_safe(self, hash: str) -> None:
service_id=service.chain_data.token,
staking_contract=STAKING[service.ledger_config.chain],
)
self.logger.info(f"Checking staking status for: {service.chain_data.token}")
if state != StakingState.STAKED:
self.logger.info(
f"Staking status for service {service.chain_data.token}: {state}"
)
if state not in {StakingState.STAKED, StakingState.EVICTED}:
self.logger.info("Cannot unstake service, it's not staked")
service.chain_data.staked = False
service.store()
Expand Down Expand Up @@ -879,51 +932,55 @@ def update_service(
new_hash: str,
rpc: t.Optional[str] = None,
on_chain_user_params: t.Optional[OnChainUserParams] = None,
from_safe: bool = True,
from_safe: bool = True, # pylint: disable=unused-argument
) -> Service:
"""Update a service."""
old_service = self.create_or_load(
hash=old_hash,
)
(
self.unstake_service_on_chain_from_safe
if from_safe
else self.unstake_service_on_chain
)(
hash=old_hash,
)
(
self.terminate_service_on_chain_from_safe
if from_safe
else self.terminate_service_on_chain
)(
hash=old_hash,
)
(
self.unbond_service_on_chain_from_safe
if from_safe
else self.unbond_service_on_chain
)(
hash=old_hash,
)

owner, *_ = old_service.chain_data.instances
if from_safe:
sftx = self.get_eth_safe_tx_builder(service=old_service)
sftx.new_tx().add(
sftx.get_swap_data(
service_id=old_service.chain_data.token,
multisig=old_service.chain_data.multisig,
owner_key=str(self.keys_manager.get(key=owner).private_key),
)
).settle()
else:
ocm = self.get_on_chain_manager(service=old_service)
ocm.swap(
service_id=old_service.chain_data.token,
multisig=old_service.chain_data.multisig,
owner_key=str(self.keys_manager.get(key=owner).private_key),
)
# TODO code for updating service commented until safe swap transaction is implemented
# This is a temporary fix that will only work for services that have not started the
# update flow. Services having started the update flow must need to manually change
# the Safe owner to the Operator.
# ( # noqa: E800
# self.unstake_service_on_chain_from_safe # noqa: E800
# if from_safe # noqa: E800
# else self.unstake_service_on_chain # noqa: E800
# )( # noqa: E800
# hash=old_hash, # noqa: E800
# ) # noqa: E800
# ( # noqa: E800
# self.terminate_service_on_chain_from_safe # noqa: E800
# if from_safe # noqa: E800
# else self.terminate_service_on_chain # noqa: E800
# )( # noqa: E800
# hash=old_hash, # noqa: E800
# ) # noqa: E800
# ( # noqa: E800
# self.unbond_service_on_chain_from_safe # noqa: E800
# if from_safe # noqa: E800
# else self.unbond_service_on_chain # noqa: E800
# )( # noqa: E800
# hash=old_hash, # noqa: E800
# ) # noqa: E800

# owner, *_ = old_service.chain_data.instances # noqa: E800
# if from_safe: # noqa: E800
# sftx = self.get_eth_safe_tx_builder(service=old_service) # noqa: E800
# sftx.new_tx().add( # noqa: E800
# sftx.get_swap_data( # noqa: E800
# service_id=old_service.chain_data.token, # noqa: E800
# multisig=old_service.chain_data.multisig, # noqa: E800
# owner_key=str(self.keys_manager.get(key=owner).private_key), # noqa: E800
# ) # noqa: E800
# ).settle() # noqa: E800
# else: # noqa: E800
# ocm = self.get_on_chain_manager(service=old_service) # noqa: E800
# ocm.swap( # noqa: E800
# service_id=old_service.chain_data.token, # noqa: E800
# multisig=old_service.chain_data.multisig, # noqa: E800
# owner_key=str(self.keys_manager.get(key=owner).private_key), # noqa: E800
# ) # noqa: E800

new_service = self.create_or_load(
hash=new_hash,
Expand Down
9 changes: 4 additions & 5 deletions operate/services/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def check_staking_compatibility(
"""Check if service can be staked."""
status = self.status(service_id, staking_contract)
if status == StakingState.STAKED:
raise ValueError("Service already stacked")
raise ValueError("Service already staked")

if status == StakingState.EVICTED:
raise ValueError("Service is evicted")
Expand Down Expand Up @@ -328,10 +328,9 @@ def check_if_unstaking_possible(
staking_contract: str,
) -> None:
"""Check unstaking availability"""
if (
self.status(service_id=service_id, staking_contract=staking_contract)
!= StakingState.STAKED
):
if self.status(
service_id=service_id, staking_contract=staking_contract
) not in {StakingState.STAKED, StakingState.EVICTED}:
raise ValueError("Service not staked.")

ts_start = t.cast(int, self.service_info(staking_contract, service_id)[3])
Expand Down
4 changes: 2 additions & 2 deletions operate/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ def _setup_agent(working_dir: Path) -> None:
"""Setup agent."""
env = json.loads((working_dir / "agent.json").read_text(encoding="utf-8"))
# Patch for trader agent
if "SKILL_TRADER_ABCI_MODELS_PARAMS_ARGS_POLICY_STORE_PATH" in env:
if "SKILL_TRADER_ABCI_MODELS_PARAMS_ARGS_STORE_PATH" in env:
data_dir = working_dir / "data"
data_dir.mkdir(exist_ok=True)
env["SKILL_TRADER_ABCI_MODELS_PARAMS_ARGS_POLICY_STORE_PATH"] = str(data_dir)
env["SKILL_TRADER_ABCI_MODELS_PARAMS_ARGS_STORE_PATH"] = str(data_dir)

# TODO: Dynamic port allocation, backport to service builder
env["CONNECTION_ABCI_CONFIG_HOST"] = "localhost"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"main": "electron/main.js",
"name": "olas-operate-app",
"productName": "Pearl",
"version": "0.1.0-rc40",
"version": "0.1.0-rc46",
"dependencies": {
"@ant-design/cssinjs": "^1.18.4",
"@ant-design/icons": "^5.3.0",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "olas-operate-middleware"
version = "0.1.0-rc40"
version = "0.1.0-rc46"
description = ""
authors = ["David Vilela <[email protected]>", "Viraj Patel <[email protected]>"]
readme = "README.md"
Expand Down
Loading

0 comments on commit bccc507

Please sign in to comment.