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

Commit

Permalink
Add openVaultSharePrice to calcOpenShort (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyDelott authored Mar 27, 2024
1 parent afe5d55 commit 9484dc1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 53 deletions.
86 changes: 52 additions & 34 deletions crates/hyperdrive-wasm/example/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import * as hyperdriveWasm from '@delvtech/hyperdrive-wasm';
import * as hyperdriveWasm from "@delvtech/hyperdrive-wasm";

const ZERO_ADDRESS = '0x'.padEnd(42, '0');
const MAX_U256 = '0x'.padEnd(66, 'F');
const ZERO_ADDRESS = "0x".padEnd(42, "0");
const MAX_U256 = "0x".padEnd(66, "F");
const MAX_BUDGET = BigInt(MAX_U256).toString();

const examplePoolInfo = {
shareReserves: '10000000000000000000000000',
shareAdjustment: '0',
zombieBaseProceeds: '0',
zombieShareReserves: '0',
bondReserves: '10217899519533796120000000',
lpTotalSupply: '9999990000000000000000000',
sharePrice: '1000000000000000000',
longsOutstanding: '0',
longAverageMaturityTime: '0',
shortsOutstanding: '0',
shortAverageMaturityTime: '0',
withdrawalSharesReadyToWithdraw: '0',
withdrawalSharesProceeds: '0',
lpSharePrice: '1000000000000000000',
longExposure: '0',
shareReserves: "10000000000000000000000000",
shareAdjustment: "0",
zombieBaseProceeds: "0",
zombieShareReserves: "0",
bondReserves: "10217899519533796120000000",
lpTotalSupply: "9999990000000000000000000",
vaultSharePrice: "1000000000000000000",
sharePrice: "1000000000000000000",
longsOutstanding: "0",
longAverageMaturityTime: "0",
shortsOutstanding: "0",
shortAverageMaturityTime: "0",
withdrawalSharesReadyToWithdraw: "0",
withdrawalSharesProceeds: "0",
lpSharePrice: "1000000000000000000",
longExposure: "0",
};
const examplePoolConfig = {
baseToken: ZERO_ADDRESS,
governance: ZERO_ADDRESS,
feeCollector: ZERO_ADDRESS,
fees: {
curve: '100000000000000000',
flat: '500000000000000',
governanceLP: '10000000000000000',
governanceZombie: '100000000000000000',
curve: "100000000000000000",
flat: "500000000000000",
governanceLP: "10000000000000000",
governanceZombie: "100000000000000000",
},
initialSharePrice: '1000000000000000000',
minimumShareReserves: '10000000000000000000',
minimumTransactionAmount: '1000000000000000',
timeStretch: '44463125629060298',
positionDuration: '604800',
checkpointDuration: '3600',
linkerCodeHash: '0x'.padEnd(66, '0'),
initialVaultSharePrice: "1000000000000000000",
minimumShareReserves: "10000000000000000000",
minimumTransactionAmount: "1000000000000000",
timeStretch: "44463125629060298",
positionDuration: "604800",
checkpointDuration: "3600",
linkerCodeHash: "0x".padEnd(66, "0"),
linkerFactory: ZERO_ADDRESS,
sweepCollector: ZERO_ADDRESS,
};

async function main() {
Expand All @@ -48,28 +50,44 @@ async function main() {
examplePoolInfo,
examplePoolConfig
);
console.log('spotRate:', spotRate);
console.log("spotRate:", spotRate);

const maxLong = hyperdriveWasm.getMaxLong(
examplePoolInfo,
examplePoolConfig,
MAX_BUDGET,
'90844806244066488'
"90844806244066488"
);
console.log('maxLong:', maxLong);
console.log("maxLong:", maxLong);

const baseForMaxLong = hyperdriveWasm.calcOpenLong(
examplePoolInfo,
examplePoolConfig,
maxLong
);
console.log('baseForMaxLong:', baseForMaxLong);
console.log("baseForMaxLong:", baseForMaxLong);

const spotPrice = hyperdriveWasm.getSpotPrice(
examplePoolInfo,
examplePoolConfig
);
console.log('spotPrice:', spotPrice);
console.log("spotPrice:", spotPrice);

// Returns: 15977080231906768517
// const openShortPreview = hyperdriveWasm.calcOpenShort(
// examplePoolInfo,
// examplePoolConfig,
// (10_000n * 10n ** 18n).toString(),
// "0"
// );
// Returns: 1127088191343017879628
const openShortPreview = hyperdriveWasm.calcOpenShort(
examplePoolInfo,
examplePoolConfig,
(10_000n * 10n ** 18n).toString(),
"900000000000000000"
);
console.log("openShortPreview", openShortPreview);
}

main();
8 changes: 4 additions & 4 deletions crates/hyperdrive-wasm/example/package-lock.json

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/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"dependencies": {
"@delvtech/hyperdrive-wasm": "file:delvtech-hyperdrive-wasm-0.9.0.tgz"
}
}
}
8 changes: 4 additions & 4 deletions crates/hyperdrive-wasm/example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@delvtech/hyperdrive-wasm@file:delvtech-hyperdrive-wasm-0.5.0.tgz":
version "0.5.0"
resolved "file:delvtech-hyperdrive-wasm-0.5.0.tgz"
integrity sha512-15IAqWt3hKn43k0jZccJA0KsdWHdf0fehV8rVAotNXn7fD+AkJ5CyyC8UURsSg0yQbZ+CRgFMexoxLgI3VeiCA==
"@delvtech/hyperdrive-wasm@file:delvtech-hyperdrive-wasm-0.9.0.tgz":
version "0.9.0"
resolved "file:delvtech-hyperdrive-wasm-0.9.0.tgz"
integrity sha512-PlypSekWUGarjnjPssDmA9rNdXVZazyICwe0jJLaoA/AKZIsCKLxx2MJC0LxHB1oCLqDfYSDodSyTa4VCAIIsg==

