Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions run_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -621,22 +621,23 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
if [ "$(get_on_chain_service_state "$service_id")" == "PRE_REGISTRATION" ]; then
echo "[Service owner] Updating on-chain service $service_id..."
nft="bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq"
export cmd="poetry run autonomy mint \
export cmd=""
if [ "${use_staking}" = true ]; then
cost_of_bonding=1000000000000000000
poetry run python "../scripts/update_service.py" "../$operator_pkey_path" "$nft" "$AGENT_ID" "$service_id" "$CUSTOM_OLAS_ADDRESS" "$cost_of_bonding" "packages/valory/services/trader/" "$rpc"
else
cost_of_bonding=10000000000000000
cmd="poetry run autonomy mint \
--skip-hash-check \
--use-custom-chain \
service packages/valory/services/trader/ \
--key \"../$operator_pkey_path\" \
--nft $nft \
-a $AGENT_ID \
-n $n_agents \
-c $cost_of_bonding \
--threshold $n_agents \
--update \"$service_id\""
if [ "${use_staking}" = true ]; then
cost_of_bonding=1000000000000000000
cmd+=" -c $cost_of_bonding --token $CUSTOM_OLAS_ADDRESS"
else
cost_of_bonding=10000000000000000
cmd+=" -c $cost_of_bonding"
Comment on lines -638 to -639
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this missing in the else now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

fi
output=$(eval "$cmd")
if [[ $? -ne 0 ]]; then
Expand Down
203 changes: 203 additions & 0 deletions scripts/update_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This script updates the service."""

import argparse
import sys
import traceback
from pathlib import Path

from aea_ledger_ethereum.ethereum import EthereumApi, EthereumCrypto
from autonomy.chain.base import UnitType, registry_contracts
from autonomy.chain.mint import sort_service_dependency_metadata
from autonomy.chain.config import ChainType, ContractConfigs
from aea.crypto.base import Crypto, LedgerApi
from typing import List
from math import ceil
from autonomy.chain.constants import (
AGENT_REGISTRY_CONTRACT,
COMPONENT_REGISTRY_CONTRACT,
REGISTRIES_MANAGER_CONTRACT,
SERVICE_MANAGER_CONTRACT,
SERVICE_REGISTRY_CONTRACT,
)
from autonomy.chain.metadata import NFTHashOrPath, publish_metadata
from aea.configurations.loader import load_configuration_object
from aea.configurations.data_types import PackageType
from aea.helpers.base import IPFSHash

from autonomy.configurations.base import PACKAGE_TYPE_TO_CONFIG_CLASS

from utils import send_tx_and_wait_for_receipt


def update_service( # pylint: disable=too-many-arguments,too-many-locals
ledger_api: LedgerApi,
crypto: Crypto,
service_id: int,
nft: str,
chain_type: ChainType,
agent_ids: List[int],
number_of_slots_per_agent: List[int],
cost_of_bond_per_agent: List[int],
threshold: int,
token: str,
directory: Path,
) -> None:
"""Publish component on-chain."""

package = load_configuration_object(
package_type=PackageType.SERVICE,
directory=directory,
package_type_config_class=PACKAGE_TYPE_TO_CONFIG_CLASS,
)
metadata_hash, _ = publish_metadata(
package_id=package.package_id,
package_path=directory,
nft=IPFSHash(nft),
description=package.description,
)

if len(agent_ids) == 0:
raise ValueError("Please provide at least one agent id")

if len(number_of_slots_per_agent) == 0:
raise ValueError("Please for provide number of slots for agents")

if len(cost_of_bond_per_agent) == 0:
raise ValueError("Please for provide cost of bond for agents")

if (
len(agent_ids) != len(number_of_slots_per_agent)
or len(agent_ids) != len(cost_of_bond_per_agent)
or len(number_of_slots_per_agent) != len(cost_of_bond_per_agent)
):
raise ValueError(
"Make sure the number of agent ids, number of slots for agents and cost of bond for agents match"
)

if any(map(lambda x: x == 0, number_of_slots_per_agent)):
raise ValueError("Number of slots cannot be zero")

if any(map(lambda x: x == 0, cost_of_bond_per_agent)):
raise ValueError("Cost of bond cannot be zero")

number_of_agent_instances = sum(number_of_slots_per_agent)
if threshold < (ceil((number_of_agent_instances * 2 + 1) / 3)):
raise ValueError(
"The threshold value should at least be greater than or equal to ceil((n * 2 + 1) / 3), "
"n is total number of agent instances in the service"
)

(
agent_ids,
number_of_slots_per_agent,
cost_of_bond_per_agent,
) = sort_service_dependency_metadata(
agent_ids=agent_ids,
number_of_slots_per_agents=number_of_slots_per_agent,
cost_of_bond_per_agent=cost_of_bond_per_agent,
)

agent_params = [
[n, c] for n, c in zip(number_of_slots_per_agent, cost_of_bond_per_agent)
]

tx = registry_contracts.service_manager.get_update_transaction(
ledger_api=ledger_api,
contract_address=ContractConfigs.get(SERVICE_MANAGER_CONTRACT.name).contracts[
chain_type
],
service_id=service_id,
sender=crypto.address,
metadata_hash=metadata_hash,
agent_ids=agent_ids,
agent_params=agent_params,
threshold=threshold,
token=token,
raise_on_try=True,
)

send_tx_and_wait_for_receipt(ledger_api, crypto, tx)


if __name__ == "__main__":
try:
print(f" - Starting {Path(__file__).name} script...")

parser = argparse.ArgumentParser(description="Update the service.")
parser.add_argument(
"owner_private_key_path",
type=str,
help="Path to the file containing the service owner's Ethereum private key",
)
parser.add_argument(
"nft",
type=str,
help="The nft to be used.",
)
parser.add_argument(
"agent_id",
type=int,
help="The service registry contract address.",
)
parser.add_argument(
"service_id",
type=int,
help="The service id.",
)
parser.add_argument(
"token",
type=str,
help="The token address to be used.",
)
parser.add_argument(
"bond_amount",
type=int,
help="The bond amount.",
)
parser.add_argument(
"directory",
type=str,
help="The directory of the service package.",
)
parser.add_argument("rpc", type=str, help="RPC for the Gnosis chain")
args = parser.parse_args()
ledger_api = EthereumApi(address=args.rpc)
owner_crypto = EthereumCrypto(private_key_path=args.owner_private_key_path)
update_service(
ledger_api=ledger_api,
crypto=owner_crypto,
service_id=args.service_id,
nft=args.nft,
chain_type=ChainType.CUSTOM,
agent_ids=[args.agent_id],
number_of_slots_per_agent=[1],
cost_of_bond_per_agent=[args.bond_amount],
threshold=1,
token=args.token,
directory=Path(args.directory),
)
print(f"Service {args.service_id} updated successfully.")
except Exception as e: # pylint: disable=broad-except
print(f"An error occurred while executing {Path(__file__).name}: {str(e)}")
traceback.print_exc()
sys.exit(1)