From 2a1de80c110e77d40c6d870f62c23f73057a48c6 Mon Sep 17 00:00:00 2001 From: Roman505050 Date: Mon, 20 Oct 2025 01:56:22 +0300 Subject: [PATCH 1/4] Add static methods for converting to and from wei with decimals --- newsfragments/3768.feature.rst | 1 + web3/main.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 newsfragments/3768.feature.rst diff --git a/newsfragments/3768.feature.rst b/newsfragments/3768.feature.rst new file mode 100644 index 0000000000..881c0cc919 --- /dev/null +++ b/newsfragments/3768.feature.rst @@ -0,0 +1 @@ +Add static methods for converting to and from wei with decimals \ No newline at end of file diff --git a/web3/main.py b/web3/main.py index 28a945b2c7..d11bfdabb2 100644 --- a/web3/main.py +++ b/web3/main.py @@ -14,6 +14,7 @@ add_0x_prefix, apply_to_return_value, from_wei, + from_wei_decimals, is_address, is_checksum_address, keccak as eth_utils_keccak, @@ -23,6 +24,7 @@ to_int, to_text, to_wei, + to_wei_decimals, ) from functools import ( wraps, @@ -261,6 +263,20 @@ def to_wei(number: Union[int, float, str, decimal.Decimal], unit: str) -> Wei: def from_wei(number: int, unit: str) -> Union[int, decimal.Decimal]: return from_wei(number, unit) + @staticmethod + @wraps(to_wei_decimals) + def to_wei_decimals( + number: Union[int, float, str, decimal.Decimal], decimals: int + ) -> Wei: + return cast(Wei, to_wei_decimals(number, decimals)) + + @staticmethod + @wraps(from_wei_decimals) + def from_wei_decimals( + number: int, decimals: int + ) -> Union[int, decimal.Decimal]: + return from_wei_decimals(number, decimals) + # Address Utility @staticmethod @wraps(is_address) From 8a70b9a903bb6f7a300930a99691307d47c1ecfc Mon Sep 17 00:00:00 2001 From: Roman505050 Date: Mon, 20 Oct 2025 02:05:45 +0300 Subject: [PATCH 2/4] Add docs --- docs/web3.main.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/web3.main.rst b/docs/web3.main.rst index d7e09e1b3c..e5c76c0ae8 100644 --- a/docs/web3.main.rst +++ b/docs/web3.main.rst @@ -324,6 +324,30 @@ Currency Conversions Decimal('1') +.. py:method:: Web3.to_wei_decimals(value, decimals) + + Returns the value in the denomination specified by the ``decimals`` argument + converted to wei. + + + .. code-block:: python + + >>> Web3.to_wei_decimals(1, 18) + 1000000000000000000 + + +.. py:method:: Web3.from_wei_decimals(value, decimals) + + Returns the value in wei converted to the given number of decimals. The value is returned + as a ``Decimal`` to ensure precision down to the wei. + + + .. code-block:: python + + >>> Web3.from_wei_decimals(1000000000000000000, 18) + Decimal('1') + + .. _overview_addresses: Addresses From afe1ef4c5a7523bb2718bb6f1cabb3b2dfda8675 Mon Sep 17 00:00:00 2001 From: Roman505050 Date: Mon, 20 Oct 2025 02:07:16 +0300 Subject: [PATCH 3/4] Fix tests --- web3/main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/web3/main.py b/web3/main.py index d11bfdabb2..c07cbc3934 100644 --- a/web3/main.py +++ b/web3/main.py @@ -272,9 +272,7 @@ def to_wei_decimals( @staticmethod @wraps(from_wei_decimals) - def from_wei_decimals( - number: int, decimals: int - ) -> Union[int, decimal.Decimal]: + def from_wei_decimals(number: int, decimals: int) -> Union[int, decimal.Decimal]: return from_wei_decimals(number, decimals) # Address Utility From 174314667714d0f11b991d7bd9eb990707906b2b Mon Sep 17 00:00:00 2001 From: Roman505050 Date: Mon, 20 Oct 2025 02:12:22 +0300 Subject: [PATCH 4/4] Fix tests --- newsfragments/3768.feature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/newsfragments/3768.feature.rst b/newsfragments/3768.feature.rst index 881c0cc919..dcb990e770 100644 --- a/newsfragments/3768.feature.rst +++ b/newsfragments/3768.feature.rst @@ -1 +1 @@ -Add static methods for converting to and from wei with decimals \ No newline at end of file +Add static methods for converting to and from wei with decimals