Integrate click types, Stakes, and NU values in existing CLI tooling.

pull/822/head
Kieran Prasch 2019-03-14 14:29:29 -07:00
parent 79ca05605d
commit 9865eabdf5
No known key found for this signature in database
GPG Key ID: 199AB839D4125A62
4 changed files with 31 additions and 28 deletions

View File

@ -190,10 +190,13 @@ class Stake:
self.value = value
self.start_period = start_period
self.end_period = end_period
self.duration = self.end_period - self.start_period
# Internals
self.start_datetime = period_to_datetime(period=start_period)
self.end_datetime = period_to_datetime(period=end_period)
self.start_datetime = datetime_at_period(period=start_period)
self.end_datetime = datetime_at_period(period=end_period)
self.duration_delta = self.end_datetime - self.start_datetime
self.miner_agent = owner.miner_agent
def __repr__(self):

View File

@ -345,7 +345,7 @@ def ursula(click_config,
# Selection
if index is None:
painting.paint_stakes(stakes=URSULA.stakes)
index = click.prompt("Select a stake to divide", type=click.INT)
index = click.prompt("Select a stake to divide", type=click.IntRange(min=0, max=len(URSULA.stakes)-1))
# Lookup the stake
current_stake = URSULA.stakes[index]
@ -364,7 +364,7 @@ def ursula(click_config,
if not force:
painting.paint_staged_stake_division(ursula=URSULA,
original_index=index,
original_stake_info=current_stake,
original_stake=current_stake,
target_value=value,
extension=extension)

View File

@ -15,14 +15,13 @@ You should have received a copy of the GNU Affero General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
import os
from typing import Tuple
import click
import maya
from constant_sorrow.constants import NO_KNOWN_NODES
from web3 import Web3
from typing import Tuple
from nucypher.blockchain.eth.utils import datetime_at_period
from nucypher.blockchain.eth.utils import datetime_at_period, NU
from nucypher.characters.banners import NUCYPHER_BANNER
from nucypher.characters.control.emitters import StdoutEmitter
from nucypher.config.constants import SEEDNODES
@ -234,50 +233,45 @@ or start your Ursula node by running 'nucypher ursula run'.
''', fg='green')
def prettify_stake(stake_index: int, stake_info: Tuple[int, int, str]) -> str:
start, expiration, stake_wei = stake_info
def prettify_stake(stake_index: int, stake) -> str:
stake_nu = NU(int(stake_wei), 'NUWei')
start_datetime = str(datetime_at_period(period=start).slang_date())
expiration_datetime = str(datetime_at_period(period=expiration).slang_date())
duration = expiration - start
start_datetime = str(stake.start_datetime.slang_date())
expiration_datetime = str(stake.end_datetime.slang_date())
duration = stake.duration
pretty_periods = f'{duration} periods {"." if len(str(duration)) == 2 else ""}'
pretty = f'| {stake_index} | {pretty_periods} | {start_datetime} .. | {expiration_datetime} ... | {stake_nu}'
pretty = f'| {stake_index} | {pretty_periods} | {start_datetime} .. | {expiration_datetime} ... | {str(stake.value)}'
return pretty
def paint_stakes(stakes):
header = f'| # | Duration | Enact | Expiration | Value '
breaky = f'| - | ------------ | --------- | -----------| ----- '
header = f'| # | Duration | Enact | Expiration | Value '
breaky = f'| - | ------------ | ----------- | -----------| ----- '
click.secho(header, bold=True)
click.secho(breaky, bold=True)
for index, stake_info in enumerate(stakes):
row = prettify_stake(stake_index=index, stake_info=stake_info)
for index, stake in enumerate(stakes):
row = prettify_stake(stake_index=index, stake=stake)
click.echo(row)
return
def paint_staged_stake_division(ursula,
original_index,
original_stake_info,
original_stake,
target_value,
extension):
original_start, original_expiration, original_stake_wei = original_stake_info
new_end_period = original_expiration + extension
new_duration = new_end_period - original_start
new_end_period = original_stake.end_period + extension
new_duration = new_end_period - original_stake.start_period
division_message = f"""
{ursula}
~ Original Stake: {prettify_stake(stake_index=original_index, stake_info=original_stake_info)}
~ Original Stake: {prettify_stake(stake_index=original_index, stake=original_stake)}
"""
paint_staged_stake(ursula=ursula,
stake_value=target_value,
duration=new_duration,
start_period=original_start,
start_period=original_stake.start_period,
end_period=new_end_period,
division_message=division_message)

View File

@ -22,6 +22,7 @@ from eth_utils import is_checksum_address
from nucypher.blockchain.eth.constants import MIN_ALLOWED_LOCKED, MAX_MINTING_PERIODS, MIN_LOCKED_PERIODS, \
MAX_ALLOWED_LOCKED
from nucypher.blockchain.eth.utils import NU
class ChecksumAddress(click.ParamType):
@ -44,12 +45,17 @@ class IPv4Address(click.ParamType):
else:
return value
# Staking
STAKE_DURATION = click.IntRange(min=MIN_LOCKED_PERIODS, max=MAX_MINTING_PERIODS, clamp=False)
STAKE_EXTENSION = click.IntRange(min=1, max=MAX_MINTING_PERIODS, clamp=False)
STAKE_VALUE = click.IntRange(min=MIN_ALLOWED_LOCKED, max=MAX_ALLOWED_LOCKED, clamp=False)
STAKE_VALUE = click.IntRange(min=NU(MIN_ALLOWED_LOCKED, 'NUWei').to_tokens(),
max=NU(MAX_ALLOWED_LOCKED, 'NUWei').to_tokens(), clamp=False)
# Filesystem
EXISTING_WRITABLE_DIRECTORY = click.Path(exists=True, dir_okay=True, file_okay=False, writable=True)
EXISTING_READABLE_FILE = click.Path(exists=True, dir_okay=False, file_okay=True, readable=True)
# Network
NETWORK_PORT = click.IntRange(min=0, max=65535, clamp=False)
IPV4_ADDRESS = IPv4Address()
EIP55_CHECKSUM_ADDRESS = ChecksumAddress()