Reorganize payments and transactions

This commit is contained in:
Michał Sałaban 2018-01-25 08:50:09 +01:00
parent 8ae386904a
commit a16ae37a94
7 changed files with 146 additions and 98 deletions

View file

@ -10,7 +10,7 @@ from .. import exceptions
from ..account import Account
from ..address import address, Address, SubAddress
from ..numbers import from_atomic, to_atomic, PaymentID
from ..transaction import Transaction, Payment, Transfer
from ..transaction import Transaction, IncomingPayment, OutgoingPayment
_log = logging.getLogger(__name__)
@ -173,11 +173,10 @@ class JSONRPCWallet(object):
'account_index': account,
'payment_id': str(payment_id)})
pmts = []
for tx in _payments['payments']:
data = self._tx2dict(tx)
for data in _payments['payments']:
# Monero <= 0.11 : no address is passed because there's only one
data['local_address'] = data['local_address'] or self._master_address
pmts.append(Payment(**data))
data['address'] = data['address'] or self._master_address
pmts.append(self._inpayment(data))
return pmts
def get_transactions_in(self, account=0, confirmed=True, unconfirmed=False):
@ -186,8 +185,7 @@ class JSONRPCWallet(object):
txns = _txns.get('in', [])
if unconfirmed:
txns.extend(_txns.get('pool', []))
return [Payment(**self._tx2dict(tx)) for tx in
sorted(txns, key=operator.itemgetter('timestamp'))]
return [self._inpayment(tx) for tx in sorted(txns, key=operator.itemgetter('timestamp'))]
def get_transactions_out(self, account=0, confirmed=True, unconfirmed=True):
_txns = self.raw_request('get_transfers',
@ -195,33 +193,47 @@ class JSONRPCWallet(object):
txns = _txns.get('out', [])
if unconfirmed:
txns.extend(_txns.get('pool', []))
return [Transfer(**self._tx2dict(tx)) for tx in
sorted(txns, key=operator.itemgetter('timestamp'))]
return [self._outpayment(tx) for tx in sorted(txns, key=operator.itemgetter('timestamp'))]
def get_transaction(self, txhash):
_tx = self.raw_request('get_transfer_by_txid', {'txid': str(txhash)})['transfer']
try:
_class = {'in': Payment, 'out': Transfer}[_tx['type']]
except KeyError:
_class = Transaction
return _class(**self._tx2dict(_tx))
if _tx['type'] == 'in':
return self._inpayment(tx)
elif _tx['type'] == 'out':
return self._outpayment(tx)
return Payment(**self._paymentdict(tx))
def _tx2dict(self, tx):
pid = tx.get('payment_id', None)
def _paymentdict(self, data):
pid = data.get('payment_id', None)
return {
'hash': tx.get('txid', tx.get('tx_hash')),
'timestamp': datetime.fromtimestamp(tx['timestamp']) if 'timestamp' in tx else None,
'amount': from_atomic(tx['amount']),
'fee': from_atomic(tx['fee']) if 'fee' in tx else None,
'height': tx.get('height', tx.get('block_height')) or None,
'txhash': data.get('txid', data.get('tx_hash')),
'payment_id': None if pid is None else PaymentID(pid),
'note': tx.get('note'),
# NOTE: address will be resolved only after PR#3010 has been merged to Monero
'local_address': address(tx['address']) if 'address' in tx else None,
'key': tx.get('key'),
'blob': tx.get('blob', None),
'amount': from_atomic(data['amount']),
'timestamp': datetime.fromtimestamp(data['timestamp']) if 'timestamp' in data else None,
'note': data.get('note'),
'transaction': self._tx(data)
}
def _inpayment(self, data):
p = self._paymentdict(data)
p.update({'received_by': address(data['address']) if 'address' in data else None})
return IncomingPayment(**p)
def _outpayment(self, data):
p = self._paymentdict(data)
p.update({'sent_from': address(data['address']) if 'address' in data else None})
return OutgoingPayment(**p)
def _tx(self, data):
return Transaction(**{
'hash': data.get('txid', data.get('tx_hash')),
'fee': from_atomic(data['fee']) if 'fee' in data else None,
'key': data.get('key'),
'height': data.get('height', data.get('block_height')) or None,
'timestamp': datetime.fromtimestamp(data['timestamp']) if 'timestamp' in data else None,
'blob': data.get('blob', None),
})
def transfer(self, destinations, priority, ringsize,
payment_id=None, unlock_time=0, account=0,
relay=True):
@ -247,7 +259,7 @@ class JSONRPCWallet(object):
'tx_hash_list', 'amount_list', 'fee_list', 'tx_key_list', 'tx_blob_list')]))]
for d in _pertx:
d['payment_id'] = payment_id
return [Transfer(**self._tx2dict(tx)) for tx in _pertx]
return [self._tx(data) for data in _pertx]
def raw_request(self, method, params=None):
hdr = {'Content-Type': 'application/json'}

View file

@ -8,8 +8,8 @@ class Daemon(object):
def get_height(self):
return self._backend.get_info()['height']
def send_transaction(self, blob):
return self._backend.send_transaction(blob)
def send_transaction(self, tx):
return self._backend.send_transaction(tx.blob)
def get_mempool(self):
return self._backend.get_mempool()

View file

@ -1,45 +1,48 @@
class Transaction(object):
hash = None
height = None
timestamp = None
fee = None
blob = None
def __init__(self, hash=None, **kwargs):
self.hash = hash
self.height = kwargs.get('height', self.height)
self.timestamp = kwargs.get('timestamp', self.timestamp)
self.fee = kwargs.get('fee', self.fee)
self.blob = kwargs.get('blob', self.blob)
def __repr__(self):
return self.hash
class LocalTransaction(Transaction):
"""A transaction that concerns local wallet, either incoming or outgoing."""
class Payment(object):
tx_hash = None
payment_id = None
amount = None
local_address = None
timestamp = None
transaction = None
def __init__(self, **kwargs):
super(LocalTransaction, self).__init__(**kwargs)
self.payment_id = kwargs.get('payment_id', self.payment_id)
self.tx_hash = kwargs.get('tx_hash', self.tx_hash)
self.amount = kwargs.get('amount', self.amount)
self.local_address = kwargs.get('local_address', self.local_address)
self.timestamp = kwargs.get('timestamp', self.timestamp)
self.payment_id = kwargs.get('payment_id', self.payment_id)
self.transaction = kwargs.get('transaction', self.transaction)
class Payment(LocalTransaction):
"""Incoming Transaction"""
pass
class IncomingPayment(Payment):
received_by = None
def __init__(self, **kwargs):
super(IncomingPayment, self).__init__(**kwargs)
self.received_by = kwargs.get('received_by', self.received_by)
class Transfer(LocalTransaction):
"""Outgoing Transaction"""
key = None
class OutgoingPayment(Payment):
sent_from = None
note = ''
def __init__(self, **kwargs):
super(Transfer, self).__init__(**kwargs)
super(OutgoingPayment, self).__init__(**kwargs)
self.sent_from = kwargs.get('sent_from', self.sent_from)
self.note = kwargs.get('note', self.sent_from)
class Transaction(object):
hash = None
fee = None
height = None
timestamp = None
key = None
blob = None
def __init__(self, **kwargs):
self.hash = kwargs.get('hash', self.hash)
self.fee = kwargs.get('fee', self.fee)
self.height = kwargs.get('height', self.height)
self.timestamp = kwargs.get('timestamp', self.timestamp)
self.key = kwargs.get('key', self.key)
self.note = kwargs.get('note', self.note)
self.blob = kwargs.get('blob', self.blob)