From 8a7c0c6d0eb983af690c7f58afdcaf0ba38b6666 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 3 May 2024 19:13:08 -0400 Subject: [PATCH] fix: inject poa and attrdict middlewares to sync w3 too (#198) --- dank_mids/controller.py | 7 ++----- dank_mids/helpers/_helpers.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/dank_mids/controller.py b/dank_mids/controller.py index 06e99441..35951432 100644 --- a/dank_mids/controller.py +++ b/dank_mids/controller.py @@ -3,7 +3,6 @@ from collections import defaultdict from contextlib import suppress from functools import lru_cache -from importlib.metadata import version from typing import Any, DefaultDict, List, Literal, Optional, Set, Union import eth_retry @@ -53,15 +52,13 @@ class DankMiddlewareController: def __init__(self, w3: Web3) -> None: logger.info('Dank Middleware initializing... Strap on your rocket boots...') self.w3: Web3 = w3 - w3_version = version("web3") - w3_version_major = int(w3_version.split(".")[0]) - if w3_version_major >= 6: + if _helpers.w3_version_major >= 6: from web3.middleware import async_attrdict_middleware try: self.w3.middleware_onion.add(async_attrdict_middleware) except ValueError as e: if str(e) != "You can't add the same un-named instance twice": - raise e + raise # NOTE: the web3 instance already has the middleware self.sync_w3 = _sync_w3_from_async(w3) diff --git a/dank_mids/helpers/_helpers.py b/dank_mids/helpers/_helpers.py index f68e366c..21fce370 100644 --- a/dank_mids/helpers/_helpers.py +++ b/dank_mids/helpers/_helpers.py @@ -1,6 +1,7 @@ import asyncio from functools import wraps +from importlib.metadata import version from typing import (TYPE_CHECKING, Any, Awaitable, Callable, Coroutine, Iterable, List, Literal, Optional, TypeVar) @@ -26,10 +27,13 @@ from dank_mids._requests import _Request dank_w3s: List[Web3] = [] +sync_w3s: List[Web3] = [] T = TypeVar("T") P = ParamSpec("P") +w3_version_major = int(version("web3").split(".")[0]) + class DankEth(AsyncEth): @alru_cache(ttl=0) async def get_block_number(self) -> BlockNumber: # type: ignore [override] @@ -56,6 +60,22 @@ def setup_dank_w3(async_w3: Web3) -> DankWeb3: def setup_dank_w3_from_sync(sync_w3: Web3) -> DankWeb3: """ Creates a new async Web3 instance from `w3.provider.endpoint_uri` and injects it with Dank Middleware. """ assert not sync_w3.eth.is_async and isinstance(sync_w3.provider, BaseProvider) + if sync_w3 not in sync_w3s: + if w3_version_major >= 6: + from web3.middleware import attrdict_middleware + try: + sync_w3.middleware_onion.add(attrdict_middleware) + except ValueError as e: + if str(e) != "You can't add the same un-named instance twice": + raise e + if w3_version_major >= 7: + # we need to make sure our sync w3 instance is compatible with poa chains + from web3.middleware.proof_of_authority import ExtraDataToPOAMiddleware + sync_w3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0) + elif w3_version_major >= 6: + from web3.middleware import geth_poa_middleware + sync_w3.middleware_onion.inject(geth_poa_middleware, layer=0) + sync_w3s.append(sync_w3) return setup_dank_w3(get_async_w3(sync_w3)) async def await_all(futs: Iterable[Awaitable]) -> None: