async def publish_price(self, force: bool = False) -> None: """ Publish price feed once. :param bool force: force-publish price even if update is not needed """ # flag variable which determine should we actually update price or not need_publish = False # calculate prices if self.price_source == PriceSource.graphene: price = await self.calc_price_gbg_golos_bitshares() elif self.price_source == PriceSource.kuna: price = await self.calc_price_kuna() else: raise ValueError("Unknown price source") witness_data = Witness(self.witness) old_price = self.get_witness_pricefeed(witness_data) median_price = self.converter.sbd_median_price() log.info('Current conversion price: {:.3f}'.format(median_price)) # apply correction if k defined if self.correction != 1: price = price * self.correction log.info('Price after correction: {:.3f}'.format(price)) # check whether our price is too old last_price_update_too_old = self.is_last_price_too_old( witness_data, self.max_age) if last_price_update_too_old: log.info( 'Our last price update older than max_age, forcing update') need_publish = True # check for price difference between our old price and new price diff_rel = abs((old_price / price) - 1) if diff_rel > self.threshold_pct: log.info( 'Publishing price, difference is: {:.2%}'.format(diff_rel)) need_publish = True else: log.debug('Price difference is too low, not publishing price') # finally publish price if needed if need_publish or force: if self.dry_run: log.info('--dry-run mode, not publishing price feed') else: final_gbg_price = format(price, '.3f') log.info('Price to publish: %s' % final_gbg_price) self.witness_feed_publish(final_gbg_price, quote='1.000', account=self.witness)
def get_witness_pricefeed(self, witness: Union[str, Dict]) -> float: """Obtain current published price for single witness.""" if isinstance(witness, str): witness_data = Witness(witness) else: witness_data = witness base = Amount(witness_data['sbd_exchange_rate']['base']).amount quote = Amount(witness_data['sbd_exchange_rate']['quote']).amount # whether witness not exists yet, return 0 if quote == 0: return 0 price = base / quote return price
def main(ctx, param): """Get witness voting for a particular chain param.""" witnesses_names = ctx.helper.get_active_witnesses() witnesses = [Witness(i) for i in witnesses_names] voting = [] for i in witnesses: element = {'name': i['owner'], 'param': i['props'][param]} voting.append(element) voting = sorted(voting, key=lambda i: i['param']) median = voting[10]['param'] for el in voting: print('{name:<16} {param}'.format(**el)) print(f'{param} median: {median}')
def main(ctx, shutdown): """Update witness data or votable params.""" # set pubkey to special value whether we need to shutdown witness if shutdown: pubkey = 'GLS1111111111111111111111111111111114T1Anm' else: pubkey = ctx.config['witness_pubkey'] witness = Witness(ctx.config['witness']) if witness['url'] != ctx.config['url'] or witness['signing_key'] != pubkey: ctx.helper.witness_update(pubkey, ctx.config['url'], ctx.config['props'], account=ctx.config['witness']) ctx.helper.chain_properties_update(ctx.config['props'], account=ctx.config['witness'])
def main(ctx): """Get voting for inflation targets props.""" witnesses_names = ctx.helper.get_active_witnesses() witnesses = [Witness(i) for i in witnesses_names] voting = [] for i in witnesses: element = { 'name': i['owner'], 'worker': i['props']['worker_reward_percent'], 'witness': i['props']['witness_reward_percent'], 'vesting': i['props']['vesting_reward_percent'], } element['author'] = 10000 - element['worker'] - element[ 'witness'] - element['vesting'] voting.append(element) for key in ['worker', 'witness', 'vesting']: voting = get_median_voting(voting, key)
def get_price_feeds(self) -> List[feed]: """Get current price feeds as reported by witnesses.""" witnesses = self.get_active_witnesses() witnesses = [Witness(i) for i in witnesses] # add price key for i in witnesses: base = Amount(i['sbd_exchange_rate']['base']).amount quote = Amount(i['sbd_exchange_rate']['quote']).amount try: i['price'] = base / quote except ZeroDivisionError: pass feeds = [ feed(i['owner'], i['price']) for i in witnesses if 'price' in i ] feeds = sorted(feeds, key=lambda k: k.price) return feeds
def run_once(self) -> None: """Do miss check once.""" in_sync = self.check_node_sync() if not in_sync: return witness = Witness(self.witness) producing_locally = witness['signing_key'] == self.witness_pubkey if producing_locally: if self.miss_count: log.info('stopping monitoring, reason: local block production') self.miss_count = 0 return elif not self.miss_count: self.set_miss_data(witness) log.info( 'starting/resuming monitoring, current miss count: {}'.format( self.miss_count)) return self.do_witness_check(witness)
def main(witness): """Show WITNESS object.""" wit = Witness(witness) pprint(wit)