Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
Add hyperwasm bindings for calcSpotPriceAfterLong, `calcSpotPriceAf…
Browse files Browse the repository at this point in the history
…terShort`, `calcCloseShort`, `calcCloseLong`, `getOpenLongCurveFees`, and `getOpenShortCurveFees` (#66)

Co-authored-by: Ryan Goree <goree.ryan@gmail.com>
  • Loading branch information
DannyDelott and ryangoree authored Mar 1, 2024
1 parent 12c2b57 commit a9d8396
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
uses: actions/checkout@master
with:
repository: delvtech/hyperdrive
ref: "v0.7.0"
ref: "v0.8.0"
path: "./hyperdrive"
ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }}

Expand Down
82 changes: 82 additions & 0 deletions crates/hyperdrive-wasm/src/long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,85 @@ pub fn getMaxLong(

U256::from(result_fp).to_string()
}

/// Gets the curve fee paid by longs for a given base amount.
///
/// @param poolInfo - The current state of the pool
///
/// @param poolConfig - The pool's configuration
///
/// @param baseAmount - The amount of base tokens to spend
#[wasm_bindgen(skip_jsdoc)]
pub fn getOpenLongCurveFees(
poolInfo: &JsPoolInfo,
poolConfig: &JsPoolConfig,
baseAmount: String,
) -> String {
set_panic_hook();
let state = State {
info: poolInfo.into(),
config: poolConfig.into(),
};

let _baseAmount = FixedPoint::from(U256::from_dec_str(&baseAmount).unwrap());

let result_fp = state.open_long_curve_fees(_baseAmount);

U256::from(result_fp).to_string()
}

/// Gets the spot price after opening the short on the YieldSpace curve and
/// before calculating the fees.
///
/// @param poolInfo - The current state of the pool
///
/// @param poolConfig - The pool's configuration
///
/// @param baseAmount - The amount of base to spend
#[wasm_bindgen(skip_jsdoc)]
pub fn calcSpotPriceAfterLong(
poolInfo: &JsPoolInfo,
poolConfig: &JsPoolConfig,
baseAmount: String,
) -> String {
set_panic_hook();
let state = State {
info: poolInfo.into(),
config: poolConfig.into(),
};
let _baseAmount = FixedPoint::from(U256::from_dec_str(&baseAmount).unwrap());

let result_fp = state.get_spot_price_after_long(_baseAmount);

U256::from(result_fp).to_string()
}

/// Gets the amount of shares the trader will receive after fees for closing a
/// long
///
/// @param poolInfo - The current state of the pool
///
/// @param poolConfig - The pool's configuration
///
/// @param bondAmount - The amount of bonds to close
///
/// @param normalizedTimeRemaining - 0 for mature bonds, 1 for not matured bonds
#[wasm_bindgen(skip_jsdoc)]
pub fn calcCloseLong(
poolInfo: &JsPoolInfo,
poolConfig: &JsPoolConfig,
bondAmount: String,
normalizedTimeRemaining: String,
) -> String {
set_panic_hook();
let state = State {
info: poolInfo.into(),
config: poolConfig.into(),
};
let _bondAmount = U256::from_dec_str(&bondAmount).unwrap();
let _normalizedTimeRemaining = U256::from_dec_str(&normalizedTimeRemaining).unwrap();

let result_fp = state.calculate_close_long(_bondAmount, _normalizedTimeRemaining);

U256::from(result_fp).to_string()
}
94 changes: 94 additions & 0 deletions crates/hyperdrive-wasm/src/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,97 @@ pub fn getMaxShort(

U256::from(result_fp).to_string()
}

/// Gets the curve fee paid by the trader when they open a short.
///
/// @param poolInfo - The current state of the pool
///
/// @param poolConfig - The pool's configuration
///
/// @param shortAmount - The number of bonds to short
///
/// @param spotPrice - The spot price of the pool
#[wasm_bindgen(skip_jsdoc)]
pub fn getOpenShortCurveFees(
poolInfo: &JsPoolInfo,
poolConfig: &JsPoolConfig,
shortAmount: String,
spotPrice: String,
) -> String {
set_panic_hook();
let state = State {
info: poolInfo.into(),
config: poolConfig.into(),
};
let _shortAmount = FixedPoint::from(U256::from_dec_str(&shortAmount).unwrap());
let _spotPrice = FixedPoint::from(U256::from_dec_str(&spotPrice).unwrap());

let result_fp = state.open_short_curve_fee(_shortAmount, _spotPrice);

U256::from(result_fp).to_string()
}

/// Gets the spot price after opening the short on the YieldSpace curve and
/// before calculating the fees.
///
/// @param poolInfo - The current state of the pool
///
/// @param poolConfig - The pool's configuration
///
/// @param bondAmount - The number of bonds to short
#[wasm_bindgen(skip_jsdoc)]
pub fn calcSpotPriceAfterShort(
poolInfo: &JsPoolInfo,
poolConfig: &JsPoolConfig,
bondAmount: String,
) -> String {
set_panic_hook();
let state = State {
info: poolInfo.into(),
config: poolConfig.into(),
};
let _bondAmount = FixedPoint::from(U256::from_dec_str(&bondAmount).unwrap());


let result_fp = state.get_spot_price_after_short(_bondAmount);

U256::from(result_fp).to_string()
}

/// Gets the amount of shares the trader will receive after fees for closing a
/// short
///
/// @param poolInfo - The current state of the pool
///
/// @param poolConfig - The pool's configuration
///
/// @param bondAmount - The amount of bonds to close
///
/// @param openVaultSharePrice - The vault share price at the checkpoint when the position was opened
///
/// @param closeVaultSharePrice - The current vault share price, or if the position has matured, the vault share price from the closing checkpoint
///
/// @param normalizedTimeRemaining - 0 for mature bonds, 1 for not matured bonds
#[wasm_bindgen(skip_jsdoc)]
pub fn calcCloseShort(
poolInfo: &JsPoolInfo,
poolConfig: &JsPoolConfig,
bondAmount: String,
openVaultSharePrice: String,
closeVaultSharePrice: String,
normalizedTimeRemaining: String,
) -> String {
set_panic_hook();
let state = State {
info: poolInfo.into(),
config: poolConfig.into(),
};
let _bondAmount = U256::from_dec_str(&bondAmount).unwrap();
let _openVaultSharePrice = U256::from_dec_str(&openVaultSharePrice).unwrap();
let _closeVaultSharePrice = U256::from_dec_str(&closeVaultSharePrice).unwrap();
let _normalizedTimeRemaining = U256::from_dec_str(&normalizedTimeRemaining).unwrap();

let result_fp = state.calculate_close_short(_bondAmount, _openVaultSharePrice, _closeVaultSharePrice, _normalizedTimeRemaining);

U256::from(result_fp).to_string()
}
2 changes: 1 addition & 1 deletion crates/hyperdrive-wasm/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ethers::core::types::{Address, I256, U256};
use hyperdrive_wrappers::wrappers::i_hyperdrive::{Fees, PoolConfig, PoolInfo};
use hyperdrive_wrappers::wrappers::ihyperdrive::{Fees, PoolConfig, PoolInfo};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use wasm_bindgen::prelude::*;
Expand Down

0 comments on commit a9d8396

Please sign in to comment.