Skip to content

Instantly share code, notes, and snippets.

@kaustubhkapatral
Last active April 24, 2022 19:43
Show Gist options
  • Save kaustubhkapatral/94fcbff19d5d735a836ae3130707a67d to your computer and use it in GitHub Desktop.
Save kaustubhkapatral/94fcbff19d5d735a836ae3130707a67d to your computer and use it in GitHub Desktop.
Auto-delegation for cosmos based networks
#!/bin/bash -e
BINARY="" #Name of the cli binary to execute. eg - gaiacli, akashctl etc.
KEY="" #Name of the the key name
DENOM="" #Denomination of the token. eg - uatom, uakt etc.
MINIMUM_AMOUNT="" #Min amount to be delegated. If the reward is less than this number deleagtion will not ake place
VALIDATOR="" #Validator address to deleagte to
CHAIN_ID="" #Chain id of the nework
NODE="" #External node to connect to
FEE=""$DENOM #Fees to attach with the transaction.
# For executing this script it is assumed the passphrase is exported as env variable in form of PASSPHRASE='' in a screen session for security purpose.
while true
do
COMMISSION_QUERY=$(${BINARY} query distribution commission ${VALIDATOR} --chain-id ${CHAIN_ID} --node ${NODE} --output json)
if [ "${COMMISSION_QUERY}" == "null" ]
then
# Checking to see if query returns empty response.
COMMISSION="0"
else
COMMISSION=$(echo ${COMMISSION_QUERY} | jq -r ".[] | select(.denom == \"${DENOM}\") | .amount" || true)
if [ -z "${COMMISSION_QUERY}" ]
then
# If the respoonse is 0 there is no commission for this address present.
COMMISSION="0"
else
# Truncating to remove decimals.
COMMISSION=${COMMISSION%.*}
fi
fi
echo "Rewards for address is ${COMMISSION}${DENOM}"
if [ ${COMMISSION} -gt ${MINIMUM_AMOUNT} ]
then
echo "Withdrawing rewards"
{
echo "${PASSPHRASE}"
echo "${PASSPHRASE}"
} | ${BINARY} tx distribution withdraw-rewards ${VALIDATOR} --commission -y --from ${KEY} --chain-id ${CHAIN_ID} --node ${NODE} --fees ${FEE}
sleep 30
echo "Delegating ${COMMISSION}${DENOM} tokens to validator ${VALIDATOR}"
{
echo "${PASSPHRASE}"
echo "${PASSPHRASE}"
} | ${BINARY} tx staking delegate ${VALIDATOR} ${COMMISSION}${DENOM} --yes --from ${KEY} --chain-id ${CHAIN_ID} --node ${NODE} --fees ${FEE}
sleep 30
fi
echo "Sleeping for 2 days"
sleep 172800
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment