Skip to content

Commit

Permalink
fix delta(s) var name
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaiton committed Jul 22, 2024
1 parent 60050f3 commit 95e96fb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
8 changes: 4 additions & 4 deletions bindings/hyperdrivepy/python/hyperdrivepy/hyperdrive_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def calculate_pool_deltas_after_open_long(
pool_info: types.PoolInfoType,
base_amount: str,
) -> tuple[str, str]:
"""Calculate the bond deltas to be applied to the pool after opening a long.
"""Calculate the share and bond deltas to be applied to the pool after opening a long.
Arguments
---------
Expand Down Expand Up @@ -309,12 +309,12 @@ def calculate_open_short(
return _get_interface(pool_config, pool_info).calculate_open_short(bond_amount, open_vault_share_price)


def calculate_pool_deltas_after_open_short(
def calculate_pool_share_delta_after_open_short(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
bond_amount: str,
) -> str:
"""Calculate the share deltas to be applied to the pool after opening a short.
"""Calculate the share delta to be applied to the pool after opening a short.
Arguments
---------
Expand All @@ -332,7 +332,7 @@ def calculate_pool_deltas_after_open_short(
str (FixedPoint)
The amount of shares to add to the pool reserves.
"""
return _get_interface(pool_config, pool_info).calculate_pool_deltas_after_open_short(bond_amount)
return _get_interface(pool_config, pool_info).calculate_pool_share_delta_after_open_short(bond_amount)


def calculate_close_short(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl HyperdriveState {
})?);
let (share_result_fp, bond_result_fp) = self
.state
.calculate_pool_share_bond_deltas_after_open_long(base_amount_fp, None)
.calculate_pool_deltas_after_open_long(base_amount_fp, None)
.map_err(|err| {
PyErr::new::<PyValueError, _>(format!(
"calculate_pool_deltas_after_open_long: {:?}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ impl HyperdriveState {
Ok(result)
}

pub fn calculate_pool_deltas_after_open_short(&self, bond_amount: &str) -> PyResult<String> {
pub fn calculate_pool_share_delta_after_open_short(
&self,
bond_amount: &str,
) -> PyResult<String> {
let bond_amount_fp = FixedPoint::from(U256::from_dec_str(bond_amount).map_err(|err| {
PyErr::new::<PyValueError, _>(format!(
"Failed to convert bond_amount string {} to U256: {}",
Expand All @@ -44,10 +47,10 @@ impl HyperdriveState {
})?);
let result_fp = self
.state
.calculate_pool_share_deltas_after_open_short(bond_amount_fp)
.calculate_pool_share_delta_after_open_short(bond_amount_fp)
.map_err(|err| {
PyErr::new::<PyValueError, _>(format!(
"calculate_pool_deltas_after_open_short: {}",
"calculate_pool_share_delta_after_open_short: {}",
err
))
})?;
Expand Down
14 changes: 7 additions & 7 deletions crates/hyperdrive-math/src/long/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ impl State {
pub fn calculate_pool_state_after_open_long(
&self,
base_amount: FixedPoint,
maybe_bond_deltas: Option<FixedPoint>,
maybe_bond_delta: Option<FixedPoint>,
) -> Result<Self> {
let (share_deltas, bond_deltas) =
self.calculate_pool_share_bond_deltas_after_open_long(base_amount, maybe_bond_deltas)?;
let (share_delta, bond_delta) =
self.calculate_pool_deltas_after_open_long(base_amount, maybe_bond_delta)?;
let mut state = self.clone();
state.info.bond_reserves -= bond_deltas.into();
state.info.share_reserves += share_deltas.into();
state.info.bond_reserves -= bond_delta.into();
state.info.share_reserves += share_delta.into();
Ok(state)
}

/// Calculate the share deltas to be applied to the pool after opening a long.
pub fn calculate_pool_share_bond_deltas_after_open_long(
/// Calculate the share and bond deltas to be applied to the pool after opening a long.
pub fn calculate_pool_deltas_after_open_long(
&self,
base_amount: FixedPoint,
maybe_bond_delta: Option<FixedPoint>,
Expand Down
10 changes: 5 additions & 5 deletions crates/hyperdrive-math/src/short/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,15 @@ impl State {
bond_amount: FixedPoint,
checkpoint_exposure: I256,
) -> Result<FixedPoint> {
let share_deltas = self.calculate_pool_share_deltas_after_open_short(bond_amount)?;
if self.share_reserves() < share_deltas {
let share_delta = self.calculate_pool_share_delta_after_open_short(bond_amount)?;
if self.share_reserves() < share_delta {
return Err(eyre!(
"expected share_reserves={:#?} >= share_deltas={:#?}",
"expected share_reserves={:#?} >= share_delta={:#?}",
self.share_reserves(),
share_deltas
share_delta
));
}
let new_share_reserves = self.share_reserves() - share_deltas;
let new_share_reserves = self.share_reserves() - share_delta;
let exposure_shares = {
let checkpoint_exposure = FixedPoint::try_from(checkpoint_exposure.max(I256::zero()))?;
if self.long_exposure() < checkpoint_exposure {
Expand Down
31 changes: 15 additions & 16 deletions crates/hyperdrive-math/src/short/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,16 @@ impl State {
) -> Result<Self> {
let share_amount = match maybe_share_amount {
Some(share_amount) => share_amount,
None => self.calculate_pool_share_deltas_after_open_short(bond_amount)?,
None => self.calculate_pool_share_delta_after_open_short(bond_amount)?,
};
let mut state = self.clone();
state.info.bond_reserves += bond_amount.into();
state.info.share_reserves -= share_amount.into();
Ok(state)
}

/// Calculate the share deltas to be applied to the pool after opening a short.
pub fn calculate_pool_share_deltas_after_open_short(
/// Calculate the share delta to be applied to the pool after opening a short.
pub fn calculate_pool_share_delta_after_open_short(
&self,
bond_amount: FixedPoint,
) -> Result<FixedPoint> {
Expand Down Expand Up @@ -319,7 +319,7 @@ impl State {
) -> Result<FixedPoint> {
let share_amount = match maybe_base_amount {
Some(base_amount) => base_amount / self.vault_share_price(),
None => self.calculate_pool_share_deltas_after_open_short(bond_amount)?,
None => self.calculate_pool_share_delta_after_open_short(bond_amount)?,
};
let updated_state =
self.calculate_pool_state_after_open_short(bond_amount, Some(share_amount))?;
Expand All @@ -328,7 +328,7 @@ impl State {

/// Calculate the spot rate after a short has been opened.
/// If a base_amount is not provided, then one is estimated
/// using [calculate_pool_deltas_after_open_short](State::calculate_pool_deltas_after_open_short).
/// using [calculate_pool_share_delta_after_open_short](State::calculate_pool_share_delta_after_open_short).
///
/// We calculate the rate for a fixed length of time as:
///
Expand Down Expand Up @@ -533,7 +533,7 @@ mod tests {
}

#[tokio::test]
async fn test_sol_calculate_pool_deltas_after_open_short() -> Result<()> {
async fn test_sol_calculate_pool_share_delta_after_open_short() -> Result<()> {
let test_tolerance = fixed!(10);

let chain = TestChain::new().await?;
Expand Down Expand Up @@ -566,7 +566,7 @@ mod tests {
continue;
}
let bond_amount = rng.gen_range(state.minimum_transaction_amount()..=max_bond_amount);
let rust_pool_deltas = state.calculate_pool_share_deltas_after_open_short(bond_amount);
let rust_pool_delta = state.calculate_pool_share_delta_after_open_short(bond_amount);
let curve_fee_base = state.open_short_curve_fee(bond_amount)?;
let gov_fee_base =
state.open_short_governance_fee(bond_amount, Some(curve_fee_base))?;
Expand All @@ -585,16 +585,16 @@ mod tests {
.call()
.await
{
Ok(sol_pool_deltas) => {
let sol_pool_deltas_with_fees = FixedPoint::from(sol_pool_deltas) - fees;
let rust_pool_deltas_unwrapped = rust_pool_deltas.unwrap();
let result_equal = sol_pool_deltas_with_fees
<= rust_pool_deltas_unwrapped + test_tolerance
&& sol_pool_deltas_with_fees >= rust_pool_deltas_unwrapped - test_tolerance;
Ok(sol_pool_delta) => {
let sol_pool_delta_with_fees = FixedPoint::from(sol_pool_delta) - fees;
let rust_pool_delta_unwrapped = rust_pool_delta.unwrap();
let result_equal = sol_pool_delta_with_fees
<= rust_pool_delta_unwrapped + test_tolerance
&& sol_pool_delta_with_fees >= rust_pool_delta_unwrapped - test_tolerance;
assert!(result_equal, "Should be equal.");
}
Err(_) => {
assert!(rust_pool_deltas.is_err())
assert!(rust_pool_delta.is_err())
}
};
}
Expand Down Expand Up @@ -879,8 +879,7 @@ mod tests {
let price_with_default = state.calculate_spot_price_after_short(bond_amount, None)?;

// Using a pre-calculated base amount
let base_amount = match state.calculate_pool_share_deltas_after_open_short(bond_amount)
{
let base_amount = match state.calculate_pool_share_delta_after_open_short(bond_amount) {
Ok(share_amount) => Some(share_amount * state.vault_share_price()),
Err(_) => continue,
};
Expand Down

0 comments on commit 95e96fb

Please sign in to comment.