Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Enable caching to reduce RPC calls #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 14 uniswap/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
from typing import Set, cast
from web3.types import ( # noqa: F401
RPCEndpoint,
)

# look at web3/middleware/cache.py for reference
# RPC methods that will be cached inside _get_eth_simple_cache_middleware
SIMPLE_CACHE_RPC_WHITELIST = cast(
Set[RPCEndpoint],
{
"eth_chainId",
},
)

ETH_ADDRESS = "0x0000000000000000000000000000000000000000"
WETH9_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"

Expand Down
7 changes: 6 additions & 1 deletion 7 uniswap/uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .token import ERC20Token
from .exceptions import InvalidToken, InsufficientBalance
from .util import (
_get_eth_simple_cache_middleware,
_str_to_addr,
_addr_to_str,
_validate_address,
Expand Down Expand Up @@ -78,6 +79,7 @@ def __init__(
# use_eip1559: bool = True,
factory_contract_addr: str = None,
router_contract_addr: str = None,
enable_caching: bool = False,
ErikBjare marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
"""
:param address: The public address of the ETH wallet to use.
Expand All @@ -88,6 +90,7 @@ def __init__(
:param default_slippage: Default slippage for a trade, as a float (0.01 is 1%). WARNING: slippage is untested.
:param factory_contract_addr: Can be optionally set to override the address of the factory contract.
:param router_contract_addr: Can be optionally set to override the address of the router contract (v2 only).
:param enable_caching: Optionally enables middleware caching RPC method calls.
"""
self.address = _str_to_addr(
address or "0x0000000000000000000000000000000000000000"
Expand Down Expand Up @@ -115,7 +118,9 @@ def __init__(
provider = os.environ["PROVIDER"]
self.w3 = Web3(Web3.HTTPProvider(provider, request_kwargs={"timeout": 60}))

# Cache netid to avoid extra RPC calls
if enable_caching:
self.w3.middleware_onion.inject(_get_eth_simple_cache_middleware(), layer=0)

self.netid = int(self.w3.net.version)
if self.netid in _netid_to_name:
self.netname = _netid_to_name[self.netid]
Expand Down
15 changes: 13 additions & 2 deletions 15 uniswap/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@
import json
import math
import functools
from typing import Any, Generator, Sequence, Union, List, Tuple
import lru

from typing import Any, Generator, Sequence, Union, List, Tuple, Type, Dict, cast

from web3 import Web3
from web3.exceptions import NameNotFound
from web3.contract import Contract
from web3.middleware.cache import construct_simple_cache_middleware
from web3.types import Middleware

from .constants import MIN_TICK, MAX_TICK, _tick_spacing
from .constants import MIN_TICK, MAX_TICK, _tick_spacing, SIMPLE_CACHE_RPC_WHITELIST
from .types import AddressLike, Address


def _get_eth_simple_cache_middleware() -> Middleware:
return construct_simple_cache_middleware(
cache_class=cast(Type[Dict[Any, Any]], functools.partial(lru.LRU, 256)),
rpc_whitelist=SIMPLE_CACHE_RPC_WHITELIST,
)


def _str_to_addr(s: Union[AddressLike, str]) -> Address:
"""Idempotent"""
if isinstance(s, str):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.