Skip to content

Commit

Permalink
chore: make logs lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler committed Sep 12, 2023
1 parent 9bcf744 commit 8eb7bc5
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 63 deletions.
8 changes: 4 additions & 4 deletions y/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def contract_creation_block(address: AnyAddressType, when_no_history_return_0: b
NOTE Requires access to historical state. Doesn't account for CREATE2 or SELFDESTRUCT.
"""
address = convert.to_address(address)
logger.debug(f"contract creation block {address}")
logger.debug("contract creation block %s", address)
height = chain.height

if height == 0:
Expand Down Expand Up @@ -109,11 +109,11 @@ def contract_creation_block(address: AnyAddressType, when_no_history_return_0: b
barrier = mid
lo = mid
if hi == lo + 1 == barrier + 1 and when_no_history_return_0:
logger.warning(f'could not determine creation block for {address} on {Network.name()} (deployed prior to barrier)')
logger.debug(f"contract creation block {address} -> 0")
logger.warning('could not determine creation block for %s on %s (deployed prior to barrier)', address, Network.name())
logger.debug("contract creation block %s -> 0", address)
return 0
if hi != height:
logger.debug(f"contract creation block {address} -> {hi}")
logger.debug("contract creation block %s -> %s", address, hi)
return hi
raise ValueError(f"Unable to find deploy block for {address} on {Network.name()}")

Expand Down
6 changes: 3 additions & 3 deletions y/prices/chainlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async def _start_feed_loop(self) -> Union[None, NoReturn]:
self._cached_feeds[log["asset"]] = ERC20(log["latestAggregator"], asynchronous=self.asynchronous)

self._feeds_loaded.set()
logger.info(f'loaded {len(self._cached_feeds)} feeds')
logger.info('loaded %s feeds', len(self._cached_feeds))

while await dank_w3.eth.block_number < block+1:
await asyncio.sleep(15)
Expand All @@ -211,7 +211,7 @@ async def _start_feed_loop(self) -> Union[None, NoReturn]:
if log['denomination'] == DENOMINATIONS['USD'] and log['latestAggregator'] != ZERO_ADDRESS:
asset, latest_aggregator = log["asset"], log["latestAggregator"]
self._cached_feeds[asset] = ERC20(latest_aggregator, asynchronous=self.asynchronous)
logger.info(f'loaded new feed {latest_aggregator}')
logger.info('loaded new feed %s', latest_aggregator)

@a_sync.aka.property
async def feeds(self) -> Dict[ERC20, str]:
Expand Down Expand Up @@ -254,7 +254,7 @@ async def _get_price(self, asset, block: Block) -> Optional[UsdPrice]:
self.feed_scale(asset, sync=False),
)
except ValueError as e:
logger.debug(feed.address)
logger.debug("error for feed address %s:", feed.address)
logger.debug(str(e))
return None

Expand Down
4 changes: 2 additions & 2 deletions y/prices/dex/balancer/balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ async def get_price(self, token_address: AnyAddressType, block: Optional[Block]
v2: BalancerV2 = await self.v2
price = await v2.get_token_price(token_address, block, sync=False)
if price:
logger.debug(f"balancer v2 -> ${price}")
logger.debug("balancer v2 -> $%s", price)
return price

if not price and chain.id == Network.Mainnet:
v1: BalancerV1 = await self.v1
price = await v1.get_token_price(token_address, block, sync=False)
if price:
logger.debug(f"balancer v1 -> ${price}")
logger.debug("balancer v1 -> $%s", price)
return price

balancer_multiplexer = BalancerMultiplexer(asynchronous=True)
2 changes: 1 addition & 1 deletion y/prices/dex/balancer/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def deepest_pool_for(self, token_address: Address, block: Optional[Block]
pool_balance = pool_balance[0]
if pool_balance > deepest_pool['balance']:
deepest_pool = {'pool': pool, 'balance': pool_balance}
logger.debug(f"deepest pool {deepest_pool}")
logger.debug("deepest pool %s", deepest_pool)
return tuple(deepest_pool.values())


Expand Down
16 changes: 8 additions & 8 deletions y/prices/dex/uniswap/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ async def get_price(
logger.debug('smol')

fees = 0.997 ** (len(path) - 1)
logger.debug(f'router: {self.label} path: {path}')
logger.debug('router: %s path: %s', self.label, path)
quote, out_scale = await asyncio.gather(self.get_quote(amount_in, path, block=block, sync=False), ERC20(path[-1],asynchronous=True).scale)
if quote is not None:
amount_out = quote[-1] / out_scale
Expand Down Expand Up @@ -318,7 +318,7 @@ def _smol_brain_path_selector(self, token_in: AddressOrContract, token_out: Addr
@a_sync.aka.cached_property
async def pools(self) -> Dict[Address,Dict[Address,Address]]:
PairCreated = ['0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9']
logger.info(f'Fetching pools for {self.label} on {Network.printable()}. If this is your first time using ypricemagic, this can take a while. Please wait patiently...')
logger.info('Fetching pools for %s on %s. If this is your first time using ypricemagic, this can take a while. Please wait patiently...', self.label, Network.printable())
try:
logs = await get_logs_asap(self.factory, PairCreated, sync=False)
pairs, pools = await _parse_pairs_from_events(logs)
Expand All @@ -334,15 +334,15 @@ async def pools(self) -> Dict[Address,Dict[Address,Address]]:
for id, pair in pairs.items():
if id > all_pairs_len:
logger.debug(id, pair)
logger.debug(f'factory: {self.factory}')
logger.debug(f'len pairs: {len(pairs)}')
logger.debug(f'len allPairs: {all_pairs_len}')
logger.debug('factory: %s', self.factory)
logger.debug('len pairs: %s', len(pairs))
logger.debug('len allPairs: %s', all_pairs_len)
# TODO debug why this scenario occurs. Likely a strange interation between asyncio and joblib, or an incorrect cache value.
#raise ValueError("Returning more pairs than allPairsLength, something is wrong.")
else: # <
logger.debug(f"Oh no! Looks like your node can't look back that far. Checking for the missing {all_pairs_len - len(pairs)} pools...")
logger.debug("Oh no! Looks like your node can't look back that far. Checking for the missing %s pools...", all_pairs_len - len(pairs))
pools_your_node_couldnt_get = [i for i in range(all_pairs_len) if i not in pairs]
logger.debug(f'pools: {pools_your_node_couldnt_get}')
logger.debug('pools: %s', pools_your_node_couldnt_get)
pools_your_node_couldnt_get = await multicall_same_func_same_contract_different_inputs(
self.factory, 'allPairs(uint256)(address)', inputs=[i for i in pools_your_node_couldnt_get], sync=False
)
Expand All @@ -369,7 +369,7 @@ async def pool_mapping(self) -> Dict[Address,Dict[Address,Address]]:
token0, token1 = tokens.values()
pool_mapping[token0][pool] = token1
pool_mapping[token1][pool] = token0
logger.info(f'Loaded {len(await self.__pools__(sync=False))} pools supporting {len(pool_mapping)} tokens on {self.label}')
logger.info('Loaded %s pools supporting %s tokens on %s', len(await self.__pools__(sync=False)), len(pool_mapping), self.label)
return pool_mapping

async def pools_for_token(self, token_address: Address, block: Optional[Block] = None) -> Dict[Address,Address]:
Expand Down
8 changes: 4 additions & 4 deletions y/prices/dex/velodrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ async def pool_mapping(self) -> Dict[Address,Dict[Address,Address]]:
token0, token1, stable = params.values()
pool_mapping[token0][pool] = token1
pool_mapping[token1][pool] = token0
logger.info(f'Loaded {len(await self.__pools__(sync=False))} pools supporting {len(pool_mapping)} tokens on {self.label}')
logger.info('Loaded %s pools supporting %s tokens on %s', len(await self.__pools__(sync=False)), len(pool_mapping), self.label)
return pool_mapping

@a_sync.aka.cached_property
async def pools(self) -> Dict[Address, Dict[Address,Address]]:
logger.info(f'Fetching pools for {self.label} on {Network.printable()}. If this is your first time using ypricemagic, this can take a while. Please wait patiently...')
logger.info('Fetching pools for %s on %s. If this is your first time using ypricemagic, this can take a while. Please wait patiently...', self.label, Network.printable())
try:
factory = await Contract.coroutine(self.factory)
if 'PoolCreated' not in factory.topics:
Expand All @@ -81,9 +81,9 @@ async def pools(self) -> Dict[Address, Dict[Address,Address]]:
if len(pools) == all_pairs_len:
return pools
else:
logger.debug(f"Oh no! Looks like your node can't look back that far. Checking for the missing {all_pairs_len - len(pools)} pools...")
logger.debug("Oh no! Looks like your node can't look back that far. Checking for the missing %s pools...", all_pairs_len - len(pools))
pools_your_node_couldnt_get = [i for i in range(all_pairs_len) if i not in range(len(pools))]
logger.debug(f'pools: {pools_your_node_couldnt_get}')
logger.debug('pools: %s', pools_your_node_couldnt_get)
pools_your_node_couldnt_get = await asyncio.gather(
*[Call(self.factory, ['allPairs(uint256)(address)']).coroutine(i) for i in pools_your_node_couldnt_get]
)
Expand Down
24 changes: 12 additions & 12 deletions y/prices/lending/aave.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __contains__(self, __o: object) -> bool:

async def contains(self, __o: object) -> bool:
contains = convert.to_address(__o) in await self.__atokens__(sync=False)
logger.debug(f'{self} contains {__o}: {contains}')
logger.debug('%s contains %s: %s', self, __o, contains)
return contains


Expand All @@ -90,15 +90,15 @@ async def atokens(self) -> List[ERC20]:
reserves_data = await Call(self.address, ['getReserves()(address[])']).coroutine()
reserves_data = await asyncio.gather(*[self.contract.getReserveData.coroutine(reserve) for reserve in reserves_data])
atokens = [ERC20(reserve['aTokenAddress'], asynchronous=self.asynchronous) for reserve in reserves_data]
logger.info(f'loaded {len(atokens)} v1 atokens for {self.__repr__()}')
logger.info('loaded %s v1 atokens for %s', len(atokens), self.__repr__())
return atokens


class AaveMarketV2(AaveMarketBase):
@a_sync.a_sync(ram_cache_maxsize=256)
async def underlying(self, token_address: AddressOrContract) -> ERC20:
underlying = await raw_call(token_address, 'UNDERLYING_ASSET_ADDRESS()',output='address', sync=False)
logger.debug(f"underlying: {underlying}")
logger.debug("underlying: %s", underlying)
return ERC20(underlying, asynchronous=self.asynchronous)

@a_sync.aka.cached_property
Expand All @@ -114,19 +114,19 @@ async def atokens(self) -> List[ERC20]:

try:
atokens = [ERC20(reserve_data[7], asynchronous=self.asynchronous) for reserve_data in reserves_data]
logger.info(f'loaded {len(atokens)} v2 atokens for {self.__repr__()}')
logger.info('loaded %s v2 atokens for %s', len(atokens), self.__repr__())
return atokens
except TypeError as e: # TODO figure out what to do about non verified aave markets
logger.exception(e)
logger.warning(f'failed to load tokens for {self.__repr__()}')
logger.warning('failed to load tokens for %s', self.__repr__())
return []


class AaveMarketV3(AaveMarketBase):
@a_sync.a_sync(ram_cache_maxsize=256)
async def underlying(self, token_address: AddressOrContract) -> ERC20:
underlying = await raw_call(token_address, 'UNDERLYING_ASSET_ADDRESS()',output='address', sync=False)
logger.debug(f"underlying: {underlying}")
logger.debug("underlying: %s", underlying)
return ERC20(underlying, asynchronous=self.asynchronous)

@a_sync.aka.cached_property
Expand All @@ -136,11 +136,11 @@ async def atokens(self) -> List[ERC20]:

try:
atokens = [ERC20(reserve_data[8], asynchronous=self.asynchronous) for reserve_data in reserves_data]
logger.info(f'loaded {len(atokens)} v3 atokens for {self.__repr__()}')
logger.info('loaded %s v3 atokens for %s', len(atokens), self.__repr__())
return atokens
except TypeError as e: # TODO figure out what to do about non verified aave markets
logger.exception(e)
logger.warning(f'failed to load tokens for {self.__repr__()}')
logger.warning('failed to load tokens for %s', self.__repr__())
return []


Expand All @@ -160,19 +160,19 @@ async def pools(self) -> List[Union[AaveMarketV1, AaveMarketV2]]:
@a_sync.aka.cached_property
async def pools_v1(self) -> List[AaveMarketV1]:
pools = [AaveMarketV1(pool, asynchronous=self.asynchronous) for pool in v1_pools]
logger.debug(f"{self} v1 pools {pools}")
logger.debug("%s v1 pools %s", self, pools)
return pools

@a_sync.aka.cached_property
async def pools_v2(self) -> List[AaveMarketV2]:
pools = [AaveMarketV2(pool, asynchronous=self.asynchronous) for pool in v2_pools]
logger.debug(f"{self} v2 pools {pools}")
logger.debug("%s v2 pools %s", self, pools)
return pools

@a_sync.aka.cached_property
async def pools_v3(self) -> List[AaveMarketV2]:
pools = [AaveMarketV3(pool, asynchronous=self.asynchronous) for pool in v3_pools]
logger.debug(f"{self} v3 pools {pools}")
logger.debug("%s v3 pools %s", self, pools)
return pools

async def pool_for_atoken(self, token_address: AnyAddressType) -> Optional[Union[AaveMarketV1, AaveMarketV2, AaveMarketV3]]:
Expand All @@ -190,7 +190,7 @@ def __contains__(self, __o: object) -> bool:
async def is_atoken(self, token_address: AnyAddressType) -> bool:
logger = get_price_logger(token_address, block=None)
is_atoken = any(await asyncio.gather(*[pool.contains(token_address, sync=False) for pool in await self.__pools__(sync=False)]))
logger.debug(f"is_atoken: {is_atoken}")
logger.debug("is_atoken: %s", is_atoken)
return is_atoken

async def is_wrapped_atoken_v2(self, token_address: AnyAddressType) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion y/prices/lending/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async def markets(self) -> Tuple[CToken]:
logger.warning(f'had trouble loading markets for {self.__repr__()}')
response = set()
markets = tuple(CToken(market, comptroller=self, asynchronous=self.asynchronous) for market in response)
logger.info(f"loaded {len(markets)} markets for {self.__repr__()}")
logger.info("loaded %s markets for %s", len(markets), self.__repr__())
return markets

async def oracle(self, block: Optional[Block] = None) -> Contract:
Expand Down
18 changes: 9 additions & 9 deletions y/prices/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async def _get_price(
symbol = None

logger = get_price_logger(token, block, 'magic')
logger.debug(f'fetching price for {symbol}')
logger.debug('fetching price for %s', symbol)
logger._debugger = asyncio.create_task(_debug_tsk(symbol, logger))

# Helps to detect stuck code
Expand All @@ -136,17 +136,17 @@ async def _get_price(

if ypriceapi.should_use and token not in ypriceapi.skip_tokens:
price = await ypriceapi.get_price(token, block)
logger.debug(f"ypriceapi -> {price}")
logger.debug("ypriceapi -> %s", price)
if price is not None:
logger.debug(f"{symbol} price: {price}")
logger.debug("%s price: %s", symbol, price)
logger._debugger.cancel()
return price

price = await _exit_early_for_known_tokens(token, block=block)
logger.debug(f"early exit -> {price}")
logger.debug("early exit -> %s", price)
if price is not None:
await _sense_check(token, price)
logger.debug(f"{symbol} price: {price}")
logger.debug("%s price: %s", symbol, price)
logger._debugger.cancel()
return price

Expand All @@ -168,17 +168,17 @@ async def _get_price(
# If price is 0, we can at least try to see if balancer gives us a price. If not, its probably a shitcoin.
if price is None or price == 0:
new_price = await balancer_multiplexer.get_price(token, block=block, sync=False)
logger.debug(f"balancer -> {price}")
logger.debug("balancer -> %s", price)
if new_price:
logger.debug(f"replacing price {price} with new price {new_price}")
logger.debug("replacing price %s with new price %s", price, new_price)
price = new_price

if price is None:
_fail_appropriately(logger, symbol, fail_to_None=fail_to_None, silent=silent)
if price:
await _sense_check(token, price)

logger.debug(f"{symbol} price: {price}")
logger.debug("%s price: %s", symbol, price)
# Don't need this anymore
logger._debugger.cancel()
return price
Expand Down Expand Up @@ -267,4 +267,4 @@ async def _debug_tsk(symbol: str, logger: logging.Logger) -> NoReturn:
"""Prints a log every 1 minute until the creating coro returns"""
while True:
await asyncio.sleep(60)
logger.debug(f"price still fetching for {symbol}")
logger.debug("price still fetching for %s", symbol)
2 changes: 1 addition & 1 deletion y/prices/stable_swap/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ async def _load_factories(self) -> None:
# please refer to commit 3f70c4246615017d87602e03272b3ed18d594d3c to see how to add them manually
await asyncio.gather(*[self._load_factory(factory) for factory in self.identifiers[Ids.CryptoPool_Factory] + self.identifiers[Ids.Cryptopool_Factory]])
if not self._all_loaded.is_set():
logger.info(f'loaded {len(self.token_to_pool)} pools from {len(self.registries)} registries and {len(self.factories)} factories')
logger.info('loaded %s pools from %s registries and %s factories', len(self.token_to_pool), len(self.registries), len(self.factories))
self._all_loaded.set()

async def load_all(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion y/prices/synthetix.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def synths(self) -> List[EthAddress]:
proxy_erc20 = await self.get_address('ProxyERC20', sync=False)
synth_count = await proxy_erc20.availableSynthCount.coroutine()
synths = await asyncio.gather(*[proxy_erc20.availableSynths.coroutine(i) for i in range(synth_count)])
logger.info(f'loaded {len(synths)} synths')
logger.info('loaded %s synths', len(synths))
return synths

async def is_synth(self, token: AnyAddressType) -> bool:
Expand Down
Loading

0 comments on commit 8eb7bc5

Please sign in to comment.