Commit 15a391ac authored by matograine's avatar matograine

add display_output_and_amount ; display_amount, display_pubkey and tests for tx.

parent b02c17c7
...@@ -146,6 +146,45 @@ def check_transaction_values( ...@@ -146,6 +146,45 @@ 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".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 is_member(pubkey)
if id:
tx.append([message + " (id)", id["uid"]])
async def display_output_and_amount(tx, outputAddresses, tx_amount, currency_symbol):
"""
For transaction_confirmation,
Displays the receiver(s) and the amount(s) of one or many output(s).
"""
for outputAddress in outputAddresses:
await display_pubkey(tx, "to", outputAddress)
await display_amount(tx, "amount", tx_amount, currency_symbol)
async def transaction_confirmation( async def transaction_confirmation(
issuer_pubkey, pubkey_amount, tx_amount, outputAddresses, outputBackChange, comment issuer_pubkey, pubkey_amount, tx_amount, outputAddresses, outputBackChange, comment
): ):
...@@ -158,20 +197,11 @@ async def transaction_confirmation( ...@@ -158,20 +197,11 @@ async def transaction_confirmation(
tx.append( tx.append(
["pubkey’s balance before tx", str(pubkey_amount / 100) + " " + currency_symbol] ["pubkey’s balance before tx", str(pubkey_amount / 100) + " " + currency_symbol]
) )
tx.append(
[ await display_amount(
"tx amount (unit)", tx, "total amount", float(tx_amount * len(outputAddresses)), currency_symbol
str(tx_amount / 100 * len(outputAddresses)) + " " + currency_symbol,
]
)
tx.append(
[
"tx amount (relative)",
str(round(tx_amount / await UDValue().ud_value, 4))
+ " UD "
+ currency_symbol,
]
) )
tx.append( tx.append(
[ [
"pubkey’s balance after tx", "pubkey’s balance after tx",
...@@ -180,20 +210,12 @@ async def transaction_confirmation( ...@@ -180,20 +210,12 @@ async def transaction_confirmation(
+ currency_symbol, + currency_symbol,
] ]
) )
tx.append(["from (pubkey)", issuer_pubkey])
id_from = await is_member(issuer_pubkey) await display_pubkey(tx, "from", issuer_pubkey)
if id_from: await display_output_and_amount(tx, outputAddresses, tx_amount, currency_symbol)
tx.append(["from (id)", id_from["uid"]])
for outputAddress in outputAddresses:
tx.append(["to (pubkey)", outputAddress])
id_to = await is_member(outputAddress)
if id_to:
tx.append(["to (id)", id_to["uid"]])
if outputBackChange: if outputBackChange:
tx.append(["Backchange (pubkey)", outputBackChange]) await display_pubkey(tx, "Backchange", outputBackChange)
id_backchange = await is_member(outputBackChange)
if id_backchange:
tx.append(["Backchange (id)", id_backchange["uid"]])
tx.append(["comment", comment]) tx.append(["comment", comment])
return tx return tx
......
import pytest import pytest
from silkaj.tx import truncBase from silkaj.tx import (
truncBase,
display_pubkey,
display_amount,
display_output_and_amount,
)
from silkaj.money import UDValue
# truncBase()
@pytest.mark.parametrize( @pytest.mark.parametrize(
"amount,base,expected", "amount,base,expected",
[(0, 0, 0), (10, 2, 0), (100, 2, 100), (306, 2, 300), (3060, 3, 3000)], [(0, 0, 0), (10, 2, 0), (100, 2, 100), (306, 2, 300), (3060, 3, 3000)],
) )
def test_truncBase(amount, base, expected): def test_truncBase(amount, base, expected):
assert truncBase(amount, base) == expected assert truncBase(amount, base) == expected
# display_amount()
@pytest.mark.parametrize("message, amount, currency_symbol", [("Total", 1000, "Ğ1")])
@pytest.mark.asyncio
async def test_display_amount(message, amount, currency_symbol):
amount_UD = round(amount / await UDValue().ud_value, 4)
expected = [
[
message + " (unit | relative)",
str(amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
]
]
tx = list()
await display_amount(tx, message, amount, currency_symbol)
assert tx == expected
# display_pubkey()
@pytest.mark.parametrize(
"message, pubkey, id",
[
("From", "CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd", "Matograine"),
("To", "9qJjmuYi4Sti89Lm81G7AoFo5QLpmxd1NSMqN4Z1BwhS", ""),
],
)
@pytest.mark.asyncio
async def test_display_pubkey(message, pubkey, id):
if id == "":
expected = [[message + " (pubkey)", pubkey]]
else:
expected = [[message + " (pubkey)", pubkey], [message + " (id)", id]]
tx = list()
await display_pubkey(tx, message, pubkey)
print("DEBUG : tx : ", tx) # to see if display_pubkey gets the ID
assert tx == expected
# display_output_and_amount()
@pytest.mark.parametrize(
"outputAddresses, amount, currency_symbol, ids",
[
(["CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd"], 1000, "Ğ1", ["Matograine"]),
(["d88fPFbDdJXJANHH7hedFMaRyGcnVZj9c5cDaE76LRN"], 1000, "Ğ1", [""]),
(
[
"CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd",
"d88fPFbDdJXJANHH7hedFMaRyGcnVZj9c5cDaE76LRN",
],
1000,
"Ğ1",
["Matograine", ""],
),
],
)
@pytest.mark.asyncio
async def test_display_output_and_amount(
outputAddresses, amount, currency_symbol, ids
):
amount_UD = round(amount / await UDValue().ud_value, 4)
expected = list()
if len(outputAddresses) == 1:
if ids(0) == "":
expected.append(
["to (pubkey)", outputAddresses[0]],
[
"amount (unit | relative)",
str(amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
],
)
else:
expected.append(
["to (pubkey)", outputAddresses],
["to (id)", ids(0)],
[
"amount (unit | relative)",
str(amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
],
)
else:
for outputAddress, id in zip(outputAddresses, ids):
if id == "":
expected.append(
["to (pubkey)", outputAddress],
[
"amount (unit | relative)",
str(amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
],
)
else:
expected.append(
["to (pubkey)", outputAddress],
["to (id)", id],
[
"amount (unit | relative)",
str(amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
],
)
tx = list()
await display_output_and_amount(tx, outputAddresses, amount, currency_symbol)
assert tx == expected
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment