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

Commit

Permalink
[hyperdrive-wasm] Update to hyperdrive v0.4.0 (#50)
Browse files Browse the repository at this point in the history
Updates the hyperdrive-wasm crate to be compatible with hyperdrive
v0.4.0. The version has been updated to match the hyperdrive version.
It'll stay synced with hyperdrive moving forward.

TODO: Bump hyperdrive crate versions in new hyperdrive releases.
  • Loading branch information
ryangoree authored Jan 5, 2024
1 parent eb8bbd2 commit b426965
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 97 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.3.0'
ref: 'v0.4.0'
path: './hyperdrive'
ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }}

Expand Down
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
@@ -1,6 +1,6 @@
[package]
name = "hyperdrive-wasm"
version = "0.2.0"
version = "0.4.0"
edition = "2021"

[lib]
Expand Down
43 changes: 25 additions & 18 deletions crates/hyperdrive-wasm/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ const ZERO_ADDRESS = '0x'.padEnd(42, '0');

// const MAX_BUDGET = BigInt(MAX_U256).toString();
const mockPoolInfo = {
shareReserves: '50000000000000000000000',
bondReserves: '151005848028396475250000',
sharePrice: '1001342906360984271',
longsOutstanding: '0',
shortsOutstanding: '0',
longExposure: '26169526102456856203830',
shareReserves: '10000000000000000000000000',
shareAdjustment: '0',
zombieShareReserves: '0',
bondReserves: '10217899519533796120000000',
lpTotalSupply: '9999990000000000000000000',
sharePrice: '1000000000000000000',
longsOutstanding: '0',
longAverageMaturityTime: '0',
shortsOutstanding: '0',
shortAverageMaturityTime: '0',
lpTotalSupply: '49999999000000000000000',
lpSharePrice: '1001342906360984270',
withdrawalSharesProceeds: '0',
withdrawalSharesReadyToWithdraw: '0',
withdrawalSharesProceeds: '0',
lpSharePrice: '1000000000000000000',
longExposure: '0',
};
const mockPoolConfig = {
baseToken: ZERO_ADDRESS,
Expand All @@ -26,26 +27,32 @@ const mockPoolConfig = {
fees: {
curve: '100000000000000000',
flat: '500000000000000',
governance: '150000000000000000',
governanceLP: '10000000000000000',
governanceZombie: '100000000000000000',
},
initialSharePrice: '1000000000000000000',
minimumShareReserves: '1000000000000000',
minimumTransactionAmount: '1000000000000',
timeStretch: '31124187940342208',
positionDuration: '31536000',
checkpointDuration: '86400',
linkerCodeHash: '0x2b287bf09de636779ceb93a28afed387376a9bdabfcf1470369558a3dc67accc',
minimumShareReserves: '10000000000000000000',
minimumTransactionAmount: '1000000000000000',
timeStretch: '44463125629060298',
positionDuration: '604800',
checkpointDuration: '3600',
linkerCodeHash: '0x'.padEnd(66, '0'),
linkerFactory: ZERO_ADDRESS,
precisionThreshold: .5e18.toString(),
};

async function main() {
hyperdriveWasm.initSync(hyperdriveWasm.wasmBuffer);

const spotRate = hyperdriveWasm.getSpotRate(mockPoolInfo, mockPoolConfig);
console.log('spotRate:', spotRate);

const spotPrice = hyperdriveWasm.getSpotPrice(mockPoolInfo, mockPoolConfig);
console.log('spotPrice:', spotPrice);

const baseAmountRequired = hyperdriveWasm.calcOpenShort(
mockPoolInfo,
mockPoolConfig,
10e18.toString(),
(10e18).toString()
);
console.log('baseAmountRequired:', baseAmountRequired);
}
Expand Down
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 @@ -17,6 +17,6 @@
"webpack-dev-server": "^4.15.1"
},
"dependencies": {
"@delvtech/hyperdrive-wasm": "file:delvtech-hyperdrive-wasm-0.1.0.tgz"
"@delvtech/hyperdrive-wasm": "file:delvtech-hyperdrive-wasm-0.4.0.tgz"
}
}
89 changes: 18 additions & 71 deletions crates/hyperdrive-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const FEES: &'static str = r#"
interface Fees {
curve: string;
flat: string;
governance: string;
governanceLP: string;
governanceZombie: string;
}"#;

#[wasm_bindgen(typescript_custom_section)]
Expand All @@ -36,7 +37,6 @@ interface PoolConfig {
fees: Fees,
linkerFactory: string,
linkerCodeHash: string,
precisionThreshold: string,
}"#;

#[wasm_bindgen(typescript_custom_section)]
Expand All @@ -55,6 +55,7 @@ interface PoolInfo {
withdrawalSharesProceeds: string,
lpSharePrice: string,
longExposure: string,
zombieShareReserves: string,
}
"#;

Expand Down Expand Up @@ -124,8 +125,7 @@ pub fn getMaxShort(
.to_string()
}

/// Gets the number of bonds received when opening a long for a given base
/// amount.
/// Gets the long amount that will be opened for a given base amount.
///
/// @param poolInfo - The current state of the pool
///
Expand All @@ -141,11 +141,10 @@ pub fn calcOpenLong(poolInfo: &PoolInfo, poolConfig: &PoolConfig, baseAmount: St
});
let base_amount: FixedPoint = FixedPoint::from(U256::from_dec_str(&baseAmount).unwrap());

_state.get_long_amount(base_amount).to_string()
_state.calculate_open_long(base_amount).to_string()
}

/// Gets the amount of base the trader will need to deposit for a short of
/// a given number of bonds.
/// Gets the amount of base the trader will need to deposit for a short of a given size.
///
/// @param poolInfo - The current state of the pool
///
Expand All @@ -164,13 +163,12 @@ pub fn calcOpenShort(poolInfo: &PoolInfo, poolConfig: &PoolConfig, shortAmount:
let spot_price = _state.get_spot_price();

_state
.get_short_deposit(short_amount, spot_price, fixed!(0))
.calculate_open_short(short_amount, spot_price, fixed!(0))
.unwrap()
.to_string()
}

/// Get the max amount of base tokens that can be spent on a long position
/// given the current state of the pool.
/// Gets the max long that can be opened given a budget.
///
/// @param poolInfo - The current state of the pool
///
Expand Down Expand Up @@ -207,7 +205,7 @@ pub fn getMaxLong(
.to_string()
}

/// Get the price of a single long token given the current state of the pool.
/// Gets the pool's spot price, i.e. the price to open a long of 1.
///
/// @param poolInfo - The current state of the pool
///
Expand Down Expand Up @@ -238,66 +236,12 @@ pub fn getSpotRate(poolInfo: &PoolInfo, poolConfig: &PoolConfig) -> String {
_state.get_spot_rate().to_string()
}

/// Gets the long amount that will be opened 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 open a long for
#[wasm_bindgen(skip_jsdoc)]
pub fn getLongAmount(poolInfo: &PoolInfo, poolConfig: &PoolConfig, baseAmount: String) -> String {
utils::set_panic_hook();
let _state = State::from(&WasmState {
info: serde_wasm_bindgen::from_value(poolInfo.into()).unwrap(),
config: serde_wasm_bindgen::from_value(poolConfig.into()).unwrap(),
});

_state
.get_long_amount(U256::from_dec_str(&baseAmount).unwrap())
.to_string()
}

/// Gets the amount of base the trader will need to deposit for a short of a
/// given size.
///
/// @param poolInfo - The current state of the pool
///
/// @param poolConfig - The pool's configuration
///
/// @param shortAmount - The amount of longs to short
///
/// @param openSharePrice - The open share price of the pool's current
/// checkpoint
#[wasm_bindgen(skip_jsdoc)]
pub fn getShortDeposit(
poolInfo: &PoolInfo,
poolConfig: &PoolConfig,
shortAmount: String,
spotPrice: String,
openSharePrice: String,
) -> String {
utils::set_panic_hook();
let _state = State::from(&WasmState {
info: serde_wasm_bindgen::from_value(poolInfo.into()).unwrap(),
config: serde_wasm_bindgen::from_value(poolConfig.into()).unwrap(),
});

_state
.get_short_deposit(
FixedPoint::from(U256::from_dec_str(&shortAmount).unwrap()),
FixedPoint::from(I256::from_raw(U256::from_dec_str(&spotPrice).unwrap())),
FixedPoint::from(I256::from_raw(U256::from_dec_str(&openSharePrice).unwrap())),
)
.unwrap()
.to_string()
}

#[derive(Serialize, Deserialize)]
pub struct WasmFees {
pub curve: String,
pub flat: String,
pub governance: String,
pub governanceLP: String,
pub governanceZombie: String,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -314,7 +258,6 @@ pub struct WasmPoolConfig {
pub fees: WasmFees,
pub linkerFactory: String,
pub linkerCodeHash: String,
pub precisionThreshold: String,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -332,6 +275,7 @@ pub struct WasmPoolInfo {
pub withdrawalSharesProceeds: String,
pub lpSharePrice: String,
pub longExposure: String,
pub zombieShareReserves: String,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -350,7 +294,10 @@ impl From<&WasmState> for State {
fees: _Fees {
curve: U256::from_dec_str(&wasm_state.config.fees.curve).unwrap(),
flat: U256::from_dec_str(&wasm_state.config.fees.flat).unwrap(),
governance: U256::from_dec_str(&wasm_state.config.fees.governance).unwrap(),
governance_lp: U256::from_dec_str(&wasm_state.config.fees.governanceLP)
.unwrap(),
governance_zombie: U256::from_dec_str(&wasm_state.config.fees.governanceZombie)
.unwrap(),
},
initial_share_price: U256::from_dec_str(&wasm_state.config.initialSharePrice)
.unwrap(),
Expand All @@ -369,8 +316,6 @@ impl From<&WasmState> for State {
.unwrap()
.try_into()
.unwrap(),
precision_threshold: U256::from_dec_str(&wasm_state.config.precisionThreshold)
.unwrap(),
},
info: _PoolInfo {
share_reserves: U256::from_dec_str(&wasm_state.info.shareReserves).unwrap(),
Expand Down Expand Up @@ -398,6 +343,8 @@ impl From<&WasmState> for State {
&wasm_state.info.withdrawalSharesReadyToWithdraw,
)
.unwrap(),
zombie_share_reserves: U256::from_dec_str(&wasm_state.info.zombieShareReserves)
.unwrap(),
},
}
}
Expand Down

0 comments on commit b426965

Please sign in to comment.