Skip to content

Commit

Permalink
Re-organize lp.rs into directory. (#106)
Browse files Browse the repository at this point in the history
# Description
Re-organizes lp.rs into a directory structure to match long and short.  

Before:
```
lp.rs // has everything
```

After:
```

lp.rs			// just module exports
lp/
  add.rs		// add functionality of HyperdriveLP.sol
  remove.rs		// remove functionality of HyperdriveLP.sol
  math.rs		// LPMath.sol

base.rs			// HyperdriveBase.sol
```
# Review Checklists

Please check each item **before approving** the pull request. While
going
through the checklist, it is recommended to leave comments on items that
are
referenced in the checklist to make sure that they are reviewed.

- [ ] **Testing**
    - [ ] Are there new or updated unit or integration tests?
    - [ ] Do the tests cover the happy paths?
    - [ ] Do the tests cover the unhappy paths?
- [ ] Are there an adequate number of fuzz tests to ensure that we are
          covering the full input space?
- [ ] If matching Solidity behavior, are there differential fuzz tests
that
          ensure that Rust matches Solidity?
  • Loading branch information
sentilesdal authored May 18, 2024
1 parent f1417b1 commit b410d21
Show file tree
Hide file tree
Showing 6 changed files with 1,189 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 b410d21

Please sign in to comment.