-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathZCoinAdapter.py
68 lines (55 loc) · 1.9 KB
/
ZCoinAdapter.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
"""
Interactions with ZCoin RPC
Crafts JSON-RPC requests using the Requests library
"""
import requests
import json
class ZCoinAdapter():
"""
Example usage:
>>> z = ZCoinAdapter('127.0.0.1', 8888, 'john', 'password123')
>>> print(z.getinfo())
"""
hostname = None
port = None
username = None
password = None
def __init__(self, hostname, port=8888, username=None, password=None):
self.hostname = hostname
self.username = username
self.password = password
self.port = port
@property
def url(self):
if self.username is None:
return "http://{}:{}".format(self.hostname, self.port)
return "http://{}:{}@{}:{}".format(self.username, self.password,
self.hostname, self.port)
def call(self, method, *params):
headers = {'content-type': 'application/json'}
# Example echo method
payload = {
"method": method,
"params": list(params),
"jsonrpc": "2.0",
"id": 0,
}
response = requests.post(
self.url, data=json.dumps(payload), headers=headers).json()
if response['error'] is not None:
# see https://github.com/zcoinofficial/zcoin/blob/master/src/rpc/protocol.h for error codes
if response['error']['code'] == -10:
raise SyncingException(response['error']['message'])
raise Exception(response['error'])
return response['result']
def getinfo(self):
return self.call('getinfo')
def get_block_count(self):
info = self.getinfo()
return info['blocks']
def getnewaddress(self):
return self.call('getnewaddress')
def getreceivedbyaddress(self, address, confirmations=1):
return self.call('getreceivedbyaddress', address, confirmations)
class SyncingException(Exception):
pass