forked from near-guildnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stakingSeatBot.sh
448 lines (386 loc) · 14.8 KB
/
stakingSeatBot.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/bin/bash
# This script will manage your validators stake.
PATH="/usr/local/bin:/usr/bin:/bin"
PATH=$PATH
#############################################
# BEGIN USER SETTINGS #
#############################################
# Only guildnet has been tested
NETWORK="guildnet"
# Network will autopopulate with the above value
POOL_ID="pool.stake.$NETWORK"
ACCOUNT_ID="account.$NETWORK"
# Enter a NUMBER
NUM_SEATS_TO_OCCUPY=1
# Set Enable Email to 1 to enable email notifications and fill in the blanks
ENABLE_EMAIL=0
FROM_ADDRESS=
TO_ADDRESS=
# Number of missed blocks before an email is sent
ALERT_MISSING_BLOCKS=10
SEAT_PRICE_BUFFER=1000
TRANSACTION_MINIMUM=1000
# Descriptive Output=1
# No Output=0
DEBUG_MIN=1
#############################################
# END USER SETTINGS #
#############################################
# Epoch Lengths
GUILDNET_EPOCH_LEN=5000
BETANET_EPOCH_LEN=10000
TESTNET_EPOCH_LEN=43200
MAINET_EPOCH_LEN=43200
# Additional Script Configuration Don't Change
ADD0=000000000000000000000000
COMMA=","
#DOUBLE_QUOTE="\""
DEBUG_ALL=0
CURRENTDIR=$(pwd | tail -n 1)
# Export the network to the environment
export NEAR_ENV=$NETWORK
# Select the correct RPC server for the network
case $NETWORK in
guildnet)
HOST="https://rpc.openshards.io"
;;
mainnet)
HOST="https://rpc.near.org"
;;
testnet)
HOST="https://rpc.testnet.near.org/"
;;
betanet)
HOST="https://rpc.betanet.near.org/"
;;
*)
;;
esac
echo "Starting Script"
echo "---------------"
# Ensure user has configured the script
if [ "$POOL_ID" = "pool.stake.$NETWORK" ] || [ "$ACCOUNT_ID" = "account.$NETWORK" ]
then
echo "You have not properly configured this script. Please edit the file and replace every instance of ??? with valid data"
exit
fi
if [ "$POOL_ID" != pool.stake."$NETWORK" ] && [ "$ACCOUNT_ID" != account."$NETWORK" ] && [ "$DEBUG_MIN" == "1" ]
then
echo "The script is configured"
fi
PUBLIC_KEY=$(near view "$POOL_ID" get_staking_key {} | tail -n 1)
if [ ! -z "$PUBLIC_KEY" ] && [ "$DEBUG_ALL" == "1" ]
then
echo 'The public key for $POOL_ID is: $PUBLIC_KEY'
fi
if [ "$DEBUG_MIN" == "1" ]
then
echo "We have the key"
fi
VALIDATORS=$(curl -s -d '{"jsonrpc": "2.0", "method": "validators", "id": "dontcare", "params": [null]}' -H 'Content-Type: application/json' $HOST )
if [ -z "$VALIDATORS" ]
then
echo "Unable to retrieve Validator data: exiting..."
exit
fi
if [ ! -z "$VALIDATORS" ] && [ "$DEBUG_ALL" == "1" ]
then
echo "Validator Info: $VALIDATORS"
fi
if [ ! -z "$VALIDATORS" ] && [ "$DEBUG_MIN" == "1" ]
then
echo "Validator Info Received"
fi
STATUS_VAR="/status"
STATUS=$(curl -s "$HOST$STATUS_VAR")
if [ -z "$STATUS" ]
then
echo "Unable to retrieve Status: exiting..."
exit
fi
if [ ! -z "$STATUS" ] && [ "$DEBUG_ALL" == "1" ]
then
echo "STATUS: $STATUS"
fi
if [ ! -z "$STATUS" ] && [ "$DEBUG_MIN" == "1" ]
then
echo "We have the network status"
fi
EPOCH_START=$(echo "$VALIDATORS" | jq .result.epoch_start_height)
if [ ! -z "$EPOCH_START" ] && [ "$DEBUG_MIN" == "1" ]
then
echo "Epoch start: $EPOCH_START"
fi
if [ ! -z "$LAST_BLOCK" ] && [ "DEBUG_ALL" == "1" ]
then
echo "Epoch start: $EPOCH_START"
fi
LAST_BLOCK=$(echo "$STATUS" | jq .sync_info.latest_block_height)
if [ ! -z "$LAST_BLOCK" ] && [ "$DEBUG_MIN" == "1" ]
then
echo "Last Block: $LAST_BLOCK"
fi
if [ ! -z "$LAST_BLOCK" ] && [ "DEBUG_ALL" == "1" ]
then
echo "Last Block: $LAST_BLOCK"
fi
# Calculate blocks and time remaining in epoch based on the network selected
BLOCKS_COMPLETED=$((LAST_BLOCK - EPOCH_START))
case $NETWORK in
guildnet)
BLOCKS_REMAINING=$((BLOCKS_COMPLETED - GUILDNET_EPOCH_LEN))
EPOCH_MINS_REMAINING=$((BLOCKS_REMAINING / 60))
;;
mainnet)
BLOCKS_REMAINING=$((BLOCKS_COMPLETED - MAINET_EPOCH_LEN))
EPOCH_MINS_REMAINING=$((BLOCKS_REMAINING / 60))
;;
testnet)
BLOCKS_REMAINING=$((BLOCKS_COMPLETED - TESTNET_EPOCH_LEN))
EPOCH_MINS_REMAINING=$((BLOCKS_REMAINING / 60))
;;
betanet)
BLOCKS_REMAINING=$((BLOCKS_COMPLETED - BETANET_EPOCH_LEN))
EPOCH_MINS_REMAINING=$((BLOCKS_REMAINING / 60))
;;
*)
;;
esac
if [ ! -z "$BLOCKS_COMPLETED" ] && [ "$DEBUG_MIN" == "1" ]
then
echo "Blocks Completed: $BLOCKS_COMPLETED"
echo "Blocks Remaining: $BLOCKS_REMAINING"
echo "Epoch Minutes Remaining: $EPOCH_MINS_REMAINING"
fi
CURRENT_STAKE_S=$(echo "$VALIDATORS" | jq -c ".result.current_validators[] | select(.account_id | contains (\"$POOL_ID\"))" | jq .stake)
CURRENT_STAKE_L=(${CURRENT_STAKE_S//\"/})
CURRENT_STAKE="${CURRENT_STAKE_L%????????????????????????}"
if [ -z "$CURRENT_STAKE_S" ]
then
echo "$POOL_ID is not listed in the proposals for the current epoch"
fi
if [ -z "$CURRENT_STAKE_S" ] && [ "$DEBUG_MIN" == "1" ] || [ "$DEBUG_ALL" == "1" ]
then
echo "Current Stake: $CURRENT_STAKE"
echo "Current Stake_S: $CURRENT_STAKE_S"
echo "Current Stake_L: $CURRENT_STAKE_L"
fi
VALIDATOR_NEXT_STAKE_S=$(echo "$VALIDATORS" | jq -c ".result.next_validators[] | select(.account_id | contains (\"$POOL_ID\"))" | jq .stake)
VALIDATOR_NEXT_STAKE_L=(${VALIDATOR_NEXT_STAKE_S//\"/})
VALIDATOR_NEXT_STAKE="${VALIDATOR_NEXT_STAKE_L%????????????????????????}"
if [ -z "$VALIDATOR_NEXT_STAKE" ]
then
echo "$POOL_ID is not listed in the proposals for the next epoch"
fi
if [ "$DEBUG_MIN" == "1" ] || [ "$DEBUG_ALL" == "1" ] && [ ! -z "$VALIDATOR_NEXT_STAKE" ]
then
echo "Next Stake: $VALIDATOR_NEXT_STAKE"
echo "Next Stake S: $VALIDATOR_NEXT_STAKE_S"
echo "Next Stake Long: $VALIDATOR_NEXT_STAKE_L"
fi
# Proposal for epoch +2
OUR_PROPOSAL=$(echo "$VALIDATORS" | jq -c ".result.current_proposals[] | select(.account_id | contains (\"$POOL_ID\"))" | jq .stake)
PROPOSAL_STAKE_S=$(echo "$VALIDATORS" | jq -c ".result.current_proposals[] | select(.account_id | contains (\"$POOL_ID\"))" | jq .stake)
PROPOSAL_STAKE=${PROPOSAL_STAKE_S//\"/}
PROPOSAL_STAKE="${PROPOSAL_STAKE_S%?????????????????????????}"
if [[ -z "$OUR_PROPOSAL" ]]
then
echo "We dont have a proposal sending a ping"
PING_COMMAND=$(near call $POOL_ID ping "{}" --accountId $ACCOUNT_ID)
echo "$PING_COMMAND"
exit
fi
OUR_PROPOSAL_S=$(echo "$OUR_PROPOSAL" | sed 's/[^0-9]*//g')
PROPOSAL_STAKE=$(echo "$PROPOSAL_STAKE" | sed 's/[^0-9]*//g')
if [ ! -z "$PROPOSAL_STAKE" ] && [ "$DEBUG_MIN" == "1" ]
then
echo "Our Proposal: $OUR_PROPOSAL"
echo "Our Proposal_S: $OUR_PROPOSAL_S"
echo "Proposal Stake: $PROPOSAL_STAKE"
fi
if [ ! -z "$PROPOSAL_STAKE" ] && [ "$DEBUG_ALL" == "1" ]
then
echo "$VALIDATORS" | jq -c ".result.current_proposals[]"
fi
PROPOSAL_REASON=$(echo "$VALIDATORS" | jq -c ".result.current_proposals[] | select(.account_id | contains (\"$POOL_ID\"))" | jq .reason)
if [ ! -z "$PROPOSAL_REASON" ] && [ "$DEBUG_ALL" == "1" ]
then
echo Proposal Reason: "$PROPOSAL_REASON"
fi
# Current Epoch Seat Price
CURRENT_SEAT_PRICE=$(near validators current | awk '/price/ {print substr($6, 1, length($6)-2)}')
CURRENT_SEAT_PRICE="${CURRENT_SEAT_PRICE/$COMMA/}"
CURRENT_SEAT_PRICE=$((CURRENT_SEAT_PRICE+SEAT_PRICE_BUFFER))
if [ -z "$CURRENT_SEAT_PRICE" ]
then
echo "Unable to get the current seat price: exiting..."
fi
if [ "$DEBUG_ALL" == "1" ] || [ "$DEBUG_MIN" == "1" ] && [ ! -z "$CURRENT_SEAT_PRICE" ]
then
echo "Current Epoch Seat Price: $CURRENT_SEAT_PRICE"
fi
# Next Epoch Seat Price
SEAT_PRICE_NEXT=$(near validators next | awk '/price/ {print substr($7, 1, length($7)-2)}')
SEAT_PRICE_NEXT="${SEAT_PRICE_NEXT/$COMMA/}"
SEAT_PRICE_NEXT=$((SEAT_PRICE_NEXT * NUM_SEATS_TO_OCCUPY))
if [ -z "$SEAT_PRICE_NEXT" ]
then
echo "Unable to get the next epoch seat price: exiting..."
fi
if [ "$DEBUG_MIN" == "1" ] || [ "$DEBUG_ALL" == "1" ] && [ ! -z "$SEAT_PRICE_NEXT" ]
then
echo "Next Epoch Seat Price: $SEAT_PRICE_NEXT"
fi
SEAT_PRICE_PROPOSALS=$(near proposals | awk '/price =/ {print substr($15, 1, length($15)-1)}')
SEAT_PRICE_PROPOSALS="${SEAT_PRICE_PROPOSALS/$COMMA/}"
SEAT_PRICE_PROPOSALS=$((SEAT_PRICE_PROPOSALS * NUM_SEATS_TO_OCCUPY))
SEAT_PRICE_PROPOSALS=$((SEAT_PRICE_PROPOSALS + SEAT_PRICE_BUFFER))
if [ "$DEBUG_MIN" == "1" ] || [ "$DEBUG_ALL" == "1" ]
then
echo "Seat Price Proposals: $SEAT_PRICE_PROPOSALS"
fi
PRODUCED_BLOCKS=$(curl -s -d '{"jsonrpc": "2.0", "method": "validators", "id": "dontcare", "params": [null]}' -H 'Content-Type: application/json' "$HOST" | jq -c ".result.current_validators[] | select(.account_id | contains (\"$POOL_ID\"))" | jq .num_produced_blocks)
EXPECTED_BLOCKS=$(curl -s -d '{"jsonrpc": "2.0", "method": "validators", "id": "dontcare", "params": [null]}' -H 'Content-Type: application/json' "$HOST" | jq -c ".result.current_validators[] | select(.account_id | contains (\"$POOL_ID\"))" | jq .num_expected_blocks)
BLOCKS_MISSED=$((EXPECTED_BLOCKS - PRODUCED_BLOCKS))
if [ "$DEBUG_MIN" == "1" ] || [ "$DEBUG_ALL" == "1" ]
then
echo "Validator has missed $BLOCKS_MISSED blocks this epoch"
fi
function send_email_notify
{
PREVIOUS_MISSED=$(cat "$CURRENTDIR"/email_counter.txt)
COUNTER=$((BLOCKS_MISSED - PREVIOUS_MISSED))
if [ "$ENABLE_EMAIL" = 1 ] && [ "$COUNTER" -gt "$ALERT_MISSING_BLOCKS" ]
then
mail -s "NEAR Monitor: '$POOL_ID'" -a From:Admin\<$FROM_ADDRESS\> --return-address=$FROM_ADDRESS $TO_ADDRESS <<< 'Pool is Missing Blocks!!
Expected: '$EXPECTED_BLOCKS'
Produced: '$PRODUCED_BLOCKS'
Blocks Missed: '$BLOCKS_MISSED'
Alert Trigger: '$ALERT_MISSING_BLOCKS' Missing Blocks'
echo "$BLOCKS_MISSED" > "$CURRENTDIR"/email_counter.txt
fi
if [ "$ENABLE_EMAIL" = 0 ] && [ "$DEBUG_ALL" = 1 ]
then
echo "email alerts are disabled"
fi
}
# Check for missing blocks and email if over the limit
if [ "$BLOCKS_MISSED" -gt "$ALERT_MISSING_BLOCKS" ]
then
send_email_notify
fi
KICK_REASON=$(echo "$VALIDATORS" | jq -c ".result.prev_epoch_kickout[] | select(.account_id | contains (\"$POOL_ID\"))" | jq .reason)
KICKED_EMAIL=$(echo "<strong> The validator $POOL_ID has been kicked for $KICK_REASON <strong><br>Action Taken: A new proposal has been submitted.<br>Produced: $PRODUCED_BLOCKS<br>Blocks Missed: $BLOCKS_MISSED<br>Latest Seat Price: $SEAT_PRICE_PROPOSALS<br>Validators Stake: $PROPOSAL_STAKE")
function send_email_kick
{
if [ "$ENABLE_EMAIL" = 1 ]
then
mail -s "NEAR Monitor: $POOL_ID" -a From:Admin\<$FROM_ADDRESS\> --return-address=$FROM_ADDRESS $TO_ADDRESS <<< 'The validator '$POOL_ID' has been kicked for '$KICK_REASON'
Action Taken: A new proposal has been submitted.
Produced: '$PRODUCED_BLOCKS'
Blocks Missed: '$BLOCKS_MISSED'
Latest Seat Price: '$SEAT_PRICE_PROPOSALS'
Validators Stake: '$PROPOSAL_STAKE''
fi
if [ "$ENABLE_EMAIL" = 0 ] && [ "$DEBUG_ALL" = 1 ]
then
echo "Email notification are disabled"
fi
}
# Validator Kicked Check then notify
if [ "$KICK_REASON" ] && [ "$DEBUG_MIN" == "1" ]
then
echo "Validator Kicked Reason = $KICK_REASON"
PING_COMMAND=$(near call $POOL_ID ping "{}" --accountId $ACCOUNT_ID)
echo "$PING_COMMAND"
echo "$KICKED_EMAIL"
send_email_kick
exit
fi
if [ "$KICK_REASON" ] && [ "$DEBUG_MIN" == "0" ]
then
echo "$PING_COMMAND"
send_email_kick
exit
fi
function stake
{
near call $POOL_ID stake '{"amount": '"$1"'}' --accountId $ACCOUNT_ID
}
function unstake
{
near call $POOL_ID unstake '{"amount": '"$1"'}' --accountId $ACCOUNT_ID
}
# Stake is less than the Seat Price
if [[ "$PROPOSAL_STAKE" -lt "$SEAT_PRICE_PROPOSALS" ]]
then
SEAT_PRICE_DIFF=$((SEAT_PRICE_PROPOSALS - PROPOSAL_STAKE))
if [ "$DEBUG_MIN" == "1" ]
then
echo "Network Proposal Seat Price = $SEAT_PRICE_PROPOSALS"
echo "Validator Current Proposal = $PROPOSAL_STAKE"
echo "Seat Price Buffer = $SEAT_PRICE_BUFFER"
echo "$PROPOSAL_STAKE is less than $SEAT_PRICE_PROPOSALS"
fi
# If the difference between $SEAT_PRICE_PROPOSALS + $SEAT_PRICE_BUFFER and $PROPOSAL_STAKE is greater than 4500
# Check that the accountId has the funds available then increase stake by difference
if [ $SEAT_PRICE_DIFF -gt $TRANSACTION_MINIMUM ]
then
UNSTAKED_BALANCE=$(near view $POOL_ID get_account_unstaked_balance '{"account_id": '\"$ACCOUNT_ID\"'}' | tail -n 1)
UNSTAKED_BALANCE=$(echo $UNSTAKED_BALANCE | sed 's/[^0-9]*//g')
UNSTAKED_BALANCE=${UNSTAKED_BALANCE%????????????????????????}
# Ensure funds are a available for the staking transaction
if [[ "$UNSTAKED_BALANCE" -lt "$SEAT_PRICE_DIFF" ]]
then
STAKE_SHORTFALL=$((SEAT_PRICE_DIFF - UNSTAKED_BALANCE))
echo "The current account $ACCOUNT_ID is $STAKE_SHORTFALL NEAR short of the Unstaked Balance needed for the scheduled transaction"
echo "Please try to reduce your requested number of seats or increase the available Unstaked Balance for $ACCOUNT_ID"
exit
fi
# Get the difference and send the staking transaction
SEAT_PRICE_DIFF=$(echo \"$SEAT_PRICE_DIFF$ADD0\")
stake $SEAT_PRICE_DIFF
echo Stake increased by "$SEAT_PRICE_DIFF"
else
echo "The seat price difference of: $SEAT_PRICE_DIFF is not enough to trigger a transaction"
fi
fi
# Stake is more than the Seat Price
STAKED_BALANCE=$(near view $POOL_ID get_account_staked_balance '{"account_id": '\"$ACCOUNT_ID\"'}' | tail -n 1)
STAKED_BALANCE=$(echo $STAKED_BALANCE | sed 's/[^0-9]*//g')
STAKED_BALANCE=${STAKED_BALANCE%????????????????????????}
if [[ "$PROPOSAL_STAKE" -gt "$SEAT_PRICE_PROPOSALS" ]]
then
SEAT_PRICE_DIFF=$((PROPOSAL_STAKE - SEAT_PRICE_PROPOSALS))
if [ "$DEBUG_MIN" == "1" ]
then
echo "$PROPOSAL_STAKE is greater than $SEAT_PRICE_PROPOSALS"
echo "Network Proposal Seat Price = $SEAT_PRICE_PROPOSALS"
echo "Validator Current Proposal = $PROPOSAL_STAKE"
echo "Seat Price Buffer = $SEAT_PRICE_BUFFER"
echo "Stake Diff: $SEAT_PRICE_DIFF"
fi
SEAT_PRICE_DIFF=$(echo $SEAT_PRICE_DIFF | sed 's/[^0-9]*//g')
if [ "$SEAT_PRICE_DIFF" -gt $TRANSACTION_MINIMUM ]
then
AMOUNT=\"$SEAT_PRICE_DIFF$ADD0\"
if [[ "$STAKED_BALANCE" -gt "$SEAT_PRICE_DIFF" ]]
then
echo "Decreasing stake by: ${AMOUNT}"
unstake "$AMOUNT"
else
echo "Amount to unstake ("$SEAT_PRICE_DIFF") is greater than the staked balance ("$STAKED_BALANCE") doing nothing."
fi
else
echo "The seat price difference of: $NEW_PROPOSAL_NUMBERS is not sufficent to trigger a transaction"
fi
fi
# Stake is equal to the Seat Price
if [[ "$PROPOSAL_STAKE" = "$SEAT_PRICE_PROPOSALS" ]]
then
echo "The proposal stake and seat price are equal no action will be taken"
fi
# Finished
echo "Script Done"
echo "----------- "
echo " "