Skip to content

Commit

Permalink
Add bindings for calcHprGivenApr and calcHprGivenApy (#1129)
Browse files Browse the repository at this point in the history
* Add bindings for calcHprGivenApr and calcHprGivenApy

* Use current vault share price as fallback to avoid division by zero, remove arg to match latest hyperwasm

* Rebuild wasm, run changeset
  • Loading branch information
DannyDelott authored Jun 4, 2024
1 parent 19db938 commit 6a53f5a
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 392 deletions.
6 changes: 6 additions & 0 deletions .changeset/violet-ears-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@delvtech/hyperdrive-js-core": patch
"@delvtech/hyperdrive-wasm": patch
---

Add calcHprGivenApy and calcHprGivenApr methods to hyperwasm
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/hyperdrive-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "hyperdrive-wasm"
# This version will be overwritten by the build script to match the version in
# the crate's package.json which is managed by changesets.
version = "0.14.0"
version = "0.14.1"
edition = "2021"
authors = ["Ryan Goree <ryan@delv.tech>", "Danny Delott <dannyd@delv.tech>"]
license = "AGPL-3.0"
Expand Down
36 changes: 35 additions & 1 deletion crates/hyperdrive-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ mod types;
mod utils;

use ethers::types::U256;
use hyperdrive_math::State;
use fixed_point::FixedPoint;
use hyperdrive_math::{calculate_hpr_given_apr, calculate_hpr_given_apy, State};
use types::{JsPoolConfig, JsPoolInfo};
use utils::set_panic_hook;
use wasm_bindgen::prelude::*;
Expand All @@ -25,6 +26,39 @@ pub fn spotPrice(poolInfo: &JsPoolInfo, poolConfig: &JsPoolConfig) -> String {
info: poolInfo.into(),
};
let result_fp = state.calculate_spot_price().unwrap();

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

/// Calculate the holding period return (HPR) given a non-compounding,
/// annualized rate (APR).
///
/// @param apr - The annualized rate
///
/// @param positionDuration - The position duration in seconds
#[wasm_bindgen(skip_jsdoc)]
pub fn calcHprGivenApr(apr: &str, positionDuration: &str) -> String {
set_panic_hook();
let apr_fp = FixedPoint::from(U256::from_dec_str(apr).unwrap());
let position_duration_fp = FixedPoint::from(U256::from_dec_str(positionDuration).unwrap());
let result_fp = calculate_hpr_given_apr(apr_fp, position_duration_fp);

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

/// Calculate the holding period return (HPR) given a compounding, annualized
/// rate (APY).
///
/// @param apy - The annualized rate
///
/// @param positionDuration - The position duration in seconds
#[wasm_bindgen(skip_jsdoc)]
pub fn calcHprGivenApy(apy: &str, positionDuration: &str) -> String {
set_panic_hook();
let apy_fp = FixedPoint::from(U256::from_dec_str(apy).unwrap());
let position_duration_fp = FixedPoint::from(U256::from_dec_str(positionDuration).unwrap());
let result_fp = calculate_hpr_given_apy(apy_fp, position_duration_fp).unwrap();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,16 @@ export class ReadHyperdrive extends ReadModel {
timestamp,
poolConfig.checkpointDuration,
);
const { vaultSharePrice: openVaultSharePrice } = await this.getCheckpoint({

// The vault share price at the time the current checkpoint was minted is
// the most accurate, however if there is no current checkpoint we should
// just use the current vualt share price.
let { vaultSharePrice: openVaultSharePrice } = await this.getCheckpoint({
checkpointId,
});
if (!openVaultSharePrice) {
openVaultSharePrice = (await this.getPoolInfo()).vaultSharePrice;
}

const impliedRateString = hyperwasm.calcImpliedRate(
convertBigIntsToStrings(poolInfo),
Expand Down Expand Up @@ -1588,10 +1595,6 @@ export class ReadHyperdrive extends ReadModel {
convertBigIntsToStrings(poolInfo),
convertBigIntsToStrings(poolConfig),
amountOfBondsToShort.toString(),
hyperwasm.spotPrice(
convertBigIntsToStrings(poolInfo),
convertBigIntsToStrings(poolConfig),
),
),
);
let curveFee = curveFeeInBase;
Expand Down Expand Up @@ -1765,9 +1768,7 @@ export class ReadHyperdrive extends ReadModel {
minAPR,
minLpSharePrice,
maxAPR,
destination,
asBase,
extraData = DEFAULT_EXTRA_DATA,
options,
}: {
contribution: bigint;
Expand Down
Loading

0 comments on commit 6a53f5a

Please sign in to comment.