Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 700b983

Browse files
Merge pull request #13 from valory-xyz/chore/check_funds
Update check balance
2 parents 927ecad + 8ad91c0 commit 700b983

File tree

1 file changed

+109
-51
lines changed

1 file changed

+109
-51
lines changed

run_service.sh

Lines changed: 109 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,97 @@
11
#!/bin/bash
22

3-
set -e # Exit script on first error
3+
# ------------------------------------------------------------------------------
4+
#
5+
# Copyright 2023 Valory AG
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# ------------------------------------------------------------------------------
20+
21+
# Convert Hex to Dec
22+
hex_to_decimal() {
23+
$PYTHON_CMD -c "print(int('$1', 16))"
24+
}
25+
26+
# Convert Wei to Dai
27+
wei_to_dai() {
28+
local wei="$1"
29+
local decimal_precision=4 # Change this to your desired precision
30+
local dai=$($PYTHON_CMD -c "print('%.${decimal_precision}f' % ($wei / 1000000000000000000.0))")
31+
echo "$dai"
32+
}
33+
34+
# Function to get the balance of an Ethereum address
35+
get_balance() {
36+
local address="$1"
37+
curl -s -S -X POST \
38+
-H "Content-Type: application/json" \
39+
--data "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBalance\",\"params\":[\"$address\",\"latest\"],\"id\":1}" "$rpc" | \
40+
$PYTHON_CMD -c "import sys, json; print(json.load(sys.stdin)['result'])"
41+
}
42+
43+
# Function to ensure a minimum balance for an Ethereum address
44+
ensure_minimum_balance() {
45+
local address="$1"
46+
local minimum_balance="$2"
47+
local message="$3"
48+
49+
balance_hex=$(get_balance "$address")
50+
balance=$(hex_to_decimal "$balance_hex")
51+
52+
echo "$message"
53+
echo " - Address: $address"
54+
echo " - Balance: $(wei_to_dai $balance) DAI"
55+
56+
local spin='-\|/'
57+
local i=0
58+
local cycle_count=0
59+
local waited=false
60+
while [ "$($PYTHON_CMD -c "print($balance < $minimum_balance)")" == "True" ]; do
61+
printf "\r Waiting... ${spin:$i:1} "
62+
i=$(( (i+1) %4 ))
63+
sleep .1
64+
65+
# This will be checked every 10 seconds (100 cycles).
66+
cycle_count=$((cycle_count + 1))
67+
if [ "$cycle_count" -eq 100 ]; then
68+
balance_hex=$(get_balance "$address")
69+
balance=$(hex_to_decimal "$balance_hex")
70+
cycle_count=0
71+
waited=true
72+
fi
73+
done
474

5-
echo "Starting the script..."
75+
if [ "$waited" = true ]; then
76+
printf "\r Waiting... \n"
77+
echo " - Updated balance: $(wei_to_dai $balance) DAI"
78+
fi
79+
80+
echo " OK."
81+
echo ""
82+
}
83+
84+
85+
# ------------------
86+
# Script starts here
87+
# ------------------
88+
89+
set -e # Exit script on first error
90+
echo "---------------"
91+
echo " Trader runner "
92+
echo "---------------"
93+
echo "This script will assist you in setting up and running the Trader service (https://github.com/valory-xyz/trader)."
94+
echo ""
695

796
# Check if user is inside a venv
897
if [[ "$VIRTUAL_ENV" != "" ]]
@@ -21,8 +110,8 @@ else
21110
exit 1
22111
fi
23112

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

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

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

@@ -130,6 +219,8 @@ export CUSTOM_GNOSIS_SAFE_MULTISIG_ADDRESS="0x3C1fF68f5aa342D296d4DEe4Bb1cACCA91
130219

131220
if [ "$first_run" = "true" ]
132221
then
222+
echo "This is the first run of the script. The script will generate new operator and agent instance addresses."
223+
echo ""
133224
# Generate the operator's key
134225
address_start_position=17
135226
pkey_start_position=21
@@ -138,8 +229,8 @@ then
138229
operator_address=$(sed -n 3p "../$keys_json_path")
139230
operator_address=$(echo "$operator_address" | \
140231
awk '{ print substr( $0, '$address_start_position', length($0) - '$address_start_position' - 1 ) }')
141-
printf "Your operator's autogenerated public address: %s
142-
The same address will be used as the service owner.\n" "$operator_address"
232+
echo "Your operator's autogenerated public address: $operator_address"
233+
echo "(The same address will be used as the service owner.)"
143234
operator_pkey=$(sed -n 4p "../$keys_json_path")
144235
operator_pkey_file="operator_pkey.txt"
145236
echo -n "$operator_pkey" | awk '{ printf substr( $0, '$pkey_start_position', length($0) - '$pkey_start_position' ) }' > $operator_pkey_file
@@ -156,32 +247,16 @@ then
156247
awk '{ print substr( $0, '$pkey_start_position', length($0) - '$pkey_start_position' ) }')
157248
echo "Your agent instance's autogenerated public address: $agent_address"
158249
echo -n "$agent_address" > "../$agent_address_path"
250+
echo ""
159251

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

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

186261
# create service
187262
agent_id=12
@@ -257,39 +332,22 @@ else
257332
echo "$deployment"
258333
fi
259334

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

267-
echo "Your service's safe address: $safe"
342+
echo "Your agent instance's address: $agent_address"
343+
echo "Your service's Safe address: $safe"
344+
echo ""
268345

269-
# Check the safe's balance
270-
get_balance() {
271-
curl -s -S -X POST \
272-
-H "Content-Type: application/json" \
273-
--data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["'"$SAFE_CONTRACT_ADDRESS"'","latest"],"id":1}' "$rpc" | \
274-
$PYTHON_CMD -c "import sys, json; print(json.load(sys.stdin)['result'])"
275-
}
276-
277-
convert_hex_to_decimal() {
278-
$PYTHON_CMD -c "print(int('$1', 16))"
279-
}
346+
suggested_amount=50000000000000000
347+
ensure_minimum_balance $agent_address $suggested_amount "Please, fund your agent instance's address with at least $(wei_to_dai $suggested_amount) DAI."
280348

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

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

0 commit comments

Comments
 (0)