"@discoveryjs/json-ext@^0.5.0":
version "0.5.7"
Expand Down
29 changes: 19 additions & 10 deletions crates/hyperdrive-wasm/src/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::utils::set_panic_hook;
use crate::{JsPoolConfig, JsPoolInfo};
use ethers::types::{I256, U256};
use fixed_point::FixedPoint;
use fixed_point_macros::fixed;
use hyperdrive_math::State;
use wasm_bindgen::prelude::*;

Expand All @@ -13,23 +12,29 @@ use wasm_bindgen::prelude::*;
/// @param poolConfig - The pool's configuration
///
/// @param shortAmount - The amount of bonds to short
///
/// @param openVaultSharePrice - The vault share price at the start of the checkpoint
#[wasm_bindgen(skip_jsdoc)]
pub fn calcOpenShort(
poolInfo: &JsPoolInfo,
poolConfig: &JsPoolConfig,
shortAmount: String,
openVaultSharePrice: String,
) -> String {
set_panic_hook();
let state = State {
info: poolInfo.into(),
config: poolConfig.into(),
};
let short_amount: FixedPoint = FixedPoint::from(U256::from_dec_str(&shortAmount).unwrap());
let short_amount = FixedPoint::from(U256::from_dec_str(&shortAmount).unwrap());

let spot_price = state.get_spot_price();

let openVaultSharePriceFixedPoint =
FixedPoint::from(U256::from_dec_str(&openVaultSharePrice).unwrap());

let result_fp = state
.calculate_open_short(short_amount, spot_price, fixed!(0))
.calculate_open_short(short_amount, spot_price, openVaultSharePriceFixedPoint)
.unwrap();

U256::from(result_fp).to_string()
Expand Down Expand Up @@ -70,9 +75,9 @@ pub fn getMaxShort(
config: poolConfig.into(),
};
let _budget = U256::from_dec_str(&budget).unwrap();
let checkpoint_exposure: I256 =
I256::from_dec_str(&checkpointExposure).unwrap();
let open_vault_share_price: I256 = I256::from_raw(U256::from_dec_str(&openVaultSharePrice).unwrap());
let checkpoint_exposure: I256 = I256::from_dec_str(&checkpointExposure).unwrap();
let open_vault_share_price: I256 =
I256::from_raw(U256::from_dec_str(&openVaultSharePrice).unwrap());

let _maybe_conservative_price: Option<FixedPoint> = maybeConservativePrice
.as_ref()
Expand Down Expand Up @@ -139,7 +144,6 @@ pub fn calcSpotPriceAfterShort(
};
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()
Expand All @@ -157,7 +161,7 @@ pub fn calcSpotPriceAfterShort(
/// @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(
Expand All @@ -178,7 +182,12 @@ pub fn calcCloseShort(
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);
let result_fp = state.calculate_close_short(
_bondAmount,
_openVaultSharePrice,
_closeVaultSharePrice,
_normalizedTimeRemaining,
);

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

0 comments on commit 9484dc1

Please sign in to comment.