Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/hyperdrive-math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ impl Distribution<State> for Standard {
.unwrap()
}
},
// If this range returns greater than position duration, then both rust and solidity will fail
// on calls that depend on this value.
long_average_maturity_time: rng
.gen_range(fixed!(0)..=FixedPoint::from(60 * 60 * 24 * 365))
.gen_range(fixed!(0)..=FixedPoint::from(365 * one_day_in_seconds) * fixed!(1e18))
.into(),
short_average_maturity_time: rng
.gen_range(fixed!(0)..=FixedPoint::from(60 * 60 * 24 * 365))
.gen_range(fixed!(0)..=FixedPoint::from(365 * one_day_in_seconds) * fixed!(1e18))
.into(),
lp_total_supply: rng
.gen_range(fixed!(1_000e18)..=fixed!(100_000_000e18))
Expand Down
39 changes: 31 additions & 8 deletions crates/hyperdrive-math/src/lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,38 @@ impl State {
idle_shares_in_base
}

/// Function that takes in a scaled FixedPoint maturity time and calculates
/// normalized time remaining with higher precision.
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.
FixedPoint::from(scaled_maturity_time - scaled_latest_checkpoint)
.div_down(scaled_position_duration)
} else {
fixed!(0)
}
}

/// Calculates the present value of LPs capital in the pool.
pub fn calculate_present_value(&self, current_block_timestamp: U256) -> FixedPoint {
// Calculate the average time remaining for the longs and shorts.
let long_average_time_remaining = self.calculate_normalized_time_remaining(
self.long_average_maturity_time().into(),

// To keep precision of long and short average maturity time (from contract call)
// we scale the block timestamp and position duration by 1e18 to calculate
// the normalized time remaining.
let long_average_time_remaining = self.calculate_scaled_normalized_time_remaining(
self.long_average_maturity_time(),
current_block_timestamp,
);
let short_average_time_remaining = self.calculate_normalized_time_remaining(
self.short_average_maturity_time().into(),
let short_average_time_remaining = self.calculate_scaled_normalized_time_remaining(
self.short_average_maturity_time(),
current_block_timestamp,
);

Expand Down Expand Up @@ -532,14 +555,14 @@ mod tests {
minimum_share_reserves: state.config.minimum_share_reserves,
minimum_transaction_amount: state.config.minimum_transaction_amount,
long_average_time_remaining: state
.calculate_normalized_time_remaining(
state.long_average_maturity_time().into(),
.calculate_scaled_normalized_time_remaining(
state.long_average_maturity_time(),
current_block_timestamp.into(),
)
.into(),
short_average_time_remaining: state
.calculate_normalized_time_remaining(
state.short_average_maturity_time().into(),
.calculate_scaled_normalized_time_remaining(
state.short_average_maturity_time(),
current_block_timestamp.into(),
)
.into(),
Expand Down