-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc_status.py
102 lines (79 loc) · 2.94 KB
/
btc_status.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
import os
import json
import requests
from collections import Counter
from bitcoinrpc.authproxy import AuthServiceProxy
def save_dict(data, filename):
json.dump(data, open(filename, "w"))
def load_dict(filename):
if os.path.exists(filename):
return json.load(open(filename))
else:
return {}
def slacker(channel, text):
webhook_url = os.environ["SLACK"]
slack_data = {"channel": channel, "text": text}
response = requests.post(
webhook_url,
data=json.dumps(slack_data),
headers={"Content-Type": "application/json"},
)
return response.text
cnxn_str = "http://{}:{}@127.0.0.1:8332".format(
os.environ["RPC_USER"], os.environ["RPC_PASSWORD"]
)
def engine(commands):
return AuthServiceProxy(cnxn_str).batch_(commands)
def shift_fee(fee):
return round(float(fee * 100000), 1)
def get_tx(txid):
return engine([["getrawtransaction", txid, True]])[0]
def much_info():
commands = [
["getpeerinfo"],
["getblockcount"],
["getmempoolinfo"],
["getbestblockhash"],
["estimatesmartfee", 1, "ECONOMICAL"],
["estimatesmartfee", 6, "ECONOMICAL"],
["estimatesmartfee", 72, "ECONOMICAL"],
]
names = ["_".join(str(s) for s in c) for c in commands]
results = engine(commands)
kv = dict(zip(names, results))
block = engine([["getblock", kv["getbestblockhash"]]])[0]
peers = kv["getpeerinfo"]
cleaned = {
"peers": len(peers),
"blocks": kv["getblockcount"], # ['blocks']
"block_tx": len(block["tx"]),
"block_mb": round(block["size"] / 1000000, 4),
# "difficulty":round(float(kv["getblockchaininfo"]['difficulty']/1000000000000), 3),
"memsize_tx": kv["getmempoolinfo"]["size"],
"memsize_mb": round(kv["getmempoolinfo"]["bytes"] / 1000000, 2),
"feerate_01block": shift_fee(kv["estimatesmartfee_1_ECONOMICAL"]["feerate"]),
"feerate_01hour": shift_fee(kv["estimatesmartfee_6_ECONOMICAL"]["feerate"]),
"feerate_12hour": shift_fee(kv["estimatesmartfee_72_ECONOMICAL"]["feerate"]),
"onion_cnxns": sum(1 for p in peers if "onion" in p["addrlocal"]),
}
peer_versions = Counter(
["peer_" + p["subver"][1:].split(":")[0].lower() for p in peers]
)
ban_scores = Counter(["score_" + str(p["banscore"]) for p in peers])
cleaned = {**cleaned, **peer_versions, **ban_scores}
return cleaned
data = much_info()
filename = os.path.expanduser("~/btc/btc.json")
saved_data = load_dict(filename)
if (
data["peers"] != saved_data.get("peers", 0)
or data["blocks"] - saved_data.get("blocks", 0) >= 16
):
infostr = "\n".join("%s: %s" % (k, v) for k, v in sorted(data.items()))
slacker("#alerts", infostr)
save_dict(data, filename)
"""
blocktx = engine([["getblock", kv["getbestblockhash"], 2]])[0]['tx']
outs = [t['vout'] for t in blocktx[1:]]
max_tx_out = round(max([max([o['value'] for o in out]) for out in outs]),2)
"""