Skip to content

Commit

Permalink
More explicit use of abi.encode() rather than encode(), etc.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
fselmo committed Oct 6, 2022
1 parent cc4a236 commit 64a2711
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions tests/core/contracts/test_offchain_lookup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from eth_abi import (
decode,
abi,
)

from web3._utils.module_testing.module_testing_utils import (
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions web3/_utils/async_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)

from eth_abi import (
encode,
abi,
)
from eth_typing import (
URI,
Expand Down Expand Up @@ -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"]),
Expand Down
6 changes: 4 additions & 2 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)

from eth_abi import (
decode,
abi,
)
from eth_typing import (
HexStr,
Expand Down Expand Up @@ -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)
)
Expand Down
4 changes: 2 additions & 2 deletions web3/providers/eth_tester/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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}")
Expand Down
4 changes: 2 additions & 2 deletions web3/utils/async_exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
)

from eth_abi import (
encode,
abi,
)
from eth_typing import (
URI,
Expand Down Expand Up @@ -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"]),
Expand Down
4 changes: 2 additions & 2 deletions web3/utils/exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
)

from eth_abi import (
encode,
abi,
)
from eth_typing import (
URI,
Expand Down Expand Up @@ -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"]),
Expand Down

0 comments on commit 64a2711

Please sign in to comment.