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

fix calc open short #93

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion crates/hyperdrive-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const apr = hyperwasm.getSpotRate(poolInfo, poolConfig); // => '0.03499999999999
[Install wasm-pack](https://rustwasm.github.io/wasm-pack/installer/) then run:

```sh
sh ./build.sh
sh ./scripts/build.sh
```

This will create the node package at `./pkg/`, add a couple exports so the
Expand Down
4 changes: 3 additions & 1 deletion crates/hyperdrive-wasm/src/short/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ pub fn spotPriceAfterShort(
};
let bond_amount = FixedPoint::from(U256::from_dec_str(bondAmount).unwrap());

let result_fp = state.calculate_spot_price_after_short(bond_amount, None);
let result_fp = state
.calculate_spot_price_after_short(bond_amount, None, None)
.unwrap();

U256::from(result_fp).to_string()
}
8 changes: 4 additions & 4 deletions crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ def calculate_targeted_long(
budget: str,
target_rate: str,
checkpoint_exposure: str,
maybe_max_iterations: int | None,
maybe_allowable_error: str,
maybe_max_iterations: int | None = None,
maybe_allowable_error: str | None = None,
) -> str:
"""Calculate the amount of bonds that can be purchased for the given budget.

Expand All @@ -377,9 +377,9 @@ def calculate_targeted_long(
The target fixed rate.
checkpoint_exposure: str (I256)
The net exposure for the given checkpoint.
maybe_max_iterations: int, optional
maybe_max_iterations: int | None, optional
The number of iterations to use for the Newtonian method.
maybe_allowable_error: str, FixedPoint
maybe_allowable_error: str (FixedPoint) | None, optional
The amount of error supported for reaching the target rate.


Expand Down
21 changes: 17 additions & 4 deletions crates/hyperdrivepy/src/hyperdrive_state_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,19 @@ impl HyperdriveState {
&self,
bond_amount: &str,
maybe_base_amount: Option<&str>,
maybe_open_vault_share_price: Option<&str>,
) -> PyResult<String> {
let bond_amount_fp = FixedPoint::from(U256::from_dec_str(bond_amount).map_err(|_| {
PyErr::new::<PyValueError, _>("Failed to convert bond_amount string to U256")
})?);
let open_vault_share_price_fp = FixedPoint::from(
U256::from_dec_str(maybe_open_vault_share_price).map_err(|_| {
PyErr::new::<PyValueError, _>(
"Failed to convert open_vault_share_price string to U256",
)
})?,
);

let maybe_base_amount_fp = if let Some(base_amount) = maybe_base_amount {
Some(FixedPoint::from(U256::from_dec_str(base_amount).map_err(
|_| {
Expand All @@ -75,7 +84,11 @@ impl HyperdriveState {
};
let result_fp = self
.state
.calculate_spot_price_after_short(bond_amount_fp, maybe_base_amount_fp)
.calculate_spot_price_after_short(
bond_amount_fp,
open_vault_share_price_fp,
maybe_base_amount_fp,
)
.unwrap();
let result = U256::from(result_fp).to_string();
return Ok(result);
Expand Down Expand Up @@ -154,11 +167,11 @@ impl HyperdriveState {

pub fn calculate_open_short(
&self,
short_amount: &str,
bond_amount: &str,
spot_price: &str,
open_vault_share_price: &str,
) -> PyResult<String> {
let short_amount_fp = FixedPoint::from(U256::from_dec_str(short_amount).map_err(|_| {
let bond_amount_fp = FixedPoint::from(U256::from_dec_str(bond_amount).map_err(|_| {
PyErr::new::<PyValueError, _>("Failed to convert short_amount string to U256")
})?);
let spot_price_fp = FixedPoint::from(U256::from_dec_str(spot_price).map_err(|_| {
Expand All @@ -172,7 +185,7 @@ impl HyperdriveState {
})?);
let result_fp = self
.state
.calculate_open_short(short_amount_fp, spot_price_fp, open_vault_share_price_fp)
.calculate_open_short(bond_amount_fp, spot_price_fp, open_vault_share_price_fp)
.unwrap();
let result = U256::from(result_fp).to_string();
return Ok(result);
Expand Down
Loading