Auto-stake script
# To automate the delegation of rewards
# Create screen
screen -S stake
nano stake.sh
#!/bin/bash
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
DELAY=800 # 1 hour
TIKER=
DENOM="10000000000"
TOKEN=
ADDRESS=
VALOPER=
WALLET=""
for (( ;; )); do
BAL=$(${TIKER} query bank balances ${ADDRESS} -o json | jq -r --arg TOKEN $TOKEN '.balances[] | select(.denom==$TOKEN) | .amount');
COMMISSION_U=$(${TIKER} query distribution commission ${VALOPER} --output json | jq '.commission[] | select(.denom == "'"$TOKEN"'") .amount' | bc)
REWARDS_U=$(${TIKER} query distribution rewards ${ADDRESS} --output json | jq '.total[] | select(.denom == "'"$TOKEN"'") .amount' | bc)
REWARDS_TOTAL_U=$(echo "(${COMMISSION_U}+${REWARDS_U}+${BAL})/1" | bc)
if (( ${REWARDS_TOTAL_U} > ${DENOM} )); then
${TIKER} tx distribution withdraw-rewards ${VALOPER} --from ${WALLET} --commission --fees 0${TOKEN} -y
for (( timer=10; timer>0; timer-- ))
do
printf "* sleep for ${RED}%02d${NC} sec\r" $timer
sleep 1
done
# Leave 1 token on our balance for commission
BAL=$((BAL-DENOM));
echo -e "BALANCE: ${GREEN}${BAL}${NC} ${TOKEN}\n"
echo -e "Stake ALL\n"
if (( ${BAL} > $DENOM )); then
${TIKER} tx staking delegate ${VALOPER} ${BAL}${TOKEN} --from ${WALLET} --fees 0${TOKEN} -y
else
echo -e "BALANCE: ${GREEN_COLOR}${BAL}${NC} ${TOKEN} ${BAL} < $DENOM\n"
fi
else
echo -e "BALANCE: ${GREEN_COLOR}${BAL}${NC}${TOKEN} | ${BAL} < ${DENOM}"
echo -e "The balance is too small for staking\n"
fi
for (( timer=${DELAY}; timer>0; timer— ))
do
printf "* sleep for ${RED}%02d${NC} sec\r" $timer
sleep 1
done
done