Skip to content

Commit

Permalink
Rename Contract.encodeABI -> Contract.encode_abi (#3281)
Browse files Browse the repository at this point in the history
* Rename `Contract.encodeABI` -> `Contract.encode_abi`

* Remove deprecated `Contract.encodeABI`

* Docs and newsfragment for #3281

* Fix docs
  • Loading branch information
reedsa authored Mar 13, 2024
1 parent a19d15c commit e29ce51
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ API
- :attr:`Contract.events <web3.contract.Contract.events>`
- :attr:`Contract.fallback <web3.contract.Contract.fallback.call>`
- :meth:`Contract.constructor() <web3.contract.Contract.constructor>`
- :meth:`Contract.encodeABI() <web3.contract.Contract.encodeABI>`
- :meth:`Contract.encode_abi() <web3.contract.Contract.encode_abi>`
- :attr:`web3.contract.ContractFunction <web3.contract.ContractFunction>`
- :attr:`web3.contract.ContractEvents <web3.contract.ContractEvents>`

Expand Down
1 change: 1 addition & 0 deletions docs/v7_migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()``.
14 changes: 7 additions & 7 deletions docs/web3.contract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ens/async_ens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ens/ens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions newsfragments/3281.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove ``Contract.encodeABI()`` in favor of ``Contract.encode_abi()`` to follow standard conventions.
10 changes: 5 additions & 5 deletions tests/core/contracts/test_contract_method_abi_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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={
Expand Down Expand Up @@ -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(
Expand All @@ -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={
Expand Down Expand Up @@ -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(
Expand All @@ -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={
Expand Down
2 changes: 1 addition & 1 deletion web3/contract/base_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ class BaseContract:
# Public API
#
@combomethod
def encodeABI(
def encode_abi(
cls,
fn_name: str,
args: Optional[Any] = None,
Expand Down

0 comments on commit e29ce51

Please sign in to comment.