agent0.core.hyperdrive.policies.lpandarb

Agent policy for LP trading that can also arbitrage on the fixed rate.

Attributes

TOLERANCE

MAX_ITER

Classes

LPandArb

LP and Arbitrage in a fixed proportion.

Functions

arb_fixed_rate_down(...)

Return an action list for arbitraging the fixed rate down to the variable rate.

arb_fixed_rate_up(...)

Return an action list for arbitraging the fixed rate up to the variable rate.

calc_shares_needed_for_bonds(→ fixedpointmath.FixedPoint)

Calculate the shares needed to trade a certain amount of bonds, and the associate governance fee.

calc_delta_reserves_for_target_rate(...)

Calculate the bonds needed to hit the desired reserves ratio, keeping shares constant.

calc_reserves_to_hit_target_rate(...)

Calculate the bonds and shares needed to hit the target fixed rate.

apply_step_to_reserves(...)

Apply a single convergence step to pool share and bond reserve levels.

apply_step_to_pool_state(...)

Save a single convergence step into the pool info.

Module Contents

agent0.core.hyperdrive.policies.lpandarb.TOLERANCE = 1e-18
agent0.core.hyperdrive.policies.lpandarb.MAX_ITER = 50
agent0.core.hyperdrive.policies.lpandarb.arb_fixed_rate_down(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, pool_state: agent0.ethpy.hyperdrive.state.PoolState, wallet: agent0.core.hyperdrive.HyperdriveWallet, max_trade_amount_base: fixedpointmath.FixedPoint, min_trade_amount_bonds: fixedpointmath.FixedPoint, slippage_tolerance: fixedpointmath.FixedPoint | None = None, base_fee_multiple: float | None = None, priority_fee_multiple: float | None = None) list[agent0.core.base.Trade[agent0.core.hyperdrive.HyperdriveMarketAction]]

Return an action list for arbitraging the fixed rate down to the variable rate.

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

  • pool_state (PoolState) – The hyperdrive pool state.

  • wallet (HyperdriveWallet) – The agent’s wallet.

  • max_trade_amount_base (FixedPoint) – The maximum amount of base allowed to trade.

  • min_trade_amount_bonds (FixedPoint) – The minimum amount of bonds needed to open a trade.

  • slippage_tolerance (FixedPoint | None, optional) – The slippage tolerance for trades. Defaults to None.

  • base_fee_multiple (float | None, optional) – The base fee multiple for transactions. Defaults to None.

  • priority_fee_multiple (float | None, optional) – The priority fee multiple for transactions. Defaults to None.

Returns:

A list of actions for arbitration trades.

Return type:

list[MarketAction]

agent0.core.hyperdrive.policies.lpandarb.arb_fixed_rate_up(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, pool_state: agent0.ethpy.hyperdrive.state.PoolState, wallet: agent0.core.hyperdrive.HyperdriveWallet, max_trade_amount_base: fixedpointmath.FixedPoint, min_trade_amount_bonds: fixedpointmath.FixedPoint, slippage_tolerance: fixedpointmath.FixedPoint | None = None, base_fee_multiple: float | None = None, priority_fee_multiple: float | None = None) list[agent0.core.base.Trade[agent0.core.hyperdrive.HyperdriveMarketAction]]

Return an action list for arbitraging the fixed rate up to the variable rate.

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

  • pool_state (PoolState) – The hyperdrive pool state.

  • wallet (HyperdriveWallet) – The agent’s wallet.

  • max_trade_amount_base (FixedPoint) – The maximum amount of base allowed to trade.

  • min_trade_amount_bonds (FixedPoint) – The minimum amount of bonds needed to open a trade.

  • slippage_tolerance (FixedPoint | None, optional) – The slippage tolerance for trades. Defaults to None.

  • base_fee_multiple (float | None, optional) – The base fee multiple for transactions. Defaults to None.

  • priority_fee_multiple (float | None, optional) – The priority fee multiple for transactions. Defaults to None.

Returns:

A list of actions for arbitration trades.

Return type:

list[MarketAction]

agent0.core.hyperdrive.policies.lpandarb.calc_shares_needed_for_bonds(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, pool_state: agent0.ethpy.hyperdrive.state.PoolState, bonds_needed: fixedpointmath.FixedPoint, min_trade_amount_bonds: fixedpointmath.FixedPoint) fixedpointmath.FixedPoint

Calculate the shares needed to trade a certain amount of bonds, and the associate governance fee.

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

  • pool_state (PoolState) – The hyperdrive pool state.

  • bonds_needed (FixedPoint) – The given amount of bonds that is going to be traded.

  • min_trade_amount_bonds (FixedPoint) – The minimum amount of bonds needed to open a trade.

Returns:

The change in shares in the pool for the given amount of bonds.

Return type:

FixedPoint

agent0.core.hyperdrive.policies.lpandarb.calc_delta_reserves_for_target_rate(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, pool_state: agent0.ethpy.hyperdrive.state.PoolState, target_rate: fixedpointmath.FixedPoint, min_trade_amount_bonds: fixedpointmath.FixedPoint) tuple[fixedpointmath.FixedPoint, fixedpointmath.FixedPoint]

Calculate the bonds needed to hit the desired reserves ratio, keeping shares constant.

