Skip to content

Commit

Permalink
fix: auto fork evmchains networks and better access
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 14, 2024
1 parent e31c426 commit fe9104e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
17 changes: 15 additions & 2 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,27 @@ def _networks_from_plugins(self) -> dict[str, "NetworkAPI"]:
@cached_property
def _networks_from_evmchains(self) -> dict[str, "NetworkAPI"]:
# NOTE: Purposely exclude plugins here so we also prefer plugins.
return {
networks = {
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
}
forked_networks: dict[str, type[ForkedNetworkAPI]] = {}
for network_name, network in networks.items():
if network_name.endswith("-fork"):
# Already a fork.
continue

fork_network_name = f"{network_name}-fork"
if any(x == fork_network_name for x in networks):
# The forked version of this network is already known.
continue

forked_networks[fork_network_name] = ForkedNetworkAPI

return {**networks, **forked_networks}

def __post_init__(self):
if len(self.networks) == 0:
Expand Down Expand Up @@ -535,7 +549,6 @@ def get_network(self, network_name: str) -> "NetworkAPI":
Returns:
:class:`~ape.api.networks.NetworkAPI`
"""

names = {network_name, network_name.replace("-", "_"), network_name.replace("_", "-")}
networks = self.networks
for name in names:
Expand Down
47 changes: 25 additions & 22 deletions src/ape/managers/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,38 @@ def ecosystems(self) -> dict[str, EcosystemAPI]:
)
plugin_ecosystems[ecosystem_name] = ecosystem_cls

return plugin_ecosystems
return {**plugin_ecosystems, **self._evmchains_ecosystems}

@cached_property
def _plugin_ecosystems(self) -> dict[str, EcosystemAPI]:
# Load plugins.
plugins = self.plugin_manager.ecosystems
return {n: cls(name=n) for n, cls in plugins} # type: ignore[operator]

@cached_property
def _evmchains_ecosystems(self) -> dict[str, EcosystemAPI]:
ecosystems: dict[str, EcosystemAPI] = {}
for name in PUBLIC_CHAIN_META:
ecosystem_name = name.lower().replace(" ", "-")
symbol = None
for net in PUBLIC_CHAIN_META[ecosystem_name].values():
if not (native_currency := net.get("nativeCurrency")):
continue

if "symbol" not in native_currency:
continue

symbol = native_currency["symbol"]
break

symbol = symbol or "ETH"

# Is an EVM chain, can automatically make a class using evm-chains.
evm_class = self._plugin_ecosystems["ethereum"].__class__
ecosystems[name] = evm_class(name=ecosystem_name, fee_token_symbol=symbol)

return ecosystems

def create_custom_provider(
self,
connection_str: str,
Expand Down Expand Up @@ -437,29 +461,9 @@ def get_ecosystem(self, ecosystem_name: str) -> EcosystemAPI:
Returns:
:class:`~ape.api.networks.EcosystemAPI`
"""

if ecosystem_name in self.ecosystem_names:
return self.ecosystems[ecosystem_name]

elif ecosystem_name.lower().replace(" ", "-") in PUBLIC_CHAIN_META:
ecosystem_name = ecosystem_name.lower().replace(" ", "-")
symbol = None
for net in PUBLIC_CHAIN_META[ecosystem_name].values():
if not (native_currency := net.get("nativeCurrency")):
continue

if "symbol" not in native_currency:
continue

symbol = native_currency["symbol"]
break

symbol = symbol or "ETH"

# 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, fee_token_symbol=symbol)

raise EcosystemNotFoundError(ecosystem_name, options=self.ecosystem_names)

def get_provider_from_choice(
Expand Down Expand Up @@ -606,7 +610,6 @@ def set_default_ecosystem(self, ecosystem_name: str):
ecosystem_name (str): The name of the ecosystem to set
as the default.
"""

if ecosystem_name in self.ecosystem_names:
self._default_ecosystem_name = ecosystem_name

Expand Down
5 changes: 5 additions & 0 deletions tests/functional/test_network_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,8 @@ def supports_chain(cls, chain_id):
]
assert network.explorer is not None
assert network.explorer.name == NAME


def test_evm_chains_auto_forked_networks_exist(networks):
# NOTE: Moonbeam networks exist in evmchains only; that is how Ape knows about them.
assert issubclass(networks.moonbeam.moonriver_fork, ForkedNetworkAPI)

0 comments on commit fe9104e

Please sign in to comment.