Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
18 changes: 14 additions & 4 deletions report.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import time
import traceback
from argparse import ArgumentParser
from dotenv import dotenv_values
from enum import Enum
from pathlib import Path
from typing import Any
import requests
import sys
from typing import Any, List

import docker
import trades
Expand All @@ -46,12 +49,12 @@

SCRIPT_PATH = Path(__file__).resolve().parent
STORE_PATH = Path(SCRIPT_PATH, ".trader_runner")
DOTENV_PATH = Path(STORE_PATH, ".env")
RPC_PATH = Path(STORE_PATH, "rpc.txt")
AGENT_KEYS_JSON_PATH = Path(STORE_PATH, "keys.json")
OPERATOR_KEYS_JSON_PATH = Path(STORE_PATH, "operator_keys.json")
SAFE_ADDRESS_PATH = Path(STORE_PATH, "service_safe_address.txt")
SERVICE_ID_PATH = Path(STORE_PATH, "service_id.txt")
SERVICE_STAKING_CONTRACT_ADDRESS = "0x43fB32f25dce34EB76c78C7A42C8F40F84BCD237"
SERVICE_STAKING_TOKEN_JSON_PATH = Path(
SCRIPT_PATH,
"trader",
Expand Down Expand Up @@ -228,6 +231,8 @@ def _parse_args() -> Any:
with open(RPC_PATH, "r", encoding="utf-8") as file:
rpc = file.read().strip()

env_file_vars = dotenv_values(DOTENV_PATH)

# Prediction market trading
mech_requests = trades.get_mech_requests(safe_address)
mech_statistics = trades.get_mech_statistics(mech_requests)
Expand All @@ -247,12 +252,14 @@ def _parse_args() -> Any:

try:
w3 = Web3(HTTPProvider(rpc))

service_staking_contract_address = env_file_vars.get("CUSTOM_STAKING_ADDRESS")
with open(SERVICE_STAKING_TOKEN_JSON_PATH, "r", encoding="utf-8") as file:
service_staking_token_data = json.load(file)

service_staking_token_abi = service_staking_token_data.get("abi", [])
service_staking_token_contract = w3.eth.contract(
address=SERVICE_STAKING_CONTRACT_ADDRESS, abi=service_staking_token_abi
address=service_staking_contract_address, abi=service_staking_token_abi # type: ignore
)
service_staking_state = StakingState(
service_staking_token_contract.functions.getServiceStakingState(
Expand All @@ -265,6 +272,8 @@ def _parse_args() -> Any:
or service_staking_state == StakingState.EVICTED
)
_print_status("Is service staked?", _color_bool(is_staked, "Yes", "No"))
if is_staked:
_print_status("Staking program", env_file_vars.get("STAKING_PROGRAM")) # type: ignore
if service_staking_state == StakingState.STAKED:
_print_status("Staking state", service_staking_state.name)
elif service_staking_state == StakingState.EVICTED:
Expand All @@ -288,13 +297,14 @@ def _parse_args() -> Any:
abi=service_registry_token_utility_abi,
)

mech_contract_address = env_file_vars.get("MECH_CONTRACT_ADDRESS")
with open(MECH_CONTRACT_JSON_PATH, "r", encoding="utf-8") as file:
mech_contract_data = json.load(file)

mech_contract_abi = mech_contract_data.get("abi", [])

mech_contract = w3.eth.contract(
address=MECH_CONTRACT_ADDRESS, abi=mech_contract_abi
address=mech_contract_address, abi=mech_contract_abi # type: ignore
)

security_deposit = (
Expand Down
120 changes: 43 additions & 77 deletions run_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ get_on_chain_service_state() {
echo "$state"
}

get_on_chain_agent_ids() {
local service_id="$1"
local service_info=$(poetry run autonomy service --use-custom-chain info "$service_id")
local agent_ids="$(echo "$service_info" | awk '/Cannonical Agents/ {sub(/\|[ \t]*Cannonical Agents[ \t]*\|[ \t]*/, ""); sub(/[ \t]*\|[ \t]*/, ""); print}')"
echo "$agent_ids"
}

# Move a file if it exists
move_if_exists() {
local source_file="$1"
Expand Down Expand Up @@ -430,30 +437,6 @@ perform_staking_ops() {
echo ""
}

# Prompt user for staking preference
prompt_use_staking() {
while true; do
echo "Use staking?"
echo "------------"
read -p "Do you want to stake this service? (yes/no): " use_staking

case "$use_staking" in
[Yy]|[Yy][Ee][Ss])
USE_STAKING="true"
break
;;
[Nn]|[Nn][Oo])
USE_STAKING="false"
break
;;
*)
echo "Please enter 'yes' or 'no'."
;;
esac
done
echo ""
}

