Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
Merged
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
127 changes: 56 additions & 71 deletions run_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ get_private_key() {
private_key=$(echo -n "$private_key" |
awk '{ printf substr( $0, '$private_key_start_position', length($0) - '$private_key_start_position' ) }')

private_key=$(echo -n "$private_key" | awk '{gsub(/\\"/, "\"", $0); print $0}')
Copy link
Collaborator Author

@jmoreira-valory jmoreira-valory Oct 19, 2023

Choose a reason for hiding this comment

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

This is required to replace \" strings to "
(this is required in PR 55, it is irrelevant for non-encrypted keys)

private_key="${private_key#0x}"

echo -n "$private_key"
}

Expand Down Expand Up @@ -187,69 +190,75 @@ get_on_chain_service_state() {
store=".trader_runner"
rpc_path="$store/rpc.txt"
operator_keys_file="$store/operator_keys.json"
operator_pkey_path="$store/operator_pkey.txt"
keys_json="keys.json"
keys_json_path="$store/$keys_json"
agent_pkey_path="$store/agent_pkey.txt"
agent_address_path="$store/agent_address.txt"
service_id_path="$store/service_id.txt"
service_safe_address_path="$store/service_safe_address.txt"
store_readme_path="$store/README.txt"

# Function to create the .trader_runner storage
create_storage() {
local rpc="$1"

echo "This is the first run of the script. The script will generate new operator and agent instance addresses."
echo ""

mkdir "../$store"

# Generate README.txt file
echo -e 'IMPORTANT:\n\n' \
' This folder contains crucial configuration information and autogenerated keys for your Trader agent.\n' \
' Please back up this folder and be cautious if you are modifying or sharing these files to avoid potential asset loss.' > "../$store_readme_path"

# Generate the RPC file
echo -n "$rpc" > "../$rpc_path"

# Generate the operator's key
address_start_position=17
pkey_start_position=21
poetry run autonomy generate-key -n1 ethereum
mv "$keys_json" "../$keys_json_path"
operator_address=$(sed -n 3p "../$keys_json_path")
operator_address=$(echo "$operator_address" | \
awk '{ print substr( $0, '$address_start_position', length($0) - '$address_start_position' - 1 ) }')
mv "$keys_json" "../$operator_keys_file"
operator_address=$(get_address "../$operator_keys_file")
operator_pkey=$(get_private_key "../$operator_keys_file")
echo -n "$operator_pkey" > "../$operator_pkey_path"
echo "Your operator's autogenerated public address: $operator_address"
echo "(The same address will be used as the service owner.)"
operator_pkey=$(sed -n 4p "../$keys_json_path")
operator_pkey_file="operator_pkey.txt"
echo -n "$operator_pkey" | awk '{ printf substr( $0, '$pkey_start_position', length($0) - '$pkey_start_position' ) }' > $operator_pkey_file
mv "../$keys_json_path" "../$operator_keys_file"

# Generate the agent's key
poetry run autonomy generate-key -n1 ethereum
mv "$keys_json" "../$keys_json_path"
agent_address=$(sed -n 3p "../$keys_json_path")
agent_address=$(echo "$agent_address" | \
awk '{ print substr( $0, '$address_start_position', length($0) - '$address_start_position' - 1 ) }')
private_key=$(sed -n 4p "../$keys_json_path")
private_key=$(echo "$private_key" | \
awk '{ print substr( $0, '$pkey_start_position', length($0) - '$pkey_start_position' ) }')
echo "Your agent instance's autogenerated public address: $agent_address"
agent_address=$(get_address "../$keys_json_path")
agent_pkey=$(get_private_key "../$keys_json_path")
echo -n "$agent_pkey" > "../$agent_pkey_path"
echo -n "$agent_address" > "../$agent_address_path"
echo "Your agent instance's autogenerated public address: $agent_address"
echo ""

# generate private key files in the format required by the CLI tool
agent_pkey_file="agent_pkey.txt"
agent_pkey=$(get_private_key "../$keys_json_path")
agent_pkey="${agent_pkey#0x}"
echo -n "$agent_pkey" >"$agent_pkey_file"

operator_pkey_file="operator_pkey.txt"
operator_pkey=$(get_private_key "../$operator_keys_file")
operator_pkey="${operator_pkey#0x}"
echo -n "$operator_pkey" >"$operator_pkey_file"
}

# Function to read and load the .trader_runner storage information if it exists.
# Also sets `first_run` flag to identify whether we are running the script for the first time.
try_read_storage() {
if [ -d $store ]; then

# INFO: This is a fix to avoid corrupting already-created stores
if [[ -f "$operator_keys_file" && ! -f "$operator_pkey_path" ]]; then
operator_pkey=$(get_private_key "$operator_keys_file")
echo -n "$operator_pkey" > "$operator_pkey_path"
fi

# INFO: This is a fix to avoid corrupting already-created stores
if [[ -f "$keys_json_path" && ! -f "$agent_pkey_path" ]]; then
agent_pkey=$(get_private_key "$keys_json_path")
echo -n "$agent_pkey" > "$agent_pkey_path"
fi

first_run=false
paths="$rpc_path $operator_keys_file $keys_json_path $agent_address_path $service_id_path"
paths="$rpc_path $operator_keys_file $operator_pkey_path $keys_json_path $agent_address_path $agent_pkey_path $service_id_path"

for file in $paths; do
if ! [ -f "$file" ]; then
if [ "$file" == $service_id_path ]; then
first_run=true
elif [ "$file" != $service_safe_address_path ]; then
if [ "$file" != $service_safe_address_path ] && [ "$file" != $service_id_path ]; then
echo "The runner's store is corrupted!"
echo "Please manually investigate the $store folder"
echo "Make sure that you do not lose your keys or any other important information!"
Expand All @@ -260,14 +269,12 @@ try_read_storage() {

rpc=$(cat $rpc_path)
agent_address=$(cat $agent_address_path)
service_id=$(cat $service_id_path)
operator_address=$(get_address "$operator_keys_file")
if [ -f "$service_id_path" ]; then
service_id=$(cat $service_id_path)
fi
else
first_run=true
mkdir "$store"

echo -e 'IMPORTANT:\n\n' \
' This folder contains crucial configuration information and autogenerated keys for your Trader agent.\n' \
' Please back up this folder and be cautious if you are modifying or sharing these files to avoid potential asset loss.' > "$store_readme_path"
fi
}

Expand Down Expand Up @@ -341,8 +348,6 @@ if [ "$new_filter_supported" = True ]
then
echo "The given RPC ($rpc) does not support 'eth_newFilter'! Terminating script..."
exit 1
else
echo -n "$rpc" > $rpc_path
fi

# clone repo
Expand Down Expand Up @@ -374,7 +379,7 @@ fi

if [ "$first_run" = "true" ]
then
create_storage
create_storage "$rpc"
fi

echo ""
Expand All @@ -396,7 +401,7 @@ export CUSTOM_GNOSIS_SAFE_SAME_ADDRESS_MULTISIG_ADDRESS="0x3d77596beb0f130a4415d
export CUSTOM_MULTISEND_ADDRESS="0x40A2aCCbd92BCA938b02010E17A5b8929b49130D"
export AGENT_ID=12

if [ "$first_run" = "true" ]
if [ -z ${service_id+x} ];
then
# Check balances
suggested_amount=50000000000000000
Expand All @@ -411,7 +416,7 @@ then
--skip-hash-check \
--use-custom-chain \
service packages/valory/services/$directory/ \
--key "$operator_pkey_file" \
--key "../$operator_pkey_path" \
--nft $nft \
-a $AGENT_ID \
-n $n_agents \
Expand Down Expand Up @@ -470,11 +475,9 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
# (in a live service, this should be done by sending a 0 DAI transfer to its Safe)
service_safe_address=$(<"../$service_safe_address_path")
echo "[Agent instance] Swapping Safe owner..."
output=$(poetry run python "../scripts/swap_safe_owner.py" "$service_safe_address" "$agent_pkey_file" "$operator_address" "$rpc")
output=$(poetry run python "../scripts/swap_safe_owner.py" "$service_safe_address" "../$agent_pkey_path" "$operator_address" "$rpc")
if [[ $? -ne 0 ]]; then
echo "Swapping Safe owner failed.\n$output"
rm -f $agent_pkey_file
rm -f $operator_pkey_file
exit 1
fi
echo "$output"
Expand All @@ -487,13 +490,11 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
poetry run autonomy service \
--use-custom-chain \
terminate "$service_id" \
--key "$operator_pkey_file"
--key "../$operator_pkey_path"
)
if [[ $? -ne 0 ]]; then
echo "Terminating service failed.\n$output"
echo "Please, delete or rename the ./trader folder and try re-run this script again."
rm -f $agent_pkey_file
rm -f $operator_pkey_file
exit 1
fi
fi
Expand All @@ -505,13 +506,11 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
poetry run autonomy service \
--use-custom-chain \
unbond "$service_id" \
--key "$operator_pkey_file"
--key "../$operator_pkey_path"
)
if [[ $? -ne 0 ]]; then
echo "Unbonding service failed.\n$output"
echo "Please, delete or rename the ./trader folder and try re-run this script again."
rm -f $agent_pkey_file
rm -f $operator_pkey_file
exit 1
fi
fi
Expand All @@ -526,7 +525,7 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
--skip-hash-check \
--use-custom-chain \
service packages/valory/services/trader/ \
--key "$operator_pkey_file" \
--key "../$operator_pkey_path" \
--nft $nft \
-a $AGENT_ID \
-n $n_agents \
Expand All @@ -537,8 +536,6 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
if [[ $? -ne 0 ]]; then
echo "Updating service failed.\n$output"
echo "Please, delete or rename the ./trader folder and try re-run this script again."
rm -f $agent_pkey_file
rm -f $operator_pkey_file
exit 1
fi
fi
Expand All @@ -558,25 +555,21 @@ fi
# activate service
if [ "$(get_on_chain_service_state "$service_id")" == "PRE_REGISTRATION" ]; then
echo "[Service owner] Activating registration for on-chain service $service_id..."
output=$(poetry run autonomy service --use-custom-chain activate --key "$operator_pkey_file" "$service_id")
output=$(poetry run autonomy service --use-custom-chain activate --key "../$operator_pkey_path" "$service_id")
if [[ $? -ne 0 ]]; then
echo "Activating service failed.\n$output"
echo "Please, delete or rename the ./trader folder and try re-run this script again."
rm -f $agent_pkey_file
rm -f $operator_pkey_file
exit 1
fi
fi

