Skip to content

Instantly share code, notes, and snippets.

@bartenbach
Last active Jun 23, 2022
Embed
What would you like to do?
Funds validator if balance falls below a certain threshold
#!/bin/bash
SOLANA=/home/$USER/.local/share/solana/install/active_release/bin/solana
WALLET=/home/$USER/wallet.json
FUND_AMOUNT=5
THRESHOLD=6
# enter validator identity here
PUBLIC_KEY=
VALIDATOR_BALANCE=$(${SOLANA} balance "${PUBLIC_KEY}" | tr -d ' SOL')
# check if wallet can fund the validator
function check_wallet_balance() {
WALLET_BALANCE=$(${SOLANA} -k ${WALLET} balance | tr -d ' SOL')
if [[ $(echo "${WALLET_BALANCE} < ${THRESHOLD}" | bc) -eq 1 ]]; then
echo "Wallet balance ${WALLET_BALANCE} SOL (below threshold of ${THRESHOLD})"
echo "Cannot fund Validator! Please top off your wallet!"
exit 1
fi
}
function fund_validator() {
# check if validator balance is below threshold
if [[ $(echo "${VALIDATOR_BALANCE} < ${THRESHOLD}" | bc) -eq 1 ]]; then
echo "Validator balance ${VALIDATOR_BALANCE} SOL (below threshold of ${THRESHOLD})"
# automatically fund from wallet
echo "Transferring ${FUND_AMOUNT} SOL..."
if ${SOLANA} -k ${WALLET} transfer "${PUBLIC_KEY}" ${FUND_AMOUNT}; then
echo "Successfully funded validator with ${FUND_AMOUNT} SOL"
balance=$(${SOLANA} balance "${PUBLIC_KEY}" | tr -d ' SOL')
echo "New validator balance: $balance SOL"
# proactively check if wallet balance is now below funding threshold
check_wallet_balance
else
# solana transfer failed for some reason
echo "Failed to fund validator!"
exit 1
fi
else
# validator balance is fine - show balances
echo "Validator balance ${VALIDATOR_BALANCE} SOL (above threshold of ${THRESHOLD})"
echo "Wallet balance ${WALLET_BALANCE} SOL (above threshold of ${THRESHOLD})"
echo "(No action needed)"
fi
}
function main() {
check_wallet_balance
fund_validator
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment