Skip to content

Commit

Permalink
fix: inject poa and attrdict middlewares to sync w3 too (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored May 3, 2024
1 parent 85a6942 commit 8a7c0c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 2 additions & 5 deletions dank_mids/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions dank_mids/helpers/_helpers.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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]
Expand All @@ -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:
Expand Down

0 comments on commit 8a7c0c6

Please sign in to comment.