Add retrieval of transaction from daemon

This commit is contained in:
Michał Sałaban 2019-12-11 11:25:43 +01:00
parent df9dd7152b
commit 44d87be2e6
6 changed files with 208 additions and 0 deletions

View file

@ -71,6 +71,22 @@ class JSONRPCDaemon(object):
return res['headers']
raise exceptions.BackendException(res['status'])
def transactions(self, hashes):
res = self.raw_request('/get_transactions', {
'txs_hashes': hashes,
'decode_as_json': True})
if res['status'] != 'OK':
raise exceptions.BackendException(res['status'])
txs = []
for tx in res.get('txs', []):
txs.append(Transaction(
hash=tx['tx_hash'],
height=None if tx['in_pool'] else tx['block_height'],
timestamp=datetime.fromtimestamp(tx['block_timestamp']) if 'block_timestamp' in tx else None,
blob=tx['as_hex'],
json=json.loads(tx['as_json'])))
return txs
def raw_request(self, path, data):
hdr = {'Content-Type': 'application/json'}
_log.debug(u"Request: {path}\nData: {data}".format(

View file

@ -1,3 +1,5 @@
import six
class Daemon(object):
"""Monero daemon.
@ -51,3 +53,13 @@ class Daemon(object):
:rtype: list of dict
"""
return self._backend.headers(start_height, end_height)
def transactions(self, hashes):
"""
Returns transactions matching given hashes. Accepts single hash or a sequence.
:hashes: str or list of str
"""
if isinstance(hashes, six.string_types):
hashes = [hashes]
return self._backend.transactions(hashes)

View file

@ -72,8 +72,13 @@ class Transaction(object):
timestamp = None
key = None
blob = None
json = None
confirmations = None
@property
def size(self):
return len(self.blob)//2
def __init__(self, **kwargs):
self.hash = kwargs.get('hash', self.hash)
self.fee = kwargs.get('fee', self.fee)
@ -81,6 +86,7 @@ class Transaction(object):
self.timestamp = kwargs.get('timestamp', self.timestamp)
self.key = kwargs.get('key', self.key)
self.blob = kwargs.get('blob', self.blob)
self.json = kwargs.get('json', self.json)
self.confirmations = kwargs.get('confirmations', self.confirmations)
def __repr__(self):