From e29ce515208d424c9f9e54d512fcc0d2007b67bb Mon Sep 17 00:00:00 2001 From: Stuart Reed Date: Wed, 13 Mar 2024 15:34:49 -0600 Subject: [PATCH] Rename `Contract.encodeABI` -> `Contract.encode_abi` (#3281) * Rename `Contract.encodeABI` -> `Contract.encode_abi` * Remove deprecated `Contract.encodeABI` * Docs and newsfragment for #3281 * Fix docs --- docs/overview.rst | 2 +- docs/v7_migration.rst | 1 + docs/web3.contract.rst | 14 +++++++------- ens/async_ens.py | 2 +- ens/ens.py | 2 +- newsfragments/3281.removal.rst | 1 + .../contracts/test_contract_method_abi_encoding.py | 10 +++++----- web3/_utils/module_testing/eth_module.py | 12 ++++++------ web3/contract/base_contract.py | 2 +- 9 files changed, 24 insertions(+), 22 deletions(-) create mode 100644 newsfragments/3281.removal.rst diff --git a/docs/overview.rst b/docs/overview.rst index e1ed45bd1e..8f707733c1 100644 --- a/docs/overview.rst +++ b/docs/overview.rst @@ -269,7 +269,7 @@ API - :attr:`Contract.events ` - :attr:`Contract.fallback ` - :meth:`Contract.constructor() ` -- :meth:`Contract.encodeABI() ` +- :meth:`Contract.encode_abi() ` - :attr:`web3.contract.ContractFunction ` - :attr:`web3.contract.ContractEvents ` diff --git a/docs/v7_migration.rst b/docs/v7_migration.rst index 9470b8674c..22105f6a09 100644 --- a/docs/v7_migration.rst +++ b/docs/v7_migration.rst @@ -154,3 +154,4 @@ Miscellaneous Changes - ``get_default_ipc_path()`` and ``get_dev_ipc_path()`` now return the path value without checking if the ``geth.ipc`` file exists. - ``Web3.is_address()`` returns ``True`` for non-checksummed addresses. +- ``Contract.encodeABI()`` has been renamed to ``Contract.encode_abi()``. diff --git a/docs/web3.contract.rst b/docs/web3.contract.rst index f5082e29bb..776f8df6cc 100644 --- a/docs/web3.contract.rst +++ b/docs/web3.contract.rst @@ -301,18 +301,18 @@ Each Contract Factory exposes the following methods. filter_builder.fromBlock = "latest" filter_builder.fromBlock = 0 # raises a ValueError +.. py:classmethod:: Contract.encode_abi(fn_name, args=None, kwargs=None, data=None) -.. py:classmethod:: Contract.encodeABI(fn_name, args=None, kwargs=None, data=None) + Encodes the arguments using the Ethereum ABI for the contract function that + matches the given ``fn_name`` and arguments ``args``. The ``data`` parameter + defaults to the function selector. - Encodes the arguments using the Ethereum ABI for the contract function that - matches the given ``fn_name`` and arguments ``args``. The ``data`` parameter - defaults to the function selector. - - .. code-block:: python + .. code-block:: python - >>> contract.encodeABI(fn_name="register", args=["rainbows", 10]) + >>> contract.encode_abi(fn_name="register", args=["rainbows", 10]) "0xea87152b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000087261696e626f7773000000000000000000000000000000000000000000000000" + .. py:classmethod:: Contract.all_functions() Returns a list of all the functions present in a Contract where every function is diff --git a/ens/async_ens.py b/ens/async_ens.py index 0e87080287..761502edb3 100644 --- a/ens/async_ens.py +++ b/ens/async_ens.py @@ -490,7 +490,7 @@ async def _resolve( ): contract_func_with_args = (fn_name, [node]) - calldata = resolver.encodeABI(*contract_func_with_args) + calldata = resolver.encode_abi(*contract_func_with_args) contract_call_result = await resolver.caller.resolve( ens_encode_name(normal_name), calldata, diff --git a/ens/ens.py b/ens/ens.py index a27937747c..c427d9d943 100644 --- a/ens/ens.py +++ b/ens/ens.py @@ -477,7 +477,7 @@ def _resolve( if _resolver_supports_interface(resolver, ENS_EXTENDED_RESOLVER_INTERFACE_ID): contract_func_with_args = (fn_name, [node]) - calldata = resolver.encodeABI(*contract_func_with_args) + calldata = resolver.encode_abi(*contract_func_with_args) contract_call_result = resolver.caller.resolve( ens_encode_name(normal_name), calldata, diff --git a/newsfragments/3281.removal.rst b/newsfragments/3281.removal.rst new file mode 100644 index 0000000000..38938f3481 --- /dev/null +++ b/newsfragments/3281.removal.rst @@ -0,0 +1 @@ +Remove ``Contract.encodeABI()`` in favor of ``Contract.encode_abi()`` to follow standard conventions. \ No newline at end of file diff --git a/tests/core/contracts/test_contract_method_abi_encoding.py b/tests/core/contracts/test_contract_method_abi_encoding.py index 470ccc9ecc..27b76c2a60 100644 --- a/tests/core/contracts/test_contract_method_abi_encoding.py +++ b/tests/core/contracts/test_contract_method_abi_encoding.py @@ -64,7 +64,7 @@ ) def test_contract_abi_encoding(w3, abi, arguments, data, expected): contract = w3.eth.contract(abi=abi) - actual = contract.encodeABI("a", arguments, data=data) + actual = contract.encode_abi("a", arguments, data=data) assert actual == expected @@ -116,7 +116,7 @@ def test_contract_abi_encoding_non_strict( w3_non_strict_abi, abi, arguments, data, expected ): contract = w3_non_strict_abi.eth.contract(abi=abi) - actual = contract.encodeABI("a", arguments, data=data) + actual = contract.encode_abi("a", arguments, data=data) assert actual == expected @@ -128,7 +128,7 @@ def test_contract_abi_encoding_kwargs(w3): "0x6f8d2fa18448afbfe4f82143c384484ad09a0271f3a3c0eb9f629e703f883125", ], } - actual = contract.encodeABI("byte_array", kwargs=kwargs) + actual = contract.encode_abi("byte_array", kwargs=kwargs) assert ( actual == "0xf166d6f8000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025595c210956e7721f9b692e702708556aa9aabb14ea163e96afa56ffbe9fa8096f8d2fa18448afbfe4f82143c384484ad09a0271f3a3c0eb9f629e703f883125" # noqa: E501 @@ -146,7 +146,7 @@ def test_contract_abi_encoding_kwargs(w3): def test_contract_abi_encoding_strict_with_error(w3, arguments): contract = w3.eth.contract(abi=ABI_C) with pytest.raises(Web3ValidationError): - contract.encodeABI("a", arguments, data=None) + contract.encode_abi("a", arguments, data=None) @pytest.mark.parametrize( @@ -195,5 +195,5 @@ def test_contract_abi_encoding_strict_with_error(w3, arguments): ) def test_contract_abi_encoding_strict(w3, abi, arguments, data, expected): contract = w3.eth.contract(abi=abi) - actual = contract.encodeABI("a", arguments, data=data) + actual = contract.encode_abi("a", arguments, data=data) assert actual == expected diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index 3651a5ff6f..613c82500b 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -1336,7 +1336,7 @@ async def test_eth_call_revert_custom_error_with_msg( async_revert_contract: "Contract", async_unlocked_account: ChecksumAddress, ) -> None: - data = async_revert_contract.encodeABI( + data = async_revert_contract.encode_abi( fn_name="UnauthorizedWithMessage", args=["You are not authorized"] ) txn_params = async_revert_contract._prepare_transaction( @@ -1356,7 +1356,7 @@ async def test_eth_call_revert_custom_error_without_msg( async_revert_contract: "Contract", async_unlocked_account: ChecksumAddress, ) -> None: - data = async_revert_contract.encodeABI(fn_name="Unauthorized") + data = async_revert_contract.encode_abi(fn_name="Unauthorized") txn_params = async_revert_contract._prepare_transaction( fn_name="customErrorWithoutMessage", transaction={ @@ -3788,7 +3788,7 @@ def test_eth_call_custom_error_revert_with_msg( revert_contract: "Contract", unlocked_account: ChecksumAddress, ) -> None: - data = revert_contract.encodeABI( + data = revert_contract.encode_abi( fn_name="UnauthorizedWithMessage", args=["You are not authorized"] ) txn_params = revert_contract._prepare_transaction( @@ -3808,7 +3808,7 @@ def test_eth_call_custom_error_revert_without_msg( revert_contract: "Contract", unlocked_account: ChecksumAddress, ) -> None: - data = revert_contract.encodeABI(fn_name="Unauthorized") + data = revert_contract.encode_abi(fn_name="Unauthorized") txn_params = revert_contract._prepare_transaction( fn_name="customErrorWithoutMessage", transaction={ @@ -4097,7 +4097,7 @@ def test_eth_estimate_gas_custom_error_revert_with_msg( revert_contract: "Contract", unlocked_account: ChecksumAddress, ) -> None: - data = revert_contract.encodeABI( + data = revert_contract.encode_abi( fn_name="UnauthorizedWithMessage", args=["You are not authorized"] ) txn_params = revert_contract._prepare_transaction( @@ -4117,7 +4117,7 @@ def test_eth_estimate_gas_custom_error_revert_without_msg( revert_contract: "Contract", unlocked_account: ChecksumAddress, ) -> None: - data = revert_contract.encodeABI(fn_name="Unauthorized") + data = revert_contract.encode_abi(fn_name="Unauthorized") txn_params = revert_contract._prepare_transaction( fn_name="customErrorWithoutMessage", transaction={ diff --git a/web3/contract/base_contract.py b/web3/contract/base_contract.py index 75c5b5f5d1..b7f14dfce2 100644 --- a/web3/contract/base_contract.py +++ b/web3/contract/base_contract.py @@ -723,7 +723,7 @@ class BaseContract: # Public API # @combomethod - def encodeABI( + def encode_abi( cls, fn_name: str, args: Optional[Any] = None,