Skip to content

Commit

Permalink
Merge pull request #247 from bancorprotocol/243-improve-logging-error…
Browse files Browse the repository at this point in the history
…-messages

243 improve logging error messages
  • Loading branch information
mikewcasale authored Dec 10, 2023
2 parents bdf07bf + 1e922ff commit be4d62c
Show file tree
Hide file tree
Showing 16 changed files with 1,059 additions and 600 deletions.
151 changes: 99 additions & 52 deletions fastlane_bot/bot.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions fastlane_bot/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Config():

SUPPORTED_EXCHANGES = ['carbon_v1', 'bancor_v2', 'bancor_v3', 'uniswap_v2', 'uniswap_v3', 'sushiswap_v2', 'bancor_pol', 'pancakeswap_v2', 'pancakeswap_v3']

logging_header: str = None

@classmethod
def new(cls, *, config=None, loglevel=None, logging_path=None, blockchain=None, **kwargs):
"""
Expand Down
70 changes: 43 additions & 27 deletions fastlane_bot/events/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ def filter_target_tokens(self, target_tokens: List[str]):
]

self.cfg.logger.info(
f"Limiting pools by target_tokens. Removed {len(initial_state) - len(self.state)} non target-pools. {len(self.state)} pools remaining"
f"[events.interface] Limiting pools by target_tokens. Removed {len(initial_state) - len(self.state)} non target-pools. {len(self.state)} pools remaining"
)

# Log the total number of pools remaining for each exchange
self.ConfigObj.logger.info("Pools remaining per exchange:")
self.ConfigObj.logger.debug("Pools remaining per exchange:")
for exchange_name in self.exchanges:
pools = self.filter_pools(exchange_name)
self.log_pool_numbers(pools, exchange_name)
Expand All @@ -90,12 +90,12 @@ def remove_unsupported_exchanges(self) -> None:
self.state = [
pool for pool in self.state if pool["exchange_name"] in self.exchanges
]
self.cfg.logger.info(
self.cfg.logger.debug(
f"Removed {len(initial_state) - len(self.state)} unsupported exchanges. {len(self.state)} pools remaining"
)

# Log the total number of pools remaining for each exchange
self.ConfigObj.logger.info("Pools remaining per exchange:")
self.ConfigObj.logger.debug("Pools remaining per exchange:")
for exchange_name in self.exchanges:
pools = self.filter_pools(exchange_name)
self.log_pool_numbers(pools, exchange_name)
Expand Down Expand Up @@ -150,7 +150,9 @@ def get_tokens_from_exchange(self, exchange_name: str) -> List[str]:
tokens = list(set(tokens))
return tokens

def filter_pools(self, exchange_name: str, keys: List[str] = "") -> List[Dict[str, Any]]:
def filter_pools(
self, exchange_name: str, keys: List[str] = ""
) -> List[Dict[str, Any]]:
"""
Filter pools by exchange name and key
Expand All @@ -171,9 +173,9 @@ def filter_pools(self, exchange_name: str, keys: List[str] = "") -> List[Dict[st
pool
for pool in self.state
if pool["exchange_name"] == exchange_name
and self.has_balance(pool, keys)
and pool['tkn0_decimals'] is not None
and pool['tkn1_decimals'] is not None
and self.has_balance(pool, keys)
and pool["tkn0_decimals"] is not None
and pool["tkn1_decimals"] is not None
]
else:
return [
Expand All @@ -192,7 +194,7 @@ def log_pool_numbers(self, pools: List[Dict[str, Any]], exchange_name: str) -> N
The exchange name to log
"""
self.cfg.logger.info(f"{exchange_name}: {len(pools)}")
self.cfg.logger.debug(f"[events.interface] {exchange_name}: {len(pools)}")

def remove_zero_liquidity_pools(self) -> None:
"""
Expand All @@ -210,7 +212,7 @@ def remove_zero_liquidity_pools(self) -> None:
"carbon_v1",
"pancakeswap_v2",
"pancakeswap_v3",
"balancer"
"balancer",
]
keys = [
["liquidity"],
Expand Down Expand Up @@ -260,12 +262,12 @@ def remove_unmapped_uniswap_v2_pools(self) -> None:
pool
for pool in self.state
if pool["exchange_name"] != "uniswap_v2"
or (
pool["exchange_name"] in self.cfg.UNI_V2_FORKS
and pool["address"] in self.uniswap_v2_event_mappings
)
or (
pool["exchange_name"] in self.cfg.UNI_V2_FORKS
and pool["address"] in self.uniswap_v2_event_mappings
)
]
self.cfg.logger.info(
self.cfg.logger.debug(
f"Removed {len(initial_state) - len(self.state)} unmapped uniswap_v2/sushi pools. {len(self.state)} uniswap_v2/sushi pools remaining"
)
self.log_umapped_pools_by_exchange(initial_state)
Expand All @@ -279,19 +281,19 @@ def remove_unmapped_uniswap_v3_pools(self) -> None:
pool
for pool in self.state
if pool["exchange_name"] != "uniswap_v3"
or (
pool["exchange_name"] in self.cfg.UNI_V3_FORKS
and pool["address"] in self.uniswap_v3_event_mappings
)
or (
pool["exchange_name"] in self.cfg.UNI_V3_FORKS
and pool["address"] in self.uniswap_v3_event_mappings
)
]
self.cfg.logger.info(
self.cfg.logger.debug(
f"Removed {len(initial_state) - len(self.state)} unmapped uniswap_v2/sushi pools. {len(self.state)} uniswap_v2/sushi pools remaining"
)
self.log_umapped_pools_by_exchange(initial_state)

def log_umapped_pools_by_exchange(self, initial_state):
# Log the total number of pools filtered out for each exchange
self.ConfigObj.logger.info("Unmapped uniswap_v2/sushi pools:")
self.ConfigObj.logger.debug("Unmapped uniswap_v2/sushi pools:")
unmapped_pools = [pool for pool in initial_state if pool not in self.state]
assert len(unmapped_pools) == len(initial_state) - len(self.state)
# uniswap_v3_unmapped = [
Expand All @@ -311,7 +313,7 @@ def remove_faulty_token_pools(self) -> None:
"""
Remove pools with faulty tokens
"""
self.cfg.logger.info(
self.cfg.logger.debug(
f"Total number of pools. {len(self.state)} before removing faulty token pools"
)

Expand All @@ -322,8 +324,8 @@ def remove_faulty_token_pools(self) -> None:
self.get_token(pool["tkn1_key"])
safe_pools.append(pool)
except Exception as e:
self.cfg.logger.info(f"Exception: {e}")
self.cfg.logger.info(
self.cfg.logger.warning(f"[events.interface] Exception: {e}")
self.cfg.logger.warning(
f"Removing pool for exchange={pool['pair_name']}, pair_name={pool['pair_name']} token={pool['tkn0_key']} from state for faulty token"
)

Expand Down Expand Up @@ -484,7 +486,7 @@ def create_pool_and_tokens(self, idx: int, record: Dict[str, Any]) -> PoolAndTok
"tkn7_balance",
"tkn7_address",
"tkn7_decimals",
"tkn7_weight"
"tkn7_weight",
]
},
)
Expand All @@ -508,8 +510,22 @@ def get_tokens(self) -> List[Token]:
for idx in range(len(record["descr"].split("/"))):
token_set.add(self.create_token(record, f"tkn{str(idx)}_"))
if self.ConfigObj.GAS_TKN_IN_FLASHLOAN_TOKENS:
token_set.add(Token(symbol=self.ConfigObj.NATIVE_GAS_TOKEN_KEY.split("-")[0], key=self.ConfigObj.NATIVE_GAS_TOKEN_KEY, address=self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, decimals=18))
token_set.add(Token(symbol=self.ConfigObj.WRAPPED_GAS_TOKEN_KEY.split("-")[0], key=self.ConfigObj.WRAPPED_GAS_TOKEN_KEY, address=self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS, decimals=18))
token_set.add(
Token(
symbol=self.ConfigObj.NATIVE_GAS_TOKEN_KEY.split("-")[0],
key=self.ConfigObj.NATIVE_GAS_TOKEN_KEY,
address=self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS,
decimals=18,
)
)
token_set.add(
Token(
symbol=self.ConfigObj.WRAPPED_GAS_TOKEN_KEY.split("-")[0],
key=self.ConfigObj.WRAPPED_GAS_TOKEN_KEY,
address=self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS,
decimals=18,
)
)
return list(token_set)

def create_token(self, record: Dict[str, Any], prefix: str) -> Token:
Expand Down
24 changes: 15 additions & 9 deletions fastlane_bot/events/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ def update_carbon(self, current_block: int):
"""
start_time = time.time()
self.cfg.logger.info("Updating carbon pools w/ multicall...")
self.cfg.logger.info(
"[events.managers.base] Updating carbon pools w/ multicall..."
)

# Create or get CarbonController contract object
carbon_controller = self.create_or_get_carbon_controller()
Expand All @@ -313,11 +315,11 @@ def update_carbon(self, current_block: int):
# Get the fee for each pair
if not self.fee_pairs:
# Log that the fee pairs are being set
self.cfg.logger.info("Setting carbon fee pairs...")
self.cfg.logger.debug("[events.managers.base] Setting carbon fee pairs...")
self.fee_pairs = self.get_fee_pairs(pairs, carbon_controller)

# Log the time taken for the above operations
self.cfg.logger.info(
self.cfg.logger.debug(
f"Fetched {len(strategies_by_pair)} carbon strategies in {time.time() - start_time} seconds"
)

Expand All @@ -335,7 +337,7 @@ def update_carbon(self, current_block: int):
)

# Log the time taken for the above operations
self.cfg.logger.info(
self.cfg.logger.debug(
f"Updated {len(strategies_by_pair)} carbon strategies info in {time.time() - start_time} seconds"
)

Expand Down Expand Up @@ -364,7 +366,7 @@ def get_carbon_pairs(
else self.get_carbon_pairs_by_contract(carbon_controller)
)
# Log whether the carbon pairs were retrieved from the state or the contract
self.cfg.logger.info(
self.cfg.logger.debug(
f"Retrieved {len(pairs)} carbon pairs from {'state' if self.carbon_inititalized else 'contract'}"
)
if target_tokens is None or target_tokens == []:
Expand Down Expand Up @@ -498,8 +500,12 @@ def get_strats_by_contract(
self.carbon_inititalized = True

# Log that Carbon is initialized
self.cfg.logger.info(f"Carbon is initialized {self.carbon_inititalized}")
self.cfg.logger.info(f"Retrieved {len(strategies_by_pair)} carbon strategies")
self.cfg.logger.debug(
f"[events.managers.base] Carbon is initialized {self.carbon_inititalized}"
)
self.cfg.logger.debug(
f"[events.managers.base] Retrieved {len(strategies_by_pair)} carbon strategies"
)
return [s for strat in strategies_by_pair if strat for s in strat if s]

def get_strats_by_state(self, pairs: List[List[Any]]) -> List[List[int]]:
Expand Down Expand Up @@ -573,7 +579,7 @@ def get_strategies(
"""
# Log whether the carbon strats were retrieved from the state or the contract
self.cfg.logger.info(
self.cfg.logger.debug(
f"Retrieving carbon strategies from {'state' if self.carbon_inititalized else 'contract'}"
)
return (
Expand Down Expand Up @@ -839,7 +845,7 @@ def get_bancor_pol_pools(self, current_block: int):
"""
start_time = time.time()
self.cfg.logger.info("Updating Bancor POL pools")
self.cfg.logger.info("[events.managers.base] Updating Bancor POL pools...")

# Create or get CarbonController contract object
bancor_pol = self.create_or_get_bancor_pol_contract()
Expand Down
21 changes: 13 additions & 8 deletions fastlane_bot/events/managers/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,15 @@ def add_pool_info_from_contract(
"""
exchange_name = self.check_forked_exchange_names(exchange_name, address, event)
if not exchange_name:
self.cfg.logger.info(f"Exchange name not found {event}")
self.cfg.logger.warning(
f"[events.managers.contracts] Exchange name not found {event}. Skipping event..."
)
return None

if exchange_name not in self.SUPPORTED_EXCHANGES:
self.cfg.logger.debug(
f"Event exchange {exchange_name} not in exchanges={self.SUPPORTED_EXCHANGES} for address={address}"
self.cfg.logger.warning(
f"Event exchange {exchange_name} not in exchanges={self.SUPPORTED_EXCHANGES} for address={address}. "
f"Skipping event..."
)
return None

Expand Down Expand Up @@ -297,17 +300,17 @@ def get_token_info_from_contract(
tokens_filepath=tokens_filepath,
)
except self.FailedToGetTokenDetailsException as e:
self.cfg.logger.debug(f"{e}")
self.cfg.logger.debug(
f"[events.managers.contracts.get_token_info_from_contract] {e}"
)

class FailedToGetTokenDetailsException(Exception):
"""
Exception caused when token details are unable to be fetched by the contract
"""

def __init__(self, addr):
self.message = (
f"Failed to get token symbol and decimals for token address: {addr}"
)
self.message = f"[events.managers.contracts.get_token_info_from_contract] Failed to get token symbol and decimals for token address: {addr}"

def __str__(self):
return self.message
Expand Down Expand Up @@ -368,7 +371,9 @@ def _get_and_save_token_info_from_contract(
"blockchain": self.cfg.NETWORK,
}
try:
self.cfg.logger.info(f"Adding new token {key} to {tokens_filepath}")
self.cfg.logger.debug(
f"[events.managers.contracts._get_and_save_token_info_from_contract] Adding new token {key} to {tokens_filepath}"
)
except UnicodeEncodeError:
raise self.FailedToGetTokenDetailsException(addr=addr)

Expand Down
Loading

0 comments on commit be4d62c

Please sign in to comment.