Skip to content

Commit

Permalink
Gate log prefixes behind debug log level.
Browse files Browse the repository at this point in the history
  • Loading branch information
dowlandaiello committed Oct 17, 2024
1 parent b13e9b8 commit 060385e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
5 changes: 4 additions & 1 deletion local-interchaintest/tests/transfer_neutron.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion local-interchaintest/tests/transfer_osmosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -90,15 +94,15 @@ 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:
logging.basicConfig(
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] = {
Expand Down
67 changes: 48 additions & 19 deletions src/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand All @@ -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)}")

Expand All @@ -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]]:
Expand Down

0 comments on commit 060385e

Please sign in to comment.