Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0b76cde

Browse files
committedOct 7, 2022
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.
1 parent 60d3deb commit 0b76cde

File tree

9 files changed

+30
-27
lines changed

9 files changed

+30
-27
lines changed
 

‎newsfragments/2668.internal.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clean up remaining uses of deprecated ``eth_abi`` methods.

‎tests/core/contracts/test_offchain_lookup.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from eth_abi import (
4-
decode,
4+
abi,
55
)
66

77
from web3._utils.module_testing.module_testing_utils import (
@@ -74,7 +74,7 @@ def test_offchain_lookup_functionality(
7474
response = offchain_lookup_contract.caller.testOffchainLookup(
7575
OFFCHAIN_LOOKUP_TEST_DATA
7676
)
77-
assert decode(["string"], response)[0] == "web3py"
77+
assert abi.decode(["string"], response)[0] == "web3py"
7878

7979

8080
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(
123123
response = offchain_lookup_contract.functions.testOffchainLookup(
124124
OFFCHAIN_LOOKUP_TEST_DATA
125125
).call(ccip_read_enabled=True)
126-
assert decode(["string"], response)[0] == "web3py"
126+
assert abi.decode(["string"], response)[0] == "web3py"
127127

128128
w3.provider.global_ccip_read_enabled = True
129129

@@ -176,7 +176,7 @@ def test_eth_call_offchain_lookup_tries_next_url_for_non_4xx_error_status_and_te
176176
response = offchain_lookup_contract.caller.testOffchainLookup(
177177
OFFCHAIN_LOOKUP_TEST_DATA
178178
)
179-
assert decode(["string"], response)[0] == "web3py"
179+
assert abi.decode(["string"], response)[0] == "web3py"
180180

181181

182182
@pytest.mark.parametrize("status_code_4xx_error", [400, 410, 450, 499])

‎web3/_utils/async_transactions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
)
99

1010
from eth_abi import (
11-
encode,
11+
abi,
1212
)
1313
from eth_typing import (
1414
URI,
@@ -199,7 +199,7 @@ async def async_handle_offchain_lookup(
199199
# 4-byte callback function selector
200200
to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]),
201201
# encode the `data` from the result and the `extraData` as bytes
202-
encode(
202+
abi.encode(
203203
["bytes", "bytes"],
204204
[
205205
to_bytes_if_hex(result["data"]),

‎web3/_utils/events.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def construct_event_topic_set(
127127
[
128128
None
129129
if option is None
130-
else encode_hex(abi_codec.encode_single(arg["type"], option))
130+
else encode_hex(abi_codec.encode([arg["type"]], [option]))
131131
for option in arg_options
132132
]
133133
for arg, arg_options in zipped_abi_and_args
@@ -169,7 +169,7 @@ def construct_event_data_set(
169169
[
170170
None
171171
if option is None
172-
else encode_hex(abi_codec.encode_single(arg["type"], option))
172+
else encode_hex(abi_codec.encode([arg["type"]], [option]))
173173
for option in arg_options
174174
]
175175
for arg, arg_options in zipped_abi_and_args
@@ -252,7 +252,7 @@ def get_event_data(
252252
)
253253

254254
decoded_topic_data = [
255-
abi_codec.decode_single(topic_type, topic_data)
255+
abi_codec.decode([topic_type], topic_data)[0]
256256
for topic_type, topic_data in zip(log_topic_types, log_topics)
257257
]
258258
normalized_topic_data = map_abi_data(
@@ -517,7 +517,7 @@ def _encode(self, value: Any) -> HexStr:
517517
if is_dynamic_sized_type(self.arg_type):
518518
return to_hex(keccak(encode_single_packed(self.arg_type, value)))
519519
else:
520-
return to_hex(self.abi_codec.encode_single(self.arg_type, value))
520+
return to_hex(self.abi_codec.encode([self.arg_type], [value]))
521521

522522

523523
class EventLogErrorFlags(Enum):

‎web3/_utils/method_formatters.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
)
1414

1515
from eth_abi import (
16-
decode,
16+
abi,
1717
)
1818
from eth_typing import (
1919
HexStr,
@@ -623,7 +623,9 @@ def raise_solidity_error_on_revert(response: RPCResponse) -> RPCResponse:
623623
# OffchainLookup(address,string[],bytes,bytes4,bytes)
624624
if data[:10] == "0x556f1830":
625625
parsed_data_as_bytes = to_bytes(hexstr=data[10:])
626-
abi_decoded_data = decode(OFFCHAIN_LOOKUP_FIELDS.values(), parsed_data_as_bytes)
626+
abi_decoded_data = abi.decode(
627+
OFFCHAIN_LOOKUP_FIELDS.values(), parsed_data_as_bytes
628+
)
627629
offchain_lookup_payload = dict(
628630
zip(OFFCHAIN_LOOKUP_FIELDS.keys(), abi_decoded_data)
629631
)

‎web3/_utils/module_testing/eth_module.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ async def test_eth_call(self, async_w3: "Web3", math_contract: "Contract") -> No
747747
)
748748
call_result = await async_w3.eth.call(txn_params) # type: ignore
749749
assert is_string(call_result)
750-
result = async_w3.codec.decode_single("uint256", call_result)
750+
(result,) = async_w3.codec.decode(["uint256"], call_result)
751751
assert result == 18
752752

753753
@pytest.mark.asyncio
@@ -760,7 +760,7 @@ async def test_eth_call_with_override(
760760
transaction={"from": coinbase, "to": revert_contract.address},
761761
)
762762
call_result = await async_w3.eth.call(txn_params) # type: ignore
763-
result = async_w3.codec.decode_single("bool", call_result)
763+
(result,) = async_w3.codec.decode(["bool"], call_result)
764764
assert result is True
765765

766766
# override runtime bytecode: `normalFunction` returns `false`
@@ -770,7 +770,7 @@ async def test_eth_call_with_override(
770770
call_result = await async_w3.eth.call( # type: ignore
771771
txn_params, "latest", {revert_contract.address: {"code": override_code}}
772772
)
773-
result = async_w3.codec.decode_single("bool", call_result)
773+
(result,) = async_w3.codec.decode(["bool"], call_result)
774774
assert result is False
775775

776776
@pytest.mark.asyncio
@@ -785,7 +785,7 @@ async def test_eth_call_with_0_result(
785785
)
786786
call_result = await async_w3.eth.call(txn_params) # type: ignore
787787
assert is_string(call_result)
788-
result = async_w3.codec.decode_single("uint256", call_result)
788+
(result,) = async_w3.codec.decode(["uint256"], call_result)
789789
assert result == 0
790790

791791
@pytest.mark.asyncio
@@ -2569,7 +2569,7 @@ def test_eth_call(self, w3: "Web3", math_contract: "Contract") -> None:
25692569
)
25702570
call_result = w3.eth.call(txn_params)
25712571
assert is_string(call_result)
2572-
result = w3.codec.decode_single("uint256", call_result)
2572+
(result,) = w3.codec.decode(["uint256"], call_result)
25732573
assert result == 18
25742574

25752575
def test_eth_call_with_override(
@@ -2581,7 +2581,7 @@ def test_eth_call_with_override(
25812581
transaction={"from": coinbase, "to": revert_contract.address},
25822582
)
25832583
call_result = w3.eth.call(txn_params)
2584-
result = w3.codec.decode_single("bool", call_result)
2584+
(result,) = w3.codec.decode(["bool"], call_result)
25852585
assert result is True
25862586

25872587
# override runtime bytecode: `normalFunction` returns `false`
@@ -2591,7 +2591,7 @@ def test_eth_call_with_override(
25912591
call_result = w3.eth.call(
25922592
txn_params, "latest", {revert_contract.address: {"code": override_code}}
25932593
)
2594-
result = w3.codec.decode_single("bool", call_result)
2594+
(result,) = w3.codec.decode(["bool"], call_result)
25952595
assert result is False
25962596

25972597
def test_eth_call_with_0_result(
@@ -2605,7 +2605,7 @@ def test_eth_call_with_0_result(
26052605
)
26062606
call_result = w3.eth.call(txn_params)
26072607
assert is_string(call_result)
2608-
result = w3.codec.decode_single("uint256", call_result)
2608+
(result,) = w3.codec.decode(["uint256"], call_result)
26092609
assert result == 0
26102610

26112611
def test_eth_call_revert_with_msg(

‎web3/providers/eth_tester/defaults.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
Type,
1414
)
1515

16-
from eth_abi.abi import (
17-
decode,
16+
from eth_abi import (
17+
abi,
1818
)
1919
from eth_tester.exceptions import (
2020
BlockNotFound,
@@ -89,7 +89,7 @@ def call_eth_tester(
8989
data_payload = parsed_data_as_bytes[
9090
4:
9191
] # everything but the function selector
92-
abi_decoded_data = decode(OFFCHAIN_LOOKUP_FIELDS.values(), data_payload)
92+
abi_decoded_data = abi.decode(OFFCHAIN_LOOKUP_FIELDS.values(), data_payload)
9393
offchain_lookup_payload = dict(
9494
zip(OFFCHAIN_LOOKUP_FIELDS.keys(), abi_decoded_data)
9595
)

‎web3/utils/async_exception_handling.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
)
55

66
from eth_abi import (
7-
encode,
7+
abi,
88
)
99
from eth_typing import (
1010
URI,
@@ -81,7 +81,7 @@ async def async_handle_offchain_lookup(
8181
# 4-byte callback function selector
8282
to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]),
8383
# encode the `data` from the result and the `extraData` as bytes
84-
encode(
84+
abi.encode(
8585
["bytes", "bytes"],
8686
[
8787
to_bytes_if_hex(result["data"]),

‎web3/utils/exception_handling.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
)
55

66
from eth_abi import (
7-
encode,
7+
abi,
88
)
99
from eth_typing import (
1010
URI,
@@ -83,7 +83,7 @@ def handle_offchain_lookup(
8383
# 4-byte callback function selector
8484
to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]),
8585
# encode the `data` from the result and the `extraData` as bytes
86-
encode(
86+
abi.encode(
8787
["bytes", "bytes"],
8888
[
8989
to_bytes_if_hex(result["data"]),

0 commit comments

Comments
 (0)
Please sign in to comment.