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

Commit 0b42700

Browse filesBrowse files
feat: add option enable_caching that adds cache middleware for chainID, to reduce RPC calls (#276)
* Update uniswap.py enable caching, will reduce RPC calls for method chainID * custom simple middleware with - custom eth caching middleware creation - custom SIMPLE_CACHE_RPC_WHITELIST - flag to enable/disable caching for class Uniswap * fix typo * style: don't compare with True Co-authored-by: Erik Bjäreholt <erik.bjareholt@gmail.com>
1 parent dc053cd commit 0b42700
Copy full SHA for 0b42700

File tree

Expand file treeCollapse file tree

3 files changed

+33
-3
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-3
lines changed

‎uniswap/constants.py

Copy file name to clipboardExpand all lines: uniswap/constants.py
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
from typing import Set, cast
2+
from web3.types import ( # noqa: F401
3+
RPCEndpoint,
4+
)
5+
6+
# look at web3/middleware/cache.py for reference
7+
# RPC methods that will be cached inside _get_eth_simple_cache_middleware
8+
SIMPLE_CACHE_RPC_WHITELIST = cast(
9+
Set[RPCEndpoint],
10+
{
11+
"eth_chainId",
12+
},
13+
)
14+
115
ETH_ADDRESS = "0x0000000000000000000000000000000000000000"
216
WETH9_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
317

‎uniswap/uniswap.py

Copy file name to clipboardExpand all lines: uniswap/uniswap.py
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .token import ERC20Token
2424
from .exceptions import InvalidToken, InsufficientBalance
2525
from .util import (
26+
_get_eth_simple_cache_middleware,
2627
_str_to_addr,
2728
_addr_to_str,
2829
_validate_address,
@@ -78,6 +79,7 @@ def __init__(
7879
# use_eip1559: bool = True,
7980
factory_contract_addr: str = None,
8081
router_contract_addr: str = None,
82+
enable_caching: bool = False,
8183
) -> None:
8284
"""
8385
:param address: The public address of the ETH wallet to use.
@@ -88,6 +90,7 @@ def __init__(
8890
:param default_slippage: Default slippage for a trade, as a float (0.01 is 1%). WARNING: slippage is untested.
8991
:param factory_contract_addr: Can be optionally set to override the address of the factory contract.
9092
:param router_contract_addr: Can be optionally set to override the address of the router contract (v2 only).
93+
:param enable_caching: Optionally enables middleware caching RPC method calls.
9194
"""
9295
self.address = _str_to_addr(
9396
address or "0x0000000000000000000000000000000000000000"
@@ -115,7 +118,9 @@ def __init__(
115118
provider = os.environ["PROVIDER"]
116119
self.w3 = Web3(Web3.HTTPProvider(provider, request_kwargs={"timeout": 60}))
117120

118-
# Cache netid to avoid extra RPC calls
121+
if enable_caching:
122+
self.w3.middleware_onion.inject(_get_eth_simple_cache_middleware(), layer=0)
123+
119124
self.netid = int(self.w3.net.version)
120125
if self.netid in _netid_to_name:
121126
self.netname = _netid_to_name[self.netid]

‎uniswap/util.py

Copy file name to clipboardExpand all lines: uniswap/util.py
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22
import json
33
import math
44
import functools
5-
from typing import Any, Generator, Sequence, Union, List, Tuple
5+
import lru
6+
7+
from typing import Any, Generator, Sequence, Union, List, Tuple, Type, Dict, cast
68

79
from web3 import Web3
810
from web3.exceptions import NameNotFound
911
from web3.contract import Contract
12+
from web3.middleware.cache import construct_simple_cache_middleware
13+
from web3.types import Middleware
1014

11-
from .constants import MIN_TICK, MAX_TICK, _tick_spacing
15+
from .constants import MIN_TICK, MAX_TICK, _tick_spacing, SIMPLE_CACHE_RPC_WHITELIST
1216
from .types import AddressLike, Address
1317

1418

19+
def _get_eth_simple_cache_middleware() -> Middleware:
20+
return construct_simple_cache_middleware(
21+
cache_class=cast(Type[Dict[Any, Any]], functools.partial(lru.LRU, 256)),
22+
rpc_whitelist=SIMPLE_CACHE_RPC_WHITELIST,
23+
)
24+
25+
1526
def _str_to_addr(s: Union[AddressLike, str]) -> Address:
1627
"""Idempotent"""
1728
if isinstance(s, str):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.