-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.py
103 lines (85 loc) · 3.09 KB
/
rpc.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
import util
import json, requests
def get_rpc(method, params=None, wallet=None):
conf = util.get_conf('rpc')
url = f"http://{conf['user']}:{conf['password']}@{conf['ip']}:{conf['port']}"
if wallet: url += f'/wallet/{wallet}'
headers = {'content-type': 'application/json'}
paramslist = params
if type(params) == str and params:
paramslist = []
for p in params.split(','):
try:
if '.' in p: paramslist.append(float(p))
else: paramslist.append(int(p))
except:
paramslist.append(p)
payload = json.dumps({"method": method, "params": paramslist, "jsonrpc": "2.0", "id":"node-dash"})
try:
response = requests.post(url, headers=headers, data=payload).json()
if response['error']:
return response['error']
else:
return response['result']
except:
raise Exception('RPC connection failure, run nodecmd settings to edit')
def get_info():
info = {}
cfg = util.get_conf('info')
# blockchain
bc = get_rpc('getblockchaininfo')
info['chain'] = bc['chain']
info['blocks'] = f"{int(bc['blocks']):,}"
info['node.size'] = f"{int(bc['size_on_disk']/(1024**3))} GB"
# network
nt = get_rpc('getnetworkinfo')
info['node'] = f"v{nt['version']}"
info['connections'] = nt['connections']
# hashrate
n = int(cfg['hashrate_blocks'])
nh = get_rpc('getnetworkhashps', [100])
info['hashrate'] = f"{int(nh/1e18)} EH/s"
info['difficulty'] = f"{int(bc['difficulty']/1e12)}T"
# mempool
mp = get_rpc('getmempoolinfo')
info['mempool.count'] = f"{mp['size']:,}"
info['mempool.size'] = f"{int(mp['bytes']/(1024**2))} MB"
info['mempool.total_fee'] = round(float(mp['total_fee']), 4)
# wallets
wcount, wtotal = get_wallets(True)
info['wallet_count'] = wcount
info['total_balance'] = f"{wtotal}"
# estimated fee
# outputs in BTC/kvB
n = int(cfg['fee_estimate_blocks'])
fe = get_rpc('estimatesmartfee', [n])
info['fee_estimate'] = f"{int(fe['feerate'] * 100e6 / 1000)} s/vB"
return info
def get_wallets(only_summary=False):
wallets = get_rpc('listwallets')
balance = 0
wallet_info = []
for w in wallets:
wt = get_rpc('getwalletinfo', wallet=w)
balance += float(wt['balance'])
wallet_info.append(wt)
if only_summary:
return len(wallets), balance
else:
return wallet_info
def get_unspent():
wallets = get_rpc('listwallets')
wallet_info = []
for w in wallets:
utxos = get_rpc('listunspent', wallet=w)
utxos = sorted(utxos, reverse=True, key=lambda x: x['amount'])
if utxos: wallet_info.append((w, utxos))
return wallet_info
def get_chainstate():
ux = get_rpc('gettxoutsetinfo')
info = {}
info['utxo.count'] = f"{ux['txouts']:,}"
info['utxo.total_amt'] = f"{int(ux['total_amount']):,}"
info['utxo.txs'] = f"{ux['transactions']:,}"
info['utxo.size'] = f"{round(ux['disk_size']/(1024**3),1)} GB"
return info