# register agent instance
if [ "$(get_on_chain_service_state "$service_id")" == "ACTIVE_REGISTRATION" ]; then
echo "[Operator] Registering agent instance for on-chain service $service_id..."
output=$(poetry run autonomy service --use-custom-chain register --key "$operator_pkey_file" "$service_id" -a $AGENT_ID -i "$agent_address")
output=$(poetry run autonomy service --use-custom-chain register --key "../$operator_pkey_path" "$service_id" -a $AGENT_ID -i "$agent_address")
if [[ $? -ne 0 ]]; then
echo "Registering agent instance failed.\n$output"
echo "Please, delete or rename the ./trader folder and try re-run this script again."
rm -f $agent_pkey_file
rm -f $operator_pkey_file
exit 1
fi
fi
Expand All @@ -585,30 +578,22 @@ fi
service_state="$(get_on_chain_service_state "$service_id")"
if [ "$service_state" == "FINISHED_REGISTRATION" ] && [ "$first_run" = "true" ]; then
echo "[Service owner] Deploying on-chain service $service_id..."
output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "$operator_pkey_file")
output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "../$operator_pkey_path")
if [[ $? -ne 0 ]]; then
echo "Deploying service failed.\n$output"
echo "Please, delete or rename the ./trader folder and try re-run this script again."
rm -f $agent_pkey_file
rm -f $operator_pkey_file
exit 1
fi
elif [ "$service_state" == "FINISHED_REGISTRATION" ]; then
echo "[Service owner] Deploying on-chain service $service_id..."
output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "$operator_pkey_file" --reuse-multisig)
output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "../$operator_pkey_path" --reuse-multisig)
if [[ $? -ne 0 ]]; then
echo "Deploying service failed.\n$output"
echo "Please, delete or rename the ./trader folder and try re-run this script again."
rm -f $agent_pkey_file
rm -f $operator_pkey_file
exit 1
fi
fi

# delete the pkey files
rm -f $agent_pkey_file
rm -f $operator_pkey_file

# check state
service_state="$(get_on_chain_service_state "$service_id")"
if [ "$service_state" != "DEPLOYED" ]; then
Expand Down