Scripts For Rewards Basis Generation

Over the past weekend I wanted to make sure I kept track of the value of my wallet’s rewards. I chose to create a transaction that sent 0 algorands daily from an address in my wallet to another to make the rewards visible in the blockchain. This way I can use a few APIs available to keep track of the basis. I’m sure there are a few ways to set the basis, but I assume once the transaction is visible it is fair to say the value of algo at that time is the basis. I thought someone might find these little ksh scripts I wrote on my mac useful.

Send zero from one address to another (sendZero):

#!/bin/ksh

addr_from=3B7JLNYWENOUJQWM7MAAU4Y7FAWR2EOQACISMQQGPOZJO6KJHTGBSVZ4JQ
addr_to=T66AJUIJZTX6FYX4T7QHICTUXASHPJNSPRJT3XLYZ47WXKINGQLXYFUUDE

date
echo "Sending 0 uAlgos from $addr_from to $addr_to"
./goal clerk send -a 0 -f $addr_from -t $addr_to -d ./data

Generate a rewards basis from transactions (rewardsBasis):

#!/bin/ksh

addr=3B7JLNYWENOUJQWM7MAAU4Y7FAWR2EOQACISMQQGPOZJO6KJHTGBSVZ4JQ
transactions=$(curl -q "https://algoexplorerapi.io/idx2/v2/accounts/$addr/transactions" 2>/dev/null | jq -r '.transactions|reverse|.[]')
#echo $transactions

dates=$(echo "$transactions" | jq -r '."round-time"')

senders=$(echo   "$transactions" | jq -r '.sender')
receivers=$(echo "$transactions" | jq -r '."payment-transaction".receiver')

senderRewards=$(echo   "$transactions" | jq -r '."sender-rewards"')
receiverRewards=$(echo "$transactions" | jq -r '."receiver-rewards"')

fees=$(echo   "$transactions" | jq -r '.fee')
#txType=$(echo "$transactions" | jq -r '."tx-type"')

#echo $dates
#echo "=="
#echo $senders
#echo "=="
#echo $receivers
#echo "=="
#echo $senderRewards
#echo "=="
#echo $receiverRewards
#echo "=="
#echo $fees

#exit 1

set -A dates_array $dates
set -A senders_array $senders
set -A receivers_array $receivers
set -A senderRewards_array $senderRewards
set -A receiverRewards_array $receiverRewards
set -A fees_array $fees
#set -A txType_array $txType

for index in ${!dates_array[@]}; do 
#   thisTx=${txType_array[$index]}

   thisDate=$(date -r ${dates_array[$index]} -u "+%Y-%m-%dT%H:%M")
   thisDateFull=$(date -r ${dates_array[$index]})

   # close price
   thisPrice=$(curl -q "https://api.pro.coinbase.com/products/ALGO-USD/candles?start=$thisDate&end=$thisDate" 2>/dev/null| jq -r ".[][4]")

   # sender 
   thisSender=${senders_array[$index]}
   thisReward=${senderRewards_array[$index]}
   if [[ "$thisSender" == "$addr" && "$thisReward" != "0"  ]]; then
      # fee
      thisFee=${fees_array[$index]}

      total=$(awk "BEGIN{print $thisPrice * ($thisReward - $thisFee) / 1000000 }")
      echo "$thisDateFull,$thisReward,$thisFee,$thisPrice,$total"
   fi

   # receiver 
   thisReceiver=${receivers_array[$index]}
   thisReward=${receiverRewards_array[$index]}
   if [[ "$thisReceiver" == "$addr" && "$thisReward" != "0" ]]; then
      # fee
      thisFee=0

      total=$(awk "BEGIN{print $thisPrice * ($thisReward - $thisFee) / 1000000 }")
      echo "$thisDateFull,$thisReward,$thisFee,$thisPrice,$total"
   fi
done

Expect script to pass the password to a nightly script (rewards):

#!/usr/bin/expect -f

set timeout -1
set password my_password
spawn ./sendZero
expect "Please enter the password for wallet 'AlgoWallet': "
send -- "$password\r"
expect eof

Daily cron (crontab -e):

0 23 * * * cd /Users/<USER>/node && ./rewards >> rewards.log

Let me know :wink: if you find these useful

Regards,
Jose