Skip to content

Instantly share code, notes, and snippets.

@RoboticMind
Last active June 21, 2021 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoboticMind/0e66d13393d6d0868225046f3d951152 to your computer and use it in GitHub Desktop.
Save RoboticMind/0e66d13393d6d0868225046f3d951152 to your computer and use it in GitHub Desktop.
Find the % of Active Vote Weight for Gridcoin Polls
#!/bin/bash
#---check if certain programs needed in the script exist and are functioning---
if ! command -v jq &> /dev/null
then
echo "jq is not installed. Please install it to run. It is used to parse JSON data"
exit 1
fi
if ! command -v bc &> /dev/null
then
echo "bc is not installed. Please install it to run. It is used to do calulations"
exit 1
fi
if ! command -v gridcoinresearchd &> /dev/null
then
echo "gridcoinresearchd is not installed. Please install it to run. It is required by this script to do RPC commands"
exit 1
fi
if ! gridcoinresearchd help &> /dev/null
then
echo "Error runnning RPC commands. Check that your wallet is running and accepting RPC connections"
exit 1
fi
#---end program check---
#---start regular program---
read -p "enter poll ID: " -r poll_id
#-check poll ID is valid (or at least a transaction). Will still pass if a regular TXID, but at least better than no check-
if ! gridcoinresearchd gettransaction "$poll_id" &>/dev/null
then
echo "Invalid poll ID. Please enter a valid poll ID"
exit 1
fi
#-end poll ID check-
echo
echo "finding when the poll started and if it ended..."
poll_start_block_hash=$(gridcoinresearchd gettransaction "$poll_id" | jq -r ".blockhash") #poll id is the transaction ID
poll_start_block=$(gridcoinresearchd getblock "$poll_start_block_hash" | jq -r ".height")
cur_block=$(gridcoinresearchd getblockcount)
cur_time=$(gridcoinresearchd currenttime | jq -r ".Unix")
poll_length=$(gridcoinresearchd gettransaction "$poll_id" | jq -r ".contracts[0].body.duration_days")
poll_start_time=$(gridcoinresearchd gettransaction "$poll_id" | jq -r ".time")
poll_end_time=$(echo "$poll_start_time + (86400* $poll_length)" | bc -l)
if [ "$(echo "$poll_end_time > $cur_time"| bc -l)" -eq 1 ]
then
poll_end_block=$cur_block #if the poll hasn't ended yet, consider the current block to be the "end"
echo "started on block $poll_start_block and is still running"
else
echo "The poll started on $poll_start_block and the poll has ended"
echo
echo "Determining when it ended (this may take a minute)... "
poll_end_block=$(echo "$poll_start_block + (890* $poll_length)" | bc -l) #approximate end block, but underestimate so it will count up until it
while [ "$(gridcoinresearchd getblockbynumber "$poll_end_block" | jq -r ".time")" -lt "$poll_end_time" ]
do
poll_end_block=$((poll_end_block+1)) #increment until it finds the block when it ended
done
echo "The poll ended at $poll_end_block"
fi
echo
echo "finding average money supply..."
start_money_supply=$(gridcoinresearchd getblock "$poll_start_block_hash" | jq -r ".MoneySupply")
end_money_supply=$(gridcoinresearchd getblockbynumber "$poll_end_block" | jq -r ".MoneySupply")
avg_money_supply=$(echo "($end_money_supply + $start_money_supply)/2" | bc -l)
echo "average money supply is $avg_money_supply"
echo
echo "finding pool average magnitudes (this may take a minute)..."
#CPIDs of pools from the list hardcoded in the wallet
grcpool1_cpid="7d0d73fe026d66fd4ab8d5d8da32a611"
grcpool2_cpid="a914eba952be5dfcf73d926b508fd5fa"
grcpool3_cpid="163f049997e8a2dee054d69a7720bf05"
arikado_cpid="326bb50c0dd0ba9d46e15fae3484af35"
#get a list of data about the CPID for each superblock, but has to go all the way back to all block since the poll started since block ranges cannot be given
#filter out the blocks that happened after the poll ended (if ended)
#then pull out the magnitude
#then average those magnitudes
grcpool1_mag=$(gridcoinresearchd superblocks $((cur_block - poll_start_block)) false $grcpool1_cpid | jq -r "[ .[] | select((.height | tonumber) <= $poll_end_block) | .Magnitude] | add/length")
grcpool2_mag=$(gridcoinresearchd superblocks $((cur_block - poll_start_block)) false $grcpool2_cpid | jq -r "[ .[] | select((.height | tonumber) <= $poll_end_block) | .Magnitude ] | add/length")
grcpool3_mag=$(gridcoinresearchd superblocks $((cur_block - poll_start_block)) false $grcpool3_cpid | jq -r "[ .[] | select((.height | tonumber) <= $poll_end_block) | .Magnitude ] | add/length")
arikado_mag=$(gridcoinresearchd superblocks $((cur_block - poll_start_block)) false $arikado_cpid | jq -r "[ .[] | select((.height | tonumber) <= $poll_end_block) | .Magnitude ] | add/length")
pool_avg_mag=$(echo "$grcpool1_mag" + "$grcpool2_mag" + "$grcpool3_mag" + "$arikado_mag" | bc -l)
echo "average pool magnitude is $pool_avg_mag"
echo
echo "finding average difficulty (this may take a minute)..."
avg_diff=$(gridcoinresearchd getblockstats 0 "$poll_start_block" "$poll_end_block" | jq -r ".averages.posdiff")
echo "average difficulty since the start of the polls is $avg_diff"
echo
echo "computing AVW..."
network_mag=115000
avw=$(echo "($avg_diff * 9544371.769) + (($network_mag - $pool_avg_mag) * ($avg_money_supply/$network_mag)/5.67)" | bc -l)
echo "AVW is $avw"
echo
echo "computing poll voteweight (this may take a minute)..."
poll_vote_weight=$(gridcoinresearchd getpollresults "$poll_id" | jq -r ".total_weight")
echo "poll vote weight is $poll_vote_weight"
echo
avw_percent=$(echo "$poll_vote_weight/$avw * 100" | bc -l)
echo "% of AVW is $avw_percent"
#---end regular program---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment