From 85fe8817f87d6433d5743f5b12fa8dbda1514ffc Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 6 Mar 2024 15:23:27 -0800 Subject: [PATCH 1/5] remove extra z_min --- crates/hyperdrive-math/src/long/max.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/hyperdrive-math/src/long/max.rs b/crates/hyperdrive-math/src/long/max.rs index 0b0b95fec..e99806c3b 100644 --- a/crates/hyperdrive-math/src/long/max.rs +++ b/crates/hyperdrive-math/src/long/max.rs @@ -256,7 +256,7 @@ impl State { /// exposure after opening a long with $x$ base as: /// /// \begin{aligned} - /// z(x) &= z_0 + \tfrac{x - g(x)}{c} - z_{min} \\ + /// z(x) &= z_0 + \tfrac{x - g(x)}{c} \\ /// e(x) &= e_0 + min(exposure_{c}, 0) + 2 \cdot y(x) - x + g(x) \\ /// &= e_0 + min(exposure_{c}, 0) + 2 \cdot p_r^{-1} \cdot x - /// 2 \cdot c(x) - x + g(x) From da9cac4f498658c0f055cb376b40633b95516823 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 6 Mar 2024 15:23:45 -0800 Subject: [PATCH 2/5] remove extra fee eqn --- crates/hyperdrive-math/src/long/fees.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/hyperdrive-math/src/long/fees.rs b/crates/hyperdrive-math/src/long/fees.rs index 2db0bd60b..1180c3344 100644 --- a/crates/hyperdrive-math/src/long/fees.rs +++ b/crates/hyperdrive-math/src/long/fees.rs @@ -12,7 +12,6 @@ impl State { /// c(x) = \phi_{c} \cdot \left( \tfrac{1}{p} - 1 \right) \cdot x /// $$ pub fn open_long_curve_fees(&self, base_amount: FixedPoint) -> FixedPoint { - // curve fee = ((1 / p) - 1) * phi_curve * dz self.curve_fee() * ((fixed!(1e18) / self.get_spot_price()) - fixed!(1e18)) * base_amount } From 9e7af09e44a56b56c206382869ffc0e00847ca8f Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 6 Mar 2024 15:59:31 -0800 Subject: [PATCH 3/5] add note about inverse price --- crates/hyperdrive-math/src/long/max.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/hyperdrive-math/src/long/max.rs b/crates/hyperdrive-math/src/long/max.rs index e99806c3b..51b622d2a 100644 --- a/crates/hyperdrive-math/src/long/max.rs +++ b/crates/hyperdrive-math/src/long/max.rs @@ -24,6 +24,10 @@ impl State { } /// Gets the pool's solvency. + /// + /// $$ + /// s = z - \tfrac{exposure}{c} - z_min + /// $$ pub fn get_solvency(&self) -> FixedPoint { self.share_reserves() - self.long_exposure() / self.vault_share_price() @@ -107,7 +111,7 @@ impl State { // numbers. // // Proceed to the next step of Newton's method. Once we have a - // candidate solution, we check to see if the pool is solvent if + // candidate solution, we check to see if the pool is solvent after // a long is opened with the candidate amount. If the pool isn't // solvent, then we're done. let maybe_derivative = self.solvency_after_long_derivative(max_base_amount); @@ -157,7 +161,9 @@ impl State { // // y_t = (mu * z_t) * ((1 + curveFee * (1 / p_0 - 1) * (1 - flatFee)) / (1 - flatFee)) ** (1 / t_s) // - // We can use this formula to solve our YieldSpace invariant for z_t: + // Our equation for price is the inverse of that used by YieldSpace, which must be considered when + // deriving the invariant from the price equation. + // With this in mind, we can use this formula to solve our YieldSpace invariant for z_t: // // k = (c / mu) * (mu * z_t) ** (1 - t_s) + // ( From 1609fa2000153c9df452d9550ae2198624e8d69b Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 6 Mar 2024 17:57:59 -0800 Subject: [PATCH 4/5] fix bug with solvency calculation --- crates/hyperdrive-math/src/long/fees.rs | 4 ++-- crates/hyperdrive-math/src/long/max.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/hyperdrive-math/src/long/fees.rs b/crates/hyperdrive-math/src/long/fees.rs index 1180c3344..2c71ef9e6 100644 --- a/crates/hyperdrive-math/src/long/fees.rs +++ b/crates/hyperdrive-math/src/long/fees.rs @@ -35,7 +35,7 @@ impl State { bond_amount: FixedPoint, normalized_time_remaining: FixedPoint, ) -> FixedPoint { - // ((1 - p) * phi_curve * d_y * t) / c + // curveFee = ((1 - p) * phi_curve * d_y * t) / c self.curve_fee() * (fixed!(1e18) - self.get_spot_price()) * bond_amount.mul_div_down(normalized_time_remaining, self.vault_share_price()) @@ -48,7 +48,7 @@ impl State { bond_amount: FixedPoint, normalized_time_remaining: FixedPoint, ) -> FixedPoint { - // flat fee = (d_y * (1 - t) * phi_flat) / c + // flatFee = (d_y * (1 - t) * phi_flat) / c bond_amount.mul_div_down( fixed!(1e18) - normalized_time_remaining, self.vault_share_price(), diff --git a/crates/hyperdrive-math/src/long/max.rs b/crates/hyperdrive-math/src/long/max.rs index 51b622d2a..0d12df48f 100644 --- a/crates/hyperdrive-math/src/long/max.rs +++ b/crates/hyperdrive-math/src/long/max.rs @@ -274,7 +274,7 @@ impl State { /// to calculate the approximate ending solvency of: /// /// $$ - /// s(x) \approx z(x) - \tfrac{e(x)}{c} - z_{min} + /// s(x) \approx z(x) - \tfrac{e(x) - min(exposure_{c}, 0)}{c} - z_{min} /// $$ /// /// If we let the initial solvency be given by $s_0$, we can solve for From 994be87cd0bc682c5cf72d10a8aab656e1fce43b Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 9 Mar 2024 14:54:26 +0900 Subject: [PATCH 5/5] addresses feedback --- crates/hyperdrive-math/src/long/fees.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/hyperdrive-math/src/long/fees.rs b/crates/hyperdrive-math/src/long/fees.rs index 2c71ef9e6..a2dc9244d 100644 --- a/crates/hyperdrive-math/src/long/fees.rs +++ b/crates/hyperdrive-math/src/long/fees.rs @@ -35,7 +35,7 @@ impl State { bond_amount: FixedPoint, normalized_time_remaining: FixedPoint, ) -> FixedPoint { - // curveFee = ((1 - p) * phi_curve * d_y * t) / c + // curve_fee = ((1 - p) * phi_curve * d_y * t) / c self.curve_fee() * (fixed!(1e18) - self.get_spot_price()) * bond_amount.mul_div_down(normalized_time_remaining, self.vault_share_price()) @@ -48,7 +48,7 @@ impl State { bond_amount: FixedPoint, normalized_time_remaining: FixedPoint, ) -> FixedPoint { - // flatFee = (d_y * (1 - t) * phi_flat) / c + // flat_fee = (d_y * (1 - t) * phi_flat) / c bond_amount.mul_div_down( fixed!(1e18) - normalized_time_remaining, self.vault_share_price(),