Skip to content

Commit

Permalink
fix: AttrDict in web3.py>=6 (#142)
Browse files Browse the repository at this point in the history
* fix: AttrDict

* fix: middleware already present
  • Loading branch information
BobTheBuidler authored Apr 10, 2024
1 parent 42ca1db commit a1ecbd7
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dank_mids/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from collections import defaultdict
from functools import lru_cache
from importlib.metadata import version
from typing import Any, DefaultDict, List, Literal, Optional

import eth_retry
Expand Down Expand Up @@ -50,6 +51,16 @@ 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:
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
# NOTE: the web3 instance already has the middleware
self.sync_w3 = _sync_w3_from_async(w3)

self.chain_id = self.sync_w3.eth.chain_id
Expand All @@ -59,6 +70,7 @@ def __init__(self, w3: Web3) -> None:
self.state_override_not_supported: bool = ENVS.GANACHE_FORK or self.chain_id == 100 # Gnosis Chain does not support state override.

self.endpoint = self.w3.provider.endpoint_uri

self._sort_calls = "tenderly" in self.endpoint or "chainstack" in self.endpoint
if "tenderly" in self.endpoint and ENVS.MAX_JSONRPC_BATCH_SIZE > 10:
logger.info("max jsonrpc batch size for tenderly is 10, overriding existing max")
Expand Down

0 comments on commit a1ecbd7

Please sign in to comment.