-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: err handling edge case * chore: refactor with partial * feat: patch provider * fix: session * feat: log internal errs
- Loading branch information
1 parent
7e39ca8
commit bb37ccc
Showing
9 changed files
with
126 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import asyncio as _asyncio | ||
from typing import Awaitable as _Awaitable | ||
from typing import Iterable as _Iterable | ||
|
||
from tqdm.asyncio import tqdm_asyncio as _tqdm_asyncio | ||
|
||
from dank_mids.helpers.w3 import setup_dank_w3, setup_dank_w3_from_sync | ||
|
||
|
||
async def await_all(futs: _Iterable[_Awaitable], verbose: bool = False) -> None: | ||
# NOTE: 'verbose' is mainly for debugging but feel free to have fun | ||
if not isinstance(verbose, bool): | ||
raise NotImplementedError(verbose) | ||
as_completed = _tqdm_asyncio.as_completed if verbose else _asyncio.as_completed | ||
for fut in as_completed(futs if isinstance(futs, list) else [*futs]): | ||
await fut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
from dank_mids.helpers._middleware.poa import geth_poa_middleware | ||
|
||
""" | ||
Everything in this module is in web3.py now, but dank_mids wants to support versions that predates them. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
|
||
from typing import Any, Callable | ||
|
||
from eth_utils.curried import (apply_formatter_if, apply_formatters_to_dict, | ||
apply_key_map, is_null) | ||
from eth_utils.toolz import complement, compose | ||
from hexbytes import HexBytes | ||
from web3 import Web3 | ||
from web3._utils.rpc_abi import RPC | ||
from web3.types import RPCEndpoint | ||
|
||
from dank_mids.helpers._middleware.formatting import \ | ||
async_construct_formatting_middleware | ||
from dank_mids.types import AsyncMiddleware | ||
|
||
is_not_null = complement(is_null) | ||
|
||
remap_geth_poa_fields = apply_key_map( | ||
{ | ||
"extraData": "proofOfAuthorityData", | ||
} | ||
) | ||
|
||
pythonic_geth_poa = apply_formatters_to_dict( | ||
{ | ||
"proofOfAuthorityData": HexBytes, | ||
} | ||
) | ||
|
||
geth_poa_cleanup = compose(pythonic_geth_poa, remap_geth_poa_fields) | ||
|
||
async def geth_poa_middleware(make_request: Callable[[RPCEndpoint, Any], Any], w3: Web3) -> AsyncMiddleware: | ||
middleware = await async_construct_formatting_middleware( | ||
result_formatters={ | ||
RPC.eth_getBlockByHash: apply_formatter_if(is_not_null, geth_poa_cleanup), | ||
RPC.eth_getBlockByNumber: apply_formatter_if(is_not_null, geth_poa_cleanup), | ||
}, | ||
) | ||
return await middleware(make_request, w3) # type: ignore [arg-type, return-value] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
from functools import lru_cache | ||
|
||
from requests import Session | ||
from requests.adapters import HTTPAdapter | ||
|
||
|
||
@lru_cache(maxsize=1) | ||
def get_session() -> Session: | ||
adapter = HTTPAdapter(pool_connections=100, pool_maxsize=100) | ||
session = Session() | ||
session.mount("http://", adapter) | ||
session.mount("https://", adapter) | ||
return session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
from typing import List | ||
|
||
from multicall.utils import get_async_w3 | ||
from web3 import Web3 | ||
from web3.providers.async_base import AsyncBaseProvider | ||
from web3.providers.base import BaseProvider | ||
|
||
from dank_mids.helpers._middleware import geth_poa_middleware | ||
from dank_mids.middleware import dank_middleware | ||
|
||
dank_w3s: List[Web3] = [] | ||
|
||
def setup_dank_w3(async_w3: Web3) -> Web3: | ||
""" Injects Dank Middleware into an async Web3 instance. """ | ||
assert async_w3.eth.is_async and isinstance(async_w3.provider, AsyncBaseProvider) | ||
# NOTE: We use this lookup to prevent errs where 2 project dependencies both depend on dank_mids and eth-brownie. | ||
if async_w3 not in dank_w3s: | ||
async_w3.middleware_onion.inject(dank_middleware, layer=0) | ||
async_w3.middleware_onion.add(geth_poa_middleware) | ||
dank_w3s.append(async_w3) | ||
return async_w3 | ||
|
||
def setup_dank_w3_from_sync(sync_w3: Web3) -> Web3: | ||
""" 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) | ||
return setup_dank_w3(get_async_w3(sync_w3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters