Add get block eth websocket
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,7 +1,11 @@
|
||||
import json
|
||||
|
||||
from cic_syncer.client import translate
|
||||
|
||||
|
||||
translations = {
|
||||
'block_number': 'hex_to_int',
|
||||
'block_number': translate.hex_to_int,
|
||||
'get_block': json.dumps,
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +14,7 @@ class EVMResponse:
|
||||
def __init__(self, item, response_object):
|
||||
self.response_object = response_object
|
||||
self.item = item
|
||||
self.fn = getattr(translate, translations[self.item])
|
||||
self.fn = translations[self.item]
|
||||
|
||||
|
||||
def get_error(self):
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import uuid
|
||||
import json
|
||||
|
||||
@@ -6,6 +7,7 @@ import websocket
|
||||
from .response import EVMResponse
|
||||
from cic_syncer.error import RequestError
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class EVMWebsocketClient:
|
||||
@@ -35,3 +37,23 @@ class EVMWebsocketClient:
|
||||
raise RequestError(err)
|
||||
|
||||
return res.get_result()
|
||||
|
||||
|
||||
def get_block_by_integer(self, n):
|
||||
req_id = str(uuid.uuid4())
|
||||
nhx = '0x' + n.to_bytes(8, 'big').hex()
|
||||
req = {
|
||||
'jsonrpc': '2.0',
|
||||
'method': 'eth_getBlockByNumber',
|
||||
'id': str(req_id),
|
||||
'params': [nhx, False],
|
||||
}
|
||||
self.conn.send(json.dumps(req))
|
||||
r = self.conn.recv()
|
||||
res = EVMResponse('get_block', json.loads(r))
|
||||
err = res.get_error()
|
||||
if err != None:
|
||||
raise RequestError(err)
|
||||
|
||||
return res.get_result()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user