generated from ApeWorX/project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import tempfile | ||
from pathlib import Path | ||
|
||
import yaml | ||
from ape.api.projects import DependencyAPI | ||
from ape.types import AddressType | ||
from ethpm_types import PackageManifest | ||
from pydantic import AnyUrl, HttpUrl | ||
|
||
from .explorer import Etherscan | ||
|
||
|
||
class EtherscanDependency(DependencyAPI): | ||
etherscan: str | ||
ecosystem: str = "ethereum" | ||
network: str = "mainnet" | ||
|
||
@property | ||
def version_id(self) -> str: | ||
return "etherscan" # Only 1 version | ||
|
||
@property | ||
def address(self) -> AddressType: | ||
return self.network_manager.ethereum.decode_address(self.etherscan) | ||
|
||
@property | ||
def uri(self) -> AnyUrl: | ||
return HttpUrl(f"{self.explorer.get_address_url(self.address)}#code") | ||
|
||
@property | ||
def explorer(self) -> Etherscan: | ||
if self.network_manager.active_provider: | ||
explorer = self.provider.network.explorer | ||
if isinstance(explorer, Etherscan): | ||
# Could be using a different network. | ||
return explorer | ||
else: | ||
return self.network_manager.ethereum.mainnet.explorer | ||
|
||
# Assume Ethereum | ||
return self.network_manager.ethereum.mainnet.explorer | ||
|
||
def extract_manifest(self, use_cache: bool = True) -> PackageManifest: | ||
ecosystem = self.network_manager.get_ecosystem(self.ecosystem) | ||
network = ecosystem.get_network(self.network) | ||
|
||
ctx = None | ||
if self.network_manager.active_provider is None: | ||
ctx = network.use_default_provider() | ||
ctx.__enter__() | ||
|
||
try: | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
project_path = Path(temp_dir).resolve() | ||
contracts_folder = project_path / "contracts" | ||
contracts_folder.mkdir() | ||
|
||
response = self.explorer._get_source_code(self.address) | ||
|
||
# Ensure compiler settings match. | ||
if response.evm_version and response.evm_version != "Default": | ||
data = {"solidity": {"evm_version": response.evm_version}} | ||
config_file = project_path / "ape-config.yaml" | ||
with open(config_file, "w") as file: | ||
yaml.safe_dump(data, file) | ||
|
||
new_path = contracts_folder / f"{response.name}.sol" | ||
new_path.write_text(response.source_code) | ||
return self._extract_local_manifest(project_path, use_cache=use_cache) | ||
|
||
finally: | ||
if ctx: | ||
ctx.__exit__(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from ape_etherscan.dependency import EtherscanDependency | ||
|
||
|
||
def test_dependency(mock_backend): | ||
mock_backend.set_network("ethereum", "mainnet") | ||
mock_backend.setup_mock_get_contract_type_response("get_contract_response") | ||
|
||
dependency = EtherscanDependency( | ||
name="Apes", | ||
etherscan="0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512", | ||
ecosystem="ethereum", | ||
network="mainnet", | ||
) | ||
actual = dependency.extract_manifest() | ||
assert "BoredApeYachtClub.sol" in actual.sources |