From 64a27111622fa23ba73b1bc10b9d40cf4b4ed21a Mon Sep 17 00:00:00 2001 From: Felipe Selmo Date: Thu, 6 Oct 2022 09:55:54 -0600 Subject: [PATCH] More explicit use of abi.encode() rather than encode(), etc. - With commonly used words like ``encode`` and ``decode``, it is better to import the library so a reader knows what exactly is encoding / decoding. Here, it is better to import ``abi`` and use ``abi.encode()`` / ``abi.decode()`` for better context around the logic being read. --- tests/core/contracts/test_offchain_lookup.py | 8 ++++---- web3/_utils/async_transactions.py | 4 ++-- web3/_utils/method_formatters.py | 6 ++++-- web3/providers/eth_tester/main.py | 4 ++-- web3/utils/async_exception_handling.py | 4 ++-- web3/utils/exception_handling.py | 4 ++-- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/tests/core/contracts/test_offchain_lookup.py b/tests/core/contracts/test_offchain_lookup.py index 1a9a8542b2..9f26262d00 100644 --- a/tests/core/contracts/test_offchain_lookup.py +++ b/tests/core/contracts/test_offchain_lookup.py @@ -1,7 +1,7 @@ import pytest from eth_abi import ( - decode, + abi, ) from web3._utils.module_testing.module_testing_utils import ( @@ -74,7 +74,7 @@ def test_offchain_lookup_functionality( response = offchain_lookup_contract.caller.testOffchainLookup( OFFCHAIN_LOOKUP_TEST_DATA ) - assert decode(["string"], response)[0] == "web3py" + assert abi.decode(["string"], response)[0] == "web3py" def test_eth_call_offchain_lookup_raises_when_ccip_read_is_disabled( @@ -123,7 +123,7 @@ def test_offchain_lookup_call_flag_overrides_provider_flag( response = offchain_lookup_contract.functions.testOffchainLookup( OFFCHAIN_LOOKUP_TEST_DATA ).call(ccip_read_enabled=True) - assert decode(["string"], response)[0] == "web3py" + assert abi.decode(["string"], response)[0] == "web3py" w3.provider.global_ccip_read_enabled = True @@ -176,7 +176,7 @@ def test_eth_call_offchain_lookup_tries_next_url_for_non_4xx_error_status_and_te response = offchain_lookup_contract.caller.testOffchainLookup( OFFCHAIN_LOOKUP_TEST_DATA ) - assert decode(["string"], response)[0] == "web3py" + assert abi.decode(["string"], response)[0] == "web3py" @pytest.mark.parametrize("status_code_4xx_error", [400, 410, 450, 499]) diff --git a/web3/_utils/async_transactions.py b/web3/_utils/async_transactions.py index a9386e738f..a004baa305 100644 --- a/web3/_utils/async_transactions.py +++ b/web3/_utils/async_transactions.py @@ -8,7 +8,7 @@ ) from eth_abi import ( - encode, + abi, ) from eth_typing import ( URI, @@ -199,7 +199,7 @@ async def async_handle_offchain_lookup( # 4-byte callback function selector to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]), # encode the `data` from the result and the `extraData` as bytes - encode( + abi.encode( ["bytes", "bytes"], [ to_bytes_if_hex(result["data"]), diff --git a/web3/_utils/method_formatters.py b/web3/_utils/method_formatters.py index cb653ffd43..251465281c 100644 --- a/web3/_utils/method_formatters.py +++ b/web3/_utils/method_formatters.py @@ -13,7 +13,7 @@ ) from eth_abi import ( - decode, + abi, ) from eth_typing import ( HexStr, @@ -623,7 +623,9 @@ def raise_solidity_error_on_revert(response: RPCResponse) -> RPCResponse: # OffchainLookup(address,string[],bytes,bytes4,bytes) if data[:10] == "0x556f1830": parsed_data_as_bytes = to_bytes(hexstr=data[10:]) - abi_decoded_data = decode(OFFCHAIN_LOOKUP_FIELDS.values(), parsed_data_as_bytes) + abi_decoded_data = abi.decode( + OFFCHAIN_LOOKUP_FIELDS.values(), parsed_data_as_bytes + ) offchain_lookup_payload = dict( zip(OFFCHAIN_LOOKUP_FIELDS.keys(), abi_decoded_data) ) diff --git a/web3/providers/eth_tester/main.py b/web3/providers/eth_tester/main.py index e298f3fe02..a14311a5fc 100644 --- a/web3/providers/eth_tester/main.py +++ b/web3/providers/eth_tester/main.py @@ -76,7 +76,7 @@ async def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse: ) except TransactionFailed as e: try: - reason = abi.decode("(string)", e.args[0].args[0][4:])[0] + reason = abi.decode(["(string)"], e.args[0].args[0][4:])[0] except (InsufficientDataBytes, AttributeError): reason = e.args[0] raise TransactionFailed(f"execution reverted: {reason}") @@ -148,7 +148,7 @@ def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse: ) except TransactionFailed as e: try: - reason = abi.decode("(string)", e.args[0].args[0][4:])[0] + reason = abi.decode(["(string)"], e.args[0].args[0][4:])[0] except (InsufficientDataBytes, AttributeError): reason = e.args[0] raise TransactionFailed(f"execution reverted: {reason}") diff --git a/web3/utils/async_exception_handling.py b/web3/utils/async_exception_handling.py index 3f2e33eb77..8a9ab72827 100644 --- a/web3/utils/async_exception_handling.py +++ b/web3/utils/async_exception_handling.py @@ -4,7 +4,7 @@ ) from eth_abi import ( - encode, + abi, ) from eth_typing import ( URI, @@ -81,7 +81,7 @@ async def async_handle_offchain_lookup( # 4-byte callback function selector to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]), # encode the `data` from the result and the `extraData` as bytes - encode( + abi.encode( ["bytes", "bytes"], [ to_bytes_if_hex(result["data"]), diff --git a/web3/utils/exception_handling.py b/web3/utils/exception_handling.py index ac1d6c0ec0..eb53218392 100644 --- a/web3/utils/exception_handling.py +++ b/web3/utils/exception_handling.py @@ -4,7 +4,7 @@ ) from eth_abi import ( - encode, + abi, ) from eth_typing import ( URI, @@ -83,7 +83,7 @@ def handle_offchain_lookup( # 4-byte callback function selector to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]), # encode the `data` from the result and the `extraData` as bytes - encode( + abi.encode( ["bytes", "bytes"], [ to_bytes_if_hex(result["data"]),