Skip to content

Commit

Permalink
fix: issues with network in list cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 21, 2023
1 parent c1acab2 commit f590a55
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ape_safe/_cli/click_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import click
from ape import accounts, config
from ape.cli import ApeCliContextObject, ape_cli_context
from ape.exceptions import Abort
from click import BadOptionUsage, MissingParameter

from ape_safe.accounts import SafeContainer
from ape.exceptions import Abort


class SafeCliContext(ApeCliContextObject):
Expand Down
42 changes: 34 additions & 8 deletions ape_safe/_cli/safe_mgmt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import click
import rich
from ape.cli import (
ConnectedProviderCommand,
network_option,
Expand All @@ -16,14 +17,17 @@
@click.command(name="list")
@safe_cli_ctx()
@network_option(default=None)
def _list(cli_ctx: SafeCliContext, network):
@click.option("--verbose", help="Show verbose info about each safe", is_flag=True)
def _list(cli_ctx: SafeCliContext, network, provider, verbose):
"""
Show locally-tracked Safes
"""
if verbose and network is None:
cli_ctx.abort("Must use '--network' with '--verbose'.")

network_ctx = None
if network is not None:
network_ctx = cli_ctx.network_manager.parse_network_choice(network)
network_ctx = network.use_provider(provider.name)
network_ctx.__enter__()

try:
Expand All @@ -36,20 +40,42 @@ def _list(cli_ctx: SafeCliContext, network):
header = f"Found {number_of_safes} Safe"
header += "s:" if number_of_safes > 1 else ":"
click.echo(header)
total = len(cli_ctx.safes)

for account in cli_ctx.safes:
for idx, safe in enumerate(cli_ctx.safes):
extras = []
if account.alias:
extras.append(f"alias: '{account.alias}'")
if safe.alias:
extras.append(f"alias: '{safe.alias}'")

output: str = ""
try:
extras.append(f"version: '{account.version}'")
extras.append(f"version: '{safe.version}'")
except (ChainError, ProviderNotConnectedError):
# Not connected to the network where safe is deployed
extras.append("version: (not connected)")

extras_display = f" ({', '.join(extras)})" if extras else ""
click.echo(f" {account.address}{extras_display}")
else:
# NOTE: Only handle verbose if we are connected.

if verbose:
local_signers = safe.local_signers or []
if local_signers:
local_signers_str = ", ".join([x.alias for x in local_signers if x.alias])
if local_signers_str:
extras.append(f"\n local signers: '{local_signers_str}'")

extras.append(f"next nonce: '{safe.next_nonce}'")
extras_joined = ", ".join(extras)
extras_display = f" {extras_joined}" if extras else ""
output = f" {safe.address}{extras_display}"
if idx < total - 1:
output = f"{output}\n"

if not output:
extras_display = f" ({', '.join(extras)})" if extras else ""
output = f" {safe.address}{extras_display}"

rich.print(output)

finally:
if network_ctx:
Expand Down
19 changes: 11 additions & 8 deletions ape_safe/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from ape.api import AccountAPI, AccountContainerAPI, ReceiptAPI, TransactionAPI
from ape.api.address import BaseAddress
from ape.api.networks import LOCAL_NETWORK_NAME, ForkedNetworkAPI
from ape.api.networks import ForkedNetworkAPI
from ape.cli import select_account
from ape.contracts import ContractInstance
from ape.exceptions import ProviderNotConnectedError
from ape.exceptions import ContractNotFoundError, ProviderNotConnectedError
from ape.logging import logger
from ape.managers.accounts import AccountManager, TestAccountManager
from ape.types import AddressType, HexBytes, MessageSignature
Expand Down Expand Up @@ -234,7 +234,7 @@ def client(self) -> BaseSafeClient:
chain_id = self.provider.chain_id
override_url = os.environ.get("SAFE_TRANSACTION_SERVICE_URL")

if self.provider.network.name == LOCAL_NETWORK_NAME:
if self.provider.network.is_local:
return MockSafeClient(contract=self.contract)

elif chain_id in self.account_file["deployed_chain_ids"]:
Expand Down Expand Up @@ -359,15 +359,18 @@ def local_signers(self) -> List[AccountAPI]:
# TODO: Skip per user config
# TODO: Order per user config
container: Union[AccountManager, TestAccountManager]
if (
self.network_manager.active_provider
and self.provider.network.name == LOCAL_NETWORK_NAME
or self.provider.network.name.endswith("-fork")
):
if self.network_manager.active_provider and self.provider.network.is_dev:
container = self.account_manager.test_accounts
else:
container = self.account_manager

# Ensure the contract is available before continuing.
# Else, return an empty list
try:
_ = self.contract
except ContractNotFoundError:
return []

return list(container[address] for address in self.signers if address in container)

@handle_safe_logic_error()
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/test_safe_mgmt_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ def test_list_one_safe(runner, cli, one_safe):
assert "0x5FbDB2315678afecb367f032d93F642f64180aa3" in result.output


def test_list_network_not_connected(runner, cli, one_safe):
result = runner.invoke(
cli, ["list", "--network", "ethereum:local:test"], catch_exceptions=False
)
assert result.exit_code == 0, result.output
assert "Found 1 Safe" in result.output
assert "0x5FbDB2315678afecb367f032d93F642f64180aa3" in result.output
assert "not connected" in result.output


def test_list_network_connected(runner, cli, one_safe):
result = runner.invoke(
cli, ["list", "--network", "ethereum:local:foundry"], catch_exceptions=False
)
assert result.exit_code == 0, result.output
assert "Found 1 Safe" in result.output
assert "0x5FbDB2315678afecb367f032d93F642f64180aa3" in result.output
assert "not connected" not in result.output


def test_add_safe(runner, cli, no_safes, safe):
result = runner.invoke(
cli, ["add", safe.address, safe.alias], catch_exceptions=False, input="y\n"
Expand Down

0 comments on commit f590a55

Please sign in to comment.