From 2626ed4cb3b4c42371c5449f9f1804cf9290b598 Mon Sep 17 00:00:00 2001 From: rafidini Date: Sat, 21 Jan 2023 00:34:16 +0100 Subject: [PATCH] feat(cli): add `get_wallet_address` and `get_box_info` (#20) :sparkles: add CLI feature :art: add local import :bookmark: upgrade to `0.1.10.0` :pushpin: add `click` dependencies ref(issue): https://github.com/mgpai22/ergpy/issues/20 --- ergpy/__init__.py | 8 +++ ergpy/cli.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 6 +- 3 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 ergpy/__init__.py create mode 100644 ergpy/cli.py diff --git a/ergpy/__init__.py b/ergpy/__init__.py new file mode 100644 index 0000000..6c60bce --- /dev/null +++ b/ergpy/__init__.py @@ -0,0 +1,8 @@ +from . import appkit, cli, helper_functions, logs + +__all__ = [ + 'appkit', + 'cli', + 'helper_functions', + 'logs' +] diff --git a/ergpy/cli.py b/ergpy/cli.py new file mode 100644 index 0000000..fe4cd09 --- /dev/null +++ b/ergpy/cli.py @@ -0,0 +1,138 @@ +# Packages +import typing +import click +import ergpy.helper_functions as ergpy_help + + +# Constants +OPTION_NODE_URL: dict = { + 'envvar':'NODE_URL', + 'type':click.STRING, + 'default':'http://213.239.193.208:9052/', + 'help':'MainNet or TestNet' +} +OPTION_WALLET_MNEMONIC: dict = { + 'type': click.STRING, + 'help': 'Wallet mnemonic' +} +OPTION_MNEMONIC_PASSWORD: dict = { + 'type': click.STRING, + 'default': None, + 'help':'Password for the mnemonic' +} +OPTION_AMOUNT: dict = { + 'type': click.INT, + 'help': 'Amount of addresses' +} +OPTION_INDEX = { + 'type': click.INT, + 'help': 'Index' +} +OPTION_SENDER_ADDRESS: dict = { + 'type': click.STRING, + 'help': 'Address of the sender' +} + + +# Utils +def secho_iterable(iterable: typing.Iterable, color: str='blue'): + """Specific echo for iterables (list, tuples...)""" + for element in iterable: + click.secho(f'-> {element}', fg=color) + +def make_ergo(node_url: str) -> typing.Union[ergpy_help.appkit.ErgoAppKit, None]: + """Create the appkit connected with the blockchain""" + try: + ergo = ergpy_help.appkit.ErgoAppKit(node_url=node_url) + click.secho(f'Connection established!', fg='green') + return ergo + except TimeoutError: + click.secho(f'Timeout while connecting to {node_url}', fg='red') + return None + except Exception as e: + click.secho(f'Failed connecting to {node_url} : {e}', fg='red') + return None + + +# Main command +@click.group() +def cli(): + """ + ergpy official CLI + """ + pass + +# Get subcommands +@cli.group() +def get(): + """Retrieve information about wallet address, box info...""" + pass + +# Send subcommands +@cli.group() +def send(): + """simple send or send token""" + click.echo('Send what ?') + +# Create subcommands +@cli.group() +def create(): + """create issuer box, NFT, token...""" + click.echo('Create what ?') + + +# Commands +@click.command() +@click.option('--amount', **OPTION_AMOUNT) +@click.option('--wallet-mnemonic', **OPTION_WALLET_MNEMONIC) +@click.option('--mnemonic-password', **OPTION_MNEMONIC_PASSWORD) +@click.option('--node-url', **OPTION_NODE_URL) +def wallet_address(node_url: str, amount: int, wallet_mnemonic: str, mnemonic_password: str = None): + """Retrieve informations about (a) wallet address(es).""" + click.secho(f'You selected : Get (a) wallet address(es)', fg='blue') + click.echo(f'Connecting to blockchain @ {node_url}') + + # Trying to connect w/ blockchain + ergo: ergpy_help.appkit.ErgoAppKit = make_ergo(node_url=node_url) + if ergo is None: + return + + # Launch operation + click.echo(f'Retrieving wallet address(es)') + try: + wallet_address = ergpy_help.get_wallet_address(ergo, amount, wallet_mnemonic, mnemonic_password) + click.echo(f'Wallet address(es)') + secho_iterable(wallet_address) + except Exception as e: + click.secho(f'Failed to retrieve wallet address(es) : {e}', fg='red') + +@click.command() +@click.option('--node-url', **OPTION_NODE_URL) +@click.option('--index', **OPTION_INDEX) +@click.option('--sender-address', **OPTION_SENDER_ADDRESS) +def box_info(node_url: str, index: int, sender_address: str, tokens: list = None): + """Retrieve informations about box.""" + click.secho(f'You selected : Get box informations', fg='blue') + click.echo(f'Connecting to blockchain @ {node_url}') + + # Trying to connect w/ blockchain + ergo: ergpy_help.appkit.ErgoAppKit = make_ergo(node_url=node_url) + if ergo is None: + return + + # Launch operation + click.echo(f'Retrieving box informations') + try: + box_informations = ergpy_help.get_box_info(ergo, index, sender_address, tokens) + click.echo(f'Box informations') + secho_iterable(box_informations) + except Exception as e: + click.secho(f'Failed to retrieve wallet address(es) : {e}', fg='red') + +def handle_cli(): + # Registering `get` commands + get.add_command(wallet_address) + get.add_command(box_info) + + # CLI + cli() diff --git a/setup.py b/setup.py index 1516147..5658920 100644 --- a/setup.py +++ b/setup.py @@ -27,12 +27,13 @@ JPype1>=1.3.0 requests>=2.27.1 stubgenj>=0.2.5 +click==8.1.3 """ # Metadata configuration setup( name='ergpy', - version='0.1.9.0', + version='0.1.10.0', description='Python-jvm wrapper for interacting with the Ergo Blockchain', long_description=long_description, long_description_content_type='text/markdown', @@ -42,5 +43,6 @@ keywords='appkit', include_package_data=True, packages=['ergpy'], - install_requires=packages.split('\n') + install_requires=packages.split('\n'), + entry_points={'console_scripts': ['ergpy = ergpy.cli:handle_cli']} )