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
160 changes: 109 additions & 51 deletions run_service.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,97 @@
#!/bin/bash

set -e # Exit script on first error
# ------------------------------------------------------------------------------
#
# 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.
#
# ------------------------------------------------------------------------------

# Convert Hex to Dec
hex_to_decimal() {
$PYTHON_CMD -c "print(int('$1', 16))"
}

# Convert Wei to Dai
wei_to_dai() {
local wei="$1"
local decimal_precision=4 # Change this to your desired precision
local dai=$($PYTHON_CMD -c "print('%.${decimal_precision}f' % ($wei / 1000000000000000000.0))")
echo "$dai"
}

# Function to get the balance of an Ethereum address
get_balance() {
local address="$1"
curl -s -S -X POST \
-H "Content-Type: application/json" \
--data "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBalance\",\"params\":[\"$address\",\"latest\"],\"id\":1}" "$rpc" | \
$PYTHON_CMD -c "import sys, json; print(json.load(sys.stdin)['result'])"
}

# Function to ensure a minimum balance for an Ethereum address
ensure_minimum_balance() {
local address="$1"
local minimum_balance="$2"
local message="$3"

balance_hex=$(get_balance "$address")
balance=$(hex_to_decimal "$balance_hex")

echo "$message"
echo " - Address: $address"
echo " - Balance: $(wei_to_dai $balance) DAI"

local spin='-\|/'
local i=0
local cycle_count=0
local waited=false
while [ "$($PYTHON_CMD -c "print($balance < $minimum_balance)")" == "True" ]; do
printf "\r Waiting... ${spin:$i:1} "
i=$(( (i+1) %4 ))
sleep .1

# This will be checked every 10 seconds (100 cycles).
cycle_count=$((cycle_count + 1))
if [ "$cycle_count" -eq 100 ]; then
balance_hex=$(get_balance "$address")
balance=$(hex_to_decimal "$balance_hex")
cycle_count=0
waited=true
fi
done

echo "Starting the script..."
if [ "$waited" = true ]; then
printf "\r Waiting... \n"
echo " - Updated balance: $(wei_to_dai $balance) DAI"
fi

echo " OK."
echo ""
}


# ------------------
# Script starts here
# ------------------

set -e # Exit script on first error
echo "---------------"
echo " Trader runner "
echo "---------------"
echo "This script will assist you in setting up and running the Trader service (https://github.com/valory-xyz/trader)."
echo ""

# Check if user is inside a venv
if [[ "$VIRTUAL_ENV" != "" ]]
Expand All @@ -21,8 +110,8 @@ else
exit 1
fi

if [[ "$($PYTHON_CMD --version 2>&1)" != "Python 3.10."* ]]; then
echo >&2 "Python version 3.10.* is required but found $($PYTHON_CMD --version 2>&1)";
if [[ "$($PYTHON_CMD --version 2>&1)" != "Python 3.10."* ]] && [[ "$($PYTHON_CMD --version 2>&1)" != "Python 3.11."* ]]; then
echo >&2 "Python version >=3.10.0, <3.12.0 is required but found $($PYTHON_CMD --version 2>&1)";
exit 1
fi

Expand All @@ -42,7 +131,7 @@ command -v docker >/dev/null 2>&1 ||
}

docker rm -f abci0 node0 trader_abci_0 trader_tm_0 &> /dev/null ||
{ echo >&2 "Please make sure Docker is running.";
{ echo >&2 "Docker is not running!";
exit 1
}

Expand Down Expand Up @@ -130,6 +219,8 @@ export CUSTOM_GNOSIS_SAFE_MULTISIG_ADDRESS="0x3C1fF68f5aa342D296d4DEe4Bb1cACCA91

if [ "$first_run" = "true" ]
then
echo "This is the first run of the script. The script will generate new operator and agent instance addresses."
echo ""
# Generate the operator's key
address_start_position=17
pkey_start_position=21
Expand All @@ -138,8 +229,8 @@ then
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 ) }')
printf "Your operator's autogenerated public address: %s
The same address will be used as the service owner.\n" "$operator_address"
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
Expand All @@ -156,32 +247,16 @@ then
awk '{ print substr( $0, '$pkey_start_position', length($0) - '$pkey_start_position' ) }')
echo "Your agent instance's autogenerated public address: $agent_address"
echo -n "$agent_address" > "../$agent_address_path"
echo ""

