Skip to content
Snippets Groups Projects
Commit c27a8c51 authored by inso's avatar inso
Browse files

Handle universal dividend receiving ( #144 )

parent 22bd8ec7
No related branches found
No related tags found
No related merge requests found
...@@ -353,6 +353,16 @@ class Account(QObject): ...@@ -353,6 +353,16 @@ class Account(QObject):
sent.extend(w.transfers(community)) sent.extend(w.transfers(community))
return sent return sent
def dividends(self, community):
"""
Get all dividends received in this community
by the first wallet of this account
:param community: The target community
:return: All account dividends
"""
return self.wallets[0].dividends(community)
@asyncio.coroutine @asyncio.coroutine
def future_amount(self, community): def future_amount(self, community):
""" """
......
...@@ -126,4 +126,4 @@ class API(object): ...@@ -126,4 +126,4 @@ class API(object):
logging.debug(url.toString(QUrl.FullyEncoded)) logging.debug(url.toString(QUrl.FullyEncoded))
return reply return reply
from . import network, blockchain, tx, wot from . import network, blockchain, tx, wot, ud
\ No newline at end of file
...@@ -13,6 +13,7 @@ class TxHistory(): ...@@ -13,6 +13,7 @@ class TxHistory():
self._transfers = [] self._transfers = []
self.available_sources = [] self.available_sources = []
self._dividends = []
@property @property
def latest_block(self): def latest_block(self):
...@@ -35,6 +36,9 @@ class TxHistory(): ...@@ -35,6 +36,9 @@ class TxHistory():
for s in data['sources']: for s in data['sources']:
self.available_sources.append(InputSource.from_inline(s['inline'])) self.available_sources.append(InputSource.from_inline(s['inline']))
for d in data['dividends']:
self._dividends.append(d)
self.latest_block = data['latest_block'] self.latest_block = data['latest_block']
def jsonify(self): def jsonify(self):
...@@ -47,14 +51,23 @@ class TxHistory(): ...@@ -47,14 +51,23 @@ class TxHistory():
s.index = 0 s.index = 0
data_sources.append({'inline': "{0}\n".format(s.inline())}) data_sources.append({'inline': "{0}\n".format(s.inline())})
data_dividends = []
for d in self._dividends:
data_dividends.append(d)
return {'latest_block': self.latest_block, return {'latest_block': self.latest_block,
'transfers': data_transfer, 'transfers': data_transfer,
'sources': data_sources} 'sources': data_sources,
'dividends': data_dividends}
@property @property
def transfers(self): def transfers(self):
return [t for t in self._transfers if t.state != Transfer.DROPPED] return [t for t in self._transfers if t.state != Transfer.DROPPED]
@property
def dividends(self):
return self._dividends.copy()
def stop_coroutines(self): def stop_coroutines(self):
self._stop_coroutines = True self._stop_coroutines = True
...@@ -138,7 +151,15 @@ class TxHistory(): ...@@ -138,7 +151,15 @@ class TxHistory():
parsed_block = self.latest_block parsed_block = self.latest_block
current_block = community.network.latest_block current_block = community.network.latest_block
logging.debug("Refresh from : {0} to {1}".format(self.latest_block, current_block)) logging.debug("Refresh from : {0} to {1}".format(self.latest_block, current_block))
dividends_data = yield from community.bma_access.future_request(qtbma.ud.History,
req_args={'pubkey': self.wallet.pubkey})
dividends = dividends_data['history']['history']
for d in dividends:
if d['block_number'] not in range(parsed_block, parsed_block+99):
dividends.remove(d)
new_transfers = [] new_transfers = []
new_dividends = []
# Lets look if transactions took too long to be validated # Lets look if transactions took too long to be validated
awaiting = [t for t in self._transfers awaiting = [t for t in self._transfers
if t.state == Transfer.AWAITING] if t.state == Transfer.AWAITING]
...@@ -152,6 +173,12 @@ class TxHistory(): ...@@ -152,6 +173,12 @@ class TxHistory():
if self._stop_coroutines: if self._stop_coroutines:
return return
udid = 0
for d in dividends:
if d['block_number'] in range(parsed_block, parsed_block+99):
new_dividends.append(d)
udid += 1
# We parse only blocks with transactions # We parse only blocks with transactions
transactions = tx_history['history']['received'] + tx_history['history']['sent'] transactions = tx_history['history']['received'] + tx_history['history']['sent']
for (txid, txdata) in enumerate(transactions): for (txid, txdata) in enumerate(transactions):
...@@ -162,7 +189,7 @@ class TxHistory(): ...@@ -162,7 +189,7 @@ class TxHistory():
parsed_block, parsed_block,
current_block)) current_block))
else: else:
transfer = yield from self._parse_transaction(community, txdata, received_list, txid) transfer = yield from self._parse_transaction(community, txdata, received_list, udid + txid)
if transfer: if transfer:
new_transfers.append(transfer) new_transfers.append(transfer)
...@@ -179,5 +206,6 @@ class TxHistory(): ...@@ -179,5 +206,6 @@ class TxHistory():
transfer.check_refused(current_block) transfer.check_refused(current_block)
self._transfers = self._transfers + new_transfers self._transfers = self._transfers + new_transfers
self._dividends = self._dividends + new_dividends
self.wallet.refresh_finished.emit(received_list) self.wallet.refresh_finished.emit(received_list)
...@@ -331,6 +331,18 @@ class Wallet(QObject): ...@@ -331,6 +331,18 @@ class Wallet(QObject):
else: else:
return [] return []
def dividends(self, community):
"""
Get all the dividends received by this wallet
:param community: The community we want to get received dividends
:return: Result of udhistory request
"""
if community.currency in self.caches:
return self.caches[community.currency].dividends
else:
return []
def stop_coroutines(self): def stop_coroutines(self):
for c in self.caches.values(): for c in self.caches.values():
c.stop_coroutines() c.stop_coroutines()
......
...@@ -189,7 +189,7 @@ class HistoryTableModel(QAbstractTableModel): ...@@ -189,7 +189,7 @@ class HistoryTableModel(QAbstractTableModel):
@property @property
def transfers(self): def transfers(self):
return self.account.transfers(self.community) return self.account.transfers(self.community) + self.account.dividends(self.community)
def data_received(self, transfer): def data_received(self, transfer):
amount = transfer.metadata['amount'] amount = transfer.metadata['amount']
...@@ -225,6 +225,16 @@ class HistoryTableModel(QAbstractTableModel): ...@@ -225,6 +225,16 @@ class HistoryTableModel(QAbstractTableModel):
"", comment, transfer.state, txid, "", comment, transfer.state, txid,
transfer.metadata['receiver']) transfer.metadata['receiver'])
def data_dividend(self, dividend):
amount = dividend['amount']
comment = ""
receiver = self.account.name
date_ts = dividend['time']
udid = dividend['udid']
return (date_ts, receiver, amount,
"", "", Transfer.VALIDATED, udid,
self.account.pubkey)
def refresh_transfers(self): def refresh_transfers(self):
self.beginResetModel() self.beginResetModel()
self.transfers_data = [] self.transfers_data = []
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment