def divide(general_config, transacting_staker_options, config_file, force, value, lock_periods, index): """ Create a new stake from part of an existing one. """ # Setup emitter = _setup_emitter(general_config) STAKEHOLDER = transacting_staker_options.create_character( emitter, config_file) blockchain = transacting_staker_options.get_blockchain() economics = STAKEHOLDER.economics action_period = STAKEHOLDER.staking_agent.get_current_period() client_account, staking_address = handle_client_account_for_staking( emitter=emitter, stakeholder=STAKEHOLDER, staking_address=transacting_staker_options.staker_options. staking_address, individual_allocation=STAKEHOLDER.individual_allocation, force=force) # Dynamic click types (Economics) min_locked = economics.minimum_allowed_locked stake_value_range = click.FloatRange( min=NU.from_nunits(min_locked).to_tokens(), clamp=False) if transacting_staker_options.staker_options.staking_address and index is not None: # 0 is valid. STAKEHOLDER.stakes = StakeList( registry=STAKEHOLDER.registry, checksum_address=transacting_staker_options.staker_options. staking_address) STAKEHOLDER.stakes.refresh() current_stake = STAKEHOLDER.stakes[index] else: current_stake = select_stake(stakeholder=STAKEHOLDER, emitter=emitter, divisible=True, staker_address=client_account) # # Stage Stake # # Value if not value: min_allowed_locked = NU.from_nunits( STAKEHOLDER.economics.minimum_allowed_locked) max_divide_value = max(min_allowed_locked, current_stake.value - min_allowed_locked) value = click.prompt( f"Enter target value ({min_allowed_locked} - {str(max_divide_value)})", type=stake_value_range) value = NU(value, 'NU') # Duration if not lock_periods: max_extension = MAX_UINT16 - current_stake.final_locked_period divide_extension_range = click.IntRange(min=1, max=max_extension, clamp=False) extension = click.prompt(f"Enter number of periods to extend", type=divide_extension_range) else: extension = lock_periods if not force: issue_stake_suggestions(lock_periods=extension, value=value) painting.paint_staged_stake_division(emitter=emitter, stakeholder=STAKEHOLDER, original_stake=current_stake, target_value=value, extension=extension) click.confirm("Publish stake division to the blockchain?", abort=True) # Authenticate password = transacting_staker_options.get_password(blockchain, client_account) # Consistency check to prevent the above agreement from going stale. last_second_current_period = STAKEHOLDER.staking_agent.get_current_period() if action_period != last_second_current_period: emitter.echo( "Current period advanced before stake division was broadcasted. Please try again.", red='red') raise click.Abort # Execute STAKEHOLDER.assimilate(checksum_address=current_stake.staker_address, password=password) modified_stake, new_stake = STAKEHOLDER.divide_stake( stake_index=current_stake.index, target_value=value, additional_periods=extension) emitter.echo('Successfully divided stake', color='green', verbosity=1) paint_receipt_summary(emitter=emitter, receipt=new_stake.receipt, chain_name=blockchain.client.chain_name) # Show the resulting stake list painting.paint_stakes(emitter=emitter, stakeholder=STAKEHOLDER)
def create(general_config, transacting_staker_options, config_file, force, value, lock_periods): """ Initialize a new stake. """ emitter = _setup_emitter(general_config) STAKEHOLDER = transacting_staker_options.create_character( emitter, config_file) blockchain = transacting_staker_options.get_blockchain() economics = STAKEHOLDER.economics client_account, staking_address = handle_client_account_for_staking( emitter=emitter, stakeholder=STAKEHOLDER, staking_address=transacting_staker_options.staker_options. staking_address, individual_allocation=STAKEHOLDER.individual_allocation, force=force) # Dynamic click types (Economics) min_locked = economics.minimum_allowed_locked stake_value_range = click.FloatRange( min=NU.from_nunits(min_locked).to_tokens(), clamp=False) stake_duration_range = click.IntRange(min=economics.minimum_locked_periods, clamp=False) # # Stage Stake # if not value: token_balance = NU.from_nunits( STAKEHOLDER.token_agent.get_balance(staking_address)) lower_limit = NU.from_nunits( STAKEHOLDER.economics.minimum_allowed_locked) upper_limit = min( token_balance, NU.from_nunits(STAKEHOLDER.economics.maximum_allowed_locked)) value = click.prompt( f"Enter stake value in NU " f"({lower_limit} - {upper_limit})", type=stake_value_range, default=upper_limit.to_tokens()) value = NU.from_tokens(value) if not lock_periods: min_locktime = STAKEHOLDER.economics.minimum_locked_periods default_locktime = STAKEHOLDER.economics.maximum_rewarded_periods max_locktime = MAX_UINT16 - STAKEHOLDER.staking_agent.get_current_period( ) prompt = f"Enter stake duration ({min_locktime} - {max_locktime})" lock_periods = click.prompt(prompt, type=stake_duration_range, default=default_locktime) start_period = STAKEHOLDER.staking_agent.get_current_period() + 1 unlock_period = start_period + lock_periods # # Review and Publish # if not force: issue_stake_suggestions(value=value, lock_periods=lock_periods) painting.paint_staged_stake(emitter=emitter, stakeholder=STAKEHOLDER, staking_address=staking_address, stake_value=value, lock_periods=lock_periods, start_period=start_period, unlock_period=unlock_period) confirm_staged_stake(staker_address=staking_address, value=value, lock_periods=lock_periods) # Last chance to bail click.confirm("Publish staged stake to the blockchain?", abort=True) # Authenticate password = transacting_staker_options.get_password(blockchain, client_account) # Consistency check to prevent the above agreement from going stale. last_second_current_period = STAKEHOLDER.staking_agent.get_current_period() if start_period != last_second_current_period + 1: emitter.echo( "Current period advanced before stake was broadcasted. Please try again.", color='red') raise click.Abort # Authenticate and Execute STAKEHOLDER.assimilate(checksum_address=client_account, password=password) new_stake = STAKEHOLDER.initialize_stake(amount=value, lock_periods=lock_periods) painting.paint_staking_confirmation(emitter=emitter, staker=STAKEHOLDER, new_stake=new_stake)