# Check balances
agent_balance=0
operator_balance=0
suggested_amount=50000000000000000
until [[ $($PYTHON_CMD -c "print($agent_balance > ($suggested_amount-1))") == "True" && $($PYTHON_CMD -c "print($operator_balance > ($suggested_amount-1))") == "True" ]];
do
echo "Agent instance's address: $agent_address. Balance: $agent_balance WEI."
echo "Operator's address: $operator_address. Balance: $operator_balance WEI."
echo "Both of the addresses need to be funded to cover gas costs."
echo "Please fund them with at least 0.05 xDAI each to continue."
echo "Checking again in 10s..."
sleep 10
agent_balance=$(curl -s -S -X POST \
-H "Content-Type: application/json" \
--data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["'"$agent_address"'","latest"],"id":1}' "$rpc" | \
$PYTHON_CMD -c "import sys, json; print(json.load(sys.stdin)['result'])")
operator_balance=$(curl -s -S -X POST \
-H "Content-Type: application/json" \
--data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["'"$operator_address"'","latest"],"id":1}' "$rpc" | \
$PYTHON_CMD -c "import sys, json; print(json.load(sys.stdin)['result'])")
agent_balance=$((16#${agent_balance#??}))
operator_balance=$((16#${operator_balance#??}))
done

echo "Minting your service on Gnosis chain..."
ensure_minimum_balance $operator_address $suggested_amount "Please, fund the operator's address with at least $(wei_to_dai $suggested_amount) DAI."

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Adamantios Here I removed the check for the minimum balance of the agent instance address.

echo "Minting your service on the Gnosis chain..."

# create service
agent_id=12
Expand Down Expand Up @@ -257,39 +332,22 @@ else
echo "$deployment"
fi

# get the deployed service's safe address from the contract
# Get the deployed service's Safe address from the contract
safe=$(echo "$service_info" | grep "Multisig Address")
address_start_position=31
safe=$(echo "$safe" |
awk '{ print substr( $0, '$address_start_position', length($0) - '$address_start_position' - 3 ) }')
export SAFE_CONTRACT_ADDRESS=$safe

echo "Your service's safe address: $safe"
echo "Your agent instance's address: $agent_address"
echo "Your service's Safe address: $safe"
echo ""

# Check the safe's balance
get_balance() {
curl -s -S -X POST \
-H "Content-Type: application/json" \
--data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["'"$SAFE_CONTRACT_ADDRESS"'","latest"],"id":1}' "$rpc" | \
$PYTHON_CMD -c "import sys, json; print(json.load(sys.stdin)['result'])"
}

convert_hex_to_decimal() {
$PYTHON_CMD -c "print(int('$1', 16))"
}
suggested_amount=50000000000000000
ensure_minimum_balance $agent_address $suggested_amount "Please, fund your agent instance's address with at least $(wei_to_dai $suggested_amount) DAI."

suggested_amount=500000000000000000
safe_balance_hex=$(get_balance)
safe_balance=$(convert_hex_to_decimal $safe_balance_hex)
while [ "$($PYTHON_CMD -c "print($safe_balance < $suggested_amount)")" == "True" ]; do
echo "Safe's balance: $safe_balance WEI."
echo "The safe address $safe needs to be funded."
echo "Please fund it with the amount you want to use for trading (at least 0.5 xDAI) to continue."
echo "Checking again in 10s..."
sleep 10
safe_balance_hex=$(get_balance)
safe_balance=$(convert_hex_to_decimal $safe_balance_hex)
done
ensure_minimum_balance $SAFE_CONTRACT_ADDRESS $suggested_amount "Please, fund your service Safe's address with at least $(wei_to_dai $suggested_amount) DAI."

# Set environment variables. Tweak these to modify your strategy
export RPC_0="$rpc"
Expand Down