diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index 828b9903f..a5a88fff8 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -5,6 +5,7 @@ import requests import urllib import json import textwrap +import re from operator import itemgetter from binascii import hexlify, unhexlify @@ -3325,17 +3326,12 @@ class Daemon(AuthJSONRPCServer): raise ValueError("Couldn't find account: {}.".format(account_id)) @staticmethod - def get_dewies_or_error(argument: str, amount: Union[str, int, float]): + def get_dewies_or_error(argument: str, amount: str): if isinstance(amount, str): - if '.' in amount: - try: - return int(Decimal(amount) * COIN) - except InvalidOperation: - raise ValueError("Invalid decimal for '{}' argument: {}".format(argument, amount)) - elif amount.isdigit(): - amount = int(amount) - if isinstance(amount, (float, int)): - return int(amount * COIN) + result = re.search(r'^(\d{1,10})\.(\d{1,8})$', amount) + if result is not None: + whole, fractional = result.groups() + return int(whole+fractional.ljust(8,"0")) raise ValueError("Invalid value for '{}' argument: {}".format(argument, amount)) diff --git a/tests/unit/lbrynet_daemon/test_Daemon.py b/tests/unit/lbrynet_daemon/test_Daemon.py index 64e1ce83c..e7ec5a9b5 100644 --- a/tests/unit/lbrynet_daemon/test_Daemon.py +++ b/tests/unit/lbrynet_daemon/test_Daemon.py @@ -313,3 +313,29 @@ class TestFileListSorting(unittest.TestCase): setattr(lbry_file, key, faked_attributes[key]) return lbry_file + + +class TestDeweyInputOutput(unittest.TestCase): + + def test_input(self): + self.assertEqual(LBRYDaemon.get_dewies_or_error("", "1.0"), 100000000) + self.assertEqual(LBRYDaemon.get_dewies_or_error("", "2.00000000"), 200000000) + self.assertEqual(LBRYDaemon.get_dewies_or_error("", "2000000000.0"), 200000000000000000) + + def test_invalid_input(self): + with self.assertRaises(ValueError): + LBRYDaemon.get_dewies_or_error("", "1") + with self.assertRaises(ValueError): + LBRYDaemon.get_dewies_or_error("", "-1.0") + with self.assertRaises(ValueError): + LBRYDaemon.get_dewies_or_error("", "10000000000.0") + with self.assertRaises(ValueError): + LBRYDaemon.get_dewies_or_error("", "1.000000000") + with self.assertRaises(ValueError): + LBRYDaemon.get_dewies_or_error("", "-0") + with self.assertRaises(ValueError): + LBRYDaemon.get_dewies_or_error("", "1") + with self.assertRaises(ValueError): + LBRYDaemon.get_dewies_or_error("", ".1") + with self.assertRaises(ValueError): + LBRYDaemon.get_dewies_or_error("", "1e-7")