From 6ee8c48d3bbacab36e7fed4ef5a18f05cb5da1c9 Mon Sep 17 00:00:00 2001 From: matograine <tom.ngr@zaclys.net> Date: Wed, 22 Jan 2020 18:16:03 +0100 Subject: [PATCH] [mod] #111 : create MINIMAL_TX_AMOUNT, CENT_MULT_TO_UNIT constants * both constants imported to tx.py * MINIMAL_TX_AMOUNT used in --amount click option * import silkaj.money and change related functions calls * introduce one test case for send_transaction() --- silkaj/constants.py | 2 ++ silkaj/tx.py | 29 +++++++++++++++-------------- tests/test_tx.py | 5 +++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/silkaj/constants.py b/silkaj/constants.py index 1d4c3574..9d598dba 100644 --- a/silkaj/constants.py +++ b/silkaj/constants.py @@ -27,3 +27,5 @@ SUCCESS_EXIT_STATUS = 0 FAILURE_EXIT_STATUS = 1 BMA_MAX_BLOCKS_CHUNK_SIZE = 5000 PUBKEY_PATTERN = "[1-9A-HJ-NP-Za-km-z]{43,44}" +MINIMAL_TX_AMOUNT = 0.01 +CENT_MULT_TO_UNIT = 100 diff --git a/silkaj/tx.py b/silkaj/tx.py index 8d53cc45..3dacabb4 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -26,13 +26,12 @@ 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.money import ( - get_sources, - get_amount_from_pubkey, - UDValue, - amount_in_current_base, +from silkaj import money +from silkaj.constants import ( + SOURCES_PER_TX, + MINIMAL_TX_AMOUNT, + CENT_MULT_TO_UNIT, ) -from silkaj.constants import SOURCES_PER_TX from silkaj.tui import display_amount, display_pubkey from duniterpy.api.bma.tx import process @@ -46,8 +45,10 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter "--amount", "-a", multiple=True, - type=FloatRange(0.01), - help="Quantitative amount(s):\n-a <amount>\nMinimum amount is 0.01.", + type=FloatRange(MINIMAL_TX_AMOUNT), + help="Quantitative amount(s):\n-a <amount>\nMinimum amount is {0}".format( + MINIMAL_TX_AMOUNT + ), cls=MutuallyExclusiveOption, mutually_exclusive=["amountsud", "allsources"], ) @@ -104,7 +105,7 @@ async def send_transaction( key = auth_method() issuer_pubkey = key.pubkey - pubkey_amount = await get_amount_from_pubkey(issuer_pubkey) + pubkey_amount = await money.get_amount_from_pubkey(issuer_pubkey) if allsources: tx_amount = pubkey_amount[0] outputAddresses = recipients.split(":") @@ -149,7 +150,7 @@ async def transaction_amount(amount, amountUD, allSources): if amount: return round(amount * 100) if amountUD: - return round(amountUD * await UDValue().ud_value) + return round(amountUD * await money.UDValue().ud_value) def check_transaction_values( @@ -177,7 +178,7 @@ async def transaction_confirmation( """ currency_symbol = await CurrencySymbol().symbol - ud_value = await UDValue().ud_value + ud_value = await money.UDValue().ud_value tx = list() tx.append( ["pubkey’s balance before tx", str(pubkey_amount / 100) + " " + currency_symbol] @@ -212,7 +213,7 @@ async def transaction_confirmation( async def get_list_input_for_transaction(pubkey, TXamount): - listinput, amount = await get_sources(pubkey) + listinput, amount = await money.get_sources(pubkey) # generate final list source listinputfinal = [] @@ -220,8 +221,8 @@ async def get_list_input_for_transaction(pubkey, TXamount): intermediatetransaction = False for input in listinput: listinputfinal.append(input) - totalAmountInput += amount_in_current_base(input) - TXamount -= amount_in_current_base(input) + totalAmountInput += money.amount_in_current_base(input) + TXamount -= money.amount_in_current_base(input) # if more than 40 sources, it's an intermediate transaction if len(listinputfinal) >= SOURCES_PER_TX: intermediatetransaction = True diff --git a/tests/test_tx.py b/tests/test_tx.py index 89b26869..aecba4df 100644 --- a/tests/test_tx.py +++ b/tests/test_tx.py @@ -3,6 +3,7 @@ from click.testing import CliRunner from silkaj.tx import transaction_amount from silkaj.money import UDValue from silkaj.cli import cli +from silkaj.constants import MINIMAL_TX_AMOUNT @pytest.mark.asyncio @@ -61,3 +62,7 @@ def test_tx_passed_amount_cli(): result = CliRunner().invoke(cli, ["tx", "-r", "A", "-r", "B", "--allSources"]) assert "Error: the --allSources" in result.output assert result.exit_code == 1 + + result = CliRunner().invoke(cli, ["tx", "-r", "A", "-a", MINIMAL_TX_AMOUNT - 0.001]) + assert 'Error: Invalid value for "--amount"' in result.output + assert result.exit_code == 2 -- GitLab