From bda2b8cd69c680c5dcd2c2a23753ec8c6ff9b686 Mon Sep 17 00:00:00 2001
From: atrax <atrax@prtn.email>
Date: Tue, 8 Dec 2020 20:51:23 +0100
Subject: [PATCH] [fix] #296 : Prevent sending 0 with --amountUD

---
 silkaj/constants.py   |  3 ++-
 silkaj/tx.py          | 13 +++++++------
 tests/test_tx.py      |  6 ++++--
 tests/test_unit_tx.py |  4 ++--
 4 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/silkaj/constants.py b/silkaj/constants.py
index 1bfb6230..27fbae0e 100644
--- a/silkaj/constants.py
+++ b/silkaj/constants.py
@@ -27,6 +27,7 @@ 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
+MINIMAL_ABSOLUTE_TX_AMOUNT = 0.01
+MINIMAL_RELATIVE_TX_AMOUNT = 1e-6
 CENT_MULT_TO_UNIT = 100
 SHORT_PUBKEY_SIZE = 8
diff --git a/silkaj/tx.py b/silkaj/tx.py
index 49312df6..cede04cb 100644
--- a/silkaj/tx.py
+++ b/silkaj/tx.py
@@ -30,7 +30,8 @@ from silkaj.auth import auth_method
 from silkaj import money
 from silkaj.constants import (
     SOURCES_PER_TX,
-    MINIMAL_TX_AMOUNT,
+    MINIMAL_ABSOLUTE_TX_AMOUNT,
+    MINIMAL_RELATIVE_TX_AMOUNT,
     CENT_MULT_TO_UNIT,
     ASYNC_SLEEP,
 )
@@ -47,9 +48,9 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter
     "--amount",
     "-a",
     multiple=True,
-    type=FloatRange(MINIMAL_TX_AMOUNT),
+    type=FloatRange(MINIMAL_ABSOLUTE_TX_AMOUNT),
     help="Quantitative amount(s):\n-a <amount>\nMinimum amount is {0}".format(
-        MINIMAL_TX_AMOUNT
+        MINIMAL_ABSOLUTE_TX_AMOUNT
     ),
     cls=MutuallyExclusiveOption,
     mutually_exclusive=["amountsud", "allsources"],
@@ -59,8 +60,8 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter
     "--amountUD",
     "-d",
     multiple=True,
-    type=float,
-    help="Relative amount(s):\n-d <amount_UD>",
+    type=FloatRange(MINIMAL_RELATIVE_TX_AMOUNT),
+    help=f"Relative amount(s):\n-d <amount_UD>\nMinimum amount is {MINIMAL_RELATIVE_TX_AMOUNT}",
     cls=MutuallyExclusiveOption,
     mutually_exclusive=["amounts", "allsources"],
 )
@@ -191,7 +192,7 @@ def compute_amounts(amounts, multiplicator):
         computed_amount = amount * multiplicator
         # check if relative amounts are high enough
         if (multiplicator != CENT_MULT_TO_UNIT) and (
-            computed_amount < (MINIMAL_TX_AMOUNT * CENT_MULT_TO_UNIT)
+            computed_amount < (MINIMAL_ABSOLUTE_TX_AMOUNT * CENT_MULT_TO_UNIT)
         ):
             message_exit("Error: amount {0} is too low.".format(amount))
         amounts_list.append(round(computed_amount))
diff --git a/tests/test_tx.py b/tests/test_tx.py
index 4c17cff5..fae298cf 100644
--- a/tests/test_tx.py
+++ b/tests/test_tx.py
@@ -21,7 +21,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, FAILURE_EXIT_STATUS
+from silkaj.constants import MINIMAL_ABSOLUTE_TX_AMOUNT, FAILURE_EXIT_STATUS
 import patched
 
 
@@ -103,7 +103,9 @@ def test_tx_passed_amount_cli():
     )
     assert result.exit_code == FAILURE_EXIT_STATUS
 
-    result = CliRunner().invoke(cli, ["tx", "-r", "A", "-a", MINIMAL_TX_AMOUNT - 0.001])
+    result = CliRunner().invoke(
+        cli, ["tx", "-r", "A", "-a", MINIMAL_ABSOLUTE_TX_AMOUNT - 0.001]
+    )
     assert "Error: Invalid value for '--amount'" in result.output
     assert result.exit_code == 2
 
diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py
index ba0e09f8..0a784039 100644
--- a/tests/test_unit_tx.py
+++ b/tests/test_unit_tx.py
@@ -29,7 +29,7 @@ from silkaj.money import UDValue
 from silkaj.constants import (
     G1_SYMBOL,
     CENT_MULT_TO_UNIT,
-    MINIMAL_TX_AMOUNT,
+    MINIMAL_ABSOLUTE_TX_AMOUNT,
 )
 from duniterpy.documents.transaction import (
     InputSource,
@@ -280,7 +280,7 @@ async def test_transaction_amount(
 
     def too_little_amount(amounts, multiplicator):
         for amount in amounts:
-            if amount * multiplicator < MINIMAL_TX_AMOUNT * CENT_MULT_TO_UNIT:
+            if amount * multiplicator < MINIMAL_ABSOLUTE_TX_AMOUNT * CENT_MULT_TO_UNIT:
                 return True
             return False
 
-- 
GitLab