async def _run_background_jobs(self): d = self.deps if 'REPLACE_RUNE_TIMESERIES_WITH_GECKOS' in os.environ: await fill_rune_price_from_gecko(d.db) self.ppf = PoolPriceFetcher(d) await self.ppf.get_current_pool_data_full() fetcher_cap = CapInfoFetcher(d, ppf=self.ppf) fetcher_tx = StakeTxFetcher(d) fetcher_queue = QueueFetcher(d) notifier_cap = CapFetcherNotifier(d) notifier_tx = StakeTxNotifier(d) notifier_queue = QueueNotifier(d) notifier_price = PriceNotifier(d) notifier_pool_churn = PoolChurnNotifier(d) fetcher_cap.subscribe(notifier_cap) fetcher_tx.subscribe(notifier_tx) fetcher_queue.subscribe(notifier_queue) self.ppf.subscribe(notifier_price) self.ppf.subscribe(notifier_pool_churn) await asyncio.gather(*(task.run() for task in [ self.ppf, fetcher_tx, fetcher_cap, fetcher_queue, ]))
async def view_address_summary(self, query: CallbackQuery): address = self.data[self.KEY_ACTIVE_ADDRESS] # POST A LOADING STICKER sticker = await query.message.answer_sticker(LOADING_STICKER, disable_notification=True) # WORK ppf = PoolPriceFetcher(self.deps) lpf = LiqPoolFetcher(self.deps) my_pools = self.data[self.KEY_MY_POOLS] liqs = await lpf.fetch_all_pool_liquidity_info(address, my_pools) pools = list(liqs.keys()) liqs = list(liqs.values()) weekly_charts = await lpf.fetch_all_pools_weekly_charts(address, pools) stake_reports = await asyncio.gather(*[lpf.fetch_stake_report_for_pool(liq, ppf) for liq in liqs]) value_hidden = not self.data.get(self.KEY_CAN_VIEW_VALUE, True) picture = await lp_address_summary_picture(stake_reports, weekly_charts, self.loc, value_hidden=value_hidden) picture_io = img_to_bio(picture, 'Thorchain_LP_Summary.png') # ANSWER await self.show_my_pools(query, edit=False) await query.message.answer_photo(picture_io, disable_notification=True) # CLEAN UP await asyncio.gather(query.message.delete(), sticker.delete())
async def view_pool_report(self, query: CallbackQuery): _, pool = query.data.split(':') address = self.data[self.KEY_ACTIVE_ADDRESS] # POST A LOADING STICKER sticker = await query.message.answer_sticker(LOADING_STICKER, disable_notification=True) # WORK... lpf = LiqPoolFetcher(self.deps) liq = await lpf.fetch_one_pool_liquidity_info(address, pool) ppf = PoolPriceFetcher(self.deps) stake_report = await lpf.fetch_stake_report_for_pool(liq, ppf) value_hidden = not self.data.get(self.KEY_CAN_VIEW_VALUE, True) picture = await lp_pool_picture(stake_report, self.loc, value_hidden=value_hidden) picture_io = img_to_bio(picture, f'Thorchain_LP_{pool}.png') # ANSWER await self.show_my_pools(query, edit=False) await query.message.answer_photo(picture_io, # caption=self.loc.TEXT_LP_IMG_CAPTION, disable_notification=True) # CLEAN UP await asyncio.gather(query.message.delete(), sticker.delete())
async def foo13(): async with aiohttp.ClientSession() as deps.session: deps.thor_man = ThorNodeAddressManager(deps.cfg.thornode.seed, deps.session) ppf = PoolPriceFetcher(deps) data = await ppf.get_current_pool_data_full() print(data)
async def send_to_channel_test_message(d: DepContainer): d.broadcaster = Broadcaster(d) async with aiohttp.ClientSession() as d.session: d.thor_man = ThorNodeAddressManager(d.cfg.thornode.seed, d.session) lph = LastPriceHolder() ppf = PoolPriceFetcher(d) notifier_pool_churn = PoolChurnNotifier(d) await ppf.get_current_pool_data_full() # feed original pools await notifier_pool_churn.on_data(ppf, None) lph.pool_info_map = deepcopy(lph.pool_info_map) # make a copy del lph.pool_info_map['BNB.AERGO-46B'] # deleted pool del lph.pool_info_map['BNB.BEAR-14C'] # deleted pool lph.pool_info_map['BNB.FSN-E14'].status = PoolInfo.ENABLED lph.pool_info_map['BNB.RAVEN-F66'].status = PoolInfo.BOOTSTRAP lph.pool_info_map['BTC.BTC'] = PoolInfo('BTC.BTC', 18555, 18555, 100, 18555 * 100, PoolInfo.BOOTSTRAP) await notifier_pool_churn.on_data( ppf, None) # must notify about changes above ^^^ await notifier_pool_churn.on_data(ppf, None) # no update at this moment!
async def load_summary_for_address(d: DepContainer, address): async with aiohttp.ClientSession() as d.session: await d.db.get_redis() d.thor_man.session = d.session lpf = LiqPoolFetcher(d) ppf = PoolPriceFetcher(d) await ppf.get_current_pool_data_full() liqs = await lpf.fetch_all_pool_liquidity_info(address) pools = list(liqs.keys()) liqs = list(liqs.values()) weekly_charts = await lpf.fetch_all_pools_weekly_charts(address, pools) stake_reports = await asyncio.gather( *[lpf.fetch_stake_report_for_pool(liq, ppf) for liq in liqs]) return stake_reports, weekly_charts
async def load_one_pool_liquidity(d: DepContainer, addr, pool=BTCB_SYMBOL): async with aiohttp.ClientSession() as d.session: await d.db.get_redis() lpf = LiqPoolFetcher(d) ppf = PoolPriceFetcher(d) d.thor_man = ThorNodeAddressManager(d.cfg.thornode.seed, d.session) await ppf.get_current_pool_data_full() cur_liqs = await lpf.fetch_all_pool_liquidity_info(addr) cur_liq: CurrentLiquidity = cur_liqs[pool] stake_report = await lpf.fetch_stake_report_for_pool(cur_liq, ppf) # -------- print out ---------- print(f'cur_liq = {cur_liq}') print() redeem_rune, redeem_asset = stake_report.redeemable_rune_asset print(f'redeem_rune = {redeem_rune} and redeem_asset = {redeem_asset}') print() USD, ASSET, RUNE = stake_report.USD, stake_report.ASSET, stake_report.RUNE print(f'current_value(USD) = {stake_report.current_value(USD)}') print(f'current_value(ASSET) = {stake_report.current_value(ASSET)}') print(f'current_value(RUNE) = {stake_report.current_value(RUNE)}') print() gl_usd, gl_usd_p = stake_report.gain_loss(USD) gl_ass, gl_ass_p = stake_report.gain_loss(ASSET) gl_rune, gl_rune_p = stake_report.gain_loss(RUNE) print(f'gain/loss(USD) = {gl_usd}, {gl_usd_p:.1f} %') print(f'gain/loss(ASSET) = {gl_ass}, {gl_ass_p:.1f} %') print(f'gain/loss(RUNE) = {gl_rune}, {gl_rune_p:.1f} %') print() lp_abs, lp_per = stake_report.lp_vs_hold apy = stake_report.lp_vs_hold_apy print(f'stake_report.lp_vs_hold = {lp_abs}, {lp_per:.1f} %') print(f'stake_report.lp_vs_hold_apy = {apy}') return stake_report
class App: def __init__(self): d = self.deps = DepContainer() d.cfg = Config() log_level = d.cfg.get('log_level', logging.INFO) logging.basicConfig( level=logging.getLevelName(log_level), format= '%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S', ) logging.info('-' * 100) logging.info(f"Log level: {log_level}") d.loop = asyncio.get_event_loop() d.db = DB(d.loop) d.price_holder = LastPriceHolder() def create_bot_stuff(self): d = self.deps d.bot = Bot(token=d.cfg.telegram.bot.token, parse_mode=ParseMode.HTML) d.dp = Dispatcher(d.bot, loop=d.loop) d.loc_man = LocalizationManager() d.broadcaster = Broadcaster(d) init_dialogs(d) async def connect_chat_storage(self): if self.deps.dp: self.deps.dp.storage = await self.deps.db.get_storage() async def create_thor_node_connector(self): d = self.deps cfg = d.cfg.thornode d.thor_man = ThorNodeAddressManager(cfg.seed) d.thor_man.session = d.session d.thor_nodes = ThorNode(d.thor_man, d.session, cohort_size=cfg.consensus.cohort, consensus=cfg.consensus.agree) await d.thor_man.reload_nodes_ip() async def _run_background_jobs(self): d = self.deps if 'REPLACE_RUNE_TIMESERIES_WITH_GECKOS' in os.environ: await fill_rune_price_from_gecko(d.db) self.ppf = PoolPriceFetcher(d) await self.ppf.get_current_pool_data_full() fetcher_cap = CapInfoFetcher(d, ppf=self.ppf) fetcher_tx = StakeTxFetcher(d) fetcher_queue = QueueFetcher(d) notifier_cap = CapFetcherNotifier(d) notifier_tx = StakeTxNotifier(d) notifier_queue = QueueNotifier(d) notifier_price = PriceNotifier(d) notifier_pool_churn = PoolChurnNotifier(d) fetcher_cap.subscribe(notifier_cap) fetcher_tx.subscribe(notifier_tx) fetcher_queue.subscribe(notifier_queue) self.ppf.subscribe(notifier_price) self.ppf.subscribe(notifier_pool_churn) await asyncio.gather(*(task.run() for task in [ self.ppf, fetcher_tx, fetcher_cap, fetcher_queue, ])) async def on_startup(self, _): await self.connect_chat_storage() self.deps.session = aiohttp.ClientSession(json_serialize=ujson.dumps) await self.create_thor_node_connector() asyncio.create_task(self._run_background_jobs()) async def on_shutdown(self, _): await self.deps.session.close() def run_bot(self): self.create_bot_stuff() executor.start_polling(self.deps.dp, skip_updates=True, on_startup=self.on_startup, on_shutdown=self.on_shutdown)