diff --git a/README.md b/README.md index 7d3e9f50..0e1ecc6e 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ Debug information is logged to the DEBUG level, including: * Profit levels for considered trades * Intermediate debug logs -The log level may be set via the `LOGLEVEL` environment variable. Possible values are: `INFO`, `DEBUG`, or `ERROR`. +The log level may be set via the `LOGLEVEL` environment variable. Possible values are: `info`, `debug`, or `error`. An example output is as follows: diff --git a/local-interchaintest/tests/transfer_neutron.py b/local-interchaintest/tests/transfer_neutron.py index b9d49997..539ce5a4 100644 --- a/local-interchaintest/tests/transfer_neutron.py +++ b/local-interchaintest/tests/transfer_neutron.py @@ -4,7 +4,10 @@ from asyncio import Semaphore from typing import Any from src.strategies.util import transfer_raw -from src.scheduler import Ctx, MAX_SKIP_CONCURRENT_CALLS +from src.scheduler import ( + Ctx, + MAX_SKIP_CONCURRENT_CALLS, +) from src.util import try_multiple_clients from src.util import custom_neutron_network_config import aiohttp diff --git a/local-interchaintest/tests/transfer_osmosis.py b/local-interchaintest/tests/transfer_osmosis.py index d7c2bd9a..03bc07cc 100644 --- a/local-interchaintest/tests/transfer_osmosis.py +++ b/local-interchaintest/tests/transfer_osmosis.py @@ -4,7 +4,10 @@ import asyncio from typing import Any from src.strategies.util import transfer_raw -from src.scheduler import Ctx, MAX_SKIP_CONCURRENT_CALLS +from src.scheduler import ( + Ctx, + MAX_SKIP_CONCURRENT_CALLS, +) from src.util import try_multiple_clients from src.util import custom_neutron_network_config import aiohttp diff --git a/main.py b/main.py index 9c77a924..507c5990 100644 --- a/main.py +++ b/main.py @@ -19,7 +19,11 @@ from typing import Any, cast from cosmpy.aerial.client import LedgerClient from cosmpy.aerial.wallet import LocalWallet -from src.scheduler import Scheduler, Ctx, MAX_SKIP_CONCURRENT_CALLS +from src.scheduler import ( + Scheduler, + Ctx, + MAX_SKIP_CONCURRENT_CALLS, +) from src.util import ( custom_neutron_network_config, DISCOVERY_CONCURRENCY_FACTOR, @@ -90,7 +94,7 @@ async def main() -> None: format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S", filename=args.log_file, - level=os.environ.get("LOGLEVEL", "INFO").upper(), + level=os.environ.get("LOGLEVEL", "info").upper(), ) else: @@ -98,7 +102,7 @@ async def main() -> None: format="%(asctime)s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S", stream=sys.stdout, - level=os.environ.get("LOGLEVEL", "INFO").upper(), + level=os.environ.get("LOGLEVEL", "info").upper(), ) denom_file: dict[str, Any] = { diff --git a/src/scheduler.py b/src/scheduler.py index 4535bb91..e904ea64 100644 --- a/src/scheduler.py +++ b/src/scheduler.py @@ -116,6 +116,9 @@ def commit_history(self) -> Self: return self + def refresh_denom_balances(self) -> None: + self.denom_balance_cache.clear() + def cancel(self) -> Self: """ Marks the event loop for termination. @@ -180,15 +183,8 @@ def log_route( return def asset_balance_prefix(leg: Leg, asset: str) -> Optional[str]: - balance_resp_asset = try_multiple_clients( - self.clients[leg.backend.chain_id], - lambda client: client.query_bank_balance( - Address( - self.wallet.public_key(), - prefix=leg.backend.chain_prefix, - ), - asset, - ), + balance_resp_asset = self.query_denom_balance( + asset, leg.backend.chain_id, leg.backend.chain_prefix ) if balance_resp_asset is None or not isinstance(balance_resp_asset, int): @@ -209,19 +205,22 @@ def leg_balance_prefixes(leg: Leg) -> list[str]: # Log all in and out asset balances for each leg in the route, # removing any duplicate prefixes using dict.fromkeys - prefix = " ".join( - list( - dict.fromkeys( - [ - prefix - for leg_prefixes in [ - leg_balance_prefixes(leg) for leg in route.legs + prefix = "" + + if log_level == "debug": + prefix = " ".join( + list( + dict.fromkeys( + [ + prefix + for leg_prefixes in [ + leg_balance_prefixes(leg) for leg in route.legs + ] + for prefix in leg_prefixes ] - for prefix in leg_prefixes - ] + ) ) ) - ) route.logs.append(f"{log_level.upper()} {prefix} {fmt_string % tuple(args)}") @@ -245,6 +244,36 @@ def leg_balance_prefixes(leg: Leg) -> list[str]: if log_level == "debug": logger.debug(fmt_string, str(route), *args) + def query_denom_balance(self, denom: str, chain_id: str, chain_prefix: str) -> int: + """ + Gets the balance of the denom on the given chain. + """ + + denom_id = f"{denom}_{chain_id}" + + if denom_id in self.denom_balance_cache: + return self.denom_balance_cache[denom_id] + + balance_resp_asset = try_multiple_clients( + self.clients[chain_id], + lambda client: client.query_bank_balance( + Address( + self.wallet.public_key(), + prefix=chain_prefix, + ), + denom, + ), + ) + + if balance_resp_asset is None or not isinstance(balance_resp_asset, int): + self.denom_balance_cache[denom_id] = 0 + + return 0 + + self.denom_balance_cache[denom_id] = int(balance_resp_asset) + + return int(balance_resp_asset) + async def query_denom_route( self, query: DenomRouteQuery ) -> Optional[list[DenomRouteLeg]]: