diff --git a/silkaj/constants.py b/silkaj/constants.py index 1d4c3574226e6b2e8e3c0d71562f473f9ea0b271..9d598dbaaabb1cc77db96a0cd699cb81aeabb3dc 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 f372e8a4ac9450b2d712cea57df3cf7c71f1f37c..877e832df71f4fd5a920c5df6c330cf13269a293 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"], ) @@ -105,7 +106,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_amounts = [pubkey_amount[0]] @@ -150,7 +151,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( @@ -178,7 +179,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] @@ -213,7 +214,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 = [] @@ -221,8 +222,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 03ce41a4554f620368bc2ccf4db33950f8f4ba89..55f1eb4d3b9d4b3ff460858d8ab93a659512777c 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 @@ -64,3 +65,7 @@ def test_tx_passed_amount_cli(): 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