From c27a8c516bd539ac1ccfbd8198c056327a416405 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Sat, 4 Jul 2015 16:49:26 +0200
Subject: [PATCH] Handle universal dividend receiving ( #144 )

---
 src/cutecoin/core/account.py              | 10 +++++++
 src/cutecoin/core/net/api/bma/__init__.py |  2 +-
 src/cutecoin/core/txhistory.py            | 32 +++++++++++++++++++++--
 src/cutecoin/core/wallet.py               | 12 +++++++++
 src/cutecoin/models/txhistory.py          | 12 ++++++++-
 5 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py
index fe2e4c0d..d7111fa8 100644
--- a/src/cutecoin/core/account.py
+++ b/src/cutecoin/core/account.py
@@ -353,6 +353,16 @@ class Account(QObject):
             sent.extend(w.transfers(community))
         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
     def future_amount(self, community):
         """
diff --git a/src/cutecoin/core/net/api/bma/__init__.py b/src/cutecoin/core/net/api/bma/__init__.py
index 29eea7b0..9fb8038d 100644
--- a/src/cutecoin/core/net/api/bma/__init__.py
+++ b/src/cutecoin/core/net/api/bma/__init__.py
@@ -126,4 +126,4 @@ class API(object):
         logging.debug(url.toString(QUrl.FullyEncoded))
         return reply
 
-from . import network, blockchain, tx, wot
+from . import network, blockchain, tx, wot, ud
\ No newline at end of file
diff --git a/src/cutecoin/core/txhistory.py b/src/cutecoin/core/txhistory.py
index f32b0e03..2f9c6d14 100644
--- a/src/cutecoin/core/txhistory.py
+++ b/src/cutecoin/core/txhistory.py
@@ -13,6 +13,7 @@ class TxHistory():
 
         self._transfers = []
         self.available_sources = []
+        self._dividends = []
 
     @property
     def latest_block(self):
@@ -35,6 +36,9 @@ class TxHistory():
         for s in data['sources']:
             self.available_sources.append(InputSource.from_inline(s['inline']))
 
+        for d in data['dividends']:
+            self._dividends.append(d)
+
         self.latest_block = data['latest_block']
 
     def jsonify(self):
@@ -47,14 +51,23 @@ class TxHistory():
             s.index = 0
             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,
                 'transfers': data_transfer,
-                'sources': data_sources}
+                'sources': data_sources,
+                'dividends': data_dividends}
 
     @property
     def transfers(self):
         return [t for t in self._transfers if t.state != Transfer.DROPPED]
 
+    @property
+    def dividends(self):
+        return self._dividends.copy()
+
     def stop_coroutines(self):
         self._stop_coroutines = True
 
@@ -138,7 +151,15 @@ class TxHistory():
         parsed_block = self.latest_block
         current_block = community.network.latest_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_dividends = []
         # Lets look if transactions took too long to be validated
         awaiting = [t for t in self._transfers
                     if t.state == Transfer.AWAITING]
@@ -152,6 +173,12 @@ class TxHistory():
             if self._stop_coroutines:
                 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
             transactions = tx_history['history']['received'] + tx_history['history']['sent']
             for (txid, txdata) in enumerate(transactions):
@@ -162,7 +189,7 @@ class TxHistory():
                                                                              parsed_block,
                                                                              current_block))
                 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:
                         new_transfers.append(transfer)
 
@@ -179,5 +206,6 @@ class TxHistory():
             transfer.check_refused(current_block)
 
         self._transfers = self._transfers + new_transfers
+        self._dividends = self._dividends + new_dividends
 
         self.wallet.refresh_finished.emit(received_list)
diff --git a/src/cutecoin/core/wallet.py b/src/cutecoin/core/wallet.py
index 3d996bee..a0628c52 100644
--- a/src/cutecoin/core/wallet.py
+++ b/src/cutecoin/core/wallet.py
@@ -331,6 +331,18 @@ class Wallet(QObject):
         else:
             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):
         for c in self.caches.values():
             c.stop_coroutines()
diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py
index e82f44c9..2d0cd38b 100644
--- a/src/cutecoin/models/txhistory.py
+++ b/src/cutecoin/models/txhistory.py
@@ -189,7 +189,7 @@ class HistoryTableModel(QAbstractTableModel):
 
     @property
     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):
         amount = transfer.metadata['amount']
@@ -225,6 +225,16 @@ class HistoryTableModel(QAbstractTableModel):
                 "", comment, transfer.state, txid,
                 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):
         self.beginResetModel()
         self.transfers_data = []
-- 
GitLab