-
Notifications
You must be signed in to change notification settings - Fork 5
/
derohe_monitor.py
executable file
·463 lines (404 loc) · 18.5 KB
/
derohe_monitor.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/usr/bin/env python3
'''
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.
'''
import sys
import time
import json
import requests
import argparse
from beepy import beep
from dateutil import parser
from collections import deque
from datetime import datetime, timedelta
RATIO = 100000
TELEGRAM_BOT_TOKEN = None
TELEGRAM_CHAT_ID = None
DISCORD_WEBHOOK = None
wallet_rpc_server = "http://127.0.0.1:10103/json_rpc"
node_rpc_server = "http://127.0.0.1:10103/json_rpc"
HEIGHT = 0
DAYS = 7
def get_arguments():
"""
parse the argument provided to the script
"""
parser = argparse.ArgumentParser(
description='DeroHE wallet monitor',
epilog='Created by 51|Fu.R4nk',
usage='python3 %s [-a]')
parser.add_argument('--rpc-server',
action='store',
help='Wallet rpc-server address. Default 127.0.0.1:10103')
parser.add_argument('--node-rpc-server',
action='store',
help='Node wallet rpc-server address.')
parser.add_argument('--tg-bot',
action='store',
help='Telegram bot token')
parser.add_argument('--tg-chat',
action='store',
help='Telegram chat id')
parser.add_argument('--discord-webhook',
action='store',
help='Discord webhook url')
parser.add_argument('--notify-count',
action='store',
help="Notify if you don't get reward after X minutes. defult disabled")
parser.add_argument('--one-shot',
action='store_true',
help="Display data and exit")
parser.add_argument('--day-range',
action='store', type=int,
help="Number of days to plot. Default 7")
parser.add_argument('--sound',
action='store_true', default=False,
help="Play sound when a new miniblock is found")
return parser.parse_args()
class WalletParser():
def __init__(self, rpc_server, days=7, sound=False):
self.rpc_server = rpc_server
self.height = self.get_height()
self.days = int(days)
from_block = 5000 * self.days # considering 18 second block is around 4800 block every day
self.min_height = self.height - from_block if (self.height - from_block) >= 0 else 0
self.gains = self.populate_history()
self.daily_gain = self.daily_totals()
self.sound = sound
def generic_call(self, method, params=None):
headers = {'Content-Type': 'application/json'}
body = {"jsonrpc": "2.0",
"id": "1",
"method": method,
"params": params}
try:
r = requests.post(self.rpc_server, json=body,
headers=headers, timeout=(9, 120))
except:
print("RPC not found. Terminating")
sys.exit()
return r
def get_balance(self):
result = self.generic_call("GetBalance")
try:
return json.loads(result.text)['result']['balance']/RATIO
except:
print("Fail to get balance from RPC. Terminating")
sys.exit()
return None
def get_height(self):
result = self.generic_call("GetHeight")
try:
return json.loads(result.text)['result']['height']
except:
print("Fail to get height from RPC. Terminating")
sys.exit()
return None
def get_transfers(self, param=None):
result = self.generic_call("GetTransfers", param)
return json.loads(result.text)
def clean_date(self, date):
return parser.parse(date, ignoretz=True).replace(second=0, microsecond=0)
def discretize_history(self, items, start_date):
amount_by_minute = dict()
now = datetime.today().replace(second=0, microsecond=0)
while start_date <= now:
amount_by_minute[start_date] = 0
start_date += timedelta(minutes=1)
max_height = 0
for item in items:
short_date = self.clean_date(item['time'])
if short_date in amount_by_minute.keys():
amount_by_minute[short_date] += item['amount']
return amount_by_minute
def daily_totals(self):
amount_by_day = dict()
start_date = (datetime.today() - timedelta(days=self.days)
).replace(hour=0, minute=0, second=0, microsecond=0)
now = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
while start_date <= now:
amount_by_day[start_date] = 0
start_date += timedelta(days=1)
while len(amount_by_day) > self.days:
amount_by_day.pop(min(amount_by_day))
raw_items = self.get_transfers({'coinbase': True, 'min_height': self.min_height})
if 'entries' in raw_items['result']:
items = raw_items['result']['entries']
for item in items:
short_date = self.clean_date(item['time']).replace(hour=0, minute=0, second=0, microsecond=0)
if short_date in amount_by_day.keys():
amount = item['amount']/RATIO
if amount > 100:
continue
amount_by_day[short_date] += amount
return amount_by_day
def populate_history(self):
coinbase = self.get_transfers({'coinbase': True, 'min_height': self.min_height})
last_7D = (datetime.today() - timedelta(days=7)
).replace(second=0, microsecond=0)
last_24H = datetime.today() - timedelta(days=1)
last_6H = datetime.today() - timedelta(hours=7)
last_1H = datetime.today() - timedelta(hours=2)
last_15M = datetime.today() - timedelta(minutes=15)
gains = dict()
gains['avg_15'] = deque(maxlen=15)
gains['avg_60'] = deque(maxlen=60)
gains['avg_360'] = deque(maxlen=360)
gains['avg_1440'] = deque(maxlen=1440)
gains['avg_10080'] = deque(maxlen=10080)
if 'entries' in coinbase['result']:
short_hist = self.discretize_history(coinbase['result']['entries'], last_7D)
for item in short_hist:
amount = short_hist[item]/RATIO
if amount > 100:
continue
if item > last_7D:
gains['avg_10080'].append(amount)
if item > last_24H:
gains['avg_1440'].append(amount)
if item > last_6H:
gains['avg_360'].append(amount)
if item > last_1H:
gains['avg_60'].append(amount)
if item > last_15M:
gains['avg_15'].append(amount)
return gains
def update_chart(self, diff):
today = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
if today == max(self.daily_gain):
self.daily_gain[today] += diff
elif today > max(self.daily_gain):
self.daily_gain.pop(min(self.daily_gain))
self.daily_gain[today] = diff
def get_diff(self, height):
amounts = 0.0
coinbase = self.get_transfers({'coinbase': True, 'min_height': height})
if 'entries' in coinbase['result'].keys():
items = coinbase['result']['entries']
for item in items:
if item['height'] <= height:
break
amount = item['amount']/RATIO
if amount > 100:
continue
amounts += amount
if self.sound:
beep(sound="coin")
return amounts
def update(self):
diff = 0.0
current_height = self.get_height()
if current_height > self.height:
diff = self.get_diff(self.height)
self.height = current_height
self.update_chart(diff)
for item in self.gains:
self.gains[item].append(diff)
class DerodParser():
def __init__(self, rpc_server):
self.rpc_server=rpc_server
self.daily_gain=self.avg_diff()
def generic_call(self, method, params=None):
headers = {'Content-Type': 'application/json'}
body = {"jsonrpc": "2.0",
"id": "1",
"method": method,
"params": params}
try:
r = requests.post(self.rpc_server, json=body,
headers=headers, timeout=(9, 120))
except:
print("RPC not found. Terminating")
sys.exit()
return r
def get_block(self, height):
result = self.generic_call("DERO.GetBlock", {"height": height})
return json.loads(result.text)
def get_info(self):
result = self.generic_call("DERO.GetInfo")
return json.loads(result.text)
def get_height(self):
data = self.generic_call("DERO.GetHeight")
return json.loads(data.text)['result']['height']
def avg_diff(self):
current_height = self.get_height()
start_date = (datetime.today() - timedelta(days=7)
).replace(hour=0, minute=0, second=0, microsecond=0)
diff_by_day = dict()
now = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
while start_date <= now:
diff_by_day[start_date] = []
start_date += timedelta(days=1)
while len(diff_by_day) > 7:
diff_by_day.pop(min(diff_by_day))
for i in range(current_height-35000, current_height):
print(i)
blk = self.get_block(i)
short_date = datetime.fromtimestamp(blk['result']['block_header']['timestamp']//1000).replace(hour=0, minute=0, second=0, microsecond=0)
if short_date in diff_by_day.keys():
diff_by_day[short_date].append(int(blk['result']['block_header']['difficulty']))
for item in diff_by_day:
if len(diff_by_day[item]) == 0:
diff_by_day[item] = 0
else:
diff_by_day[item] = sum(diff_by_day[item])/len(diff_by_day[item])/1000000000
return diff_by_day
def plot_graph(daily_gain, unit='DERO'):
colors = {"blue": "\033[96m",
"green": "\033[92m",
"red": "033[93m",
}
lines = ""
bar = ""
max_value = max(daily_gain.values())
count = 0
for item in daily_gain:
delimiter = "█" if count%2 == 0 else "░"
if max_value > 0:
bar = delimiter*(int(daily_gain[item]/max_value*50))
lines += "| {:10}:{:51}{:9.4f} {:4} |\n".format(item.strftime('%Y-%m-%d'), bar, round(daily_gain[item],4), unit)
count += 1
return lines
def telegram(message):
url = 'https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s' % (
TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID, message)
_ = requests.get(url, timeout=10)
def discord(message):
data = {'content': message}
_ = requests.post(DISCORD_WEBHOOK, data, timeout=10)
def notify(message):
if (TELEGRAM_BOT_TOKEN is not None
and TELEGRAM_BOT_TOKEN != ""
and TELEGRAM_CHAT_ID is not None
and TELEGRAM_CHAT_ID != ""):
telegram(message)
if (DISCORD_WEBHOOK is not None
and DISCORD_WEBHOOK != ""):
discord(message)
def print_avg(data, supposed_len):
if supposed_len == 1:
return "\033[96m{}\033[00m".format(round(sum(data)/supposed_len, 4))
if len(data) == supposed_len:
return "\033[92m{}\033[00m".format(round(sum(data)/supposed_len, 4))
return "\033[93m{}\033[00m".format(round(sum(data)/supposed_len, 4))
def print_sum(data, supposed_len):
if supposed_len == 1:
return "\033[96m{:.4f}\033[00m".format(round(sum(data), 4))
if len(data) == supposed_len:
return "\033[92m{:.4f}\033[00m".format(round(sum(data), 4))
return "\033[93m{:.4f}\033[00m".format(round(sum(data), 4))
def compute_power(gain, diff):
power = dict()
for item in gain:
power[item] = (gain[item]/0.06150)*((diff[item]*1000000)/48000)/1000
return power
def run(rpc_server, max_zero, node_rpc_server=None, one_shot=False, sound=False, main_rpc=None):
count_failure = 0
passing_time = 0
flag_notify = True
diff = 0.0
wp = WalletParser(rpc_server, DAYS, sound)
node_wp = None if node_rpc_server is None else WalletParser(node_rpc_server)
dp = None if main_rpc is None else DerodParser(main_rpc)
while True:
lines = ""
sys.stdout.write("\r")
lines += "--------------------------------------------------------------------------------\n"
wp.update()
if node_wp is not None:
node_wp.update()
if dp is not None:
power = compute_power(wp.days, dp.daily_gain)
lines += "|{:^12}:{:^10}:{:^10}:{:^10}:{:^10}:{:^10}:{:^10}|\n".format(
'', '1m', '15m', '1h', '6h', '24h', '7d')
lines += "|{:^12}:{:^20}:{:^20}:{:^20}:{:^20}:{:^20}:{:^20}|\n".format('gain',
print_sum(
[diff], 1),
print_sum(
wp.gains['avg_15'], 15),
print_sum(
wp.gains['avg_60'], 60),
print_sum(
wp.gains['avg_360'], 360),
print_sum(
wp.gains['avg_1440'], 1440),
print_sum(
wp.gains['avg_10080'], 10080))
if node_wp is not None:
lines += "|{:>12}:{:^20}:{:^20}:{:^20}:{:^20}:{:^20}:{:^20}|\n".format('node gain',
print_sum(
[diff], 1),
print_sum(
node_wp.gains['avg_15'], 15),
print_sum(
node_wp.gains['avg_60'], 60),
print_sum(
node_wp.gains['avg_360'], 360),
print_sum(
node_wp.gains['avg_1440'], 1440),
print_sum(
node_wp.gains['avg_10080'], 10080))
lines += "|"+" "*78+"|\n"
if diff == 0.0:
count_failure += 1
else:
count_failure = 0
flag_notify = True
lines += "| {:14}:{:61} |\n".format("Current height", wp.height)
lines += "| {:14}:{:61} |\n".format("Wallet amount", wp.get_balance())
if node_wp is not None:
lines += "| {:14}:{:61} |\n".format("Node amount", node_wp.get_balance())
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
lines += "| {:14}:{:>61} |\n".format("Date", formatted_date)
lines += "--------------------------------------------------------------------------------\n"
lines += plot_graph(wp.daily_gain)
if dp is not None:
lines += "--------------------------------------------------------------------------------\n"
lines += plot_graph(dp.daily_gain, "GH/s")
lines += "--------------------------------------------------------------------------------\n"
lines += plot_graph(power, "MH/s")
lines += "--------------------------------------------------------------------------------\n"
if max_zero > 0:
if count_failure > max_zero:
message = 'Since {} minutes you are not receiving rewards!'.format(
count_failure)
lines += "\033[91m{}\033[00m\n".format(message)
if flag_notify:
notify(message)
count_failure = 0
if passing_time > 0:
for item in range(len(lines.split('\n'))-1):
sys.stdout.write('\x1b[1A')
sys.stdout.write('\x1b[2K')
sys.stdout.write(lines)
sys.stdout.flush()
passing_time += 1
if one_shot:
sys.exit(0)
time.sleep(60)
if __name__ == '__main__':
max_zero = 0
args = get_arguments()
node_rpc_server = None
if args.rpc_server:
wallet_rpc_server = "http://{}/json_rpc".format(args.rpc_server)
if args.node_rpc_server:
node_rpc_server = "http://{}/json_rpc".format(args.node_rpc_server)
if args.tg_bot:
TELEGRAM_BOT_TOKEN = args.tg_bot
if args.tg_chat:
TELEGRAM_CHAT_ID = args.tg_chat
if args.discord_webhook:
DISCORD_WEBHOOK = args.discord_webhook
if args.notify_count:
max_zero = int(args.notify_count)
if args.day_range:
DAYS = args.day_range
run(wallet_rpc_server, max_zero, node_rpc_server, args.one_shot, args.sound)