Skip to content

Commit

Permalink
re-organize
Browse files Browse the repository at this point in the history
  • Loading branch information
sentilesdal committed May 18, 2024
1 parent f1417b1 commit f2a2137
Show file tree
Hide file tree
Showing 6 changed files with 1,192 additions and 1,156 deletions.
50 changes: 50 additions & 0 deletions crates/hyperdrive-math/src/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use ethers::types::U256;
use fixed_point::{fixed, FixedPoint};

use crate::State;

impl State {
/// Calculates the number of share reserves that are not reserved by open
/// positions.
pub fn calculate_idle_share_reserves(&self) -> FixedPoint {
let long_exposure = self.long_exposure().div_up(self.vault_share_price());
match self.share_reserves() > long_exposure + self.minimum_share_reserves() {
true => self.share_reserves() - long_exposure - self.minimum_share_reserves(),
false => fixed!(0),
}
}

/// Calculates the number of base that are not reserved by open positions.
pub fn calculate_idle_share_reserves_in_base(&self) -> FixedPoint {
// NOTE: Round up to underestimate the pool's idle.
let long_exposure = self.long_exposure().div_up(self.vault_share_price());

// Calculate the idle base reserves.
let mut idle_shares_in_base = fixed!(0);
if self.share_reserves() > (long_exposure + self.minimum_share_reserves()) {
idle_shares_in_base =
(self.share_reserves() - long_exposure - self.minimum_share_reserves())
* self.vault_share_price();
}

idle_shares_in_base
}

/// Function that takes in a scaled FixedPoint maturity time and calculates
/// normalized time remaining with higher precision.
pub fn calculate_scaled_normalized_time_remaining(
&self,
scaled_maturity_time: FixedPoint,
current_time: U256,
) -> FixedPoint {
let scaled_latest_checkpoint =
FixedPoint::from(self.to_checkpoint(current_time)) * fixed!(1e36);
let scaled_position_duration = self.position_duration() * fixed!(1e36);
if scaled_maturity_time > scaled_latest_checkpoint {
// NOTE: Round down to underestimate the time remaining.
(scaled_maturity_time - scaled_latest_checkpoint).div_down(scaled_position_duration)
} else {
fixed!(0)
}
}
}
1 change: 1 addition & 0 deletions crates/hyperdrive-math/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod base;
mod long;
mod lp;
mod short;
Expand Down
Loading

0 comments on commit f2a2137

Please sign in to comment.