Skip to content

Instantly share code, notes, and snippets.

@gaia
Created June 30, 2020 01:53
Show Gist options
  • Save gaia/cff45baf3fa710a42c3fc4cdaafe8edc to your computer and use it in GitHub Desktop.
Save gaia/cff45baf3fa710a42c3fc4cdaafe8edc to your computer and use it in GitHub Desktop.
Near Protocol Stakewars Stake Adjusting Bot
#!/bin/bash
### Adjust these two variables to your needs ###
poolID="freshnears"
warchestID="gaia.test"
tolerance="0.2" # how many % above the min and below the max do you consider safe
# when using 0.2 (20%), it will recommend you stay within the inner 60% range.
dry_run="1" # set this to anything other than 1 to make it actually run the (un)stake cmd
# use this when you play with the values of $mystake and $mywarchest in the lines below.
### Not in use
#target_position="0.5" # where do you want to be when you adjust your stake?
# use a lower number if your warchest is low. otherwise leave it alone
# this is calculated within the tolerated range, not within the total range.
# so you can use target lower than tolerance.
#### Nothing else changes from here on, unless you want to test this script ###
valset=$(/usr/bin/near proposals | tail -n +13 | head -n -4 | sed 's/|//g' | tr -s ' ' | egrep -v "Declined|Kicked")
range=$(echo "${valset}" | awk '{print $(NF-1),"\t",$NF}' | grep ' 1')
rangemin=$(echo "${range}" | tail -n 1 | awk '{print $1}' | sed 's/[^0-9]*//g')
rangemax=$(echo "${range}" | head -n 1 | awk '{print $1}' | sed 's/[^0-9]*//g')
mystake=$(echo "${valset}" | grep "${poolID}" | awk '{print $(NF-1),"\t",$NF}' | awk '{$2=""; print $0}' | sed 's/[^0-9]*//g')
#mystake=$(echo "$mystake-15000" | bc) # use this line to test the script, change the number to put you above or below
mywarchest=$(/usr/bin/near state $warchestID | grep "formattedAmount" | tr -d '[:cntrl:]' | cut -d\' -f2 | sed 's/,//g' | awk '{print int($1+0.5)}')
mywarchest=$(echo "$mywarchest-1" | bc) # leave some for fees
#mywarchest="100000" # use this to test your warchest size
upperbound=$(echo "$rangemax-($rangemax*$tolerance)" | bc | awk '{print int($1+0.5)}')
lowerbound=$(echo "$rangemin+($rangemin*$tolerance)" | bc | awk '{print int($1+0.5)}')
calc_target () {
### Not in use
#local tolerated_range=$(echo "$upperbound-$lowerbound" | bc)
#target_stake=$(echo "($tolerated_range*$target_position)+$lowerbound" | bc | awk '{print int($1+0.5)}')
if [ "$1" == "unstake" ]; then
unstake_min=$(echo "(($rangemax-1) - $mystake)*-1" | bc)
unstake_ideal=$(echo "($upperbound - $mystake)*-1" | bc)
else
stake_min=$(echo "($rangemin+1) - $mystake" | bc)
stake_ideal=$(echo "$lowerbound - $mystake" | bc)
fi
}
add_zeros () {
stake_change=$(echo "$1*1000000000000000000000000" | bc)
}
stake_operation () {
operation="/usr/bin/near call $poolID $1 '{\"amount\": \"$stake_change\"}' --accountId $warchestID"
echo $operation
if [ "$dry_run" != "1" ]; then
eval $operation
fi
}
if (( ($mystake > $rangemin) && ($mystake < $rangemax) )); then
echo "$poolID will have 1 seat in the epoch after next."
if (( !(($mystake >= $lowerbound) && ($mystake <= $upperbound)) )); then
echo "But it is not within what you tolerate as safe."
if (( $mystake < $lowerbound )); then
calc_target "stake"
if (( $mywarchest >= $stake_ideal)); then
echo "Staking $stake_ideal..."
add_zeros $stake_ideal
stake_operation "stake"
elif (( ($mywarchest < $stake_ideal) && ($mywarchest >= $stake_min) )); then
echo "You only have $mywarchest available to stake. You will be above the minimum, but consider finding more stake to bring you in the range you consider safe, which is above $stake_ideal."
echo "Staking $mywarchest..."
add_zeros $mywarchest
stake_operation "stake"
fi
elif (( $mystake > $upperbound )); then
calc_target "unstake"
echo "Unstaking $unstake_ideal..."
add_zeros $unstake_ideal
stake_operation "unstake"
fi
fi
elif (( $mystake > $lowerbound )); then
calc_target "unstake"
echo "$poolID will have more than 1 seat in the epoch after next. You have $mystake, but you should have at most $rangemax."
echo "Unstake at least $unstake_min and ideally unstake $unstake_ideal."
echo "Unstaking $unstake_ideal."
add_zeros $unstake_ideal
stake_operation "unstake"
elif (( $mystake < $upperbound )); then
calc_target "stake"
echo "$poolID will NOT have a seat in the epoch after next. You have $mystake, but you shoudl have at least $rangemin."
echo "Add at least $stake_min, ideally $stake_ideal, to your stake."
if (( $mywarchest >= $stake_ideal)); then
echo "Staking $stake_ideal..."
add_zeros $stake_ideal
stake_operation "stake"
elif (( ($mywarchest < $stake_ideal) && ($mywarchest >= $stake_min) )); then
echo "You only have $mywarchest available to stake. You will be above the minimum, but consider finding more stake to bring you in the range you consider safe, which is above $stake_ideal."
echo "Staking $mywarchest..."
add_zeros $mywarchest
stake_operation "stake"
else
echo "WARNING: you have less than the mininum needed to keep 1 seat in the epoch after next."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment