def filter_branches(branches: List[Branch], author: str = None, older_than: int = None, newer_than: int = None, name: str = None, merged: bool = None) -> List[Branch]: filters = [] if author is not None: filters.append(lambda b: b.last_commit.author == author) if merged is not None: filters.append(lambda b: b.merged == merged) if older_than is not None: date_cutoff = maya.now() - maya.timedelta(days=older_than) date_cutoff = date_cutoff.datetime() filters.append(lambda b: b.last_commit.created_on <= date_cutoff) if newer_than is not None: date_cutoff = maya.now() + maya.timedelta(days=newer_than) date_cutoff = date_cutoff.datetime() filters.append(lambda b: b.last_commit.created_on >= date_cutoff) if name is not None: regex = re.compile(name) filters.append(lambda b: regex.match(b.name)) return [ branch for branch in branches if all(f(branch) for f in filters) ] if filters else branches
def datetime_at_period(period: int) -> maya.MayaDT: now = maya.now() current_period = datetime_to_period(datetime=now) delta_periods = period - current_period # + if delta_periods: target_period = now + maya.timedelta(days=delta_periods) # - else: target_period = now - maya.timedelta(days=delta_periods) return target_period
def time_travel(self, hours: int = None, seconds: int = None, periods: int = None): """ Wait the specified number of wait_hours by comparing block timestamps and mines a single block. """ more_than_one_arg = sum(map(bool, (hours, seconds, periods))) > 1 if more_than_one_arg: raise ValueError("Specify hours, seconds, or lock_periods, not a combination") if periods: duration = (TokenEconomics.hours_per_period * periods) * (60 * 60) base = TokenEconomics.hours_per_period * 60 * 60 elif hours: duration = hours * (60*60) base = 60 * 60 elif seconds: duration = seconds base = 1 else: raise ValueError("Specify either hours, seconds, or lock_periods.") now = self.w3.eth.getBlock(block_identifier='latest').timestamp end_timestamp = ((now+duration)//base) * base self.w3.eth.web3.testing.timeTravel(timestamp=end_timestamp) self.w3.eth.web3.testing.mine(1) delta = maya.timedelta(seconds=end_timestamp-now) self.log.info(f"Time traveled {delta} " f"| period {epoch_to_period(epoch=end_timestamp)} " f"| epoch {end_timestamp}")
def datetime_at_period(period: int) -> maya.MayaDT: """Returns the datetime object at a given period, future, or past.""" now = maya.now() current_period = datetime_to_period(datetime=now) delta_periods = period - current_period # + if delta_periods: target_period = now + maya.timedelta(days=delta_periods) # - else: target_period = now - maya.timedelta(days=delta_periods) return target_period
def paint_input_allocation_file(emitter, allocations) -> None: num_allocations = len(allocations) emitter.echo(f"Found {num_allocations} allocations:") emitter.echo(f"\n{'='*46} STAGED ALLOCATIONS {'='*45}", bold=True) emitter.echo(f"\n{'Beneficiary':42} | {'Name':20} | {'Duration':20} | {'Amount':20}", bold=True) emitter.echo("-"*(42+3+20+3+20+3+20), bold=True) for allocation in allocations: beneficiary = allocation['beneficiary_address'] amount = str(NU.from_nunits(allocation['amount'])) duration = (maya.now() + maya.timedelta(seconds=allocation['duration_seconds'])).slang_date() name = allocation.get('name', 'No name provided') emitter.echo(f"{beneficiary} | {name:20} | {duration:20} | {amount:20}") emitter.echo()
def datetime_at_period(period: int, seconds_per_period: int, start_of_period: bool = False) -> maya.MayaDT: """ Returns the datetime object at a given period, future, or past. If start_of_period, the datetime object represents the first second of said period. """ if start_of_period: datetime_at_start_of_period = maya.MayaDT(epoch=period_to_epoch(period, seconds_per_period)) return datetime_at_start_of_period else: now = maya.now() current_period = datetime_to_period(datetime=now, seconds_per_period=seconds_per_period) delta_periods = period - current_period # + if delta_periods: target_datetime = now + maya.timedelta(days=delta_periods) # - else: target_datetime = now - maya.timedelta(days=delta_periods) return target_datetime
def yesterday(today): return today - maya.timedelta(days=1)
def tomorrow(today): return today + maya.timedelta(days=1)
def future_locked_tokens_bar_chart(future_locked_tokens: dict, past_locked_tokens: dict, node_history: dict): future_periods = len(future_locked_tokens) now = maya.now() nodes_history = list(node_history.values()) # eg. Jan-23-2020 date_format = '%b-%d-%Y' past_period_range = [ d.strftime(date_format) for d in past_locked_tokens.keys() ] future_period_range = list( (now + maya.timedelta(days=p)).datetime().strftime(date_format) for p in range(1, future_periods + 1)) period_range = past_period_range + future_period_range past_token_values = [float(v) for v in past_locked_tokens.values()] future_locked_tokens, future_num_stakers = map( list, zip(*future_locked_tokens.values())) locked_tokens = past_token_values + future_locked_tokens x_coord_today = now.datetime().strftime(date_format) plots = [ # # Stakes # go.Bar(textposition='auto', x=period_range, y=locked_tokens, name='Stake (NU)', marker=go.bar.Marker(color=locked_tokens, colorscale='Viridis')), # # Known Nodes (past and future) # go.Scatter(mode='lines+markers', x=past_period_range, y=nodes_history, name='Past Stakers', yaxis='y2', xaxis='x', marker={'color': 'rgb(0, 163, 139)'}), go.Scatter(mode='lines+markers', x=future_period_range, y=future_num_stakers, name='Future Stakers', yaxis='y2', xaxis='x', marker={'color': 'rgb(0, 153, 239)'}), # Today Vertical Line go.Scatter( x=[x_coord_today, x_coord_today], y=[0, max(locked_tokens) * 1.1], # point slightly above actual max name='', text=['', 'Today'], mode='lines+text', textposition='top center', hoverinfo='none', line=dict( color='Red', width=4, dash='dashdot', ), textfont=dict(color='Red', )) ] layout = go.Layout( title= f'Stake and Stakers | {period_range[0].capitalize()} - {period_range[-1]}', xaxis={'title': 'Days'}, yaxis={ 'title': 'NU Tokens', 'rangemode': 'tozero', 'showgrid': False }, yaxis2={ 'title': f'Stakers', 'overlaying': 'y', 'side': 'right', 'rangemode': 'tozero', 'showgrid': False }, showlegend=False, legend=go.layout.Legend(x=0, y=1.0), font=dict(family="monospace", size=11, color="slategrey"), paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', autosize=True, width=None, height=None) fig = go.Figure(data=plots, layout=layout) fig.update_traces(marker_line_width=0.1, opacity=1) fig.update_layout(bargap=0.15) graph = dcc.Graph(figure=fig, id='locked-stake', config=GRAPH_CONFIG, style={'width': '100%'}) return graph