-
Notifications
You must be signed in to change notification settings - Fork 5
/
wallet.py
100 lines (84 loc) · 3.45 KB
/
wallet.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
import aiohttp
import logging
import json
from re import finditer
from datetime import datetime
import grcconf as g
'''
1 - protocol/client error
2 - invalid args
3 - net exception
4 - invalid type
'''
def scan_projects(averages: str) -> dict:
final = {}
for project_info in averages.split(';')[:-1]:
project, team_rac, __ = project_info.split(',')
if project != 'NeuralNetwork':
final[project] = team_rac + ' RAC'
return final
async def query(cmd, params):
if not all([isinstance(cmd, str), isinstance(params, list)]):
logging.warning('Invalid data sent to wallet query')
return 2
command = json.dumps({'method' : cmd, 'params' : params})
try:
async with aiohttp.ClientSession() as session:
async with session.post(g.rpc_url, data=command, headers={'content-type': "application/json", 'cache-control': "no-cache"}, auth=aiohttp.BasicAuth(g.rpc_usr, password=g.rpc_pass)) as resp:
# split up steps as a workaround for resp.json() not working
# with output from `gettransaction` for superblock transactions
# (aiohttp trying to decode <BINARY> part in hashboinc string)
raw = await resp.read()
text = raw.decode('utf-8', 'ignore')
response = json.loads(text)
except Exception as E:
logging.warning('Exception triggered in communication with GRC client: %s', E)
logging.warning('CMD: %s ARGS: %s', cmd, params)
return 3
if response['error'] != None:
if response['error']['code'] == -17:
return None
logging.warning('Error response sent by GRC client: %s', response['error'])
logging.warning('CMD: %s ARGS: %s', cmd, params)
return 1
else:
return response['result']
async def tx(addr, amount):
if isinstance(addr, str) and len(addr) > 1:
return await query('sendtoaddress', [addr, amount])
return 4
async def get_block(height):
current_block = await query('getblockcount', [])
if height < 0 or height > current_block:
return None
else:
data = {}
block_data = await query('getblockbynumber', [height])
if type(block_data) is int:
return None
data['Height'] = height
data['Hash'] = block_data['hash']
data['Time (UTC)'] = datetime.utcfromtimestamp(block_data['time']).isoformat(' ')
data['Difficulty'] = round(block_data['difficulty'], 4)
data['Net Weight'] = round(g.NET_W_MULT * block_data['difficulty'])
data['No. of TXs'] = len(block_data['tx'])
data['Amount Minted'] = block_data['mint']
data['Superblock'] = 'No' if (block_data['IsSuperBlock'] == 0) else 'Yes'
return data
async def get_last_superblock():
listdata_sb = await query('listdata', ['superblock'])
height = listdata_sb['block_number']
sb_details = await query('showblock', [int(height)])
sb_time = datetime.utcfromtimestamp(sb_details['time']).isoformat(' ')
final = {'Height' : height, 'Time (UTC)' : sb_time}
final.update(scan_projects(listdata_sb['averages']))
return final
async def get_latest_stakes():
data = await query('listtransactions', ['', g.tx_lookback])
stakes = {}
for tx in data:
if tx['category'] == 'generate':
stakes[tx['txid']] = g.STK_REWARD
return stakes
async def unlock():
return await query('walletpassphrase', [g.grc_pass, 999999999, False])