Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Vote Report | ||
|
||
This script generates a vote report at the configured file every 10 minutes. | ||
|
||
## Instructions | ||
|
||
A **[BPL-node](https://github.com/blockpool-io/bpl-node)** needs to be installed and in sync. | ||
|
||
Get the script and make it executable with the following commands: | ||
|
||
```sh | ||
wget https://raw.githubusercontent.com/blockpool-io/vote-report/master/vote-report.sh -O /usr/local/bin/vote-report.sh | ||
chmod +x /usr/local/bin/vote-report.sh | ||
``` | ||
|
||
Edit crontab by typing `crontab -e`. This will allow you to edit the crontab in your default / chosen editor. | ||
|
||
Put the following line at the end of the file to execute the restart checker every 10 minutes: | ||
|
||
``` | ||
*/10 * * * * /usr/local/bin/vote-report.sh | ||
``` | ||
|
||
Be sure to add a linebreak at the end of this file. | ||
|
||
Replace the example path above with the real location of the restart script. | ||
|
||
Save the file and exit the editor. Do `crontab -l` to verify that the new line is now in your crontab. | ||
|
||
### Dependencies | ||
|
||
#### Ubuntu | ||
`sudo apt-get install -y wget curl sed jq bc` | ||
|
||
#### CentOS | ||
`sudo yum install -y wget curl sed jq bc` | ||
|
||
## Configuration | ||
|
||
Edit the script to customize the OutputFile variable value. | ||
|
||
> OutputFile='/var/www/html/vote-report.txt' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/bin/bash | ||
|
||
OutputFile='/var/www/html/vote-report.txt' | ||
|
||
|
||
function GetVotersCount { | ||
VotersJsonData=$( curl -s "http://127.0.0.1:9030/api/delegates/voters?publicKey=$1" ) | ||
echo $VotersJsonData | jq '.accounts | length' | ||
} | ||
|
||
function PrintJsonData { | ||
echo $1 | jq -c -r '.delegates[] | { rate, username, vote, address, publicKey, approval }' | while read Line; do | ||
|
||
Rank=$( printf %03d $( echo $Line | jq -r '.rate' ) ) | ||
|
||
Delegate=$( printf %-25s $( echo $Line | jq -r '.username' ) ) | ||
Approval=$( printf %0.2f $( echo $Line | jq -r '.approval' ) ) | ||
|
||
Vote=$( expr $( echo $Line | jq -r '.vote' ) / 100000000 ) | ||
Vote=$( echo $Vote | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta' ) | ||
Vote=$( printf %10s $Vote ) | ||
|
||
PublicKey=$( echo $Line | jq -r '.publicKey' ) | ||
Voters=$( printf %4s $( GetVotersCount $PublicKey ) ) | ||
|
||
echo "| $Rank | $Delegate | $Approval | $Vote | $Voters |" >> $2 | ||
done | ||
} | ||
|
||
function PrintTotalVotingWeightData { | ||
|
||
TotalBPL=$( curl -s "http://127.0.0.1:9030/api/blocks/getSupply" | jq -r '.supply' ) | ||
TotalBPL=${TotalBPL%.*} | ||
|
||
TotalVote=0 | ||
TotalVoters=0 | ||
while read Line; do | ||
|
||
Vote=$( echo $Line | jq -r '.vote' ) | ||
TotalVote=$( expr $TotalVote + $Vote ) | ||
|
||
PublicKey=$( echo $Line | jq -r '.publicKey' ) | ||
Voters=$( GetVotersCount $PublicKey ) | ||
TotalVoters=$( expr $TotalVoters + $Voters ) | ||
|
||
done <<< "$( echo $1 | jq -c -r '.delegates[] | { rate, username, vote, address, publicKey, approval }' )" | ||
|
||
Percentage=$( bc <<< "scale=2; $TotalVote * 100 / $TotalBPL" ) | ||
|
||
TotalVote=$( expr $( echo $TotalVote ) / 100000000 ) | ||
TotalVote=$( echo $TotalVote | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta' ) | ||
|
||
TotalBPL=$( expr $( echo $TotalBPL ) / 100000000 ) | ||
TotalBPL=$( echo $TotalBPL | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta' ) | ||
|
||
echo -e "Top 201 Delegates Stats\n" >> $2 | ||
echo -e "=> Total Votes : $Percentage% ( $TotalVote / $TotalBPL )" >> $2 | ||
echo -e "=> Total Voters : $TotalVoters\n" >> $2 | ||
} | ||
|
||
JsonData1=$( curl -s 'http://127.0.0.1:9030/api/delegates' ) | ||
JsonData2=$( curl -s 'http://127.0.0.1:9030/api/delegates?limit=49&offset=201' ) | ||
|
||
WorkFile='./vote-report.txt' | ||
|
||
echo '' > $WorkFile | ||
PrintTotalVotingWeightData $JsonData1 $WorkFile | ||
echo '==================================================================' >> $WorkFile | ||
echo '| # | Delegate | Vote % | Vote BPL | Voters |' >> $WorkFile | ||
echo '==================================================================' >> $WorkFile | ||
PrintJsonData $JsonData1 $WorkFile | ||
echo '==================================================================' >> $WorkFile | ||
PrintJsonData $JsonData2 $WorkFile | ||
echo '==================================================================' >> $WorkFile | ||
Date=$( date -u "+%Y-%m-%d %H:%M:%S" ) | ||
echo -e "\n$Date UTC / vote-report.sh v1.2 / blockpool.io \n" >> $WorkFile | ||
|
||
cp -f $WorkFile $OutputFile |