Skip to content

Commit

Permalink
Agent refactor (#1487)
Browse files Browse the repository at this point in the history
This PR is the 3rd PR in a series of PRs for agent0 to support multiple
pools.

The agent classes in agent0 is reworked in this PR. Here, the main
changes include `init_agent` being a function of the chain as opposed to
the pool. Additionally, many functions now take a `pool` argument to
specify what pool the agent is making a trade on. There are helper
functions for setting an active pool to avoid passing this argument in
various functions, by calling it either in `init_agent`, or by calling
`agent.set_active(pool=...)`.

Additionally, the policy is now untied from the agent, such that an
agent can hot-swap policies on the fly by calling the
`agent.set_active(policy=...)` function.

- `init_agent` is now a function of a `LocalChain` or `Chain` object.
- This function now takes an optional `pool` argument for setting an
initial active pool.
- Many functions now require an active pool to be set, or an explicit
pool to be passed in as an argument.
- `PolicyAgent` has been deprecated in favor of using `LocalAccount`.
- Moving many logging and bookkeeping configs from `Hyperdrive` and
`LocalHyperdrive` to `Chain` and `LocalChain` respectively.
- No longer keeping track of wallet deltas, we get the wallet wrt a pool
via `get_wallet()`.
- Adding helper functions for `get_long()`, `get_short()`, `get_lp()`,
and `get_withdrawal_shares()`.
- `LocalHyperdrive` now has a flag for deploying on constructor, or
attach to existing. This is necessary for the forking workflow.
- Added tests for unit and system fuzzing.
  • Loading branch information
Sheng Lundquist authored May 23, 2024
1 parent 88157f3 commit d9abea2
Show file tree
Hide file tree
Showing 65 changed files with 2,811 additions and 2,034 deletions.
20 changes: 8 additions & 12 deletions examples/interactive_hyperdrive_forking_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from fixedpointmath import FixedPoint

from agent0 import Hyperdrive, LocalChain, PolicyZoo
from agent0 import LocalChain, LocalHyperdrive, PolicyZoo
from agent0.core.base.make_key import make_private_key

# %%
Expand All @@ -26,26 +26,22 @@
# Launch a local anvil chain forked from the rpc uri.
chain = LocalChain(fork_uri=rpc_uri, fork_block_number=fork_block_number)

hyperdrive_address = Hyperdrive.get_hyperdrive_addresses_from_registry(chain, registry_address)["sdai_14_day"]
hyperdrive_address = LocalHyperdrive.get_hyperdrive_addresses_from_registry(chain, registry_address)["sdai_14_day"]

# Note that we use Hyperdrive here instead of LocalHyperdrive,
# as LocalHyperdrive deploys a new pool, whereas we want to connect to an existing pool
# on the forked local chain.
# TODO this prevents us from using data tools provided by LocalHyperdrive, ideally we can
# load a LocalHyperdrive from an Hyperdrive object that connects to an existing pool and populates
# the database. This is blocked by needing an archive node, the fix here would be to
# (1) use event data instead, and (2) build historical data from event data.
hyperdrive_config = Hyperdrive.Config()
hyperdrive_pool = Hyperdrive(chain, hyperdrive_address, hyperdrive_config)
# Note that we pass in deploy=False and pass in an existing hyperdrive_address, as we
# want to connect to the existing pool and not deploy a new one.
hyperdrive_config = LocalHyperdrive.Config()
hyperdrive_pool = LocalHyperdrive(chain, hyperdrive_config, deploy=False, hyperdrive_address=hyperdrive_address)

# %%

# Launch a new agent
private_key = make_private_key()

# Init from private key and attach policy
hyperdrive_agent0 = hyperdrive_pool.init_agent(
hyperdrive_agent0 = chain.init_agent(
private_key=private_key,
pool=hyperdrive_pool,
policy=PolicyZoo.random,
# The configuration for the underlying policy
policy_config=PolicyZoo.random.Config(rng_seed=123),
Expand Down
80 changes: 0 additions & 80 deletions examples/interactive_local_hyperdrive_advanced_example.py

This file was deleted.

Loading

0 comments on commit d9abea2

Please sign in to comment.