forked from iwbinb/bee-swarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcashout.sh
93 lines (82 loc) · 1.89 KB
/
cashout.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#1/usr/bin/env sh
DEBUG_API=http://localhost:1635
MIN_AMOUNT=1000
function getPeers() {
curl -s "$DEBUG_API/chequebook/cheque" | jq -r '.lastcheques | .[].peer'
}
function getCumulativePayout() {
local peer=$1
local cumulativePayout=$(curl -s "$DEBUG_API/chequebook/cheque/$peer" | jq '.lastreceived.payout')
if [ $cumulativePayout == null ]
then
echo 0
else
echo $cumulativePayout
fi
}
function getLastCashedPayout() {
local peer=$1
local cashout=$(curl -s "$DEBUG_API/chequebook/cashout/$peer" | jq '.cumulativePayout')
if [ $cashout == null ]
then
echo 0
else
echo $cashout
fi
}
function getUncashedAmount() {
local peer=$1
local cumulativePayout=$(getCumulativePayout $peer)
if [ $cumulativePayout == 0 ]
then
echo 0
return
fi
cashedPayout=$(getLastCashedPayout $peer)
let uncashedAmount=$cumulativePayout-$cashedPayout
echo $uncashedAmount
}
function cashout() {
local peer=$1
txHash=$(curl -s -XPOST "$DEBUG_API/chequebook/cashout/$peer" | jq -r .transactionHash)
echo cashing out cheque for $peer in transaction $txHash >&2
result="$(curl -s $DEBUG_API/chequebook/cashout/$peer | jq .result)"
while [ "$result" == "null" ]
do
sleep 5
result=$(curl -s $DEBUG_API/chequebook/cashout/$peer | jq .result)
done
}
function cashoutAll() {
local minAmount=$1
for peer in $(getPeers)
do
local uncashedAmount=$(getUncashedAmount $peer)
if (( "$uncashedAmount" > $minAmount ))
then
echo "uncashed cheque for $peer ($uncashedAmount uncashed)" >&2
cashout $peer
fi
done
}
function listAllUncashed() {
for peer in $(getPeers)
do
local uncashedAmount=$(getUncashedAmount $peer)
if (( "$uncashedAmount" > 0 ))
then
echo $peer $uncashedAmount
fi
done
}
case $1 in
cashout)
cashout $2
;;
cashout-all)
cashoutAll $MIN_AMOUNT
;;
list-uncashed|*)
listAllUncashed
;;
esac