Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi-pool and events based database refactor (#1467)
This PR is the second in a collection of refactors to allow agent0 to support multiple pools. This PR focuses on updates to the database and related systems. In particular, this makes the following major changes to the database. ## Database schema updates - Instead of mapping a single pool to a single database, we now insert all pools into a single database, indexed by the `hyperdrive_address` field. - A new `HyperdriveAddrToName` maps the hyperdrive address to a logical name. The name defaults to `<vault_share_token_symbol>_<position_duration_in_days>_day`. - The `WalletDelta` and `HyperdriveTransactions` tables have been removed in favor of the new events table added in #1464. - The `PositionSnapshot` table calculates the absolute positions from the deltas stored in the `TradeEvent` tables. This table also stores any additional information useful for analysis, such as realized and unrealized values of positions, and the pnl. - The caching `CurrentWallet`, `Ticker`, and `WalletPNL` tables have been removed in favor of a new `PositionSnapshot` table. - The `PoolAnalysis` table has been removed in favor of putting the fields in the `PoolInfo` table. - The `UsernameToUser` table has been removed, no need to map multiple wallets to a single user. ## Analysis from remote chains - The database container is now managed by the `Chain` object as opposed to `LocalChain`. Likewise, database sessions is now managed by the `Chain` object as opposed to `LocalHyperdrive`. These changes allow the remote chain object to have access to the database for gathering events, positions, and pnl from hyperdrive pools. - The simulated environment uses the full set of tables in the database, whereas the remote chain environment uses a subset of these tables. Both workflows uses the same underlying schema, but the remote workflow updates these tables lazily, i.e., only query and insert events and snapshots into the db when the user calls the functions that need them. Additionally, the remote workflow only updates the tables with any agents initialized by agent0. In contrast, the simulated environment updates these tables with all agents that have interacted with the simulated pool, and the database is updated on every trade to these pools. ## Interactive analysis interface The interactive interface for getting information from the db has been reworked. Note that some pool analysis functions are only available in `LocalHyperdrive` method, due to lazy event querying on remote chains. ### Wallet objects and Positions - `agent.get_wallet()` has replaced `agent.get_positions()` (which replaced `agent.wallet` in #1464). This function returns a `HyperdriveWallet` object that contains the relevant positions (i.e., base, longs, shorts, lp, withdrawal shares) for a specific pool. A future PR will allow this function to take a pool as an argument, or require setting the agent's active pool. This function queries the database for current positions. - `agent.get_positions()` now returns a dataframe consisting of all open positions across all pools held by the agent, with additional pnl, unrealized, and realized value columns. An optional argument `show_closed_positions`, if set to True, will also return all closed positions and the position's realized value. - Similarity, `hyperdrive.get_positions()` returns a dataframe consisting of all positions across the `hyperdrive` pool. This function is only available in `LocalHyperdrive`. - `agent.get_trade_events()` returns a dataframe of events emitted from the agent across all pools that the agent interacted with. An optional argument `all_token_deltas`, if set to True, will repeat any `RemoveLiquidity` events with a non-zero withdrawal shares, where the additional row tracks the withdrawal share token delta. - Similarity, `hyperdrive.get_trade_events()` returns a dataframe of events emitted from the pool across all agents. This function is only available in `LocalHyperdrive`. ### Historical positions We also expose a couple of helper functions for getting positions over time. These are exclusive to `LocalHyperdrive`, due to the underlying table (i.e., `PositionSnapshot`) only being guaranteed to keep historical data for the simulated environment. - `hyperdrive.get_historical_positions()` returns a dataframe with the history of positions over time. This function is only available in `LocalHyperdrive`. - `hyperdrive.get_historical_pnl()` returns a dataframe of historical positions, aggregated across all positions to find the total pnl. This function is only available in `LocalHyperdrive`. ## Streamlit dashboard - The streamlit dashboard has been updated and cleaned up (with tests for building the DataFrames for the various views) with the new db scheams. - The dashboard now supports looking at information from multiple pools. - The `run_dashboard` command is now a function of the `LocalChain`.
- Loading branch information