-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonrpc.py
421 lines (360 loc) · 14.3 KB
/
jsonrpc.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from ethereum.abi import ContractTranslator
import serpent
import requests
import json
class EthJsonRpc:
def __init__(self, host, port, contract_code=None, contract_address=None):
self.host = host
self.port = port
self.contract_code = None
self.signature = None
self.translation = None
self.contract_address = contract_address
self.update_code(contract_code)
def update_code(self, contract_code):
if contract_code:
self.contract_code = contract_code
self.signature = serpent.mk_full_signature(contract_code)
self.translation = ContractTranslator(self.signature)
def _call(self, method, params=None, _id=0):
if params is None:
params = []
data = json.dumps({
'jsonrpc': '2.0',
'method': method,
'params': params,
'id': _id
})
response = requests.post("http://{}:{}".format(self.host, self.port), data=data).json()
return response
def create_contract(self, contract_code, value=0, from_address=None, gas=0, gas_price=0):
self.update_code(contract_code)
byte_code = serpent.compile(contract_code)
self.contract_address = self.eth_sendTransaction(data=byte_code, value=value, from_address=from_address, gas=gas, gas_price=gas_price)
return self.contract_address
def eth_sendTransaction(self, to_address=None, function_name=None, data=None, value=0, from_address=None, gas=0, gas_price=0, code=None):
"""
Creates new message call transaction or a contract creation, if the data field contains code.
"""
if code:
self.update_code(code)
else:
self.update_code(self.eth_getCode(to_address))
if function_name:
if data is None:
data = []
data = self.translation.encode(function_name, data)
params = {
'from': from_address,
'to': to_address,
'gas': hex(gas) if gas else None,
'gasPrice': hex(gas_price) if gas_price else None,
'value': hex(value) if value else None,
'data': '0x{}'.format(data.encode('hex')) if data else None
}
return self._call('eth_sendTransaction', [params])
def eth_call(self, to_address, function_name, data=None, code=None, default_block="latest"):
"""
Executes a new message call immediately without creating a transaction on the block chain.
"""
if code:
self.update_code(code)
else:
self.update_code(self.eth_getCode(to_address))
if data is None:
data = []
data = self.translation.encode(function_name, data)
params = [
{
'to': to_address,
'data': '0x{}'.format(data.encode('hex'))
},
default_block
]
response = self._call('eth_call', params)
if function_name:
response = self.translation.decode(function_name, response[2:].decode('hex'))
return response
def web3_clientVersion(self):
"""
Returns the current client version.
"""
return self._call('web3_clientVersion')
def web3_sha3(self, data):
"""
Returns SHA3 of the given data.
"""
data = str(data).encode('hex')
return self._call('web3_sha3', [data])
def net_version(self):
"""
Returns the current network protocol version.
"""
return self._call('net_version')
def net_listening(self):
"""
Returns true if client is actively listening for network connections.
"""
return self._call('net_listening')
def net_peerCount(self):
"""
Returns number of peers currenly connected to the client.
"""
return self._call('net_peerCount')
def eth_version(self):
"""
Returns the current ethereum protocol version.
"""
return self._call('eth_version')
def eth_coinbase(self):
"""
Returns the client coinbase address.
"""
return self._call('eth_coinbase')
def eth_mining(self):
"""
Returns true if client is actively mining new blocks.
"""
return self._call('eth_mining')
def eth_gasPrice(self):
"""
Returns the current price per gas in wei.
"""
return self._call('eth_gasPrice')
def eth_accounts(self):
"""
Returns a list of addresses owned by client.
"""
return self._call('eth_accounts')
def eth_blockNumber(self):
"""
Returns the number of most recent block.
"""
return self._call('eth_blockNumber')
def eth_getBalance(self, address, default_block="latest"):
"""
Returns the balance of the account of given address.
"""
return self._call('eth_getBalance', [address, default_block])
def eth_getStorageAt(self, address, position, default_block="latest"):
"""
Returns the value from a storage position at a given address.
"""
return self._call('eth_getStorageAt', [address, hex(position), default_block])
def eth_getTransactionCount(self, address, default_block="latest"):
"""
Returns the number of transactions send from a address.
"""
return self._call('eth_getTransactionCount', [address, default_block])
def eth_getBlockTransactionCountByHash(self, block_hash):
"""
Returns the number of transactions in a block from a block matching the given block hash.
"""
return self._call('eth_getTransactionCount', [block_hash])
def eth_getBlockTransactionCountByNumber(self, block_number):
"""
Returns the number of transactions in a block from a block matching the given block number.
"""
return self._call('eth_getBlockTransactionCountByNumber', [hex(block_number)])
def eth_getUncleCountByblockHash(self, block_hash):
"""
Returns the number of uncles in a block from a block matching the given block hash.
"""
return self._call('eth_getUncleCountByblockHash', [block_hash])
def eth_getUncleCountByblockNumber(self, block_number):
"""
Returns the number of uncles in a block from a block matching the given block number.
"""
return self._call('eth_getUncleCountByblockNumber', [hex(block_number)])
def eth_getCode(self, address, default_block="latest"):
"""
Returns code at a given address.
"""
return self._call('eth_getCode', [address, default_block])
def eth_getBlockByHash(self, block_hash, transaction_objects=True):
"""
Returns information about a block by hash.
"""
return self._call('eth_getBlockByHash', [block_hash, transaction_objects])
def eth_flush(self):
"""
"""
return self._call('eth_flush')
def eth_getBlockByNumber(self, block_number, transaction_objects=True):
"""
Returns information about a block by hash.
"""
return self._call('eth_getBlockByNumber', [block_number, transaction_objects])
def eth_getTransactionByHash(self, transaction_hash):
"""
Returns the information about a transaction requested by transaction hash.
"""
return self._call('eth_getTransactionByHash', [transaction_hash])
def eth_getTransactionByblockHashAndIndex(self, block_hash, index):
"""
Returns information about a transaction by block hash and transaction index position.
"""
return self._call('eth_getTransactionByblock_hashAndIndex', [block_hash, hex(index)])
def eth_getTransactionByblockNumberAndIndex(self, block_number, index):
"""
Returns information about a transaction by block number and transaction index position.
"""
return self._call('eth_getTransactionByblock_numberAndIndex', [block_number, hex(index)])
def eth_getUncleByblockHashAndIndex(self, block_hash, index, transaction_objects=True):
"""
Returns information about a uncle of a block by hash and uncle index position.
"""
return self._call('eth_getUncleByblock_hashAndIndex', [block_hash, hex(index), transaction_objects])
def eth_getUncleByblockNumberAndIndex(self, block_number, index, transaction_objects=True):
"""
Returns information about a uncle of a block by number and uncle index position.
"""
return self._call('eth_getUncleByblock_numberAndIndex', [block_number, hex(index), transaction_objects])
def eth_getCompilers(self):
"""
Returns a list of available compilers in the client.
"""
return self._call('eth_getCompilers')
def eth_compileSolidity(self, code):
"""
Returns compiled solidity code.
"""
return self._call('eth_compileSolidity', [code])
def eth_compileLLL(self, code):
"""
Returns compiled LLL code.
"""
return self._call('eth_compileLLL', [code])
def eth_compileSerpent(self, code):
"""
Returns compiled serpent code.
"""
return self._call('eth_compileSerpent', [code])
def eth_newFilter(self, from_block="latest", to_block="latest", address=None, topics=None):
"""
Creates a filter object, based on filter options, to notify when the state changes (logs).
To check if the state has changed, call eth_getFilterChanges.
"""
_filter = {
'fromBlock': from_block,
'toBlock': to_block,
'address': address,
'topics': topics
}
return self._call('eth_newFilter', [_filter])
def eth_newBlockFilter(self, default_block="latest"):
"""
Creates a filter object, based on an option string, to notify when state changes (logs). To check if the state has changed, call eth_getFilterChanges.
"""
return self._call('eth_newBlockFilter', [default_block])
def eth_uninstallFilter(self, filter_id):
"""
Uninstalls a filter with given id. Should always be called when watch is no longer needed. Additonally Filters timeout when they aren't requested with eth_getFilterChanges for a period of time.
"""
return self._call('eth_uninstallFilter', [hex(filter_id)])
def eth_getFilterChanges(self, filter_id):
"""
Polling method for a filter, which returns an array of logs which occurred since last poll.
"""
return self._call('eth_getFilterChanges', [hex(filter_id)])
def eth_getFilterLogs(self, filter_id):
"""
Returns an array of all logs matching filter with given id.
"""
return self._call('eth_getFilterLogs', [hex(filter_id)])
def eth_getLogs(self, filter_object):
"""
Returns an array of all logs matching a given filter object.
"""
return self._call('eth_getLogs', [filter_object])
def eth_getWork(self):
"""
Returns the hash of the current block, the seedHash, and the difficulty to be met.
"""
return self._call('eth_getWork')
def eth_submitWork(self, nonce, header, mix_digest):
"""
Used for submitting a proof-of-work solution.
"""
return self._call('eth_submitWork', [nonce, header, mix_digest])
def db_putString(self, database_name, key_name, string):
"""
Stores a string in the local database.
"""
return self._call('db_putString', [database_name, key_name, string])
def db_getString(self, database_name, key_name):
"""
Stores a string in the local database.
"""
return self._call('db_getString', [database_name, key_name])
def db_putHex(self, database_name, key_name, string):
"""
Stores binary data in the local database.
"""
return self._call('db_putHex', [database_name, key_name, string.encode('hex')])
def db_getHex(self, database_name, key_name):
"""
Returns binary data from the local database.
"""
return self._call('db_getString', [database_name, key_name]).decode('hex')
def shh_version(self):
"""
Returns the current whisper protocol version.
"""
return self._call('shh_version')
def shh_post(self, topics, payload, priority, ttl, _from=None, to=None):
"""
Sends a whisper message.
"""
whisper_object = {
'from': _from,
'to': to,
'topics': topics,
'payload': payload,
'priority': priority,
'ttl': ttl
}
return self._call('shh_post', [whisper_object])
def shh_newIdentinty(self):
"""
Creates new whisper identity in the client.
"""
return self._call('shh_newIdentinty')
def shh_hasIdentity(self, address):
"""
Checks if the client hold the private keys for a given identity.
"""
return self._call('shh_hasIdentity', [address])
def shh_newGroup(self):
"""
"""
return self._call('shh_hasIdentity')
def shh_addToGroup(self):
"""
"""
return self._call('shh_addToGroup')
def shh_newFilter(self, to, topics):
"""
Creates filter to notify, when client receives whisper message matching the filter options.
"""
_filter = {
'to': to,
'topics': topics
}
return self._call('shh_newFilter', [_filter])
def shh_uninstallFilter(self, filter_id):
"""
Uninstalls a filter with given id. Should always be called when watch is no longer needed.
Additonally Filters timeout when they aren't requested with shh_getFilterChanges for a period of time.
"""
return self._call('shh_uninstallFilter', [hex(filter_id)])
def shh_getFilterChanges(self, filter_id):
"""
Polling method for whisper filters.
"""
return self._call('shh_getFilterChanges', [hex(filter_id)])
def shh_getMessages(self, filter_id):
"""
Get all messages matching a filter, which are still existing in the node.
"""
return self._call('shh_getMessages', [hex(filter_id)])