forked from validator-network/cosmoshub-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosmoshub-reinvest-rewards.sh
171 lines (144 loc) · 7.54 KB
/
cosmoshub-reinvest-rewards.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
#!/bin/bash -e
# Copyright (C) 2019 Validator ApS -- https://validator.network
# This script comes without warranties of any kind. Use at your own risk.
# The purpose of this script is to withdraw rewards (if any) and delegate them to an appointed validator. This way you can reinvest (compound) rewards.
# Cosmos Hub does currently not support automatic compounding but this is planned: https://github.com/cosmos/cosmos-sdk/issues/3448
# Requirements: gaiacli, curl and jq must be in the path.
##############################################################################################################################################################
# User settings.
##############################################################################################################################################################
KEY="" # This is the key you wish to use for signing transactions, listed in first column of "gaiacli keys list".
PASSPHRASE="" # Only populate if you want to run the script periodically. This is UNSAFE and should only be done if you know what you are doing.
DENOM="uatom" # Coin denominator is uatom ("microoatom"). 1 atom = 1000000 uatom.
MINIMUM_DELEGATION_AMOUNT="25000000" # Only perform delegations above this amount of uatom. Default: 25atom.
RESERVATION_AMOUNT="100000000" # Keep this amount of uatom in account. Default: 100atom.
VALIDATOR="cosmosvaloper1sxx9mszve0gaedz5ld7qdkjkfv8z992ax69k08" # Default is Validator Network. Thank you for your patronage :-)
##############################################################################################################################################################
##############################################################################################################################################################
# Sensible defaults.
##############################################################################################################################################################
CHAIN_ID="" # Current chain id. Empty means auto-detect.
NODE="https://cosmoshub.validator.network:443" # Either run a local full node or choose one you trust.
GAS_PRICES="0.025uatom" # Gas prices to pay for transaction.
GAS_ADJUSTMENT="1.30" # Adjustment for estimated gas
GAS_FLAGS="--gas auto --gas-prices ${GAS_PRICES} --gas-adjustment ${GAS_ADJUSTMENT}"
##############################################################################################################################################################
# Auto-detect chain-id if not specified.
if [ -z "${CHAIN_ID}" ]
then
NODE_STATUS=$(curl -s --max-time 5 ${NODE}/status)
CHAIN_ID=$(echo ${NODE_STATUS} | jq -r ".result.node_info.network")
fi
# Use first command line argument in case KEY is not defined.
if [ -z "${KEY}" ] && [ ! -z "${1}" ]
then
KEY=${1}
fi
# Get information about key
KEY_STATUS=$(gaiacli keys show ${KEY} --output json)
KEY_TYPE=$(echo ${KEY_STATUS} | jq -r ".type")
if [ "${KEY_TYPE}" == "ledger" ]
then
SIGNING_FLAGS="--ledger"
fi
# Get current account balance.
ACCOUNT_ADDRESS=$(echo ${KEY_STATUS} | jq -r ".address")
ACCOUNT_STATUS=$(gaiacli query account ${ACCOUNT_ADDRESS} --chain-id ${CHAIN_ID} --node ${NODE} --output json)
ACCOUNT_SEQUENCE=$(echo ${ACCOUNT_STATUS} | jq -r ".value.sequence")
ACCOUNT_BALANCE=$(echo ${ACCOUNT_STATUS} | jq -r ".value.coins[] | select(.denom == \"${DENOM}\") | .amount" || true)
if [ -z "${ACCOUNT_BALANCE}" ]
then
# Empty response means zero balance.
ACCOUNT_BALANCE=0
fi
# Get available rewards.
REWARDS_STATUS=$(gaiacli query distr rewards ${ACCOUNT_ADDRESS} --chain-id ${CHAIN_ID} --node ${NODE} --output json)
if [ "${REWARDS_STATUS}" == "null" ]
then
# Empty response means zero balance.
REWARDS_BALANCE="0"
else
REWARDS_BALANCE=$(echo ${REWARDS_STATUS} | jq -r ".[] | select(.denom == \"${DENOM}\") | .amount" || true)
if [ -z "${REWARDS_BALANCE}" ] || [ "${REWARDS_BALANCE}" == "null" ]
then
# Empty response means zero balance.
REWARDS_BALANCE="0"
else
# Remove decimals.
REWARDS_BALANCE=${REWARDS_BALANCE%.*}
fi
fi
# Get available commission.
VALIDATOR_ADDRESS=$(gaiacli keys show ${KEY} --bech val --address)
COMMISSION_STATUS=$(gaiacli query distr commission ${VALIDATOR_ADDRESS} --chain-id ${CHAIN_ID} --node ${NODE} --output json)
if [ "${COMMISSION_STATUS}" == "null" ]
then
# Empty response means zero balance.
COMMISSION_BALANCE="0"
else
COMMISSION_BALANCE=$(echo ${COMMISSION_STATUS} | jq -r ".[] | select(.denom == \"${DENOM}\") | .amount" || true)
if [ -z "${COMMISSION_BALANCE}" ]
then
# Empty response means zero balance.
COMMISSION_BALANCE="0"
else
# Remove decimals.
COMMISSION_BALANCE=${COMMISSION_BALANCE%.*}
fi
fi
# Calculate net balance and amount to delegate.
NET_BALANCE=$((${ACCOUNT_BALANCE} + ${REWARDS_BALANCE} + ${COMMISSION_BALANCE}))
if [ "${NET_BALANCE}" -gt $((${MINIMUM_DELEGATION_AMOUNT} + ${RESERVATION_AMOUNT})) ]
then
DELEGATION_AMOUNT=$((${NET_BALANCE} - ${RESERVATION_AMOUNT}))
else
DELEGATION_AMOUNT="0"
fi
# Display what we know so far.
echo "======================================================"
echo "Account: ${KEY} (${KEY_TYPE})"
echo "Address: ${ACCOUNT_ADDRESS}"
echo "======================================================"
echo "Account balance: ${ACCOUNT_BALANCE}${DENOM}"
echo "Available rewards: ${REWARDS_BALANCE}${DENOM}"
echo "Available commission: ${COMMISSION_BALANCE}${DENOM}"
echo "Net balance: ${NET_BALANCE}${DENOM}"
echo "Reservation: ${RESERVATION_AMOUNT}${DENOM}"
echo
if [ "${DELEGATION_AMOUNT}" -eq 0 ]
then
echo "Nothing to delegate."
exit 0
fi
# Display delegation information.
VALIDATOR_STATUS=$(gaiacli query staking validator ${VALIDATOR} --chain-id ${CHAIN_ID} --node ${NODE} --output json)
VALIDATOR_MONIKER=$(echo ${VALIDATOR_STATUS} | jq -r ".description.moniker")
VALIDATOR_DETAILS=$(echo ${VALIDATOR_STATUS} | jq -r ".description.details")
echo "You are about to delegate ${DELEGATION_AMOUNT}${DENOM} to ${VALIDATOR}:"
echo " Moniker: ${VALIDATOR_MONIKER}"
echo " Details: ${VALIDATOR_DETAILS}"
echo
# Ask for passphrase to sign transactions.
if [ -z "${SIGNING_FLAGS}" ] && [ -z "${PASSPHRASE}" ]
then
read -s -p "Enter passphrase required to sign for \"${KEY}\": " PASSPHRASE
echo ""
fi
# Run transactions
MEMO=$'Reinvesting rewards @ Validator\xF0\x9F\x8C\x90Network'
if [ "${REWARDS_BALANCE}" -gt 0 ]
then
printf "Withdrawing rewards... "
echo ${PASSPHRASE} | gaiacli tx distr withdraw-all-rewards --yes --from ${KEY} --sequence ${ACCOUNT_SEQUENCE} --chain-id ${CHAIN_ID} --node ${NODE} ${GAS_FLAGS} ${SIGNING_FLAGS} --memo "${MEMO}" --broadcast-mode async
ACCOUNT_SEQUENCE=$((ACCOUNT_SEQUENCE + 1))
fi
if [ "${COMMISSION_BALANCE}" -gt 0 ]
then
printf "Withdrawing commission... "
echo ${PASSPHRASE} | gaiacli tx distr withdraw-rewards ${VALIDATOR_ADDRESS} --commission --yes --from ${KEY} --sequence ${ACCOUNT_SEQUENCE} --chain-id ${CHAIN_ID} --node ${NODE} ${GAS_FLAGS} ${SIGNING_FLAGS} --memo "${MEMO}" --broadcast-mode async
ACCOUNT_SEQUENCE=$((ACCOUNT_SEQUENCE + 1))
fi
printf "Delegating... "
echo ${PASSPHRASE} | gaiacli tx staking delegate ${VALIDATOR} ${DELEGATION_AMOUNT}${DENOM} --yes --from ${KEY} --sequence ${ACCOUNT_SEQUENCE} --chain-id ${CHAIN_ID} --node ${NODE} ${GAS_FLAGS} ${SIGNING_FLAGS} --memo "${MEMO}" --broadcast-mode async
echo
echo "Have a Cosmic day!"