Skip to content

Commit f80ab03

Browse files
committed
Add ens name-to-address support for eth_subscribe method
- Add special case for eth_subscribe logs arg for ENS name-to-address middleware
1 parent d699e35 commit f80ab03

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

web3/middleware/names.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def name_to_address_middleware(w3: "Web3") -> Middleware:
5656
# -- async -- #
5757

5858

59+
def _is_logs_subscription_with_optional_args(method: RPCEndpoint, params: Any) -> bool:
60+
return method == "eth_subscribe" and len(params) == 2 and params[0] == "logs"
61+
62+
5963
async def async_format_all_ens_names_to_address(
6064
async_web3: "AsyncWeb3",
6165
abi_types_for_method: Sequence[Any],
@@ -110,13 +114,35 @@ async def async_name_to_address_middleware(
110114
async_w3: "AsyncWeb3",
111115
) -> AsyncMiddlewareCoroutine:
112116
async def middleware(method: RPCEndpoint, params: Any) -> Any:
113-
abi_types_for_method = RPC_ABIS.get(method, None)
117+
abi_types_for_method = _get_abi_types_for_method(method, params)
114118
if abi_types_for_method is not None:
115-
params = await async_apply_ens_to_address_conversion(
116-
async_w3,
117-
params,
118-
abi_types_for_method,
119-
)
119+
if _is_logs_subscription_with_optional_args(method, params):
120+
# eth_subscribe optional logs params are unique.
121+
# Handle them separately here.
122+
(formatted_dict,) = await async_apply_ens_to_address_conversion(
123+
async_w3,
124+
(params[1],),
125+
abi_types_for_method,
126+
)
127+
params = (params[0], formatted_dict)
128+
else:
129+
params = await async_apply_ens_to_address_conversion(
130+
async_w3,
131+
params,
132+
abi_types_for_method,
133+
)
120134
return await make_request(method, params)
121135

122136
return middleware
137+
138+
139+
def _get_abi_types_for_method(
140+
method: RPCEndpoint,
141+
params: Any,
142+
) -> Union[Sequence[str], Dict[str, str]]:
143+
if _is_logs_subscription_with_optional_args(method, params):
144+
return {
145+
"address": "address",
146+
"topics": "bytes32[]",
147+
}
148+
return RPC_ABIS.get(method, None)

0 commit comments

Comments
 (0)