Skip to content

Commit

Permalink
Merge pull request #301 from KPrasch/main
Browse files Browse the repository at this point in the history
Contract listing CLI
  • Loading branch information
KPrasch authored Aug 7, 2024
2 parents 07ce241 + 983517c commit aa53f83
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions deployment/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,12 @@ def registry_filepath_from_domain(domain: str) -> Path:
raise ValueError(f"No registry found for domain '{domain}'")

return p


def get_chain_name(chain_id: int) -> str:
"""Returns the name of the chain given its chain ID."""
for ecosystem_name, ecosystem in networks.ecosystems.items():
for network_name, network in ecosystem.networks.items():
if network.chain_id == chain_id:
return f"{ecosystem_name} {network_name}"
raise ValueError(f"Chain ID {chain_id} not found in networks.")
60 changes: 60 additions & 0 deletions scripts/list_contracts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/python3


from itertools import groupby
from typing import Optional, List, Tuple

import click
from ape.cli import ConnectedProviderCommand

from deployment.constants import SUPPORTED_TACO_DOMAINS
from deployment.registry import read_registry, RegistryEntry
from deployment.utils import get_chain_name, registry_filepath_from_domain


def _format_chain_name(chain_name: str) -> str:
"""Format the chain name to capitalize each word and join with slashes."""
return "/".join(word.capitalize() for word in chain_name.split())


def _get_registry_entries(domain: Optional[str] = None) -> List[Tuple[str, List[RegistryEntry]]]:
"""Parse the registry files for the given domain or all supported domains."""
registry_entries = list()
for taco_domain in SUPPORTED_TACO_DOMAINS:
if domain and domain != taco_domain:
continue
registry_filepath = registry_filepath_from_domain(domain=taco_domain)
entries = read_registry(filepath=registry_filepath)
registry_entries.append((taco_domain, entries))
return registry_entries


def _display_registry_entries(registry_entries: List[Tuple[str, List[RegistryEntry]]]) -> None:
"""Display registry entries grouped by chain ID."""
for domain, entries in registry_entries:
grouped_entries = groupby(entries, key=lambda e: e.chain_id)
click.secho(f"\n{domain.capitalize()} Domain", fg="green")

for chain_id, chain_entries in grouped_entries:
chain_name = _format_chain_name(get_chain_name(chain_id))
click.secho(f" {chain_name}", fg="yellow")

for index, entry in enumerate(chain_entries, start=1):
click.secho(f" {index}. {entry.name} {entry.address}", fg="cyan")


@click.command(cls=ConnectedProviderCommand, name="list-contracts")
@click.option(
"--domain",
"-d",
help="TACo domain",
type=click.Choice(SUPPORTED_TACO_DOMAINS),
)
def cli(domain):
"""List all contracts in the registry. Optionally filter by domain."""
registry_entries = _get_registry_entries(domain)
_display_registry_entries(registry_entries)


if __name__ == "__main__":
cli()

0 comments on commit aa53f83

Please sign in to comment.