Skip to content

Instantly share code, notes, and snippets.

@watertim
Forked from apetresc/redelegate-cro.sh
Last active November 27, 2021 13:54
Show Gist options
  • Save watertim/2bf5e240d5b0fe338ab844adb6463bda to your computer and use it in GitHub Desktop.
Save watertim/2bf5e240d5b0fe338ab844adb6463bda to your computer and use it in GitHub Desktop.
A script to automatically claim staking rewards and re-stake them
#!/bin/bash -e
if [[ "$1" == "-h" || "$1" == "" ]]
then
echo "Usage: $0 <validator-address> [-y]"
exit
fi
# Arguments
VALIDATOR=$1
YES=$2
# Global parameters
REWARDS_THRESHOLD=10000000
RPC_NODE="http://localhost:26657"
CHAIN_ID="evmos_9000-1"
# Query Wallets
addresses=$(evmosd keys list --output=json | jq -r '.[].address')
echo "Address List:"
for i in $addresses; do
echo "$i"
done
echo "Start Restake all accounts..."
for WALLET in $addresses; do
echo "Wallet is: $WALLET"
BALANCE=$(evmosd q bank balances $WALLET --chain-id $CHAIN_ID --node $RPC_NODE --output json | jq -r '.balances[] | select(.denom == "aphoton") .amount')
echo "Balance is: $BALANCE"
REWARDS=$(evmosd q distribution rewards $WALLET --chain-id $CHAIN_ID --node $RPC_NODE --output json)
REWARDS_AMOUNT=$(echo $REWARDS | jq -r '.total[] | select(.denom == "aphoton") .amount|tonumber')
echo "There are $REWARDS_AMOUNT aphoton rewards across $(echo $REWARDS | jq -r '.rewards | length') validators to be claimed."
if awk 'BEGIN {exit !('$REWARDS_AMOUNT' >= '$REWARDS_THRESHOLD')}'
then
echo "Claiming rewards..."
evmosd --chain-id $CHAIN_ID tx distribution withdraw-all-rewards \
--from $WALLET \
--gas=500000 --gas-prices="0aphoton" \
--chain-id $CHAIN_ID \
--node $RPC_NODE $YES
NEW_BALANCE=$(evmosd q bank balances $WALLET --chain-id $CHAIN_ID --node $RPC_NODE --output json | jq -r '.balances[] | select(.denom == "aphoton") .amount')
while awk 'BEGIN {exit !('$NEW_BALANCE' <= '$BALANCE')}'
do
echo "Waiting for rewards to appear..."
sleep 2
NEW_BALANCE=$(evmosd q bank balances $WALLET --chain-id $CHAIN_ID --node $RPC_NODE --output json | jq -r '.balances[] | select(.denom == "aphoton") .amount')
done
BALANCE=$NEW_BALANCE
echo "New balance is: $BALANCE"
STAKING_AMOUNT=$(expr $BALANCE - 10000000)
echo "Amount to stake: $STAKING_AMOUNT"
evmosd --chain-id $CHAIN_ID tx staking delegate \
$VALIDATOR \
"${STAKING_AMOUNT}aphoton" \
--from $WALLET \
--gas=500000 --gas-prices="0aphoton" \
--node $RPC_NODE $YES
echo "Done!"
else
echo "Need at least $REWARDS_THRESHOLD aphoton to claim, skipping claim..."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment