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 8d53cc4592ad497fd83ce4c1b4557026ce8689a4..3dacabb4105a9e64c103cec76e42c39721bf3d3a 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 89b268697353a3472c2328e3cc28fb053fb0cc38..aecba4dfb35d556158b4e1a431afa950f680ab68 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