$ solana stake-account stake.json
#!/bin/sh
WITHDRAW_THRESHOLD_LAMPORTS=10000000
NEW_STAKE_THRESHOLD_LAMPORTS=30000000
WITHDRAWER_MIN_BALANCE=10000000
FOLDER_WITH_STAKES=/home/user/stakes
WITHDRAWER_KEY=/home/user/withdraw-authority.json
VALIDATOR_TO_RESTAKE=6hcGvZypizjf6PPsxboshZHRqefyQKSG9L8vZqYdm7UY
for file in $(ls $FOLDER_WITH_STAKES/*.json); do
owner=$(solana account --output json $file | jq -r '.account.owner')
if [ "$owner" = "Stake11111111111111111111111111111111111111" ]; then
stake_data=$(solana stake-account --output json $file)
balance=$(echo "$stake_data" | jq -r '.accountBalance')
delegated=$(echo "$stake_data" | jq -r '.delegatedStake')
reserve=$(echo "$stake_data" | jq -r '.rentExemptReserve')
free_amount=$(echo "$balance - $delegated - $reserve" | bc)
echo "Free Amount $free_amount"
if [ "$free_amount" -gt "$WITHDRAW_THRESHOLD_LAMPORTS" ]; then
echo "Withdrawing"
solana withdraw-stake $file "$WITHDRAWER_KEY" -k "$WITHDRAWER_KEY" $(echo "scale=9; $free_amount / 1000000000" | bc)
fi
fi
done
withdrawer_balance=$(solana balance --output json "$WITHDRAWER_KEY" | jq -r .lamports)
echo "Withdrawer Balance $withdrawer_balance"
if [ "$withdrawer_balance" > "$NEW_STAKE_THRESHOLD_LAMPORTS" ]; then
new_stake_amount=$(echo "scale=9; ($withdrawer_balance - $WITHDRAWER_MIN_BALANCE)/1000000000" | bc)
new_stake_filename=$(date +"stake-%Y%m%dT%H%M%S.json")
(cd $FOLDER_WITH_STAKES && \
solana-keygen new -o "$new_stake_filename" -s --no-bip39-passphrase &&
solana create-stake-account "$new_stake_filename" "$new_stake_amount" -k "$WITHDRAWER_KEY" &&
solana delegate-stake -k "$WITHDRAWER_KEY" "$new_stake_filename" "$VALIDATOR_TO_RESTAKE")
fi