# Prompt user for subgraph API key
prompt_subgraph_api_key() {
echo "Provide a Subgraph API key"
Expand Down Expand Up @@ -517,6 +500,15 @@ dotenv_set_key() {
export "$key_to_set=$value_to_set"
}

export_dotenv() {
local dotenv_path="$1"
unamestr=$(uname)
if [ "$unamestr" = 'Linux' ]; then
export $(grep -v '^#' $dotenv_path | xargs -d '\n')
elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then
export $(grep -v '^#' $dotenv_path | xargs -0)
fi
}

store=".trader_runner"
path_to_store="$PWD/$store/"
Expand Down Expand Up @@ -544,8 +536,6 @@ create_storage() {

ask_confirm_password

# Prompt use staking
prompt_use_staking
prompt_subgraph_api_key
verify_staking_slots

Expand All @@ -557,10 +547,6 @@ create_storage() {
' Please back up this folder and be cautious if you are modifying or sharing these files to avoid potential asset loss.' > "../$store_readme_path"

dotenv_set_key "../$env_file_path" "SUBGRAPH_API_KEY" "$SUBGRAPH_API_KEY" true
dotenv_set_key "../$env_file_path" "USE_STAKING" "$USE_STAKING"

AGENT_ID=14
dotenv_set_key "../$env_file_path" "AGENT_ID" "$AGENT_ID"

# Generate the RPC file
echo -n "$rpc" > "../$rpc_path"
Expand Down Expand Up @@ -621,8 +607,6 @@ try_read_storage() {
fi
done

unset USE_STAKING
unset AGENT_ID
source "$env_file_path"

rpc=$(cat $rpc_path)
Expand All @@ -638,18 +622,6 @@ try_read_storage() {
dotenv_set_key "$env_file_path" "SUBGRAPH_API_KEY" "$SUBGRAPH_API_KEY" true
fi

# INFO: This is a fix to avoid corrupting already-created stores
if [ -z "$USE_STAKING" ]; then
prompt_use_staking
dotenv_set_key "$env_file_path" "USE_STAKING" "$USE_STAKING"
fi

# INFO: This is a fix to avoid corrupting already-created stores
if [ -z "$AGENT_ID" ]; then
AGENT_ID=14
dotenv_set_key "$env_file_path" "AGENT_ID" "$AGENT_ID"
fi

ask_password_if_needed
else
first_run=true
Expand All @@ -675,33 +647,25 @@ service_version="v0.18.0"
# Define constants for on-chain interaction
gnosis_chain_id=100
n_agents=1
olas_balance_required_to_bond=10000000000000000000
olas_balance_required_to_stake=10000000000000000000
xdai_balance_required_to_bond=10000000000000000
MIN_STAKING_BOND_XDAI=10000000000000000
suggested_top_up_default=50000000000000000
suggested_safe_top_up_default=500000000000000000

export RPC_RETRIES=40
export RPC_TIMEOUT_SECONDS=120

# export CUSTOM_SERVICE_REGISTRY_ADDRESS="0x9338b5153AE39BB89f50468E608eD9d764B755fD" # Set up in .env file
# export CUSTOM_STAKING_ADDRESS="0x43fB32f25dce34EB76c78C7A42C8F40F84BCD237" # Set up in .env file
# export CUSTOM_OLAS_ADDRESS="0xcE11e14225575945b8E6Dc0D4F2dD4C570f79d9f" # Set up in .env file
# export CUSTOM_SERVICE_REGISTRY_TOKEN_UTILITY_ADDRESS="0xa45E64d13A30a51b91ae0eb182e88a40e9b18eD8" # Set up in .env file
# export MECH_CONTRACT_ADDRESS="0x77af31De935740567Cf4fF1986D04B2c964A786a" # Set up in env file
Comment on lines +656 to +661
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@kupermind Please validate the following:

  1. The parameters commented correspond to addresses that depend/can be inferred from the staking contract.
  2. The remaining addresses not commented should remain constant for all staking contracts, for the time being.
  3. Please suggest a more appropriate name for any of these variables from point 1 and 2 above.

Copy link
Collaborator

@kupermind kupermind Aug 7, 2024

Choose a reason for hiding this comment

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

CUSTOM_GNOSIS_SAFE_PROXY_FACTORY_ADDRESS => CUSTOM_GNOSIS_SAFE_MULTISIG_ADDRESS

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These commented variables will be removed before merging to main.


export CUSTOM_SERVICE_MANAGER_ADDRESS="0x04b0007b2aFb398015B76e5f22993a1fddF83644"
export CUSTOM_SERVICE_REGISTRY_ADDRESS="0x9338b5153AE39BB89f50468E608eD9d764B755fD"
export CUSTOM_STAKING_ADDRESS="0x43fB32f25dce34EB76c78C7A42C8F40F84BCD237"
export CUSTOM_OLAS_ADDRESS="0xcE11e14225575945b8E6Dc0D4F2dD4C570f79d9f"
export CUSTOM_SERVICE_REGISTRY_TOKEN_UTILITY_ADDRESS="0xa45E64d13A30a51b91ae0eb182e88a40e9b18eD8"
export CUSTOM_GNOSIS_SAFE_PROXY_FACTORY_ADDRESS="0x3C1fF68f5aa342D296d4DEe4Bb1cACCA912D95fE"
export CUSTOM_GNOSIS_SAFE_SAME_ADDRESS_MULTISIG_ADDRESS="0x6e7f594f680f7aBad18b7a63de50F0FeE47dfD06"
export CUSTOM_MULTISEND_ADDRESS="0x40A2aCCbd92BCA938b02010E17A5b8929b49130D"
export WXDAI_ADDRESS="0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d"
export MECH_CONTRACT_ADDRESS="0x77af31De935740567Cf4fF1986D04B2c964A786a"

# check if USE_NEVERMINED is set to true
if [ "$USE_NEVERMINED" == "true" ];
then
echo "A Nevermined subscription will be used to pay for the mech requests."
export MECH_CONTRACT_ADDRESS="0x327E26bDF1CfEa50BFAe35643B23D5268E41F7F9"
export AGENT_REGISTRY_ADDRESS="0xAed729d4f4b895d8ca84ba022675bB0C44d2cD52"
export MECH_REQUEST_PRICE=0
fi
export OPEN_AUTONOMY_SUBGRAPH_URL="https://subgraph.autonolas.tech/subgraphs/name/autonolas-staging"

sleep_duration=12

Expand Down Expand Up @@ -864,12 +828,11 @@ echo ""
echo "-----------------------------------------"
echo "Checking Autonolas Protocol service state"
echo "-----------------------------------------"
echo ""

# We set by default AGENT_ID=14. In Everest the AGENT_ID was 12.
# This script does not allow to stake on Everest anymore, therefore
# all stores must be correctly updated with AGENT_ID=14.
AGENT_ID=14
dotenv_set_key "../$env_file_path" "AGENT_ID" "$AGENT_ID"
# Prompt use staking
poetry run python "../scripts/choose_staking.py"
export_dotenv "../$env_file_path"

if [ -z ${service_id+x} ]; then
# Check balances
Expand All @@ -894,10 +857,10 @@ if [ -z ${service_id+x} ]; then
--threshold $n_agents"

if [ "${USE_STAKING}" = true ]; then
cost_of_bonding=$olas_balance_required_to_bond
cost_of_bonding=$MIN_STAKING_BOND_OLAS
cmd+=" -c $cost_of_bonding --token $CUSTOM_OLAS_ADDRESS"
else
cost_of_bonding=$xdai_balance_required_to_bond
cost_of_bonding=$MIN_STAKING_BOND_XDAI
cmd+=" -c $cost_of_bonding"
fi
service_id=$(eval $cmd)
Expand All @@ -919,16 +882,19 @@ packages="packages/packages.json"
local_service_hash="$(grep 'service/valory/trader' $packages | awk -F: '{print $2}' | tr -d '", ' | head -n 1)"
remote_service_hash=$(poetry run python "../scripts/service_hash.py")
operator_address=$(get_address "../$operator_keys_file")
on_chain_agent_id=$(get_on_chain_agent_ids "$service_id")

if [ "$local_service_hash" != "$remote_service_hash" ]; then
if [ "$local_service_hash" != "$remote_service_hash" ] || [ "$on_chain_agent_id" != "$AGENT_ID" ]; then
echo ""
echo "WARNING: Your on-chain service configuration is out-of-date"
echo "-----------------------------------------------------------"
echo "Your currently minted on-chain service (id $service_id) mismatches the local trader service ($service_version):"
echo "Your currently minted on-chain service (id $service_id) mismatches the local configuration:"
echo " - Local service hash ($service_version): $local_service_hash"
echo " - On-chain service hash (id $service_id): $remote_service_hash"
echo " - On-chain service hash: $remote_service_hash"
echo " - Local agent id: $AGENT_ID"
echo " - On-chain agent id: $on_chain_agent_id"
echo ""
echo "This is most likely caused due to an update of the trader service code."
echo "This is most likely caused due to an update of the trader service code or agent id."
echo "The script will proceed now to update the on-chain service."
echo "The operator and agent addresses need to have enough funds to complete the process."
echo ""
Expand Down Expand Up @@ -1013,10 +979,10 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
nft="bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq"
export cmd=""
if [ "${USE_STAKING}" = true ]; then
cost_of_bonding=$olas_balance_required_to_bond
cost_of_bonding=$MIN_STAKING_BOND_OLAS
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" $password_argument
else
cost_of_bonding=$xdai_balance_required_to_bond
cost_of_bonding=$MIN_STAKING_BOND_XDAI
cmd="poetry run autonomy mint \
--retries $RPC_RETRIES \
--timeout $RPC_TIMEOUT_SECONDS \
Expand Down Expand Up @@ -1057,11 +1023,11 @@ if [ "$(get_on_chain_service_state "$service_id")" == "PRE_REGISTRATION" ]; then
echo "[Service owner] Activating registration for on-chain service $service_id..."
export cmd="poetry run autonomy service --retries $RPC_RETRIES --timeout $RPC_TIMEOUT_SECONDS --use-custom-chain activate --key "../$operator_pkey_path" $password_argument "$service_id""
if [ "${USE_STAKING}" = true ]; then
minimum_olas_balance=$($PYTHON_CMD -c "print(int($olas_balance_required_to_bond) + int($olas_balance_required_to_stake))")
minimum_olas_balance=$($PYTHON_CMD -c "print(int($MIN_STAKING_DEPOSIT_OLAS) + int($MIN_STAKING_BOND_OLAS))")
echo "Your service is using staking. Therefore, you need to provide a total of $(wei_to_dai "$minimum_olas_balance") OLAS to your owner/operator's address."
echo " $(wei_to_dai "$olas_balance_required_to_bond") OLAS for security deposit (service owner)"
echo " $(wei_to_dai "$MIN_STAKING_DEPOSIT_OLAS") OLAS for security deposit (service owner)"
echo " +"
echo " $(wei_to_dai "$olas_balance_required_to_stake") OLAS for slashable bond (operator)."
echo " $(wei_to_dai "$MIN_STAKING_BOND_OLAS") OLAS for slashable bond (operator)."
echo ""
ensure_erc20_balance "$operator_address" $minimum_olas_balance "owner/operator's address" $CUSTOM_OLAS_ADDRESS "OLAS"

Expand Down
Loading