Skip to content

Instantly share code, notes, and snippets.

@dentesting
Forked from gaia/warchest-bot.sh
Last active July 31, 2022 02:04
Show Gist options
  • Save dentesting/3a919f394a0fe68dcfed1f5fada8ee58 to your computer and use it in GitHub Desktop.
Save dentesting/3a919f394a0fe68dcfed1f5fada8ee58 to your computer and use it in GitHub Desktop.
Near Protocol Stakewars Stake Adjusting Bot
#!/bin/bash
### Heavily rewritten warchest bot, forked from gaia/warchest-bot.sh, credits to Gaia ###
### This version is working with Pools which have user stake delegated ###
### FEEL FREE to find and fix any bugs or issues
. $HOME/.profile # to make it working as cronjob
export NODE_ENV=betanet #change to TestNet or MainNet if required
### Change below line to your PoolId and Account Id
poolID="starlink-pool.stakehouse.betanet"
accountID="starlink.betanet"
yoctoNEAR="1000000000000000000000000"
dry_run="1" # set this to anything other than 1 to make it actually run the (un)stake cmd
proposalSeatPrice=$(near proposals | head -n 1 | sed 's/|//g' | tr -s ' ' | tail -c 20 | sed 's/[^0-9]*//g')
myPoolStake=$(near view $poolID get_account '{"account_id": "'$accountID'"}' | grep staked_balance | grep -v unstaked_balance | awk '{print $2}' | tr -d "'," | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g")
myPoolStakeNear=$(echo "($myPoolStake / $yoctoNEAR)" | bc)
totalPoolStake=$(curl -s -d '{"jsonrpc": "2.0", "method": "validators", "id": "dontcare", "params": [null]}' -H 'Content-Type: application/json' https://rpc.betanet.near.org | jq '.result.current_validators[] | select(.account_id | contains ("star"))' | jq .stake | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g" | tr -d '"')
totalPoolStakeNear=$(echo "($totalPoolStake / $yoctoNEAR)" | bc)
minTolerance=$(echo "($proposalSeatPrice * 1.2)" | bc | awk '{print int($1+0.5)}')
maxTolerance=$(echo "($proposalSeatPrice * 1.7)" | bc | awk '{print int($1+0.5)}')
stake_operation () {
operation="near call $poolID $1 '{\"amount\": \"$amount\"}' --accountId $accountID"
echo $operation
if [ "$dry_run" != "1" ]; then
eval $operation
fi
}
# Simulate lower stake
# totalPoolStakeNear="73000"
if [ "$totalPoolStakeNear" -lt "$minTolerance" ]; then
amountToStakeNear=$(echo "($minTolerance - $totalPoolStakeNear)" | bc)
amountToStake=$(echo "$amountToStakeNear * $yoctoNEAR" | bc);
echo "Total stake in the $poolID is too low, staking: $amountToStakeNear";
amount=$amountToStake
stake_operation "stake"
exit 0;
fi
usersPoolStakeNear=$(echo "($totalPoolStakeNear - $myPoolStakeNear)" | bc);
amountToUnstakeNearTotal=$(echo "($totalPoolStakeNear - $maxTolerance)" | bc);
if [ "$amountToUnstakeNearTotal" -le "0" ]; then
echo "Nothing to stake or unstake, goodbye!"
exit 0;
fi
echo "To keep the pool at 1 seat total amount to unstake: $amountToUnstakeNearTotal"
if [ "$amountToUnstakeNearTotal" -ge "$myPoolStakeNear" ]; then
amountToUnstakeNear=$myPoolStakeNear
echo "But your stake in the $poolID: $myPoolStakeNear, users staking in your pool: $usersPoolStakeNear, you can only unstake: $amountToUnstakeNear";
else
amountToUnstakeNear=$amountToUnstakeNearTotal
echo "Your stake in the $poolID: $myPoolStakeNear, users staking in your pool: $usersPoolStakeNear, you can unstake maximum required: $amountToUnstakeNear";
fi
amountToUnstake=$(echo "$amountToUnstakeNear * $yoctoNEAR" | bc)
amount=$amountToUnstake
if [ "$amountToUnstakeNear" -lt "100" ]; then
echo "Amount to unstake is too low or zero: $amountToUnstakeNear, skip this time"
exit 0;
fi
echo "Unstaking $amountToUnstakeNear...."
stake_operation "unstake"
# ping after operation
near call $poolID ping '{}' --accountId $accountID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment