Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit0b42700

Browse files
feat: add option enable_caching that adds cache middleware for chainID, to reduce RPC calls (#276)
* Update uniswap.pyenable 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 TrueCo-authored-by: Erik Bjäreholt <erik.bjareholt@gmail.com>
1 parentdc053cd commit0b42700

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

‎uniswap/constants.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
fromtypingimportSet,cast
2+
fromweb3.typesimport (# 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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .tokenimportERC20Token
2424
from .exceptionsimportInvalidToken,InsufficientBalance
2525
from .utilimport (
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
addressor"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+
ifenable_caching:
122+
self.w3.middleware_onion.inject(_get_eth_simple_cache_middleware(),layer=0)
123+
119124
self.netid=int(self.w3.net.version)
120125
ifself.netidin_netid_to_name:
121126
self.netname=_netid_to_name[self.netid]

‎uniswap/util.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22
importjson
33
importmath
44
importfunctools
5-
fromtypingimportAny,Generator,Sequence,Union,List,Tuple
5+
importlru
6+
7+
fromtypingimportAny,Generator,Sequence,Union,List,Tuple,Type,Dict,cast
68

79
fromweb3importWeb3
810
fromweb3.exceptionsimportNameNotFound
911
fromweb3.contractimportContract
12+
fromweb3.middleware.cacheimportconstruct_simple_cache_middleware
13+
fromweb3.typesimportMiddleware
1014

11-
from .constantsimportMIN_TICK,MAX_TICK,_tick_spacing
15+
from .constantsimportMIN_TICK,MAX_TICK,_tick_spacing,SIMPLE_CACHE_RPC_WHITELIST
1216
from .typesimportAddressLike,Address
1317

1418

19+
def_get_eth_simple_cache_middleware()->Middleware:
20+
returnconstruct_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
ifisinstance(s,str):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp