Divide stake CLI

pull/805/head
Kieran Prasch 2019-01-20 19:14:43 -08:00
parent 7b85846256
commit be885c237c
No known key found for this signature in database
GPG Key ID: 199AB839D4125A62
2 changed files with 83 additions and 28 deletions

View File

@ -36,9 +36,7 @@ from nucypher.cli.types import (
EIP55_CHECKSUM_ADDRESS,
NETWORK_PORT,
EXISTING_READABLE_FILE,
EXISTING_WRITABLE_DIRECTORY,
STAKE_VALUE,
STAKE_DURATION
EXISTING_WRITABLE_DIRECTORY
)
from nucypher.config.characters import UrsulaConfiguration
from nucypher.utilities.logging import (
@ -71,8 +69,8 @@ from nucypher.utilities.logging import (
@click.option('--no-registry', help="Skip importing the default contract registry", is_flag=True)
@click.option('--registry-filepath', help="Custom contract registry filepath", type=EXISTING_READABLE_FILE)
@click.option('--checksum-address', type=EIP55_CHECKSUM_ADDRESS)
@click.option('--value', help="Token value of stake", type=STAKE_VALUE)
@click.option('--duration', help="Period duration of stake", type=STAKE_DURATION)
@click.option('--value', help="Token value of stake", type=click.INT)
@click.option('--duration', help="Period duration of stake", type=click.INT)
@click.option('--index', help="A specific stake index to resume", type=click.INT)
@click.option('--list', '-l', 'list_', help="List all blockchain stakes", is_flag=True)
@nucypher_click_config
@ -428,40 +426,61 @@ def ursula(click_config,
elif action == 'divide-stake':
"""Divide an existing stake by specifying the new target value and end period"""
stakes = ursula_config.miner_agent.get_all_stakes(miner_address=checksum_address)
stakes = list(ursula_config.miner_agent.get_all_stakes(miner_address=checksum_address))
if len(stakes) == 0:
raise RuntimeError("There are no active stakes for {}".format(checksum_address))
if not index:
if index is None:
for selection_index, stake_info in enumerate(stakes):
click.echo("{} ....... {}".format(selection_index, stake_info))
index = click.prompt("Select a stake to divide", type=click.INT)
target_value = click.prompt("Enter new target value", type=click.INT)
extension = click.prompt("Enter number of periods to extend", type=click.INT)
if not value:
target_value = click.prompt("Enter new target value", type=click.INT)
else:
target_value = value
click.echo("""
Current Stake: {}
if not duration:
extension = click.prompt("Enter number of periods to extend", type=click.INT)
else:
extension = duration
New target value {}
New end period: {}
if not force:
click.echo("""
Current Stake: {}
""".format(stakes[index],
target_value,
target_value + extension))
New target value {}
New end period: {}
""".format(stakes[index],
target_value,
target_value + extension))
click.confirm("Is this correct?", abort=True)
miner = Miner(is_me=True,
checksum_address=ursula_config.checksum_public_address,
blockchain=ursula_config.blockchain)
txhash_bytes = miner.divide_stake(stake_index=index,
target_value=value,
additional_periods=duration)
if not quiet:
click.secho('Successfully divided stake', fg='green')
click.secho(f'Transaction Hash ........... {txhash_bytes.hex()}')
click.confirm("Is this correct?", abort=True)
ursula_config.miner_agent.divide_stake(miner_address=checksum_address,
stake_index=index,
value=value,
periods=extension)
return
elif action == 'collect-reward': # TODO: Implement
elif action == 'collect-reward':
"""Withdraw staking reward to the specified wallet address"""
# click.confirm("Send {} to {}?".format)
# ursula_config.miner_agent.collect_staking_reward(collector_address=address)
raise NotImplementedError
miner = Miner(checksum_address=checksum_address, blockchain=ursula_config.blockchain, is_me=True)
if not force:
click.confirm(f"Send {miner.calculate_reward()} to {ursula_config.checksum_public_address}?")
miner.collect_staking_reward()
miner.collect_policy_reward()
else:
raise click.BadArgumentUsage("No such argument {}".format(action))

View File

@ -13,6 +13,9 @@ from nucypher.utilities.sandbox.constants import (
MOCK_REGISTRY_FILEPATH, TESTER_DOMAIN)
STAKE_VALUE = MIN_ALLOWED_LOCKED * 2
def test_initialize_custom_blockchain_configuration(deployed_blockchain, custom_filepath, click_runner):
blockchain, deployer_address = deployed_blockchain
@ -92,7 +95,7 @@ def test_init_ursula_stake(click_runner, deployed_blockchain):
blockchain, deployer_address = deployed_blockchain
stake_args = ('ursula', 'stake',
'--value', MIN_ALLOWED_LOCKED,
'--value', STAKE_VALUE,
'--duration', MIN_LOCKED_PERIODS,
'--dev',
'--poa',
@ -110,7 +113,7 @@ def test_init_ursula_stake(click_runner, deployed_blockchain):
stake = miner.stakes[0]
start, end, value = stake
assert (abs(end-start)+1) == MIN_LOCKED_PERIODS
assert value == MIN_ALLOWED_LOCKED
assert value == STAKE_VALUE
def test_list_ursula_stakes(click_runner, deployed_blockchain):
@ -125,4 +128,37 @@ def test_list_ursula_stakes(click_runner, deployed_blockchain):
result = click_runner.invoke(nucypher_cli, stake_args, catch_exceptions=False)
assert result.exit_code == 0
assert str(MIN_ALLOWED_LOCKED) in result.output
assert str(STAKE_VALUE) in result.output
def test_ursula_divide_stakes(click_runner, deployed_blockchain):
blockchain, _deployer_address = deployed_blockchain
deployer_address, staking_participant, *everyone_else = blockchain.interface.w3.eth.accounts
divide_args = ('ursula', 'divide-stake',
'--checksum-address', deployer_address,
'--dev',
'--poa',
'--force',
'--index', 0,
'--value', MIN_ALLOWED_LOCKED,
'--duration', 10,
'--provider-uri', TEST_PROVIDER_URI)
result = click_runner.invoke(nucypher_cli,
divide_args,
catch_exceptions=False,
env=dict(NUCYPHER_KEYRING_PASSWORD=INSECURE_DEVELOPMENT_PASSWORD))
assert result.exit_code == 0
stake_args = ('ursula', 'stake', '--list',
'--checksum-address', deployer_address,
'--dev',
'--poa',
'--provider-uri', TEST_PROVIDER_URI)
result = click_runner.invoke(nucypher_cli, stake_args, catch_exceptions=False)
assert result.exit_code == 0
miner = Miner(checksum_address=deployer_address, blockchain=blockchain, is_me=True)
assert len(miner.stakes) == 2