Skip to content

Commit

Permalink
feat(cli): add get_wallet_address and get_box_info (#20)
Browse files Browse the repository at this point in the history
✨ add CLI feature
🎨 add local import
🔖 upgrade to `0.1.10.0`
📌 add `click` dependencies

ref(issue): #20
  • Loading branch information
rafidini committed Jan 20, 2023
1 parent f9bd479 commit 2626ed4
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
8 changes: 8 additions & 0 deletions ergpy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from . import appkit, cli, helper_functions, logs

__all__ = [
'appkit',
'cli',
'helper_functions',
'logs'
]
138 changes: 138 additions & 0 deletions ergpy/cli.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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']}
)

0 comments on commit 2626ed4

Please sign in to comment.