Skip to content

Commit 71c2dfa

Browse files
committed
support v2 api
1 parent e059d7c commit 71c2dfa

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# coverage
88
*coverage*
9+
logs/
910

1011
# VSCode
1112
.vscode/*

etherscan/enums/fields_enum.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class FieldsEnum:
99
BLOCK_TYPE: str = "&blocktype="
1010
BLOCKNO: str = "&blockno="
1111
BOOLEAN: str = "&boolean="
12+
CHAIN_ID: str = "&chainid="
1213
CLIENT_TYPE: str = "&clienttype="
1314
CLOSEST: str = "&closest="
1415
CONTRACT_ADDRESS: str = "&contractaddress="
@@ -23,7 +24,7 @@ class FieldsEnum:
2324
OFFSET: str = "&offset="
2425
PAGE: str = "&page="
2526
POSITION: str = "&position="
26-
PREFIX: str = "https://api-{}.etherscan.io/api?"
27+
PREFIX: str = "https://api-{}.etherscan.io/v2/api?"
2728
SORT: str = "&sort="
2829
START_BLOCK: str = "&startblock="
2930
START_DATE: str = "&startdate="

etherscan/etherscan.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@
1010

1111

1212
class Etherscan:
13-
def __new__(cls, api_key: str, net: str = "MAIN"):
13+
def __new__(cls, api_key: str, net: str = "MAIN", chain_id: str = "1"):
1414
with resources.path(configs, f"{net.upper()}-stable.json") as path:
1515
config_path = str(path)
16-
return cls.from_config(api_key=api_key, config_path=config_path, net=net)
16+
return cls.from_config(
17+
api_key=api_key, chain_id=chain_id, config_path=config_path, net=net
18+
)
1719

1820
@staticmethod
1921
def __load_config(config_path: str) -> dict:
2022
with open(config_path, "r") as f:
2123
return json.load(f)
2224

2325
@staticmethod
24-
def __run(func, api_key: str, net: str):
26+
def __run(func, api_key: str, net: str, chain_id: str):
2527
def wrapper(*args, **kwargs):
2628
url = (
2729
f"{fields.PREFIX.format(net.lower()).replace('-main','')}"
2830
f"{func(*args, **kwargs)}"
31+
f"{fields.CHAIN_ID}"
32+
f"{chain_id}"
2933
f"{fields.API_KEY}"
3034
f"{api_key}"
3135
)
@@ -35,10 +39,10 @@ def wrapper(*args, **kwargs):
3539
return wrapper
3640

3741
@classmethod
38-
def from_config(cls, api_key: str, config_path: str, net: str):
42+
def from_config(cls, api_key: str, config_path: str, net: str, chain_id: str):
3943
config = cls.__load_config(config_path)
4044
for func, v in config.items():
4145
if not func.startswith("_"): # disabled if _
4246
attr = getattr(getattr(etherscan, v["module"]), func)
43-
setattr(cls, func, cls.__run(attr, api_key, net))
47+
setattr(cls, func, cls.__run(attr, api_key, net, chain_id))
4448
return cls

0 commit comments

Comments
 (0)