agent0.chainsync.analysis.db_to_analysis

Functions to gather data from postgres, do analysis, and add back into postgres.

Functions

db_to_analysis(→ None)

Function to query postgres data tables and insert to analysis tables.

snapshot_positions_to_db(interfaces, wallet_addr, ...)

Function to query the trade events table and takes a snapshot

Module Contents

agent0.chainsync.analysis.db_to_analysis.db_to_analysis(db_session: sqlalchemy.orm.Session, interfaces: list[agent0.ethpy.hyperdrive.HyperdriveReadInterface], block_number: int, calc_pnl: bool = True) None

Function to query postgres data tables and insert to analysis tables. Executes analysis on a batch of blocks, defined by start and end block.

Parameters:
  • db_session (Session) – The initialized db session.

  • interfaces (list[HyperdriveReadInterface]) – A collection of Hyperdrive interface objects, each connected to a pool.

  • block_number (int) – The block number to run analysis on.

  • calc_pnl (bool, optional) – Whether to calculate pnl. Defaults to True.

agent0.chainsync.analysis.db_to_analysis.snapshot_positions_to_db(interfaces: list[agent0.ethpy.hyperdrive.HyperdriveReadInterface], wallet_addr: str | None, calc_pnl: bool, db_session: sqlalchemy.orm.Session, block_number: int)

Function to query the trade events table and takes a snapshot of the current positions and pnl.

Note

This function does not scale well in simulation mode, as this table grows for all wallets, for all positions, for every snapshot period (currently set to every block).

We can try to alleviate this by (1) increasing the snapshot period, and (2) removing duplicate entries of closed positions (since their realized_value never changes).

This shouldn’t be a problem for remote mode, as we limit this table to (1) only agents managed by agent0, and (2) only adds an entry for every explicit “get_all_positions” call.

Parameters:
  • interfaces (list[HyperdriveReadInterface]) – A collection of Hyperdrive interface objects, each connected to a pool.

  • wallet_addr (str | None) – The wallet address to query. If None, will not filter events by wallet addr.

  • db_session (Session) – The database session.

  • calc_pnl (bool) – Whether to calculate pnl.

  • block_number (int) – The block number to snapshot positions on.