agent0.core.hyperdrive.interactive.exec

Helper functions for executing agent trades.

Submodules

Functions

check_for_new_block(→ tuple[bool, web3.types.BlockData])

Returns True if the chain has ticked to a block that is newer than the input block.

async_execute_agent_trades(...)

Executes a single agent's trade based on its policy.

async_execute_single_trade(...)

Executes a single trade made by the agent.

get_liquidation_trades(...)

Get liquidation trades.

get_trades(...)

Get trades from the policy.

Package Contents

agent0.core.hyperdrive.interactive.exec.check_for_new_block(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, last_block_number: eth_typing.BlockNumber | int) tuple[bool, web3.types.BlockData]

Returns True if the chain has ticked to a block that is newer than the input block.

Parameters:
  • interface (HyperdriveReadInterface) – The Hyperdrive API interface object.

  • last_block_number (BlockNumber | int) – The last block number to check against.

Returns:

Tuple with a boolean indicating if the latest block is newer than the last_block input, as well as the latest block data.

Return type:

tuple[bool, BlockData]

async agent0.core.hyperdrive.interactive.exec.async_execute_agent_trades(trades: list[agent0.core.base.Trade[agent0.core.hyperdrive.HyperdriveMarketAction]], interface: agent0.ethpy.hyperdrive.HyperdriveReadWriteInterface, account: eth_account.signers.local.LocalAccount, wallet_func: Callable[[], agent0.core.hyperdrive.HyperdriveWallet], policy: agent0.core.hyperdrive.policies.HyperdriveBasePolicy | None, preview_before_trade: bool, nonce_func: Callable[[], web3.types.Nonce] | None = None) list[agent0.core.hyperdrive.TradeResult]

Executes a single agent’s trade based on its policy. This function is async as _match_contract_call_to_trade waits for a transaction receipt.

Note

This function is not thread safe, as

  1. the agent’s policies action and post_action may not be thread safe, and

  2. the agent’s wallet update is not thread safe.

  3. acquiring the base nonce for this set of trades is not thread safe.

Parameters:
  • trades (list[Trade[HyperdriveMarketAction]]) – The list of trades to execute.

  • interface (HyperdriveReadWriteInterface) – The Hyperdrive API interface object.

  • account (LocalAccount) – The account that is conducting the trade.

  • wallet_func (Callable[[], HyperdriveWallet]) – The function to get the agent’s wallet object tied to a pool. We only use this function in crash reporting, so we only call this if a trade fails. NOTE: this function parameter doesn’t receive any arguments, so a partial function must be made to pass in arguments, e.g., the pool.

  • policy (HyperdriveBasePolicy) – The policy being executed.

  • preview_before_trade (bool) – Whether or not to preview the trade before it is executed.

  • nonce_func (Callable[[], Nonce] | None) – A callable function to use to get a nonce. This function is useful for e.g., passing in a safe nonce getter tied to an agent. Defaults to setting it to the result of get_transaction_count.

Returns:

Returns a list of TradeResult objects, one for each trade made by the agent. TradeResult handles any information about the trade, as well as any trade errors.

Return type:

list[TradeResult]

async agent0.core.hyperdrive.interactive.exec.async_execute_single_trade(interface: agent0.ethpy.hyperdrive.HyperdriveReadWriteInterface, account: eth_account.signers.local.LocalAccount, wallet_func: Callable[[], agent0.core.hyperdrive.HyperdriveWallet], trade_object: agent0.core.base.Trade[agent0.core.hyperdrive.HyperdriveMarketAction], execute_policy_post_action: bool, preview_before_trade: bool, nonce_func: Callable[[], web3.types.Nonce] | None = None, policy: agent0.core.hyperdrive.policies.HyperdriveBasePolicy | None = None) agent0.core.hyperdrive.TradeResult

Executes a single trade made by the agent.

Note

This function is not thread safe, as

  1. the agent’s wallet update is not thread safe

  2. acquiring the nonce for this trade is not thread safe

Parameters:
  • interface (HyperdriveReadWriteInterface) – The Hyperdrive API interface object.

  • account (LocalAccount) – The LocalAccount that is conducting the trade.

  • wallet_func (Callable[[], HyperdriveWallet]) – The function to get the agent’s wallet object tied to a pool. We only use this function in crash reporting, so we only call this if a trade fails. NOTE: this function parameter doesn’t receive any arguments, so a partial function must be made to pass in arguments, e.g., the pool.

  • trade_object (Trade[HyperdriveMarketAction]) – The trade to execute.

  • execute_policy_post_action (bool) – Whether or not to execute the post_action of the policy after the trade.

  • preview_before_trade (bool) – Whether or not to preview the trade before it is executed.

  • nonce_func (Callable[[], Nonce] | None) – A callable function to use to get a nonce. This function is useful for e.g., passing in a safe nonce getter tied to an agent. Defaults to setting it to the result of get_transaction_count.

  • policy (HyperdriveBasePolicy | None, optional) – The policy attached to the agent. This is only used to potentially call post_action of the policy.

Returns:

The result of the trade.

Return type:

TradeResult

agent0.core.hyperdrive.interactive.exec.get_liquidation_trades(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, wallet: agent0.core.hyperdrive.HyperdriveWallet, randomize_trades: bool, rng: numpy.random.Generator) list[agent0.core.base.Trade[agent0.core.hyperdrive.HyperdriveMarketAction]]

Get liquidation trades.

Parameters:
  • interface (HyperdriveReadInterface) – The read interface for the market.

  • wallet (HyperdriveWallet) – The wallet of the account.

  • randomize_trades (bool) – Whether to randomize trades.

  • rng (Generator) – The random number generator for randomizing trades.

Returns:

The list of liquidation trades.

Return type:

list[Trade[HyperdriveMarketAction]]

agent0.core.hyperdrive.interactive.exec.get_trades(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, policy: agent0.core.hyperdrive.policies.HyperdriveBasePolicy, wallet: agent0.core.hyperdrive.HyperdriveWallet, default_gas_limit: int | None = None) list[agent0.core.base.Trade[agent0.core.hyperdrive.HyperdriveMarketAction]]

Get trades from the policy.

Parameters:
  • interface (HyperdriveReadInterface) – The read interface for the market.

  • policy (HyperdriveBasePolicy) – The policy attached to the agent.

  • wallet (HyperdriveWallet) – The wallet of the account.

  • default_gas_limit (int | None) – The default gas limit to use if not provided by the policy.

Returns:

The list of trades from the policy.

Return type:

list[Trade[HyperdriveMarketAction]]