diff --git a/src/sakia/data/entities/transaction.py b/src/sakia/data/entities/transaction.py index 1cdde8413bc4683267f50157a6eba4c4998e326c..3f79dcb4bdf7410f107566171de453865d5f9ba8 100644 --- a/src/sakia/data/entities/transaction.py +++ b/src/sakia/data/entities/transaction.py @@ -1,7 +1,64 @@ import attr from duniterpy.documents import block_uid +from duniterpy.documents.transaction import reduce_base +import math +def parse_transaction_doc(tx_doc, pubkey, block_number, mediantime, txid): + """ + Parse a transaction + :param duniterpy.documents.Transaction tx_doc: The tx json data + :param str pubkey: The pubkey of the transaction to parse, to know if its a receiver or issuer + :param int block_number: The block number where we found the tx + :param int mediantime: Median time on the network + :param int txid: The latest txid + :return: the found transaction + """ + receivers = [o.conditions.left.pubkey for o in tx_doc.outputs + if o.conditions.left.pubkey != tx_doc.issuers[0]] + + if len(receivers) == 0: + receivers = [tx_doc.issuers[0]] + + in_issuers = len([i for i in tx_doc.issuers + if i == pubkey]) > 0 + in_outputs = len([o for o in tx_doc.outputs + if o.conditions.left.pubkey == pubkey]) > 0 + + if in_issuers or in_outputs: + # If the wallet pubkey is in the issuers we sent this transaction + if in_issuers: + outputs = [o for o in tx_doc.outputs + if o.conditions.left.pubkey != pubkey] + amount = 0 + for o in outputs: + amount += o.amount * math.pow(10, o.base) + # If we are not in the issuers, + # maybe we are in the recipients of this transaction + else: + outputs = [o for o in tx_doc.outputs + if o.conditions.left.pubkey == pubkey] + amount = 0 + for o in outputs: + amount += o.amount * math.pow(10, o.base) + amount, amount_base = reduce_base(amount, 0) + + transaction = Transaction(currency=tx_doc.currency, + sha_hash=tx_doc.sha_hash, + written_block=block_number, + blockstamp=tx_doc.blockstamp, + timestamp=mediantime, + signature=tx_doc.signatures[0], + issuer=tx_doc.issuers[0], + receiver=receivers[0], + amount=amount, + amount_base=amount_base, + comment=tx_doc.comment, + txid=txid, + state=Transaction.VALIDATED) + return transaction + return None + @attr.s() class Transaction: @@ -15,7 +72,7 @@ class Transaction: currency = attr.ib(convert=str, cmp=False) sha_hash = attr.ib(convert=str) - written_on = attr.ib(convert=block_uid, cmp=False) + written_block = attr.ib(convert=int, cmp=False) blockstamp = attr.ib(convert=block_uid, cmp=False) timestamp = attr.ib(convert=int, cmp=False) signature = attr.ib(convert=str, cmp=False) diff --git a/src/sakia/data/processors/transactions.py b/src/sakia/data/processors/transactions.py index 212883409f02dff3e5fe83b702c6c80a88b11ca7..cc427a68824b049a73436c4bf8259ba5e4c48bf9 100644 --- a/src/sakia/data/processors/transactions.py +++ b/src/sakia/data/processors/transactions.py @@ -1,10 +1,14 @@ import attr +import asyncio +import sqlite3 from ..entities import Transaction +from ..entities.transaction import parse_transaction_doc from .nodes import NodesProcessor from . import tx_lifecycle from ..connectors import BmaConnector from duniterpy.api import bma from duniterpy.documents import Block +from duniterpy.documents import Transaction as TransactionDoc @attr.s @@ -120,10 +124,24 @@ class TransactionsProcessor: self.run_state_transitions(tx, ([r.status for r in responses],)) return result - def initialize_transactions(self, currency, pubkey): + async def initialize_transactions(self, identity, log_stream): """ Request transactions from the network to initialize data for a given pubkey - :param str currency: - :param str pubkey: + :param sakia.data.entities.Identity pubkey: + :param function log_stream: """ - history = await self._bma_connector.get() \ No newline at end of file + history_data = await self._bma_connector.get(identity.currency, bma.tx.history, + req_args={'pubkey': identity.pubkey}) + txid = 0 + nb_tx = len(history_data["history"]["sent"]) + len(history_data["history"]["received"]) + log_stream("Found {0} transactions".format(nb_tx)) + for sent_data in history_data["history"]["sent"] + history_data["history"]["received"]: + sent = TransactionDoc.from_bma_history(history_data["currency"], sent_data) + log_stream("{0}/{1} transactions".format(txid, nb_tx)) + try: + self._repo.insert(parse_transaction_doc(sent, identity.pubkey, sent_data["block_number"], + sent_data["time"], txid)) + except sqlite3.IntegrityError: + log_stream("Transaction already registered in database") + await asyncio.sleep(0) + txid += 1 diff --git a/src/sakia/data/repositories/meta.sql b/src/sakia/data/repositories/meta.sql index 95513f11f3fb16bf39fabdeeb89c503bb950dbfd..aeff02cf2eabd17ffb8a066b959a6d7abc4afbd9 100644 --- a/src/sakia/data/repositories/meta.sql +++ b/src/sakia/data/repositories/meta.sql @@ -70,7 +70,7 @@ CREATE TABLE IF NOT EXISTS certifications( CREATE TABLE IF NOT EXISTS transactions( currency VARCHAR(30), sha_hash VARCHAR(50), - written_on VARCHAR(100), + written_on INT, blockstamp VARCHAR(100), ts INT, signature VARCHAR(100), diff --git a/src/sakia/gui/dialogs/connection_cfg/controller.py b/src/sakia/gui/dialogs/connection_cfg/controller.py index ab797579dc11d6f6ae60114ac1e9356c6fa016a5..7b3a38819b5042a6d36a6e1d060c0c91baaf4d0a 100644 --- a/src/sakia/gui/dialogs/connection_cfg/controller.py +++ b/src/sakia/gui/dialogs/connection_cfg/controller.py @@ -154,6 +154,8 @@ class ConnectionConfigController(ComponentController): await self.model.initialize_identity(connection_identity, log_stream=self.view.stream_log) self.view.stream_log("Initializing certifications informations...") await self.model.initialize_certifications(connection_identity, log_stream=self.view.stream_log) + self.view.stream_log("Initializing transactions history...") + await self.model.initialize_transactions(connection_identity, log_stream=self.view.stream_log) self.view.progress_bar.setValue(2) if mode == ConnectionConfigController.REGISTER: diff --git a/src/sakia/gui/dialogs/connection_cfg/model.py b/src/sakia/gui/dialogs/connection_cfg/model.py index 4d85c955bbe46715d724e0053e7990087a20dab0..ecf115db90b0c15ec9c95f9c1447b0a51fbcb989 100644 --- a/src/sakia/gui/dialogs/connection_cfg/model.py +++ b/src/sakia/gui/dialogs/connection_cfg/model.py @@ -6,7 +6,7 @@ from duniterpy.key import SigningKey from sakia.data.entities import Connection, Identity, Node from sakia.data.connectors import NodeConnector from sakia.data.processors import ConnectionsProcessor, NodesProcessor, BlockchainProcessor, \ - SourcesProcessor, CertificationsProcessor + SourcesProcessor, CertificationsProcessor, TransactionsProcessor from sakia.gui.component.model import ComponentModel @@ -94,6 +94,16 @@ class ConnectionConfigModel(ComponentModel): certifications_processor = CertificationsProcessor.instanciate(self.app) await certifications_processor.initialize_certifications(identity, log_stream) + async def initialize_transactions(self, identity, log_stream): + """ + Download certifications information locally + :param sakia.data.entities.Identity identity: the identity to initialize + :param function log_stream: a method to log data in the screen + :return: + """ + transactions_processor = TransactionsProcessor.instanciate(self.app) + await transactions_processor.initialize_transactions(identity, log_stream) + async def publish_selfcert(self, salt, password): """" Publish the self certification of the connection identity diff --git a/src/sakia/gui/navigation/informations/model.py b/src/sakia/gui/navigation/informations/model.py index 0276bd25d77e93d104755495268ea0bf92f50083..570fd613e90ff8c3e72a1ab54bb5d4b0b7542c58 100644 --- a/src/sakia/gui/navigation/informations/model.py +++ b/src/sakia/gui/navigation/informations/model.py @@ -57,7 +57,7 @@ class InformationsModel(ComponentModel): if last_ud: # display float values - localized_data['ud'] = await self.app.current_ref.instance(last_ud * math.pow(10, last_ud_base), + localized_data['ud'] = self.app.current_ref.instance(last_ud * math.pow(10, last_ud_base), self.connection.currency, self.app) \ .diff_localized(True, self.app.parameters.international_system_of_units) @@ -66,11 +66,11 @@ class InformationsModel(ComponentModel): computed_dividend = self.blockchain_service.computed_dividend() # display float values - localized_data['ud_plus_1'] = await self.app.current_ref.instance(computed_dividend, + localized_data['ud_plus_1'] = self.app.current_ref.instance(computed_dividend, self.connection.currency, self.app) \ .diff_localized(True, self.app.parameters.international_system_of_units) - localized_data['mass'] = await self.app.current_ref.instance(self.blockchain_service.current_mass(), + localized_data['mass'] = self.app.current_ref.instance(self.blockchain_service.current_mass(), self.connection.currency, self.app) \ .diff_localized(True, self.app.parameters.international_system_of_units) @@ -89,10 +89,10 @@ class InformationsModel(ComponentModel): if previous_ud: mass_minus_1_per_member = (float(0) if previous_ud == 0 else previous_monetary_mass / previous_members_count) - localized_data['mass_minus_1_per_member'] = await self.app.current_ref.instance(mass_minus_1_per_member, + localized_data['mass_minus_1_per_member'] = self.app.current_ref.instance(mass_minus_1_per_member, self.connection.currency, self.app) \ .diff_localized(True, self.app.parameters.international_system_of_units) - localized_data['mass_minus_1'] = await self.app.current_ref.instance(previous_monetary_mass, + localized_data['mass_minus_1'] = self.app.current_ref.instance(previous_monetary_mass, self.connection.currency, self.app) \ .diff_localized(True, self.app.parameters.international_system_of_units) # avoid divide by zero ! @@ -111,7 +111,7 @@ class InformationsModel(ComponentModel): async def get_identity_data(self): amount = self.sources_service.amount(self.connection.pubkey) - localized_amount = await self.app.current_ref.instance(amount, self.connection.currency, self.app)\ + localized_amount = self.app.current_ref.instance(amount, self.connection.currency, self.app)\ .localized(units=True, international_system=self.app.parameters.international_system_of_units) mstime_remaining_text = self.tr("Expired or never published") diff --git a/src/sakia/gui/navigation/txhistory/controller.py b/src/sakia/gui/navigation/txhistory/controller.py index 4a749ab55e103ffb99fc29e13e8a7d894d070402..1cfd4b488bc2231a6271f250d34977d0f4258164 100644 --- a/src/sakia/gui/navigation/txhistory/controller.py +++ b/src/sakia/gui/navigation/txhistory/controller.py @@ -84,7 +84,7 @@ class TxHistoryController(ComponentController): @asyncify async def refresh_balance(self): self.view.busy_balance.show() - localized_amount = await self.model.localized_balance() + localized_amount = self.model.localized_balance() self.view.set_balance(localized_amount) self.view.busy_balance.hide() diff --git a/src/sakia/gui/navigation/txhistory/model.py b/src/sakia/gui/navigation/txhistory/model.py index b183ce53b7af67186d0068e6935acf7eb1f587dd..ab652acbde6b89a9a9b8863b477a57331d7b9ca2 100644 --- a/src/sakia/gui/navigation/txhistory/model.py +++ b/src/sakia/gui/navigation/txhistory/model.py @@ -75,18 +75,10 @@ class TxHistoryModel(ComponentModel): :rtype: Tuple[PyQt5.QtCore.QDateTime, PyQt5.QtCore.QDateTime] :return: minimum and maximum datetime """ - try: - blockchain_time = self.blockchain_service.time() - minimum_datetime = QDateTime() - minimum_datetime.setTime_t(blockchain_time) - minimum_datetime.setTime(QTime(0, 0)) - tomorrow_datetime = QDateTime().currentDateTime().addDays(1) - return minimum_datetime, tomorrow_datetime - except NoPeerAvailable as e: - logging.debug(str(e)) - except errors.DuniterError as e: - logging.debug(str(e)) - return QDateTime().currentDateTime(), QDateTime.currentDateTime().addDays(1) + minimum_datetime = QDateTime() + minimum_datetime.setTime_t(0) + tomorrow_datetime = QDateTime().currentDateTime().addDays(1) + return minimum_datetime, tomorrow_datetime async def received_amount(self, received_list): """ diff --git a/src/sakia/gui/navigation/txhistory/table_model.py b/src/sakia/gui/navigation/txhistory/table_model.py index 2ce9d2c3cc7fee714828f32754edadd6ea5b8829..43cd35947229889db6afda8f9cc8b6eddf8a24ae 100644 --- a/src/sakia/gui/navigation/txhistory/table_model.py +++ b/src/sakia/gui/navigation/txhistory/table_model.py @@ -247,8 +247,8 @@ class HistoryTableModel(QAbstractTableModel): amount = transfer.amount * 10**transfer.amount_base try: - deposit = self.account.current_ref.instance(amount, self.connection.currency, self.app, block_number)\ - .diff_localized(international_system=self.app.preferences['international_system_of_units']) + deposit = self.app.current_ref.instance(amount, self.connection.currency, self.app, block_number)\ + .diff_localized(international_system=self.app.parameters.international_system_of_units) except NoPeerAvailable: deposit = "Could not compute" @@ -263,9 +263,9 @@ class HistoryTableModel(QAbstractTableModel): return (date_ts, sender, "", deposit, transfer.comment, transfer.state, txid, - transfer.metadata['issuer'], block_number, amount) + transfer.issuer, block_number, amount) - async def data_sent(self, transfer): + def data_sent(self, transfer): """ Converts a transaction to table data :param sakia.data.entities.Transaction transfer: the transaction @@ -300,23 +300,16 @@ class HistoryTableModel(QAbstractTableModel): def refresh_transfers(self): self.beginResetModel() - transfers_data = [] - data_list = [] + self.transfers_data = [] count = 0 transfers = self.transfers() for transfer in transfers: count += 1 if type(transfer) is Transaction: if transfer.issuer == self.connection.pubkey: - data_list += self.data_sent(transfer) + self.transfers_data.append(self.data_sent(transfer)) else: - data_list += self.data_received(transfer) - elif type(transfer) is dict: - data_list += self.data_dividend(transfer) - - for data in data_list: - transfers_data.append(data) - self.transfers_data = transfers_data + self.transfers_data.append(self.data_received(transfer)) self.endResetModel() def rowCount(self, parent): @@ -352,8 +345,6 @@ class HistoryTableModel(QAbstractTableModel): transfer = self.transfers_data[row] if transfer[self.columns_types.index('payment')] != "": return QIcon(":/icons/sent") - elif transfer[self.columns_types.index('uid')] == self.account.name: - return QIcon(":/icons/dividend") else: return QIcon(":/icons/received") diff --git a/src/sakia/money/base_referential.py b/src/sakia/money/base_referential.py index 3682c23554735a5dbe00e20a88aec8713eb85692..4fd504cd025333d8cad1f1952906d47676fdb9c3 100644 --- a/src/sakia/money/base_referential.py +++ b/src/sakia/money/base_referential.py @@ -33,18 +33,18 @@ class BaseReferential: def diff_units(self): pass - async def value(self): + def value(self): pass - async def differential(self): + def differential(self): pass @staticmethod def to_si(value, digits): pass - async def localized(self, units=False, international_system=False): + def localized(self, units=False, international_system=False): pass - async def diff_localized(self, units=False, international_system=False): + def diff_localized(self, units=False, international_system=False): pass diff --git a/src/sakia/money/dividend_per_day.py b/src/sakia/money/dividend_per_day.py index 1b2749bb76d5ff27d81e906686ee3faf76f2db61..70fd30d0f9793c057f771688fa1e930d50c36929 100644 --- a/src/sakia/money/dividend_per_day.py +++ b/src/sakia/money/dividend_per_day.py @@ -58,7 +58,7 @@ class DividendPerDay(BaseReferential): def diff_units(self): return self.units - async def value(self): + def value(self): """ Return relative value of amount @@ -71,17 +71,17 @@ class DividendPerDay(BaseReferential): :return: float """ dividend, base = self._blockchain_processor.last_ud(self.currency) - params = await self._blockchain_processor.parameters(self.currency) + params = self._blockchain_processor.parameters(self.currency) if dividend > 0: return (self.amount * 100) / (float(dividend * (10**base)) / (params.dt / 86400)) else: return self.amount - async def differential(self): - return await self.value() + def differential(self): + return self.value() - async def localized(self, units=False, international_system=False): - value = await self.value() + def localized(self, units=False, international_system=False): + value = self.value() prefix = "" localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) @@ -93,8 +93,8 @@ class DividendPerDay(BaseReferential): else: return localized_value - async def diff_localized(self, units=False, international_system=False): - value = await self.differential() + def diff_localized(self, units=False, international_system=False): + value = self.differential() prefix = "" localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) diff --git a/src/sakia/money/quant_zerosum.py b/src/sakia/money/quant_zerosum.py index 61b486686687fb0ad3176d3a3743bbeca95b3452..2d3fef9ee940d5d4aa5cd39f32b61b13b3a834aa 100644 --- a/src/sakia/money/quant_zerosum.py +++ b/src/sakia/money/quant_zerosum.py @@ -52,7 +52,7 @@ class QuantitativeZSum(BaseReferential): def diff_units(self): return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(shortened(self.currency)) - async def value(self): + def value(self): """ Return quantitative value of amount minus the average value @@ -77,11 +77,11 @@ class QuantitativeZSum(BaseReferential): average = 0 return self.amount - average - async def differential(self): - return await Quantitative(self.amount, self.currency, self.app).value() + def differential(self): + return Quantitative(self.amount, self.currency, self.app).value() - async def localized(self, units=False, international_system=False): - value = await self.value() + def localized(self, units=False, international_system=False): + value = self.value() prefix = "" if international_system: @@ -98,7 +98,7 @@ class QuantitativeZSum(BaseReferential): else: return localized_value - async def diff_localized(self, units=False, international_system=False): - localized = await Quantitative(self.amount, shortened(self.currency), self.app).localized(units, + def diff_localized(self, units=False, international_system=False): + localized = Quantitative(self.amount, shortened(self.currency), self.app).localized(units, international_system) return localized diff --git a/src/sakia/money/quantitative.py b/src/sakia/money/quantitative.py index 7ce9ecf731610f04f8d9e0e06f8be4125ad5653b..62315be2b5f249550dde4eb0b066fc67a8a5bd97 100644 --- a/src/sakia/money/quantitative.py +++ b/src/sakia/money/quantitative.py @@ -42,7 +42,7 @@ class Quantitative(BaseReferential): def diff_units(self): return self.units - async def value(self): + def value(self): """ Return quantitative value of amount @@ -52,8 +52,8 @@ class Quantitative(BaseReferential): """ return int(self.amount) - async def differential(self): - return await self.value() + def differential(self): + return self.value() @staticmethod def to_si(value, digits): @@ -80,8 +80,8 @@ class Quantitative(BaseReferential): return localized_value, prefix - async def localized(self, units=False, international_system=False): - value = await self.value() + def localized(self, units=False, international_system=False): + value = self.value() prefix = "" if international_system: localized_value, prefix = Quantitative.to_si(value, self.app.parameters.digits_after_comma) @@ -97,8 +97,8 @@ class Quantitative(BaseReferential): else: return localized_value - async def diff_localized(self, units=False, international_system=False): - value = await self.differential() + def diff_localized(self, units=False, international_system=False): + value = self.differential() prefix = "" if international_system: localized_value, prefix = Quantitative.to_si(value, self.app.parameters.digits_after_comma) diff --git a/src/sakia/money/relative.py b/src/sakia/money/relative.py index 0c1c5ee089deb38d90c5118da419f7ac3f77e537..45a4752aab294f9cd7609f14704668a5f272bf9c 100644 --- a/src/sakia/money/relative.py +++ b/src/sakia/money/relative.py @@ -68,7 +68,7 @@ class Relative(BaseReferential): def diff_units(self): return self.units - async def value(self): + def value(self): """ Return relative value of amount @@ -84,8 +84,8 @@ class Relative(BaseReferential): else: return self.amount - async def differential(self): - return await self.value() + def differential(self): + return self.value() @staticmethod def to_si(value, digits): @@ -111,8 +111,8 @@ class Relative(BaseReferential): return localized_value, prefix - async def localized(self, units=False, international_system=False): - value = await self.value() + def localized(self, units=False, international_system=False): + value = self.value() prefix = "" if international_system: localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) @@ -127,8 +127,8 @@ class Relative(BaseReferential): else: return localized_value - async def diff_localized(self, units=False, international_system=False): - value = await self.differential() + def diff_localized(self, units=False, international_system=False): + value = self.differential() prefix = "" if international_system and value != 0: localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) diff --git a/src/sakia/money/relative_to_past.py b/src/sakia/money/relative_to_past.py index 5da0720007a19391f7ea1f96656a56ce37e31576..2062c57628061c8a64c723e7794386df0aa389fb 100644 --- a/src/sakia/money/relative_to_past.py +++ b/src/sakia/money/relative_to_past.py @@ -50,31 +50,31 @@ class RelativeToPast(BaseReferential): def diff_units(self): return self.units - async def value(self): + def value(self): """ Return relative to past value of amount :return: float """ - dividend = await self.community.dividend() + dividend = self.community.dividend() if dividend > 0: return self.amount / float(dividend) else: return self.amount - async def differential(self): + def differential(self): """ Return relative to past differential value of amount :return: float """ - dividend = await self.community.dividend(self._block_number) + dividend = self.community.dividend(self._block_number) if dividend > 0: return self.amount / float(dividend) else: return self.amount - async def localized(self, units=False, international_system=False): + def localized(self, units=False, international_system=False): from . import Relative - value = await self.value() + value = self.value() last_ud_time = self._blockchain_processor.last_ud_time(self.currency) prefix = "" if international_system: @@ -95,10 +95,10 @@ class RelativeToPast(BaseReferential): else: return localized_value - async def diff_localized(self, units=False, international_system=False): + def diff_localized(self, units=False, international_system=False): from . import Relative - value = await self.differential() - block = await self.community.get_ud_block(0, self._block_number) + value = self.differential() + block = self.community.get_ud_block(0, self._block_number) prefix = "" if international_system and value != 0: localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) diff --git a/src/sakia/money/relative_zerosum.py b/src/sakia/money/relative_zerosum.py index c01c57de1419bc7064b90573926874dd56ee5cb2..9516f184d2bfc58a4b85458d5715c9d23015749e 100644 --- a/src/sakia/money/relative_zerosum.py +++ b/src/sakia/money/relative_zerosum.py @@ -51,7 +51,7 @@ class RelativeZSum(BaseReferential): def diff_units(self): return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(shortened(self.currency)) - async def value(self): + def value(self): """ Return relative value of amount minus the average value @@ -66,8 +66,8 @@ class RelativeZSum(BaseReferential): :param sakia.core.community.Community community: Community instance :return: float """ - ud_block = await self.community.get_ud_block() - ud_block_minus_1 = await self.community.get_ud_block(x=1) + ud_block = self.community.get_ud_block() + ud_block_minus_1 = self.community.get_ud_block(x=1) if ud_block_minus_1 and ud_block['membersCount'] > 0: median = ud_block_minus_1['monetaryMass'] / ud_block['membersCount'] relative_value = self.amount / float(ud_block['dividend']) @@ -77,11 +77,11 @@ class RelativeZSum(BaseReferential): relative_median = 0 return relative_value - relative_median - async def differential(self): - return await Relative(self.amount, self.currency, self.app).value() + def differential(self): + return Relative(self.amount, self.currency, self.app).value() - async def localized(self, units=False, international_system=False): - value = await self.value() + def localized(self, units=False, international_system=False): + value = self.value() prefix = "" if international_system: @@ -97,8 +97,8 @@ class RelativeZSum(BaseReferential): else: return localized_value - async def diff_localized(self, units=False, international_system=False): - value = await self.differential() + def diff_localized(self, units=False, international_system=False): + value = self.differential() prefix = "" if international_system and value != 0: diff --git a/src/sakia/money/udd_to_past.py b/src/sakia/money/udd_to_past.py index 38667963fb1714e09bfc7614f54ff59ae35dfe9e..4c71afff0578f8126bc746c3ba5d8f32cd0f95b9 100644 --- a/src/sakia/money/udd_to_past.py +++ b/src/sakia/money/udd_to_past.py @@ -52,7 +52,7 @@ class UDDToPast(BaseReferential): def diff_units(self): return self.units - async def value(self): + def value(self): """ Return relative value of amount @@ -65,14 +65,14 @@ class UDDToPast(BaseReferential): :param sakia.core.community.Community community: Community instance :return: float """ - dividend = await self.community.dividend() - params = await self.community.parameters() + dividend = self.community.dividend() + params = self.community.parameters() if dividend > 0: return (self.amount * 100) / (float(dividend) / (params['dt'] / 86400)) else: return self.amount - async def differential(self): + def differential(self): """ Return relative value of amount @@ -85,17 +85,17 @@ class UDDToPast(BaseReferential): :param sakia.core.community.Community community: Community instance :return: float """ - dividend = await self.community.dividend(self._block_number) - params = await self.community.parameters() + dividend = self.community.dividend(self._block_number) + params = self.community.parameters() if dividend > 0: return (self.amount * 100) / (float(dividend) / (params['dt'] / 86400)) else: return self.amount - async def localized(self, units=False, international_system=False): + def localized(self, units=False, international_system=False): from . import Relative - value = await self.value() - block = await self.community.get_block() + value = self.value() + block = self.community.get_block() prefix = "" if international_system: localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) @@ -115,10 +115,10 @@ class UDDToPast(BaseReferential): else: return localized_value - async def diff_localized(self, units=False, international_system=False): + def diff_localized(self, units=False, international_system=False): from . import Relative - value = await self.differential() - block = await self.community.get_block(self._block_number) + value = self.differential() + block = self.community.get_block(self._block_number) prefix = "" if international_system and value != 0: localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) diff --git a/src/sakia/services/transactions.py b/src/sakia/services/transactions.py index a3365470d53b36c901dc160dff8dbdd24c6f9360..109a4b06c7b8bace657704ae3b8f3ac17ed99adf 100644 --- a/src/sakia/services/transactions.py +++ b/src/sakia/services/transactions.py @@ -1,10 +1,7 @@ from PyQt5.QtCore import QObject -from duniterpy.documents.transaction import reduce_base -from duniterpy.documents import Transaction, SimpleTransaction -from sakia.data.entities import Transaction -import math +from sakia.data.entities.transaction import parse_transaction_doc +from duniterpy.documents import SimpleTransaction import logging -import hashlib class TransactionsService(QObject): @@ -28,60 +25,6 @@ class TransactionsService(QObject): self.currency = currency self._logger = logging.getLogger('sakia') - async def _parse_transaction(self, tx_doc, blockUID, mediantime, txid): - """ - Parse a transaction - :param duniterpy.documents.Transaction tx_doc: The tx json data - :param duniterpy.documents.BlockUID blockUID: The block id where we found the tx - :param int mediantime: Median time on the network - :param int txid: The latest txid - :return: the found transaction - """ - receivers = [o.conditions.left.pubkey for o in tx_doc.outputs - if o.conditions.left.pubkey != tx_doc.issuers[0]] - - if len(receivers) == 0: - receivers = [tx_doc.issuers[0]] - - in_issuers = len([i for i in tx_doc.issuers - if i == self.wallet.pubkey]) > 0 - in_outputs = len([o for o in tx_doc.outputs - if o.conditions.left.pubkey == self.wallet.pubkey]) > 0 - - tx_hash = hashlib.sha256(tx_doc.signed_raw().encode("ascii")).hexdigest().upper() - if in_issuers or in_outputs: - # If the wallet pubkey is in the issuers we sent this transaction - if in_issuers: - outputs = [o for o in tx_doc.outputs - if o.conditions.left.pubkey != self.wallet.pubkey] - amount = 0 - for o in outputs: - amount += o.amount * math.pow(10, o.base) - # If we are not in the issuers, - # maybe we are in the recipients of this transaction - else: - outputs = [o for o in tx_doc.outputs - if o.conditions.left.pubkey == self.wallet.pubkey] - amount = 0 - for o in outputs: - amount += o.amount * math.pow(10, o.base) - amount, amount_base = reduce_base(amount, 0) - - transaction = Transaction(currency=self.currency, - sha_hash=tx_hash, - written_on=blockUID.number, - blockstamp=tx_doc.blockstamp, - timestamp=mediantime, - signature=tx_doc.signatures[0], - issuer=tx_doc.issuers[0], - receiver=receivers[0], - amount=amount, - amount_base=amount_base, - comment=tx_doc.comment, - txid=txid) - return transaction - return None - async def _parse_block(self, block_doc, txid): """ Parse a block @@ -98,8 +41,7 @@ class TransactionsService(QObject): and SimpleTransaction.is_simple(t)] for (i, tx_doc) in enumerate(new_transactions): - tx = await self._parse_transaction(tx_doc, block_doc.blockUID, - block_doc.mediantime, txid+i) + tx = parse_transaction_doc(tx_doc, block_doc.blockUID.number, block_doc.mediantime, txid+i) if tx: #logging.debug("Transfer amount : {0}".format(transfer.metadata['amount'])) self._transactions_processor.commit(tx)