Skip to content

Commit c09264d

Browse files
committed
v0.1.36 added cached HexBytes wrapper and made all cache sizes configurable via env variables.
1 parent 83b326e commit c09264d

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

IceCreamSwapWeb3/AddressCalculator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
import os
12
from functools import lru_cache
23

34
import eth_utils
45
import rlp
56
from eth_utils import to_bytes
67
from web3 import Web3
78

8-
@lru_cache(maxsize=1024)
9+
10+
@lru_cache(maxsize=int(os.getenv("DICT_SLOT_CACHE_SIZE", 1024)))
911
def get_dict_slot(slot: int, key: int) -> int:
1012
return int.from_bytes(Web3.solidity_keccak(["uint256", "uint256"], [key, slot]), byteorder='big')
1113

1214

13-
@lru_cache(maxsize=1024)
15+
@lru_cache(maxsize=int(os.getenv("CREATE_ADDRESS_CACHE_SIZE", 1024)))
1416
def calculate_create_address(sender: str, nonce: int) -> str:
1517
assert len(sender) == 42
1618
sender_bytes = to_bytes(hexstr=sender)
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
import os
2+
from functools import lru_cache
23
from typing import cast
34
from eth_hash.auto import keccak
45
from eth_typing import ChecksumAddress
56

6-
CACHE_SIZE = int(os.getenv("CHECKSUM_CACHE_SIZE", 1000))
7-
CHECKSUM_CACHE = {}
8-
7+
@lru_cache(maxsize=int(os.getenv("CHECKSUM_CACHE_SIZE", 16384)))
98
def to_checksum_address(address: str) -> ChecksumAddress:
109
"""Fast checksum address with caching."""
1110
normalized_address = address.lower().replace("0x", "")
1211
assert len(normalized_address) == 40, "Address has incorrect length"
1312

14-
if normalized_address in CHECKSUM_CACHE:
15-
return CHECKSUM_CACHE[normalized_address]
16-
1713
hashed_address = keccak(normalized_address.encode()).hex()
1814
checksum_address = "0x" + "".join(
1915
char.upper() if int(hashed_address[i], 16) >= 8 else char
2016
for i, char in enumerate(normalized_address)
2117
)
2218

23-
if len(CHECKSUM_CACHE) >= CACHE_SIZE:
24-
CHECKSUM_CACHE.pop(next(iter(CHECKSUM_CACHE)))
25-
26-
CHECKSUM_CACHE[normalized_address] = checksum_address
2719
return cast(ChecksumAddress, checksum_address)

IceCreamSwapWeb3/FastHexBytes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
from functools import lru_cache
3+
from typing import Type
4+
5+
from hexbytes import HexBytes as _HexBytes
6+
from hexbytes.main import BytesLike
7+
8+
9+
class HexBytes(_HexBytes):
10+
@lru_cache(maxsize=int(os.getenv("HEX_BYTES_CACHE_SIZE", 16384)))
11+
def __new__(cls: Type[bytes], val: BytesLike) -> "_HexBytes":
12+
return super().__new__(cls, val)

IceCreamSwapWeb3/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .Web3Advanced import Web3Advanced
22
from .FastChecksumAddress import to_checksum_address
3-
from .AddressCalculator import get_dict_slot, calculate_create_address
3+
from .AddressCalculator import get_dict_slot, calculate_create_address
4+
from .FastHexBytes import HexBytes

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '0.1.35'
3+
VERSION = '0.1.36'
44
DESCRIPTION = 'IceCreamSwap Web3.py wrapper'
55
LONG_DESCRIPTION = 'IceCreamSwap Web3.py wrapper with automatic retries, multicall and other advanced functionality'
66

0 commit comments

Comments
 (0)