forked from delta1512/GRC-Wallet-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.py
40 lines (35 loc) · 1.29 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
import aiohttp
import logging
import json
import grcconf as g
'''
1 - protocol/client error
2 - invalid args
3 - net exception
4 - invalid type
'''
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:
response = await resp.json()
except Exception as E:
logging.warning('Exception triggered in communication with GRC client: %s', E)
return 3
if response['error'] != None:
if response['error']['code'] == -17:
return None
logging.warning('Error response sent by GRC client: %s', response['error'])
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 unlock():
return await query('walletpassphrase', [g.grc_pass, 999999999, False])