Skip to content

Instantly share code, notes, and snippets.

@ilap
Last active February 9, 2023 14:43
Show Gist options
  • Save ilap/1d273251c1afc931cc610546891b900d to your computer and use it in GitHub Desktop.
Save ilap/1d273251c1afc931cc610546891b900d to your computer and use it in GitHub Desktop.
Epoch Rewards

Zsh script

#!/usr/bin/env zsh
# 
# usage:
# epoch_rewards.sh {317..390}
# or
# epoch_rewards.sh 390

# For fixed accounts
# accounts=("statke11..." stake22..." ... "stake1n" )

do_curl() {
	curl -X POST "https://api.koios.rest/api/v0/account_rewards" -H "accept: application/json" -H "content-type: application/json" -d "{\"_stake_addresses\":[\"$1\"],\"_epoch_no\":$epoch}"
}

# ADA USD Exchange rate. Note: only current rate. 
curr="usd"
curru=${curr:u}
xrate=$( curl -s  "https://api.coinconvert.net/convert/ada/$curr?amount=1" | jq .$curru 2>/dev/null )

sum=0.0

hdr="%8s %15.6f %13.2f\n"
sep="${(l:38::=:)}"

printf "%8s %15s %13s\n" "Epoch" "ADA" $curru
print "$sep"
for epoch in $argv[*]; do
	esum=0.0
	for i in ${accounts[*]}; do
		amount=$(do_curl $i 2>/dev/null | jq "[.[].rewards[].amount | (tonumber // 0) / 1000000 ] | add // 0.0")
		esum=$(( $esum + $amount ))
	done
	printf "$hdr" $epoch $esum  $(( $esum * $xrate ))
	sum=$(( $sum + $esum ))
done

print "$sep"
printf "$hdr" "Total" $sum  $(( $sum * $xrate ))

Output Example

$ ./epoch_rewards.zsh {385..390}
   Epoch             ADA           USD
======================================
     385     1811.437709        703.23
     386     1959.761746        760.81
     387     1922.495043        746.34
     388     1666.352377        646.90
     389     1725.474793        669.85
     390     1795.260766        696.95
======================================
   Total    10880.782434       4224.08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment