diff --git a/silkaj/tui.py b/silkaj/tui.py new file mode 100644 index 0000000000000000000000000000000000000000..31a6de127fd9cbf6c572b576300200801b644624 --- /dev/null +++ b/silkaj/tui.py @@ -0,0 +1,45 @@ +""" +Copyright 2016-2019 Maël Azimi <m.a@moul.re> + +Silkaj is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Silkaj is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Silkaj. If not, see <https://www.gnu.org/licenses/>. +""" + +from silkaj import wot + + +def display_amount(tx, message, amount, ud_value, currency_symbol): + """ + Displays an amount in unit and relative reference. + """ + amount_UD = round((amount / ud_value), 4) + tx.append( + [ + message + " (unit|relative)", + "{unit_amount} {currency_symbol} | {UD_amount} UD {currency_symbol}".format( + unit_amount=str(amount / 100), + currency_symbol=currency_symbol, + UD_amount=str(amount_UD), + ), + ] + ) + + +async def display_pubkey(tx, message, pubkey): + """ + Displays a pubkey and the eventually associated id. + """ + tx.append([message + " (pubkey)", pubkey]) + id = await wot.is_member(pubkey) + if id: + tx.append([message + " (id)", id["uid"]]) diff --git a/silkaj/tx.py b/silkaj/tx.py index a150a35a79fe9b672cd17b50ae8f1fbecea7002b..c0237d014e64305d94324eedad59d20416a02feb 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -26,7 +26,6 @@ from silkaj.network_tools import ClientInstance, HeadBlock from silkaj.crypto_tools import check_public_key from silkaj.tools import message_exit, CurrencySymbol, coroutine from silkaj.auth import auth_method -from silkaj import wot from silkaj.money import ( get_sources, get_amount_from_pubkey, @@ -34,6 +33,7 @@ from silkaj.money import ( amount_in_current_base, ) from silkaj.constants import SOURCES_PER_TX +from silkaj.tui import display_amount, display_pubkey from duniterpy.api.bma.tx import process from duniterpy.documents import BlockUID, Transaction @@ -149,35 +149,6 @@ def check_transaction_values( ) -async def display_amount(tx, message, amount, currency_symbol): - """ - For transaction_confirmation, - Displays an amount in unit and relative reference. - """ - amount_UD = round((amount / await UDValue().ud_value), 4) - tx.append( - [ - message + " (unit|relative)", - "{unit_amount} {currency_symbol} | {UD_amount} UD {currency_symbol}".format( - unit_amount=str(amount / 100), - currency_symbol=currency_symbol, - UD_amount=str(amount_UD), - ), - ] - ) - - -async def display_pubkey(tx, message, pubkey): - """ - For transaction_confirmation, - Displays a pubkey and the eventually associated id. - """ - tx.append([message + " (pubkey)", pubkey]) - id = await wot.is_member(pubkey) - if id: - tx.append([message + " (id)", id["uid"]]) - - async def transaction_confirmation( issuer_pubkey, pubkey_amount, tx_amount, outputAddresses, outputBackChange, comment ): @@ -186,13 +157,18 @@ async def transaction_confirmation( """ currency_symbol = await CurrencySymbol().symbol + ud_value = await UDValue().ud_value tx = list() tx.append( ["pubkey’s balance before tx", str(pubkey_amount / 100) + " " + currency_symbol] ) - await display_amount( - tx, "total amount", float(tx_amount * len(outputAddresses)), currency_symbol + display_amount( + tx, + "total amount", + float(tx_amount * len(outputAddresses)), + ud_value, + currency_symbol, ) tx.append( @@ -207,7 +183,7 @@ async def transaction_confirmation( await display_pubkey(tx, "from", issuer_pubkey) for outputAddress in outputAddresses: await display_pubkey(tx, "to", outputAddress) - await display_amount(tx, "amount", tx_amount, currency_symbol) + display_amount(tx, "amount", tx_amount, ud_value, currency_symbol) if outputBackChange: await display_pubkey(tx, "Backchange", outputBackChange) diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 069958ce7574e021f2159820df47ae8df91bebb5..eca98c118fb13dcf87cb313de5c2984ea6036293 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -1,10 +1,6 @@ import pytest -from silkaj.tx import ( - truncBase, - display_pubkey, - display_amount, - transaction_confirmation, -) +from silkaj.tx import truncBase, transaction_confirmation +from silkaj.tui import display_pubkey, display_amount from silkaj.money import UDValue from silkaj.constants import G1_SYMBOL import patched @@ -22,11 +18,9 @@ def test_truncBase(amount, base, expected): @pytest.mark.parametrize( "message, amount, currency_symbol", [("Total", 1000, G1_SYMBOL)] ) -@pytest.mark.asyncio -async def test_display_amount(message, amount, currency_symbol, monkeypatch): - monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched.ud_value) - - amount_UD = round(amount / await UDValue().ud_value, 4) +def test_display_amount(message, amount, currency_symbol, monkeypatch): + ud_value = patched.mock_ud_value + amount_UD = round(amount / ud_value, 4) expected = [ [ message + " (unit|relative)", @@ -40,7 +34,7 @@ async def test_display_amount(message, amount, currency_symbol, monkeypatch): ] ] tx = list() - await display_amount(tx, message, amount, currency_symbol) + display_amount(tx, message, amount, ud_value, currency_symbol) assert tx == expected @@ -122,6 +116,7 @@ async def test_transaction_confirmation( ) # creating expected list + ud_value = await UDValue().ud_value expected = list() expected.append( [ @@ -130,10 +125,11 @@ async def test_transaction_confirmation( ] ) - await display_amount( + display_amount( expected, "total amount", float(tx_amount * len(outputAddresses)), + ud_value, currency_symbol, ) @@ -149,7 +145,7 @@ async def test_transaction_confirmation( await display_pubkey(expected, "from", issuer_pubkey) for outputAddress in outputAddresses: await display_pubkey(expected, "to", outputAddress) - await display_amount(expected, "amount", tx_amount, currency_symbol) + display_amount(expected, "amount", tx_amount, ud_value, currency_symbol) if outputBackChange: await display_pubkey(expected, "Backchange", outputBackChange)