Skip to content

Commit

Permalink
feat: poc
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Oct 19, 2024
1 parent 3f2f301 commit 2c52d42
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
28 changes: 24 additions & 4 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from eth_utils import keccak, to_int
from ethpm_types import BaseModel, ContractType
from ethpm_types.abi import ABIType, ConstructorABI, EventABI, MethodABI
from evmchains import PUBLIC_CHAIN_META
from pydantic import model_validator

from ape.exceptions import (
Expand Down Expand Up @@ -120,13 +121,11 @@ def custom_network(self) -> "NetworkAPI":
if ethereum_class is None:
raise NetworkError("Core Ethereum plugin missing.")

request_header = self.config_manager.REQUEST_HEADER
init_kwargs = {"name": "ethereum", "request_header": request_header}
init_kwargs = {"name": "ethereum"}
ethereum = ethereum_class(**init_kwargs) # type: ignore
return NetworkAPI(
name="custom",
ecosystem=ethereum,
request_header=request_header,
_default_provider="node",
_is_custom=True,
)
Expand Down Expand Up @@ -296,6 +295,11 @@ def networks(self) -> dict[str, "NetworkAPI"]:
network_api._is_custom = True
networks[net_name] = network_api

# Add any remaining networks from EVM chains here (but don't override).
# NOTE: Only applicable to EVM-based ecosystems, of course.
# Otherwise, this is a no-op.
networks = {**self._networks_from_evmchains, **networks}

return networks

@cached_property
Expand All @@ -306,6 +310,17 @@ def _networks_from_plugins(self) -> dict[str, "NetworkAPI"]:
if ecosystem_name == self.name
}

@cached_property
def _networks_from_evmchains(self) -> dict[str, "NetworkAPI"]:
# NOTE: Purposely exclude plugins here so we also prefer plugins.
return {
network_name: create_network_type(data["chainId"], data["chainId"])(
name=network_name, ecosystem=self
)
for network_name, data in PUBLIC_CHAIN_META.get(self.name, {}).items()
if network_name not in self._networks_from_plugins
}

def __post_init__(self):
if len(self.networks) == 0:
raise NetworkError("Must define at least one network in ecosystem")
Expand Down Expand Up @@ -1046,7 +1061,6 @@ def providers(self): # -> dict[str, Partial[ProviderAPI]]
Returns:
dict[str, partial[:class:`~ape.api.providers.ProviderAPI`]]
"""

from ape.plugins._utils import clean_plugin_name

providers = {}
Expand Down Expand Up @@ -1078,6 +1092,12 @@ def providers(self): # -> dict[str, Partial[ProviderAPI]]
network=self,
)

# Any EVM-chain works with node provider.
if "node" not in providers and self.name in self.ecosystem._networks_from_evmchains:
# NOTE: Arbitrarily using sepolia to access the Node class.
node_provider_cls = self.network_manager.ethereum.sepolia.get_provider("node").__class__
providers["node"] = partial(node_provider_cls, name="node", network=self)

return providers

def _get_plugin_providers(self):
Expand Down
16 changes: 10 additions & 6 deletions src/ape/managers/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from functools import cached_property
from typing import Optional, Union

from evmchains import PUBLIC_CHAIN_META

from ape.api import EcosystemAPI, ProviderAPI, ProviderContextManager
from ape.api.networks import NetworkAPI
from ape.exceptions import EcosystemNotFoundError, NetworkError, NetworkNotFoundError
Expand Down Expand Up @@ -51,7 +53,6 @@ def active_provider(self) -> Optional[ProviderAPI]:
"""
The currently connected provider if one exists. Otherwise, returns ``None``.
"""

return self._active_provider

@active_provider.setter
Expand Down Expand Up @@ -162,7 +163,6 @@ def ecosystem_names(self) -> set[str]:
"""
The set of all ecosystem names in ``ape``.
"""

return set(self.ecosystems)

@property
Expand Down Expand Up @@ -435,10 +435,15 @@ def get_ecosystem(self, ecosystem_name: str) -> EcosystemAPI:
:class:`~ape.api.networks.EcosystemAPI`
"""

if ecosystem_name not in self.ecosystem_names:
raise EcosystemNotFoundError(ecosystem_name, options=self.ecosystem_names)
if ecosystem_name in self.ecosystem_names:
return self.ecosystems[ecosystem_name]

elif ecosystem_name in PUBLIC_CHAIN_META:
# Is an EVM chain, can automatically make a class using evm-chains.
evm_class = self._plugin_ecosystems["ethereum"].__class__
return evm_class(name=ecosystem_name)

return self.ecosystems[ecosystem_name]
raise EcosystemNotFoundError(ecosystem_name, options=self.ecosystem_names)

def get_provider_from_choice(
self,
Expand Down Expand Up @@ -546,7 +551,6 @@ def parse_network_choice(
Returns:
:class:`~api.api.networks.ProviderContextManager`
"""

provider = self.get_provider_from_choice(
network_choice=network_choice, provider_settings=provider_settings
)
Expand Down

0 comments on commit 2c52d42

Please sign in to comment.