From a90931122a0dd0b000b70fdf3ef2b424da201ffe Mon Sep 17 00:00:00 2001 From: Richymaestro Date: Thu, 7 Aug 2025 16:09:44 +0100 Subject: [PATCH 1/4] add support for web3py version >7 --- multicall/utils.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/multicall/utils.py b/multicall/utils.py index e790593..b4f139f 100644 --- a/multicall/utils.py +++ b/multicall/utils.py @@ -6,6 +6,7 @@ import eth_retry from aiohttp import ClientTimeout +import web3 from web3 import AsyncHTTPProvider, Web3 from web3.eth import AsyncEth from web3.providers.async_base import AsyncBaseProvider @@ -73,12 +74,15 @@ def get_async_w3(w3: Web3) -> Web3: else: provider = AsyncHTTPProvider(endpoint, request_kwargs) - # In older web3 versions, AsyncHTTPProvider objects come - # with incompatible synchronous middlewares by default. - if AsyncWeb3: # type: ignore [truthy-function] - async_w3 = AsyncWeb3(provider=provider, middlewares=[]) # type: ignore [call-arg] - else: - async_w3 = Web3(provider=provider, middlewares=[]) + # Older versions of web3.py (v6 and below) use 'middlewares' instead of 'middleware'. + major_version = int(web3.__version__.split('.')[0]) + key = "middleware" if major_version >= 7 else "middlewares" + kwargs = {key: []} + + w3_class = AsyncWeb3 if AsyncWeb3 else Web3 # type: ignore [truthy-function] + async_w3 = w3_class(provider=provider, **kwargs) # type: ignore [call-arg] + + if not AsyncWeb3: # type: ignore [truthy-function] async_w3.eth = AsyncEth(async_w3) async_w3s[w3] = async_w3 # type: ignore [assignment] From b56c5c62d5baa5c9414e2e98ca9c088eb970100a Mon Sep 17 00:00:00 2001 From: Richymaestro Date: Thu, 7 Aug 2025 16:29:56 +0100 Subject: [PATCH 2/4] fix type --- multicall/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multicall/utils.py b/multicall/utils.py index b4f139f..95b915f 100644 --- a/multicall/utils.py +++ b/multicall/utils.py @@ -77,7 +77,7 @@ def get_async_w3(w3: Web3) -> Web3: # Older versions of web3.py (v6 and below) use 'middlewares' instead of 'middleware'. major_version = int(web3.__version__.split('.')[0]) key = "middleware" if major_version >= 7 else "middlewares" - kwargs = {key: []} + kwargs = {key: []} # type: ignore [dict-item] w3_class = AsyncWeb3 if AsyncWeb3 else Web3 # type: ignore [truthy-function] async_w3 = w3_class(provider=provider, **kwargs) # type: ignore [call-arg] From 28ebbbe394aadfd29fa4a07a33e4ca9e40a82950 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:49:22 -0400 Subject: [PATCH 3/4] Update utils.py --- multicall/utils.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/multicall/utils.py b/multicall/utils.py index 95b915f..1020b52 100644 --- a/multicall/utils.py +++ b/multicall/utils.py @@ -74,15 +74,19 @@ def get_async_w3(w3: Web3) -> Web3: else: provider = AsyncHTTPProvider(endpoint, request_kwargs) + kwargs = {provider: provider} + # Older versions of web3.py (v6 and below) use 'middlewares' instead of 'middleware'. major_version = int(web3.__version__.split('.')[0]) - key = "middleware" if major_version >= 7 else "middlewares" - kwargs = {key: []} # type: ignore [dict-item] + if major_version >= 7: + kwargs["middleware"] = [] + else: + kwargs["middlewares"] = [] - w3_class = AsyncWeb3 if AsyncWeb3 else Web3 # type: ignore [truthy-function] - async_w3 = w3_class(provider=provider, **kwargs) # type: ignore [call-arg] + w3_class = Web3 if AsyncWeb3 is None else AsyncWeb3 + async_w3 = w3_class(**kwargs) - if not AsyncWeb3: # type: ignore [truthy-function] + if w3_class is Web3: async_w3.eth = AsyncEth(async_w3) async_w3s[w3] = async_w3 # type: ignore [assignment] From 7b986d00102aeafa347b456974f07770215fa18a Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:54:08 -0400 Subject: [PATCH 4/4] Update utils.py --- multicall/utils.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/multicall/utils.py b/multicall/utils.py index 1020b52..7e7edc1 100644 --- a/multicall/utils.py +++ b/multicall/utils.py @@ -74,19 +74,17 @@ def get_async_w3(w3: Web3) -> Web3: else: provider = AsyncHTTPProvider(endpoint, request_kwargs) - kwargs = {provider: provider} - - # Older versions of web3.py (v6 and below) use 'middlewares' instead of 'middleware'. - major_version = int(web3.__version__.split('.')[0]) - if major_version >= 7: - kwargs["middleware"] = [] + # In older web3 versions, AsyncHTTPProvider objects come + # with incompatible synchronous middlewares by default. + if AsyncWeb3 is not None: + # Older versions of web3.py (v6 and below) use 'middlewares' instead of 'middleware'. + major_version = int(web3.__version__.split('.')[0]) + if major_version >= 7: + async_w3 = AsyncWeb3(provider, middleware=[]) + else: + async_w3 = AsyncWeb3(provider, middlewares=[]) else: - kwargs["middlewares"] = [] - - w3_class = Web3 if AsyncWeb3 is None else AsyncWeb3 - async_w3 = w3_class(**kwargs) - - if w3_class is Web3: + async_w3 = Web3(provider=provider, middlewares=[]) async_w3.eth = AsyncEth(async_w3) async_w3s[w3] = async_w3 # type: ignore [assignment]