Add network_type to network selection in cli to differentiate between eth and polygon

pull/2917/head
James Campbell 2022-04-13 13:04:26 -05:00
parent 9119e10a31
commit aff2f7bb2e
6 changed files with 45 additions and 19 deletions

View File

@ -22,6 +22,7 @@ class NetworksInventory: # TODO: See #1564
IBEX = 'ibex'
LYNX = 'lynx'
ORYX = 'oryx'
ETH = 'ethereum'
# TODO: Use naming scheme to preserve multiple compatibility with multiple deployments to a single network?
POLYGON = 'polygon'
@ -30,18 +31,22 @@ class NetworksInventory: # TODO: See #1564
UNKNOWN = 'unknown' # TODO: Is there a better way to signal an unknown network?
DEFAULT = MAINNET
__to_ethereum_chain_id = {
__to_chain_id_eth = {
MAINNET: 1, # Ethereum Mainnet
ORYX: 3, # Ropsten
IBEX: 4, # Rinkeby
LYNX: 5, # Goerli
}
__to_chain_id_polygon = {
# TODO: Use naming scheme?
POLYGON: 137, # Polygon Mainnet
MUMBAI: 80001, # Polygon Testnet (Mumbai)
}
NETWORKS = tuple(__to_ethereum_chain_id.keys())
ETH_NETWORKS = tuple(__to_chain_id_eth.keys())
POLY_NETWORKS = tuple(__to_chain_id_polygon.keys())
NETWORKS = ETH_NETWORKS + POLY_NETWORKS
class UnrecognizedNetwork(RuntimeError):
pass

View File

@ -136,13 +136,23 @@ def select_client_account(emitter,
return chosen_account
def select_network(emitter: StdoutEmitter, message: Optional[str] = None) -> str:
def select_network(emitter: StdoutEmitter, network_type: str, message: Optional[str] = None) -> str:
"""Interactively select a network from nucypher networks inventory list"""
emitter.message(message=message or str(), color='yellow')
rows = [[n] for n in NetworksInventory.NETWORKS]
emitter.echo(tabulate(rows, showindex='always'))
choice = click.prompt(SELECT_NETWORK, default=0, type=click.IntRange(0, len(NetworksInventory.NETWORKS)-1))
network = NetworksInventory.NETWORKS[choice]
emitter.message(message=message or str(), color="yellow")
if network_type == NetworksInventory.ETH:
network_list = NetworksInventory.ETH_NETWORKS
elif network_type == NetworksInventory.POLYGON:
network_list = NetworksInventory.POLY_NETWORKS
else:
raise(ValueError("Network type must be either 'eth' or 'polygon'"))
rows = [[n] for n in network_list]
emitter.echo(tabulate(rows, showindex="always"))
choice = click.prompt(
SELECT_NETWORK,
default=0,
type=click.IntRange(0, len(rows) - 1),
)
network = network_list[choice]
return network

View File

@ -23,6 +23,7 @@ from eth_typing import ChecksumAddress
from nucypher.blockchain.eth.agents import ContractAgency, PREApplicationAgent
from nucypher.blockchain.eth.constants import NULL_ADDRESS
from nucypher.blockchain.eth.networks import NetworksInventory
from nucypher.blockchain.eth.signers import Signer
from nucypher.cli.actions.auth import get_client_password
from nucypher.cli.actions.select import select_network
@ -103,7 +104,7 @@ def bond(registry_filepath, eth_provider_uri, signer_uri, operator_address, stak
emitter.message('--signer is required', color='red')
raise click.Abort()
if not network:
network = select_network(emitter=emitter)
network = select_network(emitter=emitter, network_type=NetworksInventory.ETH)
signer = Signer.from_signer_uri(signer_uri)
transacting_power = TransactingPower(account=staking_provider, signer=signer)
@ -167,7 +168,7 @@ def unbond(registry_filepath, eth_provider_uri, signer_uri, staking_provider, ne
emitter.message('--signer is required', color='red')
raise click.Abort()
if not network:
network = select_network(emitter=emitter)
network = select_network(emitter=emitter, network_type=NetworksInventory.ETH)
connect_to_blockchain(eth_provider_uri=eth_provider_uri, emitter=emitter)
registry = get_registry(network=network, registry_filepath=registry_filepath)

View File

@ -21,6 +21,7 @@ from pathlib import Path
import click
from nucypher.blockchain.eth.signers.software import ClefSigner
from nucypher.blockchain.eth.networks import NetworksInventory
from nucypher.cli.actions.auth import get_client_password, get_nucypher_password, recover_keystore
from nucypher.cli.actions.configure import (
destroy_configuration,
@ -73,6 +74,7 @@ from nucypher.config.constants import (
from nucypher.crypto.keystore import Keystore
class UrsulaConfigOptions:
__option_name__ = 'config_options'
@ -342,9 +344,9 @@ def init(general_config, config_options, force, config_root, key_material):
if not config_options.federated_only and not config_options.eth_provider_uri:
raise click.BadOptionUsage('--eth-provider', message=click.style("--eth-provider is required to initialize a new ursula.", fg="red"))
if not config_options.federated_only and not config_options.domain:
config_options.domain = select_network(emitter, message="Select Staking Network")
config_options.domain = select_network(emitter, message="Select Staking Network", network_type=NetworksInventory.ETH)
if not config_options.federated_only and not config_options.payment_network:
config_options.payment_network = select_network(emitter, message="Select Payment Network")
config_options.payment_network = select_network(emitter, message="Select Payment Network", network_type=NetworksInventory.POLYGON)
ursula_config = config_options.generate_config(emitter=emitter,
config_root=config_root,
force=force,

View File

@ -487,6 +487,7 @@ class CharacterConfiguration(BaseConfiguration):
blockchain_args = {'filepath': registry_filepath,
'poa': poa,
'eth_provider_uri': eth_provider_uri,
'payment_provider': payment_provider,
'gas_strategy': gas_strategy,
'max_gas_price': max_gas_price}
if any(blockchain_args.values()):

View File

@ -21,16 +21,23 @@ from nucypher.blockchain.eth.networks import NetworksInventory
from nucypher.cli.actions.select import select_network
__NETWORKS = NetworksInventory.NETWORKS
__POLY_NETWORKS = NetworksInventory.POLY_NETWORKS
__ETH_NETWORKS = NetworksInventory.ETH_NETWORKS
@pytest.mark.parametrize('user_input', range(0, len(__NETWORKS)-1))
def test_select_network_cli_action(test_emitter, capsys, mock_stdin, user_input):
@pytest.mark.parametrize('user_input', range(0, len(__ETH_NETWORKS)-1))
def test_select_network_cli_action_eth(test_emitter, capsys, mock_stdin, user_input):
mock_stdin.line(str(user_input))
selection = __NETWORKS[user_input]
result = select_network(emitter=test_emitter)
selection = __ETH_NETWORKS[user_input]
result = select_network(emitter=test_emitter, network_type=NetworksInventory.ETH)
assert result == selection
assert result not in __POLY_NETWORKS
captured = capsys.readouterr()
for name in __NETWORKS:
for name in __ETH_NETWORKS:
assert name in captured.out
assert mock_stdin.empty()
def test_select_network_cli_action_neither(test_emitter):
with pytest.raises(Exception):
select_network(emitter=test_emitter, network_type="FAKE COIN")