-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
56 lines (39 loc) · 1.49 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import importlib.util
import re
from typing import Any
def has_module(module_name: str) -> bool:
"""Check if module is available."""
return importlib.util.find_spec(module_name) is not None
def extract_digits(value: int | str) -> int:
value_str = str(value).strip("\"'")
digit_match = re.search(r"\d+", value_str)
if not digit_match:
raise ValueError(f"Input '{value}' does not contain any digits")
extracted_digits = digit_match.group()
if not extracted_digits.isdigit():
raise ValueError(f"Extracted value '{extracted_digits}' is not a valid digit string")
return int(extracted_digits)
def normalize_chain_id(
in_value: int | str | list[int | str] | None,
) -> int | list[int] | None:
"""Normalize str values integers."""
if in_value is None:
return None
if isinstance(in_value, list):
return [extract_digits(c) for c in in_value]
return extract_digits(in_value)
def is_encoded(encoded_data: str) -> bool:
encoded_data = encoded_data.removeprefix("0x")
try:
bytes.fromhex(encoded_data)
return True
except ValueError:
return False
def clean_resolve(out: dict[str, Any]):
if "transactions" in out["data"]:
for transaction in out["data"]["transactions"]:
if "data" in transaction and is_encoded(transaction["data"]):
transaction.pop("data")
if "logs_bloom" in transaction:
transaction.pop("logs_bloom")
return out