Refactor and fix style

pull/2634/head
Piotr Roslaniec 2021-04-22 09:38:53 +02:00
parent f14214d5cb
commit 1303076a76
5 changed files with 60 additions and 56 deletions

View File

@ -192,8 +192,9 @@ class BaseEconomics:
self.minimum_allowed_locked = minimum_allowed_locked
self.maximum_allowed_locked = maximum_allowed_locked
self.minimum_worker_periods = minimum_worker_periods
self.genesis_seconds_per_period = genesis_hours_per_period * 60 * 60 # Genesis seconds in single period
self.seconds_per_period = hours_per_period * 60 * 60 # Seconds in single period
self.genesis_seconds_per_period = genesis_hours_per_period * 60 * 60 # Genesis seconds in a single period
self.seconds_per_period = hours_per_period * 60 * 60 # Seconds in a single period
self.days_per_period = hours_per_period // 24 # Days in a single period
#
# Adjudicator

View File

@ -118,7 +118,8 @@ from nucypher.cli.painting.staking import (
paint_stakes,
paint_staking_accounts,
paint_staking_confirmation,
paint_all_stakes, paint_staking_rewards
paint_all_stakes,
paint_staking_rewards
)
from nucypher.cli.painting.transactions import paint_receipt_summary
from nucypher.cli.types import (
@ -1416,12 +1417,12 @@ def rewards():
@group_staker_options
@option_config_file
@group_general_config
@click.option('--num_past_periods', help="Number of past periods for which to calculate rewards", type=click.INT)
def show_rewards(general_config, staker_options, config_file, num_past_periods):
@click.option('--periods', help="Number of past periods for which to calculate rewards", type=click.INT)
def show_rewards(general_config, staker_options, config_file, periods):
"""Show staking rewards."""
if num_past_periods and num_past_periods < 0:
raise click.BadOptionUsage(option_name='--num_past_periods', message='--num_past_periods must positive')
if periods and periods < 0:
raise click.BadOptionUsage(option_name='--periods', message='--periods must positive')
emitter = setup_emitter(general_config)
stakeholder = staker_options.create_character(emitter, config_file)
@ -1431,4 +1432,4 @@ def show_rewards(general_config, staker_options, config_file, num_past_periods):
blockchain = staker_options.get_blockchain()
staking_agent = stakeholder.staker.staking_agent
paint_staking_rewards(stakeholder, blockchain, emitter, num_past_periods, staking_address, staking_agent)
paint_staking_rewards(stakeholder, blockchain, emitter, periods, staking_address, staking_agent)

View File

@ -276,48 +276,49 @@ Minimum acceptable fee rate (set by staker for their associated worker):
emitter.echo(rate_payload)
def paint_staking_rewards(stakeholder, blockchain, emitter, num_past_periods, staking_address, staking_agent):
if num_past_periods:
economics = stakeholder.staker.economics
seconds_per_period = economics.seconds_per_period
days_per_period = economics.hours_per_period // 24
current_period = staking_agent.get_current_period()
from_period = current_period - num_past_periods
latest_block = blockchain.client.block_number
from_block = estimate_block_number_for_period(period=from_period,
seconds_per_period=seconds_per_period,
latest_block=latest_block)
argument_filters = {'staker': staking_address}
event_type = staking_agent.contract.events['Minted']
entries = event_type.getLogs(fromBlock=from_block,
toBlock='latest',
argument_filters=argument_filters)
rows = []
rewards_total = 0
for event_record in entries:
token_reward = NU(event_record['args']['value'], 'NuNit').to_tokens()
period = event_record['args']['period']
date = datetime_at_period(period, seconds_per_period, start_of_period=True)
rows.append([
date.local_datetime().strftime("%b %d %Y"),
int(event_record['block_number']),
int(period),
token_reward,
])
rewards_total += token_reward
periods_as_days = math.floor(days_per_period * num_past_periods)
if rows:
emitter.echo(message=TOKEN_REWARD_PAST_HEADER.format(periods=num_past_periods, days=periods_as_days))
emitter.echo(tabulate.tabulate(rows,
headers=REWARDS_TABLE_COLUMNS,
tablefmt="fancy_grid",
floatfmt=".18g"))
emitter.echo(message=TOKEN_REWARD_PAST.format(reward_amount=rewards_total))
else:
emitter.echo(TOKEN_REWARD_NOT_FOUND)
else:
def paint_staking_rewards(stakeholder, blockchain, emitter, past_periods, staking_address, staking_agent):
if not past_periods:
reward_amount = stakeholder.staker.calculate_staking_reward().to_tokens()
emitter.echo(message=TOKEN_REWARD_CURRENT.format(reward_amount=reward_amount))
return
economics = stakeholder.staker.economics
seconds_per_period = economics.seconds_per_period
current_period = staking_agent.get_current_period()
from_period = current_period - past_periods
latest_block = blockchain.client.block_number
from_block = estimate_block_number_for_period(period=from_period,
seconds_per_period=seconds_per_period,
latest_block=latest_block)
argument_filters = {'staker': staking_address}
event_type = staking_agent.contract.events['Minted']
entries = event_type.getLogs(fromBlock=from_block,
toBlock='latest',
argument_filters=argument_filters)
rows = []
rewards_total = 0
for event_record in entries:
token_reward = NU(event_record['args']['value'], 'NuNit').to_tokens()
period = event_record['args']['period']
date = datetime_at_period(period, seconds_per_period, start_of_period=True)
rows.append([
date.local_datetime().strftime("%b %d %Y"),
int(event_record['block_number']),
int(period),
token_reward,
])
rewards_total += token_reward
if not rows:
emitter.echo(TOKEN_REWARD_NOT_FOUND)
return
periods_as_days = math.floor(economics.days_per_period * past_periods)
emitter.echo(message=TOKEN_REWARD_PAST_HEADER.format(periods=past_periods, days=periods_as_days))
emitter.echo(tabulate.tabulate(rows,
headers=REWARDS_TABLE_COLUMNS,
tablefmt="fancy_grid",
floatfmt=".18g"))
emitter.echo(message=TOKEN_REWARD_PAST.format(reward_amount=rewards_total))

View File

@ -42,6 +42,7 @@ class WorklockParameters(Tuple):
class StakingEscrowParameters(Tuple):
seconds_per_period: int
days_per_period: int
minting_coefficient: int
lock_duration_coefficient_1: int
lock_duration_coefficient_2: int

View File

@ -1435,7 +1435,7 @@ def test_show_rewards(click_runner, surrogate_stakers, mock_staking_agent, mocke
@pytest.mark.usefixtures("test_registry_source_manager", "patch_stakeholder_configuration")
def test_show_rewards_for_period(click_runner, surrogate_stakers, mock_staking_agent, token_economics, mocker):
num_past_periods = 30
periods = 30
periods_per_day = token_economics.hours_per_period / 24
seconds_per_period = token_economics.seconds_per_period
latest_block = 100_000_000
@ -1468,12 +1468,12 @@ def test_show_rewards_for_period(click_runner, surrogate_stakers, mock_staking_a
'--provider', MOCK_PROVIDER_URI,
'--network', TEMPORARY_DOMAIN,
'--staking-address', surrogate_stakers[0],
'--num_past_periods', num_past_periods)
'--periods', periods)
result = click_runner.invoke(stake, collection_args, catch_exceptions=False)
assert result.exit_code == 0
periods_as_days = math.floor(periods_per_day*num_past_periods)
assert TOKEN_REWARD_PAST_HEADER.format(periods=num_past_periods, days=periods_as_days) in result.output
periods_as_days = math.floor(periods_per_day*periods)
assert TOKEN_REWARD_PAST_HEADER.format(periods=periods, days=periods_as_days) in result.output
for header in REWARDS_TABLE_COLUMNS:
assert header in result.output
for event in events:
@ -1496,7 +1496,7 @@ def test_show_rewards(click_runner, surrogate_stakers, mock_staking_agent, mocke
'--provider', MOCK_PROVIDER_URI,
'--network', TEMPORARY_DOMAIN,
'--staking-address', surrogate_stakers[0],
'--num_past_periods', 10)
'--periods', 10)
result = click_runner.invoke(stake, collection_args, catch_exceptions=False)
assert result.exit_code == 0