From f6237540545766fee7f14c4c625f106c569d33ce Mon Sep 17 00:00:00 2001 From: asimaranov Date: Tue, 27 Jun 2023 00:26:02 -0300 Subject: [PATCH 1/3] Added support for custom unit decimals providing --- eth_utils/currency.py | 15 +++++++++++---- tests/currency-utils/test_currency_tools.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/eth_utils/currency.py b/eth_utils/currency.py index b3b6a7af..3653ebe0 100644 --- a/eth_utils/currency.py +++ b/eth_utils/currency.py @@ -36,7 +36,7 @@ class denoms: MAX_WEI = 2**256 - 1 -def from_wei(number: int, unit: str) -> Union[int, decimal.Decimal]: +def from_wei(number: int, unit: Union[str, int]) -> Union[int, decimal.Decimal]: """ Takes a number of wei and converts it to any other ether unit. """ @@ -51,7 +51,10 @@ def from_wei(number: int, unit: str) -> Union[int, decimal.Decimal]: if number < MIN_WEI or number > MAX_WEI: raise ValueError("value must be between 1 and 2**256 - 1") - unit_value = units[unit.lower()] + if is_integer(unit): + unit_value = decimal.Decimal(10) ** decimal.Decimal(unit) + else: + unit_value = units[unit.lower()] with localcontext() as ctx: ctx.prec = 999 @@ -61,7 +64,7 @@ def from_wei(number: int, unit: str) -> Union[int, decimal.Decimal]: return result_value -def to_wei(number: Union[int, float, str, decimal.Decimal], unit: str) -> int: +def to_wei(number: Union[int, float, str, decimal.Decimal], unit: Union[str, int]) -> int: """ Takes a number of a unit and converts it to wei. """ @@ -80,7 +83,11 @@ def to_wei(number: Union[int, float, str, decimal.Decimal], unit: str) -> int: raise TypeError("Unsupported type. Must be one of integer, float, or string") s_number = str(number) - unit_value = units[unit.lower()] + + if is_integer(unit): + unit_value = decimal.Decimal(10) ** decimal.Decimal(unit) + else: + unit_value = units[unit.lower()] if d_number == decimal.Decimal(0): return 0 diff --git a/tests/currency-utils/test_currency_tools.py b/tests/currency-utils/test_currency_tools.py index 537bbb83..2d5dc664 100644 --- a/tests/currency-utils/test_currency_tools.py +++ b/tests/currency-utils/test_currency_tools.py @@ -116,6 +116,23 @@ def test_float_ether(value): assert from_wei(to_wei(value, "ether"), "ether") == decimal.Decimal(str(value)) +@pytest.mark.parametrize( + "value", + [ + 1, # 1000000000000000000 wei + 0.1, # 100000000000000000 wei + 0.01, # 10000000000000000 wei + 0.001, # 1000000000000000 wei + 0.00000000000000001, # 10 wei + 0.000000000000000001, # 1 wei + 1.234567891234567891, # 1234567891234567891 wei + 0, # 0 wei + ], +) +def test_float_usdt(value): + assert from_wei(to_wei(value, 6), 6) == decimal.Decimal(str(value)) + + @pytest.mark.parametrize( "value", [ From 8a7b5fa9c34933503a9492688935966c946eb386 Mon Sep 17 00:00:00 2001 From: asimaranov Date: Tue, 27 Jun 2023 01:12:04 -0300 Subject: [PATCH 2/3] Fixed failed tests --- eth_utils/currency.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eth_utils/currency.py b/eth_utils/currency.py index 3653ebe0..cd8cb728 100644 --- a/eth_utils/currency.py +++ b/eth_utils/currency.py @@ -40,7 +40,7 @@ def from_wei(number: int, unit: Union[str, int]) -> Union[int, decimal.Decimal]: """ Takes a number of wei and converts it to any other ether unit. """ - if unit.lower() not in units: + if not is_integer(number) and unit.lower() not in units: raise ValueError( "Unknown unit. Must be one of {0}".format("/".join(units.keys())) ) @@ -68,7 +68,7 @@ def to_wei(number: Union[int, float, str, decimal.Decimal], unit: Union[str, int """ Takes a number of a unit and converts it to wei. """ - if unit.lower() not in units: + if not is_integer(number) and unit.lower() not in units: raise ValueError( "Unknown unit. Must be one of {0}".format("/".join(units.keys())) ) From eab8c16fa7741cfc5af80426d5d7880d6e6716f7 Mon Sep 17 00:00:00 2001 From: asimaranov Date: Tue, 27 Jun 2023 02:13:45 -0300 Subject: [PATCH 3/3] Fixed tests --- eth_utils/currency.py | 4 ++-- tests/currency-utils/test_currency_tools.py | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/eth_utils/currency.py b/eth_utils/currency.py index cd8cb728..f0590167 100644 --- a/eth_utils/currency.py +++ b/eth_utils/currency.py @@ -40,7 +40,7 @@ def from_wei(number: int, unit: Union[str, int]) -> Union[int, decimal.Decimal]: """ Takes a number of wei and converts it to any other ether unit. """ - if not is_integer(number) and unit.lower() not in units: + if is_string(number) and unit.lower() not in units: raise ValueError( "Unknown unit. Must be one of {0}".format("/".join(units.keys())) ) @@ -68,7 +68,7 @@ def to_wei(number: Union[int, float, str, decimal.Decimal], unit: Union[str, int """ Takes a number of a unit and converts it to wei. """ - if not is_integer(number) and unit.lower() not in units: + if is_string(number) and unit.lower() not in units: raise ValueError( "Unknown unit. Must be one of {0}".format("/".join(units.keys())) ) diff --git a/tests/currency-utils/test_currency_tools.py b/tests/currency-utils/test_currency_tools.py index 2d5dc664..21c62584 100644 --- a/tests/currency-utils/test_currency_tools.py +++ b/tests/currency-utils/test_currency_tools.py @@ -119,14 +119,10 @@ def test_float_ether(value): @pytest.mark.parametrize( "value", [ - 1, # 1000000000000000000 wei - 0.1, # 100000000000000000 wei - 0.01, # 10000000000000000 wei - 0.001, # 1000000000000000 wei - 0.00000000000000001, # 10 wei - 0.000000000000000001, # 1 wei - 1.234567891234567891, # 1234567891234567891 wei - 0, # 0 wei + 1, # 1000000 wei + 0.1, # 100000 wei + 0.01, # 10000 wei + 0.001, # 1000 wei ], ) def test_float_usdt(value):