From 51b17522db579108185eeeb5ac0eabe2dc1909db Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Thu, 17 Oct 2024 12:31:41 -0500 Subject: [PATCH] feat: supported chains --- ape_etherscan/explorer.py | 25 +++++++++++++++++++++++++ ape_etherscan/utils.py | 6 +++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ape_etherscan/explorer.py b/ape_etherscan/explorer.py index e30adb8..302bad7 100644 --- a/ape_etherscan/explorer.py +++ b/ape_etherscan/explorer.py @@ -1,13 +1,17 @@ import json from typing import Optional +import requests from ape.api import ExplorerAPI, PluginConfig from ape.contracts import ContractInstance from ape.exceptions import ProviderNotConnectedError from ape.managers.project import ProjectManager from ape.types import AddressType, ContractType +from ape.utils import ZERO_ADDRESS + from ethpm_types import Compiler, PackageManifest from ethpm_types.source import Source +from evmchains import PUBLIC_CHAIN_META from ape_etherscan.client import ( ClientFactory, @@ -18,6 +22,7 @@ from ape_etherscan.exceptions import ContractNotVerifiedError from ape_etherscan.types import EtherscanInstance from ape_etherscan.verify import SourceVerifier +from ape_etherscan.utils import NETWORKS class Etherscan(ExplorerAPI): @@ -47,6 +52,26 @@ def etherscan_api_uri(self): self._config, self.network.ecosystem.name, self.network.name.replace("-fork", "") ) + @classmethod + def get_supported_chains(cls) -> list[dict]: + """ + Get a list of chain data for all chains Etherscan supports. + https://docs.etherscan.io/contract-verification/supported-chains + + Returns: + list[dict] + """ + response = requests.get("https://api.etherscan.io/v2/chainlist") + response.raise_for_status() + data = response.json() + return data.get("result", []) + + @classmethod + def supports_chain(cls, chain_id: int) -> bool: + chain_data = cls.get_supported_chains() + chain_ids = [int(c["chainid"]) for c in chain_data if "chainid" in c] + return chain_id in chain_ids + def get_address_url(self, address: str) -> str: return f"{self.etherscan_uri}/address/{address}" diff --git a/ape_etherscan/utils.py b/ape_etherscan/utils.py index e1f3ead..a57e66e 100644 --- a/ape_etherscan/utils.py +++ b/ape_etherscan/utils.py @@ -1,4 +1,4 @@ -# TODO: Remove in 0.9 and make this a calculated property. +# TODO: (deprecated) Remove in 0.9 and make this a calculated property. API_KEY_ENV_KEY_MAP = { "arbitrum": "ARBISCAN_API_KEY", "avalanche": "SNOWTRACE_API_KEY", @@ -20,11 +20,11 @@ "unichain": "UNISCAN_API_KEY", } NETWORKS = { - "arbitrum": [ + "arbitrum": { "mainnet", "sepolia", "nova", - ], + }, "avalanche": [ "mainnet", "fuji",