delta_bonds tells us the number of bonds to hit the desired reserves ratio, keeping shares constant. However trades modify both bonds and shares in amounts of equal value. We modify bonds by only half of delta_bonds, knowing that an amount of equal value will move shares in the other direction, toward our desired ratio. This guess is very bad when slippage is high, so we check how bad, then scale accordingly. To avoid negative share reserves, we increase the divisor until they are no longer negative.

Parameters:
  • interface (HyperdriveReadInterface) – Interface for the market on which this agent will be executing trades (MarketActions).

  • pool_state (PoolState) – The current pool state.

  • target_rate (FixedPoint) – The target rate the pool will have after the calculated change in bonds and shares.

  • min_trade_amount_bonds (FixedPoint) – The minimum amount of bonds needed to open a trade.

Returns:

The delta (bonds, shares) needed to hit the desired fixed rate.

Return type:

tuple[FixedPoint, FixedPoint]

agent0.core.hyperdrive.policies.lpandarb.calc_reserves_to_hit_target_rate(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, pool_state: agent0.ethpy.hyperdrive.state.PoolState, target_rate: fixedpointmath.FixedPoint, min_trade_amount_bonds: fixedpointmath.FixedPoint) tuple[fixedpointmath.FixedPoint, fixedpointmath.FixedPoint]

Calculate the bonds and shares needed to hit the target fixed rate.

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

  • pool_state (PoolState) – The current pool state.

  • target_rate (FixedPoint) – The target rate the pool will have after the calculated change in bonds and shares.

  • min_trade_amount_bonds (FixedPoint) – The minimum amount of bonds needed to open a trade.

Returns:

total_shares_needed: FixedPoint

Total amount of shares needed to be added into the pool to hit the target rate.

total_bonds_needed: FixedPoint

Total amount of bonds needed to be added into the pool to hit the target rate.

Return type:

tuple[FixedPoint, FixedPoint, int]

agent0.core.hyperdrive.policies.lpandarb.apply_step_to_reserves(share_reserves: fixedpointmath.FixedPoint, delta_shares: fixedpointmath.FixedPoint, bond_reserves: fixedpointmath.FixedPoint, delta_bonds: fixedpointmath.FixedPoint) tuple[fixedpointmath.FixedPoint, fixedpointmath.FixedPoint]

Apply a single convergence step to pool share and bond reserve levels.

Parameters:
  • share_reserves (FixedPoint) – The current Hyperdrive pool’s share reserves.

  • delta_shares (FixedPoint) – The amount of shares to add or remove from the reserves, depending on the delta bonds sign.

  • bond_reserves (FixedPoint) – The current Hyperdrive pool’s bond reserves.

  • delta_bonds (FixedPoint) – The amount of bonds to add or remove from the reserves.

Returns:

The resulting share reserves and bond reserves after the delta updates are applied.

Return type:

tuple[FixedPoint, FixedPoint]

agent0.core.hyperdrive.policies.lpandarb.apply_step_to_pool_state(pool_state: agent0.ethpy.hyperdrive.state.PoolState, delta_bonds: fixedpointmath.FixedPoint, delta_shares: fixedpointmath.FixedPoint) agent0.ethpy.hyperdrive.state.PoolState

Save a single convergence step into the pool info.

Parameters:
  • pool_state (PoolState) – The current pool state.

  • delta_bonds (FixedPoint) – The amount of bonds that is going to be traded.

  • delta_shares (FixedPoint) – The amount of shares that is going to be traded.

Returns:

The updated pool state.

Return type:

PoolState

class agent0.core.hyperdrive.policies.lpandarb.LPandArb(policy_config: Config)

Bases: agent0.core.hyperdrive.policies.hyperdrive_policy.HyperdriveBasePolicy

LP and Arbitrage in a fixed proportion.

classmethod description() str

Describe the policy in a user friendly manner that allows newcomers to decide whether to use it.

Returns:

The description of the policy, as described above.

Return type:

str

class Config

Bases: agent0.core.hyperdrive.policies.hyperdrive_policy.HyperdriveBasePolicy.Config

Custom config arguments for this policy.

lp_portion: fixedpointmath.FixedPoint

The portion of capital assigned to LP. Defaults to 0.

high_fixed_rate_thresh: fixedpointmath.FixedPoint

Amount over variable rate to arbitrage.

low_fixed_rate_thresh: fixedpointmath.FixedPoint

Amount below variable rate to arbitrage. Defaults to 0.

auto_fixed_rate_thresh: bool = False

If set, override the high and low rate thresholds to compute profitable amounts based on fees.

rate_slippage: fixedpointmath.FixedPoint
done_on_empty: bool = False

Whether to exit the bot if there are no trades.

min_trade_amount_bonds: fixedpointmath.FixedPoint

The minimum bond trade amount below which the agent won’t submit a trade.

property arb_portion: fixedpointmath.FixedPoint

The portion of capital assigned to arbitrage.

min_trade_amount_bonds
action(interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, wallet: agent0.core.hyperdrive.HyperdriveWallet) tuple[list[agent0.core.base.Trade[agent0.core.hyperdrive.HyperdriveMarketAction]], bool]

Specify actions.

Parameters:
Returns:

A tuple where the first element is a list of actions, and the second element defines if the agent is done trading.

Return type:

tuple[list[MarketAction], bool]