Skip to content

Commit

Permalink
Minor logging fixes for service bots (#1687)
Browse files Browse the repository at this point in the history
- Different log levels on negative interest if pool is paused.
- Fixing a bug with encoded event lookup on event trigger.
- Cleaning up checkpoint bot rollbar logging
  • Loading branch information
slundqui authored Sep 23, 2024
1 parent 7ea7bc7 commit ee041f0
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dev = [
"nbconvert",
"nbsphinx>=0.8.12",
"numpydoc>=1.5.0",
"pylint",
"pylint>=3.3.0",
"pytest-postgresql",
"pyright",
"sphinx>=6",
Expand Down
15 changes: 4 additions & 11 deletions scripts/checkpoint_bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ async def run_checkpoint_bot(
"""
# pylint: disable=too-many-arguments
# pylint: disable=too-many-branches
# pylint: disable=too-many-positional-arguments

# TODO pull this function out and put into agent0
web3 = chain._web3 # pylint: disable=protected-access
Expand Down Expand Up @@ -214,11 +215,6 @@ async def run_checkpoint_bot(
f"{checkpoint_bot_eth_balance=}"
)
logging.info(logging_str)
if log_to_rollbar:
log_rollbar_message(
message=logging_str,
log_level=logging.INFO,
)

# Check to see if the pool is paused. We don't run checkpoint bots on this pool if it's paused.
paused_events = hyperdrive_contract.events.PauseStatusUpdated.get_logs(
Expand All @@ -242,11 +238,6 @@ async def run_checkpoint_bot(
if enough_time_has_elapsed and checkpoint_doesnt_exist and not is_paused:
logging_str = f"Pool {pool_name} for {checkpoint_time=}: submitting checkpoint"
logging.info(logging_str)
if log_to_rollbar:
log_rollbar_message(
message=logging_str,
log_level=logging.INFO,
)

# To prevent race conditions with the checkpoint bot submitting transactions
# for multiple pools simultaneously, we wait a random amount of time before
Expand Down Expand Up @@ -425,7 +416,9 @@ async def main(argv: Sequence[str] | None = None) -> None:
# Reset hyperdrive objs
deployed_pools = Hyperdrive.get_hyperdrive_addresses_from_registry(chain, registry_address)

log_message = f"Running checkpoint bots for pools {list(deployed_pools.keys())}..."
# pylint: disable=protected-access
checkpoint_bot_eth_balance = FixedPoint(scaled_value=get_account_balance(chain._web3, sender.address))
log_message = f"Running checkpoint bots for pools {list(deployed_pools.keys())}. {checkpoint_bot_eth_balance=}"
logging.info(log_message)
log_rollbar_message(message=log_message, log_level=logging.INFO)

Expand Down
2 changes: 1 addition & 1 deletion scripts/invariant_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ async def run_event_handler(

subscription_id = response["subscription"]

encoded_event = response["result"]["topics"][0].hex()
encoded_event = response["result"]["topics"][0].to_0x_hex()
event_str = REVERSE_ENCODED_EVENTS[encoded_event]
check_block = response["result"]["blockNumber"]

Expand Down
7 changes: 5 additions & 2 deletions src/agent0/chainsync/db/hyperdrive/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def get_trade_events(
A DataFrame that consists of the queried trade events data.
"""
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments

query = session.query(TradeEvent)

Expand Down Expand Up @@ -289,6 +290,7 @@ def get_current_positions(
A DataFrame that consists of the queried pool info data.
"""
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments

query = session.query(
TradeEvent.hyperdrive_address,
Expand Down Expand Up @@ -601,8 +603,6 @@ def get_all_traders(session: Session, hyperdrive_address: str | None = None) ->
# Analysis schema interfaces


# Lots of arguments, most are defaults
# pylint: disable=too-many-arguments
def get_position_snapshot(
session: Session,
hyperdrive_address: str | list[str] | None = None,
Expand Down Expand Up @@ -641,6 +641,9 @@ def get_position_snapshot(
DataFrame
A DataFrame that consists of the queried pool info data.
"""
# Lots of arguments, most are defaults
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
query = session.query(PositionSnapshot)

if isinstance(hyperdrive_address, list):
Expand Down
10 changes: 6 additions & 4 deletions src/agent0/chainsync/exec/acquire_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
_SLEEP_AMOUNT = 1


# TODO cleanup
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
def acquire_data(
start_block: int = 0,
lookback_block_limit: int = 3000,
Expand Down Expand Up @@ -79,6 +75,12 @@ def acquire_data(
backfill: bool, optional
If true, will fill in missing pool info data for every block. Defaults to True.
"""

# TODO cleanup
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-positional-arguments
# TODO implement logger instead of global logging to suppress based on module name.

hyperdrive_name_mapping = None
Expand Down
10 changes: 6 additions & 4 deletions src/agent0/chainsync/exec/analyze_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
_SLEEP_AMOUNT = 1


# TODO cleanup
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
def analyze_data(
start_block: int = 0,
interfaces: list[HyperdriveReadInterface] | None = None,
Expand Down Expand Up @@ -67,6 +63,12 @@ def analyze_data(
calc_pnl: bool
Whether to calculate pnl. Defaults to True.
"""
# TODO cleanup
# pylint: disable=too-many-arguments
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-positional-arguments

# TODO implement logger instead of global logging to suppress based on module name.

## Initialization
Expand Down
3 changes: 3 additions & 0 deletions src/agent0/core/hyperdrive/agent/hyperdrive_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def close_long_trade(
Trade[HyperdriveMarketAction]
The trade object for closing a long in a Hyperdrive pool.
"""
# pylint: disable=too-many-positional-arguments
return Trade(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
Expand Down Expand Up @@ -222,6 +223,7 @@ def close_short_trade(
Trade[HyperdriveMarketAction]
The trade object for closing a short in a Hyperdrive pool.
"""
# pylint: disable=too-many-positional-arguments
return Trade(
market_type=MarketType.HYPERDRIVE,
market_action=HyperdriveMarketAction(
Expand Down Expand Up @@ -271,6 +273,7 @@ def add_liquidity_trade(
Trade[HyperdriveMarketAction]
The trade object for adding liquidity to a Hyperdrive pool.
"""
# pylint: disable=too-many-positional-arguments
if min_apr is None:
min_apr = FixedPoint(scaled_value=1)
if max_apr is None:
Expand Down
2 changes: 2 additions & 0 deletions src/agent0/core/hyperdrive/crash_report/crash_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def setup_hyperdrive_crash_report_logging(log_format_string: str | None = None)
# pylint: disable=too-many-statements
# pylint: disable=too-many-branches
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
def build_crash_trade_result(
exception: Exception,
interface: HyperdriveReadInterface,
Expand Down Expand Up @@ -218,6 +219,7 @@ def build_crash_trade_result(


# pylint: disable=too-many-locals
# pylint: disable=too-many-positional-arguments
def log_hyperdrive_crash_report(
trade_result: TradeResult,
log_level: int | None = None,
Expand Down
1 change: 1 addition & 0 deletions src/agent0/core/hyperdrive/interactive/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ def init_agent(
The agent object for a user to execute trades with.
"""
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments

policy_config = self._handle_policy_config(policy, policy_config)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def get_trades(


# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
async def async_execute_agent_trades(
trades: list[Trade[HyperdriveMarketAction]],
interface: HyperdriveReadWriteInterface,
Expand Down
1 change: 1 addition & 0 deletions src/agent0/core/hyperdrive/interactive/hyperdrive_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

# pylint: disable=protected-access
# pylint: disable=too-many-lines
# pylint: disable=too-many-positional-arguments


class HyperdriveAgent:
Expand Down
1 change: 1 addition & 0 deletions src/agent0/core/hyperdrive/interactive/local_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ def init_agent(
The agent object for a user to execute trades with.
"""
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments

# Explicit type checking
if pool is not None and not isinstance(pool, LocalHyperdrive):
Expand Down
3 changes: 2 additions & 1 deletion src/agent0/core/hyperdrive/interactive/local_hyperdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ def get_hyperdrive_pools_from_registry(

return registered_pools

# pylint: disable=too-many-arguments
def __init__(
self,
chain: LocalChain,
Expand Down Expand Up @@ -282,6 +281,8 @@ def __init__(
NOTE: backfilling can be slow.
"""
# pylint: disable=too-many-branches
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments

# We don't call super's init since we do specific type checking
# in Hyperdrive's init. Instead, we call _initialize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
from .hyperdrive import Hyperdrive
from .local_chain import LocalChain

# TODO cleanup
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments


class LocalHyperdriveAgent(HyperdriveAgent):
"""Interactive Local Hyperdrive Agent."""
Expand Down Expand Up @@ -77,7 +81,6 @@ def __init__(
private_key: str | None, optional
The private key of the associated account. Default is auto-generated.
"""
# pylint: disable=too-many-arguments

# Explicit type checking
if pool is not None and not isinstance(pool, LocalHyperdrive):
Expand Down Expand Up @@ -138,7 +141,6 @@ def add_funds(
If the token is not in the mapping, will attempt to call `mint` on
the token contract. Defaults to an empty mapping.
"""
# pylint: disable=too-many-arguments

# Explicit type checking
if pool is not None and not isinstance(pool, LocalHyperdrive):
Expand Down Expand Up @@ -542,7 +544,6 @@ def get_positions(
pd.DataFrame
The agent's positions across all hyperdrive pools.
"""
# pylint: disable=too-many-arguments

if registry_address is not None:
raise ValueError("registry_address not used with local agents")
Expand Down
1 change: 1 addition & 0 deletions src/agent0/core/hyperdrive/policies/lpandarb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# pylint: disable=too-many-arguments
# ruff: noqa: PLR0913
# pylint: disable=too-many-locals
# pylint: disable=too-many-positional-arguments


def arb_fixed_rate_down(
Expand Down
12 changes: 6 additions & 6 deletions src/agent0/ethpy/base/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
# too many return statements in _contract_function_abi_outputs
# ruff: noqa: PLR0911

# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
# pylint: disable=too-many-locals


# We define the function to check the exception to retry on
# for preview calls.
Expand Down Expand Up @@ -140,10 +144,6 @@ def smart_contract_read(
return {f"value{idx}": value for idx, value in enumerate(return_values)}


# TODO cleanup
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-arguments
def smart_contract_preview_transaction(
contract: Contract,
signer_address: ChecksumAddress,
Expand Down Expand Up @@ -189,6 +189,8 @@ def smart_contract_preview_transaction(
function to dynamically assign types to output variables
would be cool if this also put stuff into FixedPoint
"""
# TODO cleanup
# pylint: disable=too-many-branches
if read_retry_count is None:
read_retry_count = DEFAULT_READ_RETRY_COUNT
if block_identifier is None:
Expand Down Expand Up @@ -578,8 +580,6 @@ async def _async_send_transaction_and_wait_for_receipt(
return tx_receipt


# TODO cleanup args
# pylint: disable=too-many-arguments
async def async_smart_contract_transact(
web3: Web3,
contract: Contract,
Expand Down
1 change: 1 addition & 0 deletions src/agent0/ethpy/hyperdrive/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
# Deploying a Hyperdrive pool requires a long sequence of contract and RPCs,
# resulting in long functions with many parameter arguments.
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
# pylint: disable=too-many-locals

UINT256_MAX = int(2**256 - 1)
Expand Down
1 change: 1 addition & 0 deletions src/agent0/ethpy/hyperdrive/interface/_contract_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
# Number of arguments is influenced by the underlying solidity contract
# pylint: disable=too-many-arguments
# ruff: noqa: PLR0913
# pylint: disable=too-many-positional-arguments


def _get_total_supply_withdrawal_shares(
Expand Down
1 change: 1 addition & 0 deletions src/agent0/ethpy/hyperdrive/interface/read_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-public-methods
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
# pylint: disable=too-many-statements
# ruff: noqa: PLR0913
# We only worry about protected access for anyone outside of this folder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

# We have no control over the number of arguments since it is specified by the smart contracts
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
# ruff: noqa: PLR0913
# We only worry about protected access for anyone outside of this folder.
# pylint: disable=protected-access
Expand Down
23 changes: 18 additions & 5 deletions src/agent0/hyperfuzz/system_fuzz/invariant_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def run_invariant_checks(
# TODO cleanup
# pylint: disable=too-many-locals
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
# pylint: disable=too-many-branches

logging.info("Running invariant checks on pool %s", pool_name)
Expand Down Expand Up @@ -225,14 +226,26 @@ def _check_negative_interest(interface: HyperdriveReadInterface, pool_state: Poo
previous_vault_share_price = previous_pool_state.pool_info.vault_share_price

if (current_vault_share_price - previous_vault_share_price) <= -NEGATIVE_INTEREST_EPSILON:
exception_message = (
f"Negative interest detected. {current_vault_share_price=}, {previous_vault_share_price=}. "
f"Difference in wei: {current_vault_share_price.scaled_value - previous_vault_share_price.scaled_value}."
)
exception_data["invariance_check:current_vault_share_price"] = current_vault_share_price
exception_data["invariance_check:previous_vault_share_price"] = previous_vault_share_price
failed = True
log_level = logging.ERROR
# Different error messages and log levels if the pool is paused
if interface.get_pool_is_paused():
exception_message = (
"Negative interest detected on paused pool. "
f"{current_vault_share_price=}, {previous_vault_share_price=}. "
"Difference in wei: "
f"{current_vault_share_price.scaled_value - previous_vault_share_price.scaled_value}."
)
log_level = logging.WARNING
else:
exception_message = (
"Negative interest detected on unpaused pool. "
f"{current_vault_share_price=}, {previous_vault_share_price=}. "
"Difference in wei: "
f"{current_vault_share_price.scaled_value - previous_vault_share_price.scaled_value}."
)
log_level = logging.CRITICAL

return InvariantCheckResults(failed, exception_message, exception_data, log_level=log_level)

Expand Down
1 change: 1 addition & 0 deletions src/agent0/hyperfuzz/system_fuzz/run_fuzz_bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def run_fuzz_bots(
"""
# TODO cleanup
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
Expand Down
Loading

0 comments on commit ee041f0

Please sign in to comment.