Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-pool and events based database refactor #1467

Merged
merged 79 commits into from
May 17, 2024

Conversation

slundqui
Copy link
Contributor

@slundqui slundqui commented May 15, 2024

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 Adding event queries to db #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 Adding event queries to db #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.

Copy link

codecov bot commented May 16, 2024

Codecov Report

Attention: Patch coverage is 87.60234% with 106 lines in your changes are missing coverage. Please review.

Project coverage is 83.54%. Comparing base (9950db3) to head (dc79d09).

Current head dc79d09 differs from pull request most recent head fbf0dda

Please upload reports for the commit fbf0dda to get more accurate results.

Files Patch % Lines
.../agent0/core/hyperdrive/interactive/local_chain.py 50.94% 26 Missing ⚠️
src/agent0/chainsync/db/hyperdrive/convert_data.py 82.87% 25 Missing ⚠️
src/agent0/core/hyperdrive/interactive/chain.py 66.66% 18 Missing ⚠️
src/agent0/chainsync/db/hyperdrive/interface.py 89.84% 13 Missing ⚠️
...t0/core/hyperdrive/interactive/local_hyperdrive.py 78.00% 11 Missing ⚠️
...c/agent0/core/hyperdrive/interactive/hyperdrive.py 94.04% 5 Missing ⚠️
src/agent0/chainsync/exec/acquire_data.py 66.66% 3 Missing ⚠️
src/agent0/chainsync/exec/analyze_data.py 75.00% 2 Missing ⚠️
...c/agent0/chainsync/analysis/calc_position_value.py 95.65% 1 Missing ⚠️
.../agent0/chainsync/dashboard/build_dashboard_dfs.py 98.38% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1467      +/-   ##
==========================================
+ Coverage   82.28%   83.54%   +1.26%     
==========================================
  Files          90       87       -3     
  Lines        6019     5623     -396     
==========================================
- Hits         4953     4698     -255     
+ Misses       1066      925     -141     
Flag Coverage Δ
unittests 83.54% <87.60%> (+1.26%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@dpaiton dpaiton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heroic effort! Well done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants