From 71c2dfaa07530da806386020abc908688bd4ffdd Mon Sep 17 00:00:00 2001 From: ZHANG Cheng Date: Fri, 27 Dec 2024 16:01:29 +0800 Subject: [PATCH] support v2 api --- .gitignore | 1 + etherscan/enums/fields_enum.py | 3 ++- etherscan/etherscan.py | 14 +++++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index a7f9bdb..ce5c88d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # coverage *coverage* +logs/ # VSCode .vscode/* diff --git a/etherscan/enums/fields_enum.py b/etherscan/enums/fields_enum.py index b0bcc4d..d4442c2 100644 --- a/etherscan/enums/fields_enum.py +++ b/etherscan/enums/fields_enum.py @@ -9,6 +9,7 @@ class FieldsEnum: BLOCK_TYPE: str = "&blocktype=" BLOCKNO: str = "&blockno=" BOOLEAN: str = "&boolean=" + CHAIN_ID: str = "&chainid=" CLIENT_TYPE: str = "&clienttype=" CLOSEST: str = "&closest=" CONTRACT_ADDRESS: str = "&contractaddress=" @@ -23,7 +24,7 @@ class FieldsEnum: OFFSET: str = "&offset=" PAGE: str = "&page=" POSITION: str = "&position=" - PREFIX: str = "https://api-{}.etherscan.io/api?" + PREFIX: str = "https://api-{}.etherscan.io/v2/api?" SORT: str = "&sort=" START_BLOCK: str = "&startblock=" START_DATE: str = "&startdate=" diff --git a/etherscan/etherscan.py b/etherscan/etherscan.py index 5749aa8..35df73c 100644 --- a/etherscan/etherscan.py +++ b/etherscan/etherscan.py @@ -10,10 +10,12 @@ class Etherscan: - def __new__(cls, api_key: str, net: str = "MAIN"): + def __new__(cls, api_key: str, net: str = "MAIN", chain_id: str = "1"): with resources.path(configs, f"{net.upper()}-stable.json") as path: config_path = str(path) - return cls.from_config(api_key=api_key, config_path=config_path, net=net) + return cls.from_config( + api_key=api_key, chain_id=chain_id, config_path=config_path, net=net + ) @staticmethod def __load_config(config_path: str) -> dict: @@ -21,11 +23,13 @@ def __load_config(config_path: str) -> dict: return json.load(f) @staticmethod - def __run(func, api_key: str, net: str): + def __run(func, api_key: str, net: str, chain_id: str): def wrapper(*args, **kwargs): url = ( f"{fields.PREFIX.format(net.lower()).replace('-main','')}" f"{func(*args, **kwargs)}" + f"{fields.CHAIN_ID}" + f"{chain_id}" f"{fields.API_KEY}" f"{api_key}" ) @@ -35,10 +39,10 @@ def wrapper(*args, **kwargs): return wrapper @classmethod - def from_config(cls, api_key: str, config_path: str, net: str): + def from_config(cls, api_key: str, config_path: str, net: str, chain_id: str): config = cls.__load_config(config_path) for func, v in config.items(): if not func.startswith("_"): # disabled if _ attr = getattr(getattr(etherscan, v["module"]), func) - setattr(cls, func, cls.__run(attr, api_key, net)) + setattr(cls, func, cls.__run(attr, api_key, net, chain_id)) return cls pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy