Skip to content

Commit

Permalink
change rounding behavior to match solidity
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaiton committed May 13, 2024
1 parent 3c33c0f commit 29135ed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
25 changes: 16 additions & 9 deletions crates/hyperdrive-math/src/long/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ impl State {
/// \Phi_{c,ol}(\Delta x) = \phi_c \cdot \left( \tfrac{1}{p} - 1 \right) \cdot \Delta x
/// $$
pub fn open_long_curve_fee(&self, base_amount: FixedPoint) -> FixedPoint {
// NOTE: Round up to overestimate the curve fee.
self.curve_fee()
* ((fixed!(1e18) / self.calculate_spot_price()) - fixed!(1e18))
* base_amount
.mul_up(fixed!(1e18).div_up(self.calculate_spot_price()) - fixed!(1e18))
.mul_up(base_amount)
}

/// Calculates the governance fee paid when opening longs with a given base amount.
Expand All @@ -36,7 +37,8 @@ impl State {
Some(maybe_curve_fee) => maybe_curve_fee,
None => self.open_long_curve_fee(base_amount),
};
self.governance_lp_fee() * self.calculate_spot_price() * curve_fee
// NOTE: Round down to underestimate the governance curve fee.
(self.calculate_spot_price() * curve_fee).mul_down(self.governance_lp_fee())
}

/// Calculates the curve fee paid when closing longs for a given bond amount.
Expand All @@ -57,9 +59,11 @@ impl State {
) -> FixedPoint {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
// NOTE: Round up to overestimate the curve fee.
self.curve_fee()
* (fixed!(1e18) - self.calculate_spot_price())
* bond_amount.mul_div_down(normalized_time_remaining, self.vault_share_price())
.mul_up(fixed!(1e18) - self.calculate_spot_price())
.mul_up(bond_amount)
.mul_div_down(normalized_time_remaining, self.vault_share_price())
}

/// Calculates the flat fee paid when closing longs for a given bond amount.
Expand All @@ -80,9 +84,12 @@ impl State {
) -> FixedPoint {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
bond_amount.mul_div_down(
fixed!(1e18) - normalized_time_remaining,
self.vault_share_price(),
) * self.flat_fee()
// NOTE: Round up to overestimate the flat fee.
bond_amount
.mul_div_up(
fixed!(1e18) - normalized_time_remaining,
self.vault_share_price(),
)
.mul_up(self.flat_fee())
}
}
21 changes: 14 additions & 7 deletions crates/hyperdrive-math/src/short/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl State {
Some(maybe_curve_fee) => maybe_curve_fee,
None => self.open_short_curve_fee(bond_amount),
};
self.governance_lp_fee() * curve_fee
// NOTE: Round down to underestimate the governance fee.
curve_fee.mul_down(self.governance_lp_fee())
}

/// Calculates the curve fee paid when opening shorts with a given bond amount.
Expand All @@ -59,9 +60,11 @@ impl State {
) -> FixedPoint {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
// NOTE: Round up to overestimate the curve fee.
self.curve_fee()
* (fixed!(1e18) - self.calculate_spot_price())
* bond_amount.mul_div_down(normalized_time_remaining, self.vault_share_price())
.mul_up(fixed!(1e18) - self.calculate_spot_price())
.mul_up(bond_amount)
.mul_div_up(normalized_time_remaining, self.vault_share_price())
}

/// Calculate the governance fee paid when closing shorts with a given bond amount.
Expand All @@ -85,6 +88,7 @@ impl State {
Some(maybe_curve_fee) => maybe_curve_fee,
None => self.close_short_curve_fee(bond_amount, maturity_time, current_time),
};
// NOTE: Round down to underestimate the governance fee.
curve_fee.mul_down(self.governance_lp_fee())
}

Expand All @@ -106,9 +110,12 @@ impl State {
) -> FixedPoint {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
bond_amount.mul_div_down(
fixed!(1e18) - normalized_time_remaining,
self.vault_share_price(),
) * self.flat_fee()
// NOTE: Round up to overestimate the flat fee.
bond_amount
.mul_div_up(
fixed!(1e18) - normalized_time_remaining,
self.vault_share_price(),
)
.mul_up(self.flat_fee())
}
}

0 comments on commit 29135ed

Please sign in to comment.