Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update docs & fix CI #1

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 10 additions & 6 deletions .github/workflows/rust_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
token: ${{ secrets.GITHUB_TOKEN }}

# NOTE: This is needed to ensure that hyperdrive-wrappers builds correctly.
- name: install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: check out hyperdrive
uses: actions/checkout@main
uses: actions/checkout@v3
dpaiton marked this conversation as resolved.
Show resolved Hide resolved
with:
repository: delvtech/hyperdrive
ref: "latest"
dpaiton marked this conversation as resolved.
Show resolved Hide resolved
path: "./hyperdrive"

- name: install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- name: build hyperdrive
run: cd hyperdrive && make build-sol

- uses: actions-rs/toolchain@v1
with:
Expand Down
17 changes: 10 additions & 7 deletions .github/workflows/rust_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@ jobs:
submodules: recursive
token: ${{ secrets.GITHUB_TOKEN }}

- name: check out hyperdrive
uses: actions/checkout@main
with:
repository: delvtech/hyperdrive
ref: "latest"
path: "./hyperdrive"

# NOTE: This is needed to ensure that hyperdrive-wrappers builds correctly.
- name: install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: check out hyperdrive
uses: actions/checkout@v3
with:
repository: delvtech/hyperdrive
path: "./hyperdrive"

- name: build hyperdrive
run: cd hyperdrive && make build-sol

- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy

- name: test
run: make test
41 changes: 21 additions & 20 deletions crates/hyperdrive-math/src/long/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ use fixed_point_macros::fixed;
use crate::{State, YieldSpace};

