Skip to content

Commit af40716

Browse files
committed
v0.1.29
implemented faster to_checksum_address with caching as it is surprisingly slow. Also speed up HexBytes conversion in Subsquid get_filter function
1 parent 29650d7 commit af40716

File tree

7 files changed

+39
-8
lines changed

7 files changed

+39
-8
lines changed

IceCreamSwapWeb3/EthAdvanced.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def main(
299299
node_url="https://rpc-core.icecreamswap.com",
300300
usdt_address="0x900101d06A7426441Ae63e9AB3B9b0F63Be145F1",
301301
):
302-
from eth_utils import to_checksum_address
302+
from IceCreamSwapWeb3 import to_checksum_address
303303

304304
usdt_address = to_checksum_address(usdt_address)
305305

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
from typing import cast
3+
from eth_hash.auto import keccak
4+
from eth_typing import ChecksumAddress
5+
6+
CACHE_SIZE = int(os.getenv("CHECKSUM_CACHE_SIZE", 1000))
7+
CHECKSUM_CACHE = {}
8+
9+
def to_checksum_address(address: str) -> ChecksumAddress:
10+
"""Fast checksum address with caching."""
11+
normalized_address = address.lower().replace("0x", "")
12+
assert len(normalized_address) == 40, "Address has incorrect length"
13+
14+
if normalized_address in CHECKSUM_CACHE:
15+
return CHECKSUM_CACHE[normalized_address]
16+
17+
hashed_address = keccak(normalized_address.encode()).hex()
18+
checksum_address = "0x" + "".join(
19+
char.upper() if int(hashed_address[i], 16) >= 8 else char
20+
for i, char in enumerate(normalized_address)
21+
)
22+
23+
if len(CHECKSUM_CACHE) >= CACHE_SIZE:
24+
CHECKSUM_CACHE.pop(next(iter(CHECKSUM_CACHE)))
25+
26+
CHECKSUM_CACHE[normalized_address] = checksum_address
27+
return cast(ChecksumAddress, checksum_address)

IceCreamSwapWeb3/Multicall.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import eth_abi
66
import eth_utils
77
import rlp
8-
from eth_utils import to_checksum_address, to_bytes
8+
from eth_utils import to_bytes
99
from eth_utils.abi import get_abi_output_types, get_abi_input_types
1010
from web3.contract.contract import ContractFunction, ContractConstructor
1111
from web3.exceptions import ContractLogicError
1212

13-
from IceCreamSwapWeb3 import Web3Advanced
13+
from IceCreamSwapWeb3 import Web3Advanced, to_checksum_address
1414

1515
# load multicall abi
1616
with files("IceCreamSwapWeb3").joinpath("./abi/Multicall.abi").open('r') as f:

IceCreamSwapWeb3/Subsquid.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from typing import cast
2+
13
import requests
4+
from IceCreamSwapWeb3 import to_checksum_address
25
from eth_utils import to_checksum_address
36
from hexbytes import HexBytes
47
from tqdm import tqdm
@@ -103,10 +106,10 @@ def get_filter(
103106
address=to_checksum_address(log['address']),
104107
blockHash=block["header"]["hash"],
105108
blockNumber=block["header"]["number"],
106-
data=HexBytes(log["data"]),
109+
data=cast(HexBytes, bytes.fromhex(log["data"][2:])),
107110
logIndex=log["logIndex"],
108-
topics=[HexBytes(topic) for topic in log["topics"]],
109-
transactionHash=HexBytes(log["transactionHash"]),
111+
topics=[cast(HexBytes, bytes.fromhex(topic[2:])) for topic in log["topics"]],
112+
transactionHash=cast(HexBytes, bytes.fromhex(log["transactionHash"][2:])),
110113
transactionIndex=log["transactionIndex"],
111114
removed=False,
112115
))

IceCreamSwapWeb3/Web3Advanced.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from importlib.resources import files
33
from time import sleep
44

5-
from eth_utils import to_checksum_address
5+
from IceCreamSwapWeb3 import to_checksum_address
66
from web3 import Web3
77
from web3.exceptions import ContractLogicError
88
from web3.main import get_default_modules

IceCreamSwapWeb3/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .Web3Advanced import Web3Advanced
2+
from .FastChecksumAddress import to_checksum_address

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.28'
3+
VERSION = '0.1.29'
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)