diff --git a/src/dexorder/bin/mirror.py b/src/dexorder/bin/mirror.py index 8ced0df..1a6b94d 100644 --- a/src/dexorder/bin/mirror.py +++ b/src/dexorder/bin/mirror.py @@ -1,8 +1,9 @@ import asyncio import logging import os +from datetime import timedelta -from dexorder import config, blockchain, current_w3 +from dexorder import config, blockchain, current_w3, now from dexorder.bin.executable import execute from dexorder.blockchain.connection import create_w3 from dexorder.blockstate import current_blockstate @@ -90,6 +91,10 @@ async def await_mirror(tx, pool_addr, mirror_addr, mirror_inverted ): log.info(f'Updated {pool_addr} => {"1/" if mirror_inverted else ""}{mirror_addr}') +async def send_update(mirrorenv, pool, price): + tx = await mirrorenv.transact.updatePool(pool, price) + await tx.wait() + async def main(): init_generating_metadata() if config.metadata is None: @@ -126,19 +131,25 @@ async def main(): mirror_pools = [] for pool in pools: mirror_addr, mirror_inverted = await mirrorenv.pools(pool) - log.debug(f'\tmirror result {mirror_addr} {mirror_inverted}') + log.debug(f'\t{pool} => {mirror_addr} inverted={mirror_inverted}') mirror_pools.append((mirror_addr, mirror_inverted)) await write_metadata(pools, mirror_pools) delay = config.polling if config.polling > 0 else 1 log.info(f'Mirroring pools every {delay} seconds') + delay = timedelta(seconds=delay) while True: + wake_up = now() + delay prices = await asyncio.gather(*[get_pool_price(pool) for pool in pools]) - try: - await mirrorenv.transact.updatePools(list(zip(pools,prices))) - except Exception as x: - log.exception(x) - log.debug('Mirrored'+''.join(f'\n\t{pool} {price}' for pool, price in zip(pools,prices))) - await asyncio.sleep(delay) + updates = [send_update(mirrorenv, pool, price) for pool, price in zip(pools, prices)] + results = await asyncio.gather(*updates, return_exceptions=True) + for result, pool, price in zip(results,pools,prices): + if isinstance(result, Exception): + log.debug(f'Could not update {pool}: {result}') + else: + log.debug(f'Mirrored {pool} {price}') + sleep = (wake_up - now()).total_seconds() + if sleep > 0: + await asyncio.sleep(sleep) if __name__ == '__main__':