Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61abdba

Browse files
committed
Added support for testnets
1 parent 2be8440 commit 61abdba

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

etherscan/enums/fields_enum.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FieldsEnum:
2323
OFFSET: str = "&offset="
2424
PAGE: str = "&page="
2525
POSITION: str = "&position="
26-
PREFIX: str = "https://api.etherscan.io/api?"
26+
PREFIX: str = "https://api-{}.etherscan.io/api?"
2727
SORT: str = "&sort="
2828
START_BLOCK: str = "&startblock="
2929
START_DATE: str = "&startdate="

etherscan/etherscan.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,35 @@
1010

1111

1212
class Etherscan:
13-
def __new__(cls, api_key: str):
14-
with resources.path(configs, "stable.json") as path:
13+
def __new__(cls, api_key: str, net: str = "MAIN"):
14+
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)
16+
return cls.from_config(api_key=api_key, config_path=config_path, net=net)
1717

1818
@staticmethod
1919
def __load_config(config_path: str) -> dict:
2020
with open(config_path, "r") as f:
2121
return json.load(f)
2222

2323
@staticmethod
24-
def __run(func, api_key: str):
24+
def __run(func, api_key: str, net: str):
2525
def wrapper(*args, **kwargs):
2626
url = (
27-
f"{fields.PREFIX}"
27+
f"{fields.PREFIX.format(net.lower()).replace('-main','')}"
2828
f"{func(*args, **kwargs)}"
2929
f"{fields.API_KEY}"
3030
f"{api_key}"
3131
)
32-
r = requests.get(url)
32+
r = requests.get(url, headers={'User-Agent':''})
3333
return parser.parse(r)
3434

3535
return wrapper
3636

3737
@classmethod
38-
def from_config(cls, api_key: str, config_path: str):
38+
def from_config(cls, api_key: str, config_path: str, net: str):
3939
config = cls.__load_config(config_path)
4040
for func, v in config.items():
4141
if not func.startswith("_"): # disabled if _
4242
attr = getattr(getattr(etherscan, v["module"]), func)
43-
setattr(cls, func, cls.__run(attr, api_key))
43+
setattr(cls, func, cls.__run(attr, api_key, net))
4444
return cls

0 commit comments

Comments
 (0)