diff --git a/silkaj/money/history.py b/silkaj/money/history.py index 748f2775653e96bcd0abca69c9c6c81d76a32e9d..216ae5882a4f072e4b1872462af8c362dac5d598 100644 --- a/silkaj/money/history.py +++ b/silkaj/money/history.py @@ -26,12 +26,8 @@ from duniterpy.api.client import Client from duniterpy.documents.transaction import OutputSource, Transaction from duniterpy.grammars.output import Condition -from silkaj.constants import ALL, ALL_DIGITAL -from silkaj.money.tools import ( - amount_in_current_base, - get_amount_from_pubkey, - get_ud_value, -) +from silkaj.constants import ALL, ALL_DIGITAL, CENT_MULT_TO_UNIT +from silkaj.money import tools as mt from silkaj.network import client_instance from silkaj.public_key import ( check_pubkey_format, @@ -66,7 +62,7 @@ def transaction_history( pubkey = validate_checksum(pubkey) client = client_instance() - ud_value = get_ud_value() + ud_value = mt.get_ud_value() currency_symbol = get_currency_symbol() received_txs, sent_txs = [], [] # type: list[Transaction], list[Transaction] @@ -107,11 +103,11 @@ def generate_header(pubkey: str, currency_symbol: str, ud_value: int) -> str: idty = wt.identity_of(pubkey) except HTTPError: idty = {"uid": ""} - balance = get_amount_from_pubkey(pubkey) + balance = mt.get_amount_from_pubkey(pubkey) balance_ud = round(balance[1] / ud_value, 2) date = arrow.now().format(ALL) return f"Transactions history from: {idty['uid']} {gen_pubkey_checksum(pubkey)}\n\ -Current balance: {balance[1] / 100} {currency_symbol}, {balance_ud} UD {currency_symbol} on {date}\n" +Current balance: {balance[1] / CENT_MULT_TO_UNIT} {currency_symbol}, {balance_ud} UD {currency_symbol} on {date}\n" def get_transactions_history( @@ -211,8 +207,8 @@ def parse_received_tx( full_pubkey, ) amounts = tx_amount(received_tx, pubkey, received_func)[0] - tx_list.append(amounts / 100) - tx_list.append(amounts / ud_value) + tx_list.append(amounts / CENT_MULT_TO_UNIT) + tx_list.append(round(amounts / ud_value, 2)) tx_list.append(received_tx.comment) received_txs_table.append(tx_list) @@ -248,7 +244,7 @@ def parse_sent_tx( total_amount, outputs = tx_amount(sent_tx, pubkey, sent_func) if len(outputs) > 1: tx_list.append("Total") - amounts = str(total_amount / 100) + amounts = str(total_amount / CENT_MULT_TO_UNIT) amounts_ud = str(round(total_amount / ud_value, 2)) else: tx_list.append("") @@ -258,10 +254,10 @@ def parse_sent_tx( for i, output in enumerate(outputs): if output_available(output.condition, ne, pubkey): amounts += prefix(None, outputs, i) + str( - neg(amount_in_current_base(output)) / 100, + neg(mt.amount_in_current_base(output)) / CENT_MULT_TO_UNIT, ) amounts_ud += prefix(None, outputs, i) + str( - round(neg(amount_in_current_base(output)) / ud_value, 2), + round(neg(mt.amount_in_current_base(output)) / ud_value, 2), ) tx_list[1] += prefix(tx_list[1], outputs, 0) + assign_idty_from_pubkey( output.condition.left.pubkey, @@ -293,13 +289,13 @@ def tx_amount( def received_func(output: OutputSource, pubkey: str) -> int: if output_available(output.condition, eq, pubkey): - return amount_in_current_base(output) + return mt.amount_in_current_base(output) return 0 def sent_func(output: OutputSource, pubkey: str) -> int: if output_available(output.condition, ne, pubkey): - return neg(amount_in_current_base(output)) + return neg(mt.amount_in_current_base(output)) return 0 diff --git a/silkaj/money/tools.py b/silkaj/money/tools.py index 2e16ae13144e0d8ef159f4ca16291992eca0a803..d753caca5bbd6176a6c58ec07e714fed88d6d112 100644 --- a/silkaj/money/tools.py +++ b/silkaj/money/tools.py @@ -19,6 +19,7 @@ from typing import Union from duniterpy.api import bma from duniterpy.documents.transaction import InputSource, OutputSource +from silkaj.constants import CENT_MULT_TO_UNIT from silkaj.network import client_instance from silkaj.public_key import gen_pubkey_checksum from silkaj.wot import tools as wt @@ -35,7 +36,7 @@ def display_amount( Displays an amount in unit and relative reference. """ UD_amount = str(round((amount / ud_value), 2)) - unit_amount = str(amount / 100) + unit_amount = str(amount / CENT_MULT_TO_UNIT) tx.append( [ f"{message} (unit|relative)", diff --git a/silkaj/money/transfer.py b/silkaj/money/transfer.py index a0144cbf4e5a3fc859604a1bf2bca83172c28a70..0dabcecc5b703206712029c25eb924c468507019 100644 --- a/silkaj/money/transfer.py +++ b/silkaj/money/transfer.py @@ -480,7 +480,7 @@ def generate_and_send_transaction( print(" - From: " + gen_pubkey_checksum(issuers)) for tx_amount, outputAddress in zip(tx_amounts, outputAddresses): display_sent_tx(outputAddress, tx_amount) - print(" - Total: " + str(sum(tx_amounts) / 100)) + print(" - Total: " + str(sum(tx_amounts) / CENT_MULT_TO_UNIT)) transaction = generate_transaction_document( issuers, @@ -499,7 +499,7 @@ def display_sent_tx(outputAddress: str, amount: int) -> None: " - To: ", gen_pubkey_checksum(outputAddress), "\n - Amount: ", - amount / 100, + amount / CENT_MULT_TO_UNIT, ) diff --git a/tests/unit/money/test_history.py b/tests/unit/money/test_history.py index 07aed17b883e5029f86b964a938a5cf96386ad06..7770d8d8f0f2b3cc2d89ef30bce26a338532b0d5 100644 --- a/tests/unit/money/test_history.py +++ b/tests/unit/money/test_history.py @@ -24,6 +24,7 @@ from silkaj import tools from silkaj.cli import cli from silkaj.constants import ( ALL_DIGITAL, + CENT_MULT_TO_UNIT, PUBKEY_MAX_LENGTH, PUBKEY_MIN_LENGTH, SHORT_PUBKEY_SIZE, @@ -34,6 +35,7 @@ from silkaj.public_key import CHECKSUM_SIZE from silkaj.wot import tools as wt from tests.patched.blockchain_tools import currency from tests.patched.money import patched_get_ud_value +from tests.patched.test_constants import mock_ud_value from tests.patched.tools import patched_get_currency_symbol from tests.patched.tx_history import patched_get_transactions_history from tests.patched.wot import patched_identities_from_pubkeys @@ -177,34 +179,35 @@ def test_prefix(tx_addresses, outputs, occurence, return_value): assert history.prefix(tx_addresses, outputs, occurence) == return_value +relative_amount = str(round(CENT_MULT_TO_UNIT / mock_ud_value, 2)) csv_reference = ( ["Date", "Issuers/Recipients", "Amounts Äž1", "Amounts UDÄž1", "Comment"], [ arrow.get(111111114).to("local").format(ALL_DIGITAL), "CvrMiUhAJpNyX5sdAyZqPE6yEFfSsf6j9EpMmeKvMCWW:DNB", "1.0", - "0.09041591320072333", + relative_amount, "initialisation", ], [ arrow.get(111111113).to("local").format(ALL_DIGITAL), "CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd:CQ5", "1.0", - "0.09041591320072333", + relative_amount, "", ], [ arrow.get(111111112).to("local").format(ALL_DIGITAL), "CvrMiUhAJpNyX5sdAyZqPE6yEFfSsf6j9EpMmeKvMCWW:DNB", "-1.0", - "-0.09", + f"-{relative_amount}", "", ], [ arrow.get(111111111).to("local").format(ALL_DIGITAL), "CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd:CQ5", "-1.0", - "-0.09", + f"-{relative_amount}", "", ], )