-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcashout.py
136 lines (111 loc) · 4.62 KB
/
cashout.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import requests
import time
#Configure start and end nodes. If running 1 node set START_NODE = 1 and END_NODE = 1
START_NODE = 1
END_NODE = 10
START_NODE_PORT = 1635 # first nodes port e.g. 1635
PORT_INTERVAL = 100
MIN_AMOUNT = 10000000000000
GAS_LIMIT = 100000 #this should be enough
DEBUG_ENDPOINT = 'http://localhost'
def node_to_port(node):
zero_node_port = START_NODE_PORT - PORT_INTERVAL # node 0 - so - PORT_INTERVAL from node 1 port
return zero_node_port + (node * PORT_INTERVAL)
def get_peers(port):
response = requests.get(DEBUG_ENDPOINT + ":{}/chequebook/cheque".format(port))
if response.status_code == 404 or response.status_code == 500:
print("Get peers: {}".format(response.status_code))
return 0
last_cheques = response.json()['lastcheques']
peers = []
# print(last_cheques)
for node in last_cheques:
if 'peer' in node:
peers.append(node['peer'])
# print(peers)
return peers
def get_cumulative_payout(port, peer, peer_index):
response = requests.get(DEBUG_ENDPOINT + ":{}/chequebook/cheque/{}".format(port, peer))
if response.status_code == 404 or response.status_code == 500:
print("Get cumulative: {}".format(response.status_code))
return 0
lastreceived = response.json()['lastreceived']
if lastreceived == None:
# print(lastreceived)
return 0
else:
# print(lastreceived['payout'])
return lastreceived['payout']
def get_last_cashed_payout(port, peer, peer_index):
response = requests.get(DEBUG_ENDPOINT + ":{}/chequebook/cashout/{}".format(port, peer))
# print(response.json())
# print(response)
cashout = response.json()
if response.status_code == 404 or response.status_code == 500:
# print("Get last cashed: {}".format(response.status_code))
# print("** Peer {}: {} **".format(peer_index, cashout['message']))
return 0
elif cashout['cumulativePayout'] == None:
return 0
else:
return cashout['cumulativePayout']
def get_uncashed_amount(port, peer, peer_index):
cumulative_payout = get_cumulative_payout(port, peer, peer_index)
if cumulative_payout == 0:
# print(cumulative_payout)
return cumulative_payout
else:
cashed_payout = get_last_cashed_payout(port, peer, peer_index)
uncashed_amount = cumulative_payout - cashed_payout
# print(uncashed_amount)
return uncashed_amount
def cashout(port, peer, peer_index):
txn_post = requests.post(DEBUG_ENDPOINT + ":{}/chequebook/cashout/{}".format(port, peer))
txn_hash = txn_post.json()
if txn_post.status_code == 500:
print("Error - cannot cash cheque!")
return
else:
print("** Peer {}: {}... cashout in txn https://goerli.etherscan.io/tx/{} **".format(peer_index, peer[0:8],
txn_hash[
'transactionHash']))
count = 0
while True:
response = requests.get(DEBUG_ENDPOINT + ":{}/chequebook/cashout/{}".format(port, peer), headers={"gas-limit":str(GAS_LIMIT)})
cashout_result = response.json()
if cashout_result['result'] == None:
print("Waiting... ", end="", flush=True)
time.sleep(5)
count += 1
if count == 12: #try for 1 minute and move on if txn not successful.
print("Moving on ... Check goerli for issues...")
break
continue
else:
print("Successful cashout")
return
def cashout_all_peers(port):
peers = get_peers(port)
print("Found {} peers... ".format(len(peers)))
for peer_index, peer in enumerate(peers):
uncashed_amount = get_uncashed_amount(port, peer, peer_index + 1)
print("[ Peer {}: {}... Uncashed: {} ] ... ".format(peer_index + 1, peer[0:8], uncashed_amount), end="",
flush=True)
if uncashed_amount > MIN_AMOUNT:
print("Cashing out!")
cashout(port, peer, peer_index + 1)
else:
print("Next.")
return
def run_cashout_all_nodes():
for node in range(START_NODE, END_NODE + 1):
port = node_to_port(node)
try:
print("Running cashout on node: {} ...".format(node), end="", flush=True)
requests.get('http://localhost:{}'.format(port))
cashout_all_peers(port)
except Exception as e:
print("Error on node {}: {}".format(node, e))
continue
if __name__ == "__main__":
run_cashout_all_nodes()