-
Notifications
You must be signed in to change notification settings - Fork 9
/
json_rpc.py
183 lines (170 loc) · 7.62 KB
/
json_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import json
from aiohttp import ClientSession, log
class PascJsonRpc():
def __init__(self, node_url: str = 'localhost:4003'):
self.node_url = node_url
self.req_id = 1
async def jsonrpc_request(self, method: str, params: dict = None, timeout: int = 30) -> dict:
try:
request_json = {
"jsonrpc": "2.0",
"method":method,
"id": self.req_id
}
if params is not None:
request_json['params'] = params
self.req_id += 1
async with ClientSession() as session:
async with session.post(f'{self.node_url}', json=request_json, timeout=timeout) as resp:
if resp.status > 299:
log.server_logger.error('Received status code %d from request %s', resp.status, json.dumps(request_json))
raise Exception
return await resp.json(content_type=None)
except Exception:
log.server_logger.exception('Exception in JRPC Request')
return None
async def findaccounts(self, start: int, b58_pubkey: str):
method = 'findaccounts'
params = {
'start': start,
'b58_pubkey': b58_pubkey
}
response = await self.jsonrpc_request(method, params)
if response is None or 'result' not in response or not isinstance(response['result'], list):
if response is not None:
log.server_logger.error(f'findaccounts: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def getaccount(self, pasa: int):
method = 'getaccount'
params = {
'account': pasa
}
response = await self.jsonrpc_request(method, params)
if response is None or 'result' not in response or 'balance' not in response['result']:
if response is not None:
log.server_logger.error(f'getaccount: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def sendto(self, sender: int, target: int, amount: float, payload:str, fee: float = 0.0001):
method = 'sendto'
params = {
'sender': sender,
'target': target,
'amount': amount - fee,
'fee': fee,
'payload': payload,
'payload_method': 'none'
}
response = await self.jsonrpc_request(method, params)
if response is None or 'result' not in response or 'ophash' not in response['result']:
if response is not None:
log.server_logger.error(f'sendto: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def changeaccountinfo(self, signer: int, target: int, new_b58_pubkey: str, fee: float = 0.0001):
method = 'changeaccountinfo'
params = {
'account_signer': signer,
'account_target': target,
'new_b58_pubkey': new_b58_pubkey,
'fee': fee
}
response = await self.jsonrpc_request(method, params)
if response is None or 'error' in response or 'result' not in response or 'ophash' not in response['result']:
if response is not None:
log.server_logger.error(f'changeaccountinfo: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def changekey(self, account: int, new_b58_pubkey: str, fee: float = 0.0001):
method = 'changekey'
params = {
'account': account,
'new_b58_pubkey': new_b58_pubkey,
'fee': fee
}
response = await self.jsonrpc_request(method, params)
if response is None or 'error' in response or 'result' not in response or 'ophash' not in response['result']:
if response is not None:
log.server_logger.error(f'changekey: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def getblockcount(self):
method = 'getblockcount'
response = await self.jsonrpc_request(method)
if response is None or 'result' not in response or not isinstance(response['result'], int):
if response is not None:
log.server_logger.error(f'getblockcount: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def getblockoperations(self, block: int):
method = 'getblockoperations'
params = {
'block': block
}
response = await self.jsonrpc_request(method, params)
if response is None or 'result' not in response or not isinstance(response['result'], list):
if response is not None:
log.server_logger.error(f'getblockoperations: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def getpendings(self):
method = 'getpendings'
response = await self.jsonrpc_request(method)
if response is None or 'result' not in response or not isinstance(response['result'], list):
if response is not None:
log.server_logger.error(f'getpendings: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def findoperation(self, ophash: str):
method = 'findoperation'
params = {
'ophash': ophash
}
response = await self.jsonrpc_request(method, params)
if response is None or 'result' not in response or 'error' in response:
if response is not None:
log.server_logger.error(f'findoperation: received invalid jsonrpc response {json.dumps(response)}')
return None
return response['result']
async def send_and_transfer(self, sender: int, target: int, amount: int, payload: str, b58_pubkey:str, fee: int = 0.0002):
method = 'multioperationaddoperation'
senders = {
'account': sender,
'amount': amount,
'payload': payload
}
receivers = {
'target': target,
'amount': amount - fee,
'payload': payload
}
changers = {
'account': sender,
'new_b58_pubkey': b58_pubkey
}
params = {
'auto_n_operation': True,
'senders': senders,
'receivers': receivers,
'changesinfo': changers
}
response = await self.jsonrpc_request(method, params)
if response is None or 'result' not in response or 'error' in response:
if response is not None:
log.server_logger.error(f'multioperationaddoperation: received invalid jsonrpc response {json.dumps(response)}')
return None
rawop = response['result']['rawoperations']
execop_resp = await self.executeoperations(rawop)
return execop_resp
async def executeoperations(self, rawoperations: str):
method = 'executeoperations'
params = {
'rawoperations': rawoperations
}
response = await self.jsonrpc_request(method, params)
if response is None or 'result' not in response or 'error' in response:
if response is not None:
log.server_logger.error(f'executeoperations: received invalid jsonrpc response {json.dumps(response)}')
return None
rawop = response['result']