-
Notifications
You must be signed in to change notification settings - Fork 4
/
commission_cashback.sh
79 lines (58 loc) · 2.1 KB
/
commission_cashback.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
CONFIG_DIR=$1
DELEGATOR_ADDR=$2
DELEGATOR_DATA_FILE=$3
source $CONFIG_DIR/config.sh
MAX_BLOCKS_TO_CHECK=90
source ./utils.sh
ADDR_LEN=$(expr length $DELEGATOR_ADDR)
if [ $ADDR_LEN -lt 1 ]; then
exit
fi
network_up_and_synced $NODE
CURRENT_HEIGHT=$(curl $NODE/status -s | jq ".result.sync_info.latest_block_height | tonumber")
get_height_reward () {
local HEIGHT=$1
local REWARD=$($PATH_TO_SERVICE q distribution rewards \
$DELEGATOR_ADDR $VALIDATOR_ADDRESS \
-o json --node $NODE --height $HEIGHT | \
/usr/bin/jq ".rewards[] | select(.denom | contains(\"$DENOM\")).amount | tonumber")
if [[ $REWARD =~ $NUMBER_RE ]] ; then
HEIGHT_REWARD=${REWARD}
else
HEIGHT_REWARD=0
fi
}
get_height_commission () {
local HEIGHT=$1
local COMMISSION=$($PATH_TO_SERVICE q staking validator ${VALIDATOR_ADDRESS} \
--output json --node $NODE --height $HEIGHT | \
/usr/bin/jq ".commission.commission_rates.rate | tonumber")
if [[ $COMMISSION =~ $NUMBER_RE ]] ; then
HEIGHT_COMMISSION=${COMMISSION}
else
HEIGHT_COMMISSION=0
fi
}
if [ ! -f "$DELEGATOR_DATA_FILE" ]; then
save_delegator_data $DELEGATOR_DATA_FILE $CURRENT_HEIGHT
exit
fi
get_last_height $DELEGATOR_DATA_FILE
get_total_cashback $DELEGATOR_DATA_FILE
get_height_reward $LAST_HEIGHT
PREV_REWARD=$HEIGHT_REWARD
if (( $(echo "($CURRENT_HEIGHT - $LAST_HEIGHT) > $MAX_BLOCKS_TO_CHECK" | bc ) == 1 )); then
LAST_HEIGHT=$(echo "$CURRENT_HEIGHT - $MAX_BLOCKS_TO_CHECK" | bc )
fi
for HEIGHT in $( eval echo {$LAST_HEIGHT..$CURRENT_HEIGHT} )
do
get_height_reward $HEIGHT
get_height_commission $HEIGHT
get_total_reward $HEIGHT_REWARD $PREV_REWARD
HEIGHT_CASHBACK=$(echo "$TOTAL_REWARD / (1 - $HEIGHT_COMMISSION) - $TOTAL_REWARD" | bc -l) #"
TOTAL_CASHBACK=$(echo "$HEIGHT_CASHBACK + $TOTAL_CASHBACK" | bc -l) #"
PREV_REWARD=$HEIGHT_REWARD
done
echo `date +%T`" - Total cashback for period: $TOTAL_CASHBACK. Last height: $CURRENT_HEIGHT"
save_delegator_data $DELEGATOR_DATA_FILE $CURRENT_HEIGHT $TOTAL_CASHBACK