impl State {
/// Calculates the amount of shares the trader will receive after fees for closing a long
pub fn calculate_close_long<F: Into<FixedPoint>>(
&self,
bond_amount: F,
maturity_time: U256,
current_time: U256,
) -> FixedPoint {
let bond_amount = bond_amount.into();

if bond_amount < self.config.minimum_transaction_amount.into() {
// TODO would be nice to return a `Result` here instead of a panic.
panic!("MinimumTransactionAmount: Input amount too low");
}

// Subtract the fees from the trade
self.calculate_close_long_flat_plus_curve(bond_amount, maturity_time, current_time)
- self.close_long_curve_fee(bond_amount, maturity_time, current_time)
- self.close_long_flat_fee(bond_amount, maturity_time, current_time)
}

/// Calculate the amount of shares returned when selling bonds without considering fees.
fn calculate_close_long_flat_plus_curve<F: Into<FixedPoint>>(
&self,
bond_amount: F,
Expand All @@ -31,26 +52,6 @@ impl State {

flat + curve
}

/// Calculates the amount of shares the trader will receive after fees for closing a long
pub fn calculate_close_long<F: Into<FixedPoint>>(
&self,
bond_amount: F,
maturity_time: U256,
current_time: U256,
) -> FixedPoint {
let bond_amount = bond_amount.into();

if bond_amount < self.config.minimum_transaction_amount.into() {
// TODO would be nice to return a `Result` here instead of a panic.
panic!("MinimumTransactionAmount: Input amount too low");
}

// Subtract the fees from the trade
self.calculate_close_long_flat_plus_curve(bond_amount, maturity_time, current_time)
- self.close_long_curve_fee(bond_amount, maturity_time, current_time)
- self.close_long_flat_fee(bond_amount, maturity_time, current_time)
}
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions crates/hyperdrive-math/src/long/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl State {
/// The curve fee $c(x)$ paid by longs is paid in bonds and is given by:
///
/// $$
/// c(x) = \phi_{c} \cdot \left( \tfrac{1}{p} - 1 \right) \cdot x
/// c(x) = \phi_c \cdot \left( \tfrac{1}{p} - 1 \right) \cdot x
/// $$
pub fn open_long_curve_fees(&self, base_amount: FixedPoint) -> FixedPoint {
self.curve_fee()
Expand All @@ -25,7 +25,7 @@ impl State {
/// is given by:
///
/// $$
/// g(x) = \phi_{g} \cdot p \cdot c(x)
/// g(x) = \phi_g \cdot p \cdot c(x)
/// $$
pub fn open_long_governance_fee(&self, base_amount: FixedPoint) -> FixedPoint {
self.governance_lp_fee()
Expand All @@ -43,7 +43,7 @@ impl State {
) -> FixedPoint {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
// curve_fee = ((1 - p) * phi_curve * d_y * t) / c
// curve_fee = ((1 - p) * phi_c * d_y * t) / c
self.curve_fee()
* (fixed!(1e18) - self.calculate_spot_price())
* bond_amount.mul_div_down(normalized_time_remaining, self.vault_share_price())
Expand All @@ -59,7 +59,7 @@ impl State {
) -> FixedPoint {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
// flat_fee = (d_y * (1 - t) * phi_flat) / c
// flat_fee = (d_y * (1 - t) * phi_f) / c
bond_amount.mul_div_down(
fixed!(1e18) - normalized_time_remaining,
self.vault_share_price(),
Expand Down
2 changes: 1 addition & 1 deletion crates/hyperdrive-math/src/long/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl State {
///
/// We calculate the rate for a fixed length of time as:
/// $$
/// r(x) = (1 - p(x)) / (p(x) t)
/// r(\Delta y) = (1 - p(\Delta y)) / (p(\Delta y) t)
/// $$
///
/// where $p(x)$ is the spot price after a long for `delta_base`$= x$ and
Expand Down
7 changes: 4 additions & 3 deletions crates/hyperdrive-math/src/short/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ impl State {
/// it is possible for traders to receive a negative interest rate even
/// if curve's spot price is less than or equal to 1.
//
/// Given the curve fee `phi_c` and the starting spot price `p_0`, the
/// Given the curve fee $\phi_c$ and the starting spot price `p_0`, the
/// maximum spot price is given by:
///
/// $$
/// p_max = 1 - phi_c * (1 - p_0)
/// p_max = 1 - \phi_c * (1 - p_0)
/// $$
fn calculate_close_short_max_spot_price(&self) -> FixedPoint {
fixed!(1e18)
Expand Down Expand Up @@ -119,7 +120,7 @@ impl State {
}

// Ensure that the trader didn't purchase bonds at a negative interest
// rate after accounting for fees
// rate after accounting for fees.
let share_curve_delta =
self.calculate_close_short_curve(bond_amount, maturity_time, current_time);
let bond_reserves_delta = bond_amount
Expand Down
10 changes: 5 additions & 5 deletions crates/hyperdrive-math/src/short/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl State {
}

/// Calculates the curve fee paid by shorts for a given bond amount.
/// Returns the fee in shares
/// Returns the fee in shares.
pub fn close_short_curve_fee(
&self,
bond_amount: FixedPoint,
Expand All @@ -34,7 +34,7 @@ impl State {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);

// ((1 - p) * phi_curve * d_y * t) / c
// ((1 - p) * phi_c * d_y * t) / c
self.curve_fee()
* (fixed!(1e18) - self.calculate_spot_price())
* bond_amount.mul_div_down(normalized_time_remaining, self.vault_share_price())
Expand All @@ -54,8 +54,8 @@ impl State {
.mul_down(self.governance_lp_fee())
}

/// Calculates the flat fee paid by shorts for a given bond amount
/// Returns the fee in shares
/// Calculates the flat fee paid by shorts for a given bond amount.
/// Returns the fee in shares.
pub fn close_short_flat_fee(
&self,
bond_amount: FixedPoint,
Expand All @@ -64,7 +64,7 @@ impl State {
) -> FixedPoint {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
// flat fee = (d_y * (1 - t) * phi_flat) / c
// flat_fee = (d_y * (1 - t) * phi_f) / c
bond_amount.mul_div_down(
fixed!(1e18) - normalized_time_remaining,
self.vault_share_price(),
Expand Down
5 changes: 4 additions & 1 deletion crates/hyperdrive-wrappers/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Write, path::Path, process::Command};
use std::{fs::create_dir_all, io::Write, path::Path, process::Command};

use ethers::prelude::Abigen;
use eyre::Result;
Expand Down Expand Up @@ -57,6 +57,9 @@ const TARGETS: &[&str] = &[
];

fn get_artifacts(artifacts_path: &Path) -> Result<Vec<(String, String)>> {
if !artifacts_path.exists() {
create_dir_all(artifacts_path)?;
dpaiton marked this conversation as resolved.
Show resolved Hide resolved
}
let mut artifacts = Vec::new();
for entry in std::fs::read_dir(artifacts_path)? {
let entry = entry?;
Expand Down
Loading