delvtech / agent0

Analysis & simulation repo for Delv
https://agent0.readthedocs.io/en/latest/
Apache License 2.0
45 stars 18 forks source link

Adding event queries to db #1464

Closed slundqui closed 2 months ago

slundqui commented 2 months ago

This PR is the first of a series of PRs to support multi-pool trading in agent0.

In this PR, we move away from exposing the agent.wallet object (as we're deprecating doing bookkeeping on the wallet itself in python) in favor of using the agent.get_positions() function. This function (1) does a query of the chain to gather events and adds them to a TradeEvent db table, and (2) queries from the TradeEvent table to get the current positions a wallet has.

The TradeEvent table handles all events on any hyperdrive tokens (i.e., long/short/lp). There's a bit of overlap with the WalletDelta table, with the main exception that the TradeEvent table is lazy - the table only gets updated when agent.get_positions() gets called, and only with the events from agent. In addition, the table handles both trade events (e.g., OpenLong) and single transfer trades (e.g., wallet to wallet transfers of tokens). We likely can deprecate the WalletDelta table with a special call to gather all trade events from a Hyperdrive pool, which fills the TradeEvent table with every wallet that has made a trade on the pool.

There are a couple of places that can be optimized. Currently, we query the chain for events for every get_positions call (from the latest entry in the db to latest block). Some bookkeeping is needed to e.g., don't get events from the logs if a user calls get_positions on the same block.

As a temporary fix, we also remove agent.wallet from remote chains, and get_positions gathers all events from the remote chain each time it's called. This will get fixed once the database is exposed in the underlying chain object, with the remote wallet also using the TradeEvent table to gather wallet positions.

Final note: the failing test here is fixed in https://github.com/delvtech/agent0/pull/1462.

Changes

codecov[bot] commented 2 months ago

Codecov Report

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

Project coverage is 82.03%. Comparing base (14475b2) to head (ab09336).

:exclamation: Current head ab09336 differs from pull request most recent head 4ef854c. Consider uploading reports for the commit 4ef854c to get more accurate results

Files Patch % Lines
src/agent0/chainsync/db/hyperdrive/interface.py 79.31% 6 Missing :warning:
src/agent0/chainsync/df_to_db.py 76.47% 4 Missing :warning:
...t0/core/hyperdrive/interactive/local_hyperdrive.py 93.54% 2 Missing :warning:
src/agent0/chainsync/db/hyperdrive/chain_to_db.py 99.20% 1 Missing :warning:
...c/agent0/core/hyperdrive/interactive/hyperdrive.py 83.33% 1 Missing :warning:
...t0/core/hyperdrive/interactive/hyperdrive_agent.py 80.00% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1464 +/- ## ========================================== + Coverage 81.27% 82.03% +0.75% ========================================== Files 89 90 +1 Lines 5769 5963 +194 ========================================== + Hits 4689 4892 +203 + Misses 1080 1071 -9 ``` | [Flag](https://app.codecov.io/gh/delvtech/agent0/pull/1464/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=delvtech) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/delvtech/agent0/pull/1464/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=delvtech) | `82.03% <94.20%> (+0.75%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=delvtech#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.