Skip to content

Commit

Permalink
Adding sync interactive hyperdrive agent from chain api (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
slundqui authored Apr 10, 2024
1 parent 98488ff commit 495d0b0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/agent0/core/hyperdrive/interactive/i_hyperdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ def _init_agent(
# with the same underlying account
self.chain._ensure_no_duplicate_addrs(agent.checksum_address) # pylint: disable=protected-access

agent.wallet = build_wallet_positions_from_chain(agent, self.interface)
self._sync_wallet(agent)
return agent

def _set_max_approval(self, agent: HyperdriveAgent):
def _set_max_approval(self, agent: HyperdriveAgent) -> None:
# Establish max approval for the hyperdrive contract
set_max_approval(
agent,
Expand All @@ -218,6 +218,10 @@ def _set_max_approval(self, agent: HyperdriveAgent):
retry_count=5,
)

def _sync_wallet(self, agent: HyperdriveAgent) -> None:
# TODO add sync from db
agent.wallet = build_wallet_positions_from_chain(agent, self.interface)

def _add_funds(
self, agent: HyperdriveAgent, base: FixedPoint, eth: FixedPoint, signer_account: LocalAccount | None = None
) -> None:
Expand Down
9 changes: 9 additions & 0 deletions src/agent0/core/hyperdrive/interactive/i_hyperdrive_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,12 @@ def set_max_approval(self) -> None:
"""
self._pool._set_max_approval(self.agent)

def sync_wallet_from_chain(self) -> None:
"""Explicitly syncs the wallet to the current state of the chain.
Uses on chain events to generate current wallet positions.
.. note:: This function can be slow, use it sparingly.
"""
self._pool._sync_wallet(self.agent)
58 changes: 58 additions & 0 deletions src/agent0/core/hyperdrive/interactive/i_hyperdrive_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,61 @@ def test_no_policy_call(chain: ILocalChain, check_remote_chain: bool):
# Attempt to execute agent policy, should throw value error
with pytest.raises(ValueError):
hyperdrive_agent.execute_policy_action()


@pytest.mark.anvil
@pytest.mark.parametrize("check_remote_chain", [True, False])
def test_sync_wallet_from_chain(chain: ILocalChain, check_remote_chain: bool):
"""Deploy a local chain and point the remote interface to the local chain."""
# Parameters for pool initialization. If empty, defaults to default values, allows for custom values if needed
# We explicitly set initial liquidity here to ensure we have withdrawal shares when trading
initial_pool_config = ILocalHyperdrive.Config(
initial_liquidity=FixedPoint(1_000),
initial_fixed_apr=FixedPoint("0.05"),
position_duration=60 * 60 * 24 * 365, # 1 year
)
# Launches a local hyperdrive pool
# This deploys the pool
interactive_local_hyperdrive = ILocalHyperdrive(chain, initial_pool_config)

# Gather relevant objects from the local hyperdrive
hyperdrive_addresses = interactive_local_hyperdrive.get_hyperdrive_address()

# Connect to the local chain using the remote hyperdrive interface
if check_remote_chain:
remote_chain = IChain(chain.rpc_uri)
interactive_remote_hyperdrive = IHyperdrive(remote_chain, hyperdrive_addresses)
else:
interactive_remote_hyperdrive = IHyperdrive(chain, hyperdrive_addresses)

# Generate trading agents from the interactive object using the same underlying wallet
private_key = make_private_key()
hyperdrive_remote_agent = interactive_remote_hyperdrive.init_agent(private_key=private_key)
hyperdrive_local_agent = interactive_local_hyperdrive.init_agent(private_key=private_key)

# TODO check balance of calls in this test

# Add funds to the local agent, remote agent wallet doesn't update
hyperdrive_local_agent.add_funds(base=FixedPoint(1_111_111), eth=FixedPoint(111))
hyperdrive_local_agent.add_liquidity(base=FixedPoint(111_111))
hyperdrive_local_agent.open_long(base=FixedPoint(222))
hyperdrive_local_agent.open_short(bonds=FixedPoint(333))

# Sync the remote agent wallet from the chain and ensure it's correct
hyperdrive_remote_agent.sync_wallet_from_chain()
assert len(hyperdrive_remote_agent.wallet.longs) == 1
assert len(hyperdrive_remote_agent.wallet.shorts) == 1
# We only check balances of shorts
assert list(hyperdrive_remote_agent.wallet.shorts.values())[0].balance == FixedPoint(333)

# Add funds to the remote agent and see local agent wallet doesn't update
hyperdrive_remote_agent.add_funds(base=FixedPoint(1_111_111), eth=FixedPoint(111))
hyperdrive_remote_agent.add_liquidity(base=FixedPoint(111_111))
hyperdrive_remote_agent.open_long(base=FixedPoint(222))
hyperdrive_remote_agent.open_short(bonds=FixedPoint(333))

# Sync local wallet from chain and ensure it's correct
hyperdrive_local_agent.sync_wallet_from_chain()
assert len(hyperdrive_local_agent.wallet.longs) == 1
assert len(hyperdrive_local_agent.wallet.shorts) == 1
assert list(hyperdrive_local_agent.wallet.shorts.values())[0].balance == FixedPoint(666)
7 changes: 7 additions & 0 deletions src/agent0/core/hyperdrive/interactive/i_local_hyperdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,13 @@ def _init_local_agent(
add_addr_to_username(name, [agent.address], self.db_session)
return agent

def _sync_wallet(self, agent: HyperdriveAgent) -> None:
# TODO add sync from db
super()._sync_wallet(agent)
# Ensure db is up to date
if not self.chain.experimental_data_threading:
self._run_blocking_data_pipeline()

def _add_funds(
self, agent: HyperdriveAgent, base: FixedPoint, eth: FixedPoint, signer_account: LocalAccount | None = None
) -> None:
Expand Down

0 comments on commit 495d0b0

Please sign in to comment.