From 87a6f43fe82eec51f9bf55081f04e93981742554 Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Mon, 4 Sep 2023 22:43:10 +0200 Subject: [PATCH 1/3] feat: Update check balance --- run_service.sh | 138 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 96 insertions(+), 42 deletions(-) diff --git a/run_service.sh b/run_service.sh index a75f9d75..007199b3 100755 --- a/run_service.sh +++ b/run_service.sh @@ -1,5 +1,91 @@ #!/bin/bash +# ------------------------------------------------------------------------------ +# +# 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 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)" + + 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 + + if [ "$waited" = true ]; then + printf "\r Waiting... \n" + echo " - Updated balance: $(wei_to_dai $balance)" + fi + + echo " OK." + echo "" +} + + +# ------------------ +# Script starts here +# ------------------ + set -e # Exit script on first error echo "Starting the script..." @@ -156,30 +242,15 @@ 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 + + ensure_minimum_balance $operator_address $suggested_amount "Please, ensure the Operator's balance is at least $(wei_to_dai $suggested_amount)." + ensure_minimum_balance $agent_address $suggested_amount "Please, ensure your Agent instance's balance is at least $(wei_to_dai $suggested_amount)." echo "Minting your service on Gnosis chain..." @@ -264,32 +335,15 @@ 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, ensure your Agent instance's balance is at least $(wei_to_dai $suggested_amount)." 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, ensure your service Safe's balance is at least $(wei_to_dai $suggested_amount)." # Set environment variables. Tweak these to modify your strategy export RPC_0="$rpc" From c76a64e98fe7324e47a82570b5dab446d58eaee8 Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Tue, 5 Sep 2023 09:18:21 +0200 Subject: [PATCH 2/3] chore: Remove redundant check, minor cosmetic changes --- run_service.sh | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/run_service.sh b/run_service.sh index 007199b3..eee39f49 100755 --- a/run_service.sh +++ b/run_service.sh @@ -28,7 +28,7 @@ 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 DAI" + echo "$dai" } # Function to get the balance of an Ethereum address @@ -51,7 +51,7 @@ ensure_minimum_balance() { echo "$message" echo " - Address: $address" - echo " - Balance: $(wei_to_dai $balance)" + echo " - Balance: $(wei_to_dai $balance) DAI" local spin='-\|/' local i=0 @@ -74,7 +74,7 @@ ensure_minimum_balance() { if [ "$waited" = true ]; then printf "\r Waiting... \n" - echo " - Updated balance: $(wei_to_dai $balance)" + echo " - Updated balance: $(wei_to_dai $balance) DAI" fi echo " OK." @@ -87,8 +87,11 @@ ensure_minimum_balance() { # ------------------ set -e # Exit script on first error - -echo "Starting the script..." +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" != "" ]] @@ -128,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 } @@ -216,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 @@ -224,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 @@ -249,10 +254,9 @@ then operator_balance=0 suggested_amount=50000000000000000 - ensure_minimum_balance $operator_address $suggested_amount "Please, ensure the Operator's balance is at least $(wei_to_dai $suggested_amount)." - ensure_minimum_balance $agent_address $suggested_amount "Please, ensure your Agent instance's balance is at least $(wei_to_dai $suggested_amount)." + ensure_minimum_balance $operator_address $suggested_amount "Please, fund the operator's address with at least $(wei_to_dai $suggested_amount) DAI." - echo "Minting your service on Gnosis chain..." + echo "Minting your service on the Gnosis chain..." # create service agent_id=12 @@ -328,22 +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 Agent instance's address: $agent_address" +echo "Your agent instance's address: $agent_address" echo "Your service's Safe address: $safe" echo "" suggested_amount=50000000000000000 -ensure_minimum_balance $agent_address $suggested_amount "Please, ensure your Agent instance's balance is at least $(wei_to_dai $suggested_amount)." +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 -ensure_minimum_balance $SAFE_CONTRACT_ADDRESS $suggested_amount "Please, ensure your service Safe's balance is at least $(wei_to_dai $suggested_amount)." +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" From a76af63d1b5fdc8a634e1e94201dbcdab1116b8e Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Tue, 5 Sep 2023 09:54:05 +0200 Subject: [PATCH 3/3] fix: Python version check --- run_service.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/run_service.sh b/run_service.sh index eee39f49..16219241 100755 --- a/run_service.sh +++ b/run_service.sh @@ -110,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