-
Notifications
You must be signed in to change notification settings - Fork 21
/
terminate_on_chain_service.sh
executable file
·221 lines (189 loc) · 8.12 KB
/
terminate_on_chain_service.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
# ------------------------------------------------------------------------------
#
# Copyright 2023-2024 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.
#
# ------------------------------------------------------------------------------
# force utf mode for python, cause sometimes there are issues with local codepages
export PYTHONUTF8=1
# Get the address from a keys.json file
get_address() {
local keys_json_path="$1"
if [ ! -f "$keys_json_path" ]; then
echo "Error: $keys_json_path does not exist."
return 1
fi
local address_start_position=17
local address=$(sed -n 3p "$keys_json_path")
address=$(echo "$address" |
awk '{ print substr( $0, '$address_start_position', length($0) - '$address_start_position' - 1 ) }')
echo -n "$address"
}
# Get the private key from a keys.json file
get_private_key() {
local keys_json_path="$1"
if [ ! -f "$keys_json_path" ]; then
echo "Error: $keys_json_path does not exist."
return 1
fi
local private_key_start_position=21
local private_key=$(sed -n 4p "$keys_json_path")
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}')
private_key="${private_key#0x}"
echo -n "$private_key"
}
# Function to retrieve on-chain service state (requires env variables set to use --use-custom-chain)
get_on_chain_service_state() {
local service_id="$1"
local service_info=$(poetry run autonomy service --retries $RPC_RETRIES --timeout $RPC_TIMEOUT_SECONDS --use-custom-chain info "$service_id")
local state="$(echo "$service_info" | awk '/Service State/ {sub(/\|[ \t]*Service State[ \t]*\|[ \t]*/, ""); sub(/[ \t]*\|[ \t]*/, ""); print}')"
echo "$state"
}
# Asks password if key files are password-protected
ask_password_if_needed() {
agent_pkey=$(get_private_key "$keys_json_path")
if [[ "$agent_pkey" = *crypto* ]]; then
echo "Enter your password"
echo "-------------------"
echo "Your key files are protected with a password."
read -s -p "Please, enter your password: " password
use_password=true
password_argument="--password $password"
echo ""
else
echo "Your key files are not protected with a password."
use_password=false
password_argument=""
fi
echo ""
}
# Validates the provided password
validate_password() {
local is_password_valid_1=$(poetry run python ../scripts/is_keys_json_password_valid.py ../$keys_json_path $password_argument)
local is_password_valid_2=$(poetry run python ../scripts/is_keys_json_password_valid.py ../$operator_keys_file $password_argument)
if [ "$is_password_valid_1" != "True" ] || [ "$is_password_valid_2" != "True" ]; then
echo "Could not decrypt key files. Please verify if your key files are password-protected, and if the provided password is correct (passwords are case-sensitive)."
echo "Terminating the script."
exit 1
fi
}
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"
env_file_path="$store/.env"
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"
source "$env_file_path"
rpc=$(cat $rpc_path)
operator_address=$(get_address $operator_keys_file)
service_id=$(cat $service_id_path)
unstake=true
gnosis_chain_id=100
# Define constants for on-chain interaction
export RPC_RETRIES=40
export RPC_TIMEOUT_SECONDS=120
export CUSTOM_CHAIN_RPC=$rpc
export CUSTOM_CHAIN_ID=$gnosis_chain_id
export CUSTOM_SERVICE_MANAGER_ADDRESS="0x04b0007b2aFb398015B76e5f22993a1fddF83644"
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"
# Check if --attended flag is passed
export ATTENDED=true
for arg in "$@"; do
if [ "$arg" = "--attended=false" ]; then
export ATTENDED=false
fi
done
export_dotenv "$env_file_path"
set -e # Exit script on first error
echo "--------------------------"
echo "Terminate on-chain service"
echo "--------------------------"
echo ""
echo "This script will terminate and unbond your on-chain service (id $service_id)."
echo "If your service is staked, you will receive the staking funds to the owner/operator address:"
echo "$operator_address"
echo
response="y"
if [ "$attended" = true ]; then
echo "Please, ensure that your service is stopped (./stop_service.sh) before proceeding."
echo "Do you want to continue? (yes/no)"
read -r response
echo ""
fi
if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "Cancelled."
exit 0
fi
ask_password_if_needed
cd trader
validate_password
# Unstake if applicable
poetry run python "../scripts/staking.py" "$service_id" "$CUSTOM_SERVICE_REGISTRY_ADDRESS" "$CUSTOM_STAKING_ADDRESS" "../$operator_pkey_path" "$rpc" "$unstake";
service_safe_address=$(cat "../$service_safe_address_path")
current_safe_owners=$(poetry run python "../scripts/get_safe_owners.py" "$service_safe_address" "../$agent_pkey_path" "$rpc" $password_argument | awk '{gsub(/"/, "\047", $0); print $0}')
agent_address=$(get_address "../$keys_json_path")
# transfer the ownership of the Safe from the agent to the service owner
# (in a live service, this should be done by sending a 0 DAI transfer to its Safe)
if [[ "$current_safe_owners" == "['$agent_address']" ]]; then
echo "[Agent instance] Swapping Safe owner..."
poetry run python "../scripts/swap_safe_owner.py" "$service_safe_address" "../$agent_pkey_path" "$operator_address" "$rpc" $password_argument
fi
# terminate current service
state="$(get_on_chain_service_state "$service_id")"
if [ "$state" == "ACTIVE_REGISTRATION" ] || [ "$state" == "FINISHED_REGISTRATION" ] || [ "$state" == "DEPLOYED" ]; then
echo "[Service owner] Terminating on-chain service $service_id..."
poetry run autonomy service \
--retries $RPC_RETRIES \
--timeout $RPC_TIMEOUT_SECONDS \
--use-custom-chain \
terminate "$service_id" \
--key "../$operator_pkey_path" $password_argument
fi
# unbond current service
if [ "$(get_on_chain_service_state "$service_id")" == "TERMINATED_BONDED" ]; then
echo "[Operator] Unbonding on-chain service $service_id..."
poetry run autonomy service \
--retries $RPC_RETRIES \
--timeout $RPC_TIMEOUT_SECONDS \
--use-custom-chain \
unbond "$service_id" \
--key "../$operator_pkey_path" $password_argument
fi
if [ "$(get_on_chain_service_state "$service_id")" == "PRE_REGISTRATION" ]; then
echo "Service $service_id is now terminated and unbonded (i.e., it is on PRE-REGISTRATION state)."
echo "You can check this on https://registry.olas.network/gnosis/services/$service_id."
echo "In order to deploy your on-chain service again, please execute './run_service.sh'."
fi
echo "Finished."