diff --git a/.github/workflows/build_wheels.yml b/.github/workflows/build_wheels.yml index ef51fec..6fe26c4 100644 --- a/.github/workflows/build_wheels.yml +++ b/.github/workflows/build_wheels.yml @@ -50,7 +50,7 @@ jobs: uses: actions/checkout@master with: repository: delvtech/hyperdrive - ref: "v0.11.0" + ref: "v0.13.0" path: "./hyperdrive" ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }} @@ -86,7 +86,7 @@ jobs: uses: actions/checkout@master with: repository: delvtech/hyperdrive - ref: "v0.11.0" + ref: "v0.13.0" path: "./hyperdrive" ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4cf22a7..66bced1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -34,7 +34,7 @@ jobs: uses: actions/checkout@master with: repository: delvtech/hyperdrive - ref: "v0.11.0" + ref: "v0.13.0" path: "./hyperdrive" ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2d1804b..6dbce48 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,7 +34,7 @@ jobs: uses: actions/checkout@master with: repository: delvtech/hyperdrive - ref: "v0.11.0" + ref: "v0.13.0" path: "./hyperdrive" ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }} diff --git a/Cargo.lock b/Cargo.lock index 05c5ca8..524a7bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1588,7 +1588,7 @@ dependencies = [ [[package]] name = "hyperdrivepy" -version = "0.11.0" +version = "0.13.0" dependencies = [ "ethers", "eyre", diff --git a/crates/hyperdrivepy/Cargo.toml b/crates/hyperdrivepy/Cargo.toml index 0db1387..921e355 100644 --- a/crates/hyperdrivepy/Cargo.toml +++ b/crates/hyperdrivepy/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "hyperdrivepy" edition = "2021" -version = "0.11.0" +version = "0.13.0" authors = [ "Dylan Paiton", "Matt Brown", diff --git a/crates/hyperdrivepy/pyproject.toml b/crates/hyperdrivepy/pyproject.toml index c00451c..5c5a1ce 100644 --- a/crates/hyperdrivepy/pyproject.toml +++ b/crates/hyperdrivepy/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "hyperdrivepy" -version = "0.11.0" +version = "0.13.0" requires-python = ">=3.7" classifiers = [ "Programming Language :: Rust", diff --git a/crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_state.py b/crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_state.py index 954a802..b2eb0b6 100644 --- a/crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_state.py +++ b/crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_state.py @@ -9,7 +9,7 @@ # pylint: disable=too-many-arguments -def get_max_spot_price( +def calculate_max_spot_price( pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, ) -> str: @@ -29,10 +29,10 @@ def get_max_spot_price( str (FixedPoint) max_spot_price = 1/1 + curve_fee * (1 / (spot_price - 1)) """ - return _get_interface(pool_config, pool_info).get_max_spot_price() + return _get_interface(pool_config, pool_info).calculate_max_spot_price() -def get_spot_price_after_long( +def calculate_spot_price_after_long( pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, long_amount: str, @@ -56,10 +56,10 @@ def get_spot_price_after_long( str (FixedPoint) The spot price after opening the long. """ - return _get_interface(pool_config, pool_info).get_spot_price_after_long(long_amount) + return _get_interface(pool_config, pool_info).calculate_spot_price_after_long(long_amount) -def get_solvency( +def calculate_solvency( pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, ) -> str: @@ -79,10 +79,10 @@ def get_solvency( str (FixedPoint) solvency = share_reserves - long_exposure / vault_share_price - minimum_share_reserves """ - return _get_interface(pool_config, pool_info).get_solvency() + return _get_interface(pool_config, pool_info).calculate_solvency() -def get_spot_rate( +def calculate_spot_rate( pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, ) -> str: @@ -102,10 +102,10 @@ def get_spot_rate( str (FixedPoint) The pool's spot rate. """ - return _get_interface(pool_config, pool_info).get_spot_rate() + return _get_interface(pool_config, pool_info).calculate_spot_rate() -def get_spot_price( +def calculate_spot_price( pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, ) -> str: @@ -125,7 +125,7 @@ def get_spot_price( str (FixedPoint) The pool's spot price. """ - return _get_interface(pool_config, pool_info).get_spot_price() + return _get_interface(pool_config, pool_info).calculate_spot_price() def calculate_open_long( @@ -158,7 +158,8 @@ def calculate_close_long( pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, bond_amount: str, - normalized_time_remaining: str, + maturity_time: str, + current_time: str, ) -> str: """Calculates the amount of shares that will be returned after fees for closing a long. @@ -172,15 +173,17 @@ def calculate_close_long( Includes attributes like reserve levels and share prices. bond_amount: str (FixedPoint) The amount of bonds to sell. - normalized_time_remaining: str (FixedPoint) - The time remaining before the long reaches maturity, normalized such that 1 is at opening and 0 is at maturity. + maturity_time: str (FixedPoint) + The maturity time of the long. + current_time: str (FixedPoint) + The current block time. Returns ------- str (FixedPoint) The amount of shares returned. """ - return _get_interface(pool_config, pool_info).calculate_close_long(bond_amount, normalized_time_remaining) + return _get_interface(pool_config, pool_info).calculate_close_long(bond_amount, maturity_time, current_time) def calculate_open_short( @@ -226,7 +229,8 @@ def calculate_close_short( bond_amount: str, open_vault_share_price: str, close_vault_share_price: str, - normalized_time_remaining: str, + maturity_time: str, + current_time: str, ) -> str: """Gets the amount of shares the trader will receive from closing a short. @@ -253,7 +257,7 @@ def calculate_close_short( The amount of shares the trader will receive for closing the short. """ return _get_interface(pool_config, pool_info).calculate_close_short( - bond_amount, open_vault_share_price, close_vault_share_price, normalized_time_remaining + bond_amount, open_vault_share_price, close_vault_share_price, maturity_time, current_time ) @@ -283,7 +287,7 @@ def to_checkpoint( return _get_interface(pool_config, pool_info).to_checkpoint(time) -def get_max_long( +def calculate_max_long( pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, budget: str, @@ -312,10 +316,10 @@ def get_max_long( str (FixedPoint) The maximum long the pool and user's wallet can support. """ - return _get_interface(pool_config, pool_info).get_max_long(budget, checkpoint_exposure, maybe_max_iterations) + return _get_interface(pool_config, pool_info).calculate_max_long(budget, checkpoint_exposure, maybe_max_iterations) -def get_max_short( +def calculate_max_short( pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, budget: str, @@ -350,7 +354,7 @@ def get_max_short( str (FixedPoint) The maximum short the pool and user's wallet can handle. """ - return _get_interface(pool_config, pool_info).get_max_short( + return _get_interface(pool_config, pool_info).calculate_max_short( budget, open_vault_share_price, checkpoint_exposure, diff --git a/crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_utils.py b/crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_utils.py index 92d9965..cb6caac 100644 --- a/crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_utils.py +++ b/crates/hyperdrivepy/python/hyperdrivepy/hyperdrive_utils.py @@ -6,7 +6,7 @@ from . import hyperdrivepy as rust_module # type: ignore -def get_time_stretch(rate: str, position_duration: str) -> str: +def calculate_time_stretch(rate: str, position_duration: str) -> str: """Calculate the time stretch parameter given a pool's spot rate. ..math:: @@ -24,10 +24,10 @@ def get_time_stretch(rate: str, position_duration: str) -> str: time_stretch: str (FixedPoint) The time stretch parameter (tau). """ - return rust_module.get_time_stretch(rate, position_duration) + return rust_module.calculate_time_stretch(rate, position_duration) -def get_effective_share_reserves( +def calculate_effective_share_reserves( share_reserves: str, share_adjustment: str, ) -> str: @@ -48,7 +48,7 @@ def get_effective_share_reserves( effective_share_reserves: str (FixedPoint) The adjusted share reserves, accounting for the zeta factor. """ - return rust_module.get_effective_share_reserves(share_reserves, share_adjustment) + return rust_module.calculate_effective_share_reserves(share_reserves, share_adjustment) def calculate_initial_bond_reserves( diff --git a/crates/hyperdrivepy/python/hyperdrivepy/pypechain_types/IHyperdriveContract.py b/crates/hyperdrivepy/python/hyperdrivepy/pypechain_types/IHyperdriveContract.py index 0bc467a..9db62e0 100644 --- a/crates/hyperdrivepy/python/hyperdrivepy/pypechain_types/IHyperdriveContract.py +++ b/crates/hyperdrivepy/python/hyperdrivepy/pypechain_types/IHyperdriveContract.py @@ -202,8 +202,8 @@ def call( class IHyperdriveCheckpointContractFunction(ContractFunction): """ContractFunction for the checkpoint method.""" - def __call__(self, checkpointTime: int) -> IHyperdriveCheckpointContractFunction: # type: ignore - clone = super().__call__(dataclass_to_tuple(checkpointTime)) + def __call__(self, checkpointTime: int, maxIterations: int) -> IHyperdriveCheckpointContractFunction: # type: ignore + clone = super().__call__(dataclass_to_tuple(checkpointTime), dataclass_to_tuple(maxIterations)) self.kwargs = clone.kwargs self.args = clone.args return self @@ -3912,8 +3912,8 @@ class IHyperdriveInsufficientLiquidityContractError: def __init__( self: "IHyperdriveInsufficientLiquidityContractError", ) -> None: - self.selector = "0x780daf16" - self.signature = "InsufficientLiquidity(uint8)" + self.selector = "0xbb55fd27" + self.signature = "InsufficientLiquidity()" def decode_error_data( # type: ignore self: "IHyperdriveInsufficientLiquidityContractError", @@ -5716,7 +5716,10 @@ def decode_custom_error(self, data: str) -> tuple[Any, ...]: { "type": "function", "name": "checkpoint", - "inputs": [{"name": "_checkpointTime", "type": "uint256", "internalType": "uint256"}], + "inputs": [ + {"name": "_checkpointTime", "type": "uint256", "internalType": "uint256"}, + {"name": "_maxIterations", "type": "uint256", "internalType": "uint256"}, + ], "outputs": [], "stateMutability": "nonpayable", }, @@ -6350,6 +6353,7 @@ def decode_custom_error(self, data: str) -> tuple[Any, ...]: "name": "CreateCheckpoint", "inputs": [ {"name": "checkpointTime", "type": "uint256", "indexed": True, "internalType": "uint256"}, + {"name": "checkpointVaultSharePrice", "type": "uint256", "indexed": False, "internalType": "uint256"}, {"name": "vaultSharePrice", "type": "uint256", "indexed": False, "internalType": "uint256"}, {"name": "maturedShorts", "type": "uint256", "indexed": False, "internalType": "uint256"}, {"name": "maturedLongs", "type": "uint256", "indexed": False, "internalType": "uint256"}, @@ -6488,13 +6492,7 @@ def decode_custom_error(self, data: str) -> tuple[Any, ...]: {"type": "error", "name": "ExpInvalidExponent", "inputs": []}, {"type": "error", "name": "ExpiredDeadline", "inputs": []}, {"type": "error", "name": "InsufficientBalance", "inputs": []}, - { - "type": "error", - "name": "InsufficientLiquidity", - "inputs": [ - {"name": "reason", "type": "uint8", "internalType": "enum IHyperdrive.InsufficientLiquidityReason"} - ], - }, + {"type": "error", "name": "InsufficientLiquidity", "inputs": []}, {"type": "error", "name": "InvalidApr", "inputs": []}, {"type": "error", "name": "InvalidCheckpointTime", "inputs": []}, {"type": "error", "name": "InvalidERC20Bridge", "inputs": []}, diff --git a/crates/hyperdrivepy/python/hyperdrivepy/pypechain_types/IHyperdriveTypes.py b/crates/hyperdrivepy/python/hyperdrivepy/pypechain_types/IHyperdriveTypes.py index c50f17f..abab20e 100644 --- a/crates/hyperdrivepy/python/hyperdrivepy/pypechain_types/IHyperdriveTypes.py +++ b/crates/hyperdrivepy/python/hyperdrivepy/pypechain_types/IHyperdriveTypes.py @@ -199,6 +199,7 @@ class WithdrawPool: anonymous=False, inputs=[ ABIEventParams(indexed=True, name="checkpointTime", type="uint256"), + ABIEventParams(indexed=False, name="checkpointVaultSharePrice", type="uint256"), ABIEventParams(indexed=False, name="vaultSharePrice", type="uint256"), ABIEventParams(indexed=False, name="maturedShorts", type="uint256"), ABIEventParams(indexed=False, name="maturedLongs", type="uint256"), @@ -422,12 +423,10 @@ class ErrorParams: ) InsufficientLiquidityError = ErrorInfo( - inputs=[ - ErrorParams(name="reason", python_type="int", solidity_type="uint8"), - ], + inputs=[], name="InsufficientLiquidity", - selector="0x780daf16", - signature="InsufficientLiquidity(uint8)", + selector="0xbb55fd27", + signature="InsufficientLiquidity()", ) InvalidAprError = ErrorInfo( diff --git a/crates/hyperdrivepy/setup.py b/crates/hyperdrivepy/setup.py index 4780692..d12851b 100644 --- a/crates/hyperdrivepy/setup.py +++ b/crates/hyperdrivepy/setup.py @@ -5,7 +5,7 @@ setup( name="hyperdrivepy", - version="0.11.0", + version="0.13.0", packages=["hyperdrivepy"], package_dir={"": "python"}, rust_extensions=[ diff --git a/crates/hyperdrivepy/src/hyperdrive_state_methods.rs b/crates/hyperdrivepy/src/hyperdrive_state_methods.rs index 499c0f1..f6ae653 100644 --- a/crates/hyperdrivepy/src/hyperdrive_state_methods.rs +++ b/crates/hyperdrivepy/src/hyperdrive_state_methods.rs @@ -22,35 +22,38 @@ impl HyperdriveState { Ok(HyperdriveState::new(state)) } - pub fn get_max_spot_price(&self) -> PyResult { - let result_fp = self.state.get_max_spot_price(); + pub fn calculate_max_spot_price(&self) -> PyResult { + let result_fp = self.state.calculate_max_spot_price(); let result = U256::from(result_fp).to_string(); return Ok(result); } - pub fn get_spot_price_after_long(&self, long_amount: &str) -> PyResult { + pub fn calculate_spot_price_after_long(&self, long_amount: &str) -> PyResult { let long_amount_fp = FixedPoint::from(U256::from_dec_str(long_amount).map_err(|_| { PyErr::new::("Failed to convert long_amount string to U256") })?); - let result_fp = self.state.get_spot_price_after_long(long_amount_fp); + let result_fp = self + .state + .calculate_spot_price_after_long(long_amount_fp, None) + .unwrap(); let result = U256::from(result_fp).to_string(); return Ok(result); } - pub fn get_solvency(&self) -> PyResult { - let result_fp = self.state.get_solvency(); + pub fn calculate_solvency(&self) -> PyResult { + let result_fp = self.state.calculate_solvency(); let result = U256::from(result_fp).to_string(); return Ok(result); } - pub fn get_spot_price(&self) -> PyResult { - let result_fp = self.state.get_spot_price(); + pub fn calculate_spot_price(&self) -> PyResult { + let result_fp = self.state.calculate_spot_price(); let result = U256::from(result_fp).to_string(); return Ok(result); } - pub fn get_spot_rate(&self) -> PyResult { - let result_fp = self.state.get_spot_rate(); + pub fn calculate_spot_rate(&self) -> PyResult { + let result_fp = self.state.calculate_spot_rate(); let result = U256::from(result_fp).to_string(); return Ok(result); } @@ -59,7 +62,7 @@ impl HyperdriveState { let base_amount_fp = FixedPoint::from(U256::from_dec_str(base_amount).map_err(|_| { PyErr::new::("Failed to convert base_amount string to U256") })?); - let result_fp = self.state.calculate_open_long(base_amount_fp); + let result_fp = self.state.calculate_open_long(base_amount_fp).unwrap(); let result = U256::from(result_fp).to_string(); return Ok(result); } @@ -67,20 +70,22 @@ impl HyperdriveState { pub fn calculate_close_long( &self, bond_amount: &str, - normalized_time_remaining: &str, + maturity_time: &str, + current_time: &str, ) -> PyResult { let bond_amount_fp = FixedPoint::from(U256::from_dec_str(bond_amount).map_err(|_| { PyErr::new::("Failed to convert bond_amount string to U256") })?); - let normalized_time_remaining_fp = - FixedPoint::from(U256::from_dec_str(normalized_time_remaining).map_err(|_| { - PyErr::new::( - "Failed to convert normalized_time_remaining string to U256", - ) - })?); - let result_fp = self - .state - .calculate_close_long(bond_amount_fp, normalized_time_remaining_fp); + let maturity_time = U256::from_dec_str(maturity_time).map_err(|_| { + PyErr::new::("Failed to convert maturity_time string to U256") + })?; + let current_time = U256::from_dec_str(current_time).map_err(|_| { + PyErr::new::("Failed to convert current_time string to U256") + })?; + + let result_fp = + self.state + .calculate_close_long(bond_amount_fp, maturity_time, current_time); let result = U256::from(result_fp).to_string(); return Ok(result); } @@ -99,7 +104,9 @@ impl HyperdriveState { })?); let open_vault_share_price_fp = FixedPoint::from(U256::from_dec_str(open_vault_share_price).map_err(|_| { - PyErr::new::("Failed to convert open_vault_share_price string to U256") + PyErr::new::( + "Failed to convert open_vault_share_price string to U256", + ) })?); let result_fp = self .state @@ -114,30 +121,36 @@ impl HyperdriveState { bond_amount: &str, open_vault_share_price: &str, close_vault_share_price: &str, - normalized_time_remaining: &str, + maturity_time: &str, + current_time: &str, ) -> PyResult { let bond_amount_fp = FixedPoint::from(U256::from_dec_str(bond_amount).map_err(|_| { PyErr::new::("Failed to convert bond_amount string to U256") })?); let open_vault_share_price_fp = FixedPoint::from(U256::from_dec_str(open_vault_share_price).map_err(|_| { - PyErr::new::("Failed to convert open_vault_share_price string to U256") + PyErr::new::( + "Failed to convert open_vault_share_price string to U256", + ) })?); let close_vault_share_price_fp = FixedPoint::from(U256::from_dec_str(close_vault_share_price).map_err(|_| { - PyErr::new::("Failed to convert close_vault_share_price string to U256") - })?); - let normalized_time_remaining_fp = - FixedPoint::from(U256::from_dec_str(normalized_time_remaining).map_err(|_| { PyErr::new::( - "Failed to convert normalized_time_remaining string to U256", + "Failed to convert close_vault_share_price string to U256", ) })?); + let maturity_time = U256::from_dec_str(maturity_time).map_err(|_| { + PyErr::new::("Failed to convert maturity_time string to U256") + })?; + let current_time = U256::from_dec_str(current_time).map_err(|_| { + PyErr::new::("Failed to convert current_time string to U256") + })?; let result_fp = self.state.calculate_close_short( bond_amount_fp, open_vault_share_price_fp, close_vault_share_price_fp, - normalized_time_remaining_fp, + maturity_time, + current_time, ); let result = U256::from(result_fp).to_string(); return Ok(result); @@ -161,7 +174,8 @@ impl HyperdriveState { // We unwrap the error here to throw panic error if this fails let result_fp = self .state - .calculate_shares_in_given_bonds_out_up_safe(amount_in_fp).unwrap(); + .calculate_shares_in_given_bonds_out_up_safe(amount_in_fp) + .unwrap(); let result = U256::from(result_fp).to_string(); return Ok(result); } @@ -196,7 +210,7 @@ impl HyperdriveState { return Ok(result); } - pub fn get_max_long( + pub fn calculate_max_long( &self, budget: &str, checkpoint_exposure: &str, @@ -210,12 +224,12 @@ impl HyperdriveState { })?; let result_fp = self.state - .get_max_long(budget_fp, checkpoint_exposure_i, maybe_max_iterations); + .calculate_max_long(budget_fp, checkpoint_exposure_i, maybe_max_iterations); let result = U256::from(result_fp).to_string(); return Ok(result); } - pub fn get_max_short( + pub fn calculate_max_short( &self, budget: &str, open_vault_share_price: &str, @@ -228,7 +242,9 @@ impl HyperdriveState { })?); let open_vault_share_price_fp = FixedPoint::from(U256::from_dec_str(open_vault_share_price).map_err(|_| { - PyErr::new::("Failed to convert open_vault_share_price string to U256") + PyErr::new::( + "Failed to convert open_vault_share_price string to U256", + ) })?); let checkpoint_exposure_i = I256::from_dec_str(checkpoint_exposure).map_err(|_| { PyErr::new::("Failed to convert checkpoint_exposure string to I256") @@ -245,7 +261,7 @@ impl HyperdriveState { } else { None }; - let result_fp = self.state.get_max_short( + let result_fp = self.state.calculate_max_short( budget_fp, open_vault_share_price_fp, checkpoint_exposure_i, @@ -271,9 +287,7 @@ impl HyperdriveState { } pub fn calculate_idle_share_reserves_in_base(&self) -> PyResult { - let result_fp = self - .state - .calculate_idle_share_reserves_in_base(); + let result_fp = self.state.calculate_idle_share_reserves_in_base(); let result = U256::from(result_fp).to_string(); return Ok(result); } diff --git a/crates/hyperdrivepy/src/hyperdrive_utils.rs b/crates/hyperdrivepy/src/hyperdrive_utils.rs index 8998069..da622ef 100644 --- a/crates/hyperdrivepy/src/hyperdrive_utils.rs +++ b/crates/hyperdrivepy/src/hyperdrive_utils.rs @@ -1,14 +1,14 @@ use ethers::core::types::{I256, U256}; use fixed_point::FixedPoint; +use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use pyo3::PyErr; -use pyo3::exceptions::PyValueError; use hyperdrive_math::{ + calculate_effective_share_reserves as rs_calculate_effective_share_reserves, calculate_initial_bond_reserves as rs_calculate_initial_bond_reserves, - get_effective_share_reserves as rs_get_effective_share_reserves, - get_time_stretch as rs_get_time_stretch, + calculate_time_stretch as rs_calculate_time_stretch, }; #[pyfunction] @@ -27,7 +27,9 @@ pub fn calculate_initial_bond_reserves( })?); let initial_vault_share_price_fp = FixedPoint::from(U256::from_dec_str(initial_vault_share_price).map_err(|_| { - PyErr::new::("Failed to convert initial_vault_share_price string to U256") + PyErr::new::( + "Failed to convert initial_vault_share_price string to U256", + ) })?); let apr_fp = FixedPoint::from( U256::from_dec_str(apr) @@ -52,7 +54,7 @@ pub fn calculate_initial_bond_reserves( } #[pyfunction] -pub fn get_effective_share_reserves( +pub fn calculate_effective_share_reserves( share_reserves: &str, share_adjustment: &str, ) -> PyResult { @@ -62,13 +64,13 @@ pub fn get_effective_share_reserves( let share_adjustment_i = I256::from_dec_str(share_adjustment).map_err(|_| { PyErr::new::("Failed to convert share_adjustment string to I256") })?; - let result_fp = rs_get_effective_share_reserves(share_reserves_fp, share_adjustment_i); + let result_fp = rs_calculate_effective_share_reserves(share_reserves_fp, share_adjustment_i); let result = U256::from(result_fp).to_string(); return Ok(result); } #[pyfunction] -pub fn get_time_stretch(rate: &str, position_duration: &str) -> PyResult { +pub fn calculate_time_stretch(rate: &str, position_duration: &str) -> PyResult { let rate_fp = FixedPoint::from( U256::from_dec_str(rate) .map_err(|_| PyErr::new::("Failed to convert rate string to U256"))?, @@ -77,7 +79,7 @@ pub fn get_time_stretch(rate: &str, position_duration: &str) -> PyResult U256::from_dec_str(position_duration) .map_err(|_| PyErr::new::("Failed to convert rate string to U256"))?, ); - let result_fp = rs_get_time_stretch(rate_fp, position_duration_fp); + let result_fp = rs_calculate_time_stretch(rate_fp, position_duration_fp); let result = U256::from(result_fp).to_string(); return Ok(result); } diff --git a/crates/hyperdrivepy/src/lib.rs b/crates/hyperdrivepy/src/lib.rs index 7bbef52..30d876b 100644 --- a/crates/hyperdrivepy/src/lib.rs +++ b/crates/hyperdrivepy/src/lib.rs @@ -10,7 +10,7 @@ use pyo3::prelude::*; use hyperdrive_state::HyperdriveState; pub use hyperdrive_state_methods::*; pub use hyperdrive_utils::{ - calculate_initial_bond_reserves, get_effective_share_reserves, get_time_stretch, + calculate_effective_share_reserves, calculate_initial_bond_reserves, calculate_time_stretch, }; pub use pool_config::PyPoolConfig; pub use pool_info::PyPoolInfo; @@ -22,7 +22,7 @@ pub use pool_info::PyPoolInfo; fn hyperdrivepy(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_class::()?; m.add_function(wrap_pyfunction!(calculate_initial_bond_reserves, m)?)?; - m.add_function(wrap_pyfunction!(get_effective_share_reserves, m)?)?; - m.add_function(wrap_pyfunction!(get_time_stretch, m)?)?; + m.add_function(wrap_pyfunction!(calculate_effective_share_reserves, m)?)?; + m.add_function(wrap_pyfunction!(calculate_time_stretch, m)?)?; Ok(()) } diff --git a/pyproject.toml b/pyproject.toml index 9455193..ff2f177 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "hyperdrivepy" -version = "0.11.0" +version = "0.13.0" authors = [ { name = "Dylan Paiton", email = "dylan@delv.tech" }, { name = "Matthew Brown", email = "matt@delv.tech" }, diff --git a/system_tests/wrapper_tests.py b/system_tests/wrapper_tests.py index 271bd77..35531ad 100644 --- a/system_tests/wrapper_tests.py +++ b/system_tests/wrapper_tests.py @@ -41,34 +41,34 @@ ) -def test_get_max_spot_price(): +def test_calculate_max_spot_price(): """test get_max_spot_rate.""" - max_spot_price = hyperdrivepy.get_max_spot_price(POOL_CONFIG, POOL_INFO) + max_spot_price = hyperdrivepy.calculate_max_spot_price(POOL_CONFIG, POOL_INFO) assert max_spot_price is not None, "Failed to get max spot price." assert isinstance(max_spot_price, str), "Expected spot rate to be a string." assert int(max_spot_price) > 0, "Expected max_spot_price to > 0." -def test_get_spot_price_after_long(): - """test get_spot_price_after_long.""" - spot_price = hyperdrivepy.get_spot_price_after_long(POOL_CONFIG, POOL_INFO, long_amount=str(1_000 * 10**18)) - assert spot_price is not None, "Failed to get spot price after long." +def test_calculate_spot_price_after_long(): + """test calculate_spot_price_after_long.""" + spot_price = hyperdrivepy.calculate_spot_price_after_long(POOL_CONFIG, POOL_INFO, long_amount=str(1_000 * 10**18)) + assert spot_price is not None, "Failed to calculate spot price after long." assert isinstance(spot_price, str), "Expected spot rate to be a string." assert int(spot_price) > 0, "Expected max_spot_price to > 0." -def test_get_solvency(): - """test get_max_spot_rate.""" - solvency = hyperdrivepy.get_solvency(POOL_CONFIG, POOL_INFO) - assert solvency is not None, "Failed to get spot price after long." +def test_calculate_solvency(): + """test calculate_max_spot_rate.""" + solvency = hyperdrivepy.calculate_solvency(POOL_CONFIG, POOL_INFO) + assert solvency is not None, "Failed to calculate spot price after long." assert isinstance(solvency, str), "Expected spot rate to be a string." assert int(solvency) > 0, "Expected max_spot_price to > 0." -def test_get_spot_rate(): - """test get_spot_rate.""" - spot_rate = hyperdrivepy.get_spot_rate(POOL_CONFIG, POOL_INFO) - assert spot_rate is not None, "Failed to get spot rate." +def test_calculate_spot_rate(): + """test calculate_spot_rate.""" + spot_rate = hyperdrivepy.calculate_spot_rate(POOL_CONFIG, POOL_INFO) + assert spot_rate is not None, "Failed to calculate spot rate." assert isinstance(spot_rate, str), "Expected spot rate to be a string." assert int(spot_rate) > 0, "Expected spot rate to > 0." @@ -80,58 +80,58 @@ def test_to_checkpoint(): assert isinstance(checkpoint_time, str), "Expected checkpoint time to be a string." -def test_get_spot_price(): - """test get_spot_price.""" - spot_price = hyperdrivepy.get_spot_price(POOL_CONFIG, POOL_INFO) - assert spot_price is not None, "Failed to get spot price." +def test_calculate_spot_price(): + """test calculate_spot_price.""" + spot_price = hyperdrivepy.calculate_spot_price(POOL_CONFIG, POOL_INFO) + assert spot_price is not None, "Failed to calculate spot price." assert isinstance(spot_price, str), "Expected spot price to be a string." assert int(spot_price) > 0, "Expected spot price to > 0." -def test_get_time_stretch(): - """test get_time_stretch.""" - time_stretch = hyperdrivepy.get_time_stretch( - hyperdrivepy.get_spot_rate(POOL_CONFIG, POOL_INFO), +def test_calculate_time_stretch(): + """test calculate_time_stretch.""" + time_stretch = hyperdrivepy.calculate_time_stretch( + hyperdrivepy.calculate_spot_rate(POOL_CONFIG, POOL_INFO), str(60 * 60 * 24 * 365), # 1 year ) - assert time_stretch is not None, "Failed to get time_stretch." + assert time_stretch is not None, "Failed to calculate time_stretch." assert isinstance(time_stretch, str), "Expected time_stretch to be a string." assert float(time_stretch) > 0, "Expected time_stretch to be > 0." - time_stretch = hyperdrivepy.get_time_stretch( - hyperdrivepy.get_spot_rate(POOL_CONFIG, POOL_INFO), + time_stretch = hyperdrivepy.calculate_time_stretch( + hyperdrivepy.calculate_spot_rate(POOL_CONFIG, POOL_INFO), str(60 * 60 * 24 * 30), # ~1 month ) - assert time_stretch is not None, "Failed to get time_stretch." + assert time_stretch is not None, "Failed to calculate time_stretch." assert isinstance(time_stretch, str), "Expected time_stretch to be a string." assert float(time_stretch) > 0, "Expected time_stretch to be > 0." -def test_get_effective_share_reserves(): - """Test get_effective_share_reserves.""" - effective_share_reserves = hyperdrivepy.get_effective_share_reserves( +def test_calculate_effective_share_reserves(): + """Test calculate_effective_share_reserves.""" + effective_share_reserves = hyperdrivepy.calculate_effective_share_reserves( str(POOL_INFO.shareReserves), str(POOL_INFO.shareAdjustment), ) - assert effective_share_reserves is not None, "Failed to get effective_share_reserves." + assert effective_share_reserves is not None, "Failed to calculate effective_share_reserves." assert isinstance(effective_share_reserves, str), "Expected effective_share_reserves to be a string." assert int(effective_share_reserves) > 0, "Expected effective_share_reserves to be > 0." def test_calculate_initial_bond_reserves(): """Test calculate_initial_bond_reserves.""" - effective_share_reserves = hyperdrivepy.get_effective_share_reserves( + effective_share_reserves = hyperdrivepy.calculate_effective_share_reserves( str(POOL_INFO.shareReserves), str(POOL_INFO.shareAdjustment), ) bonds = hyperdrivepy.calculate_initial_bond_reserves( effective_share_reserves, str(POOL_CONFIG.initialVaultSharePrice), - hyperdrivepy.get_spot_rate(POOL_CONFIG, POOL_INFO), + hyperdrivepy.calculate_spot_rate(POOL_CONFIG, POOL_INFO), str(POOL_CONFIG.positionDuration), str(POOL_CONFIG.timeStretch), ) - assert bonds is not None, "Failed to get bonds." + assert bonds is not None, "Failed to calculate bonds." assert isinstance(bonds, str), "Expected bonds to be a string." assert int(bonds) > 0, "Expected bonds to be > 0." @@ -174,15 +174,18 @@ def test_calculate_open_long(): def test_calculate_close_long(): """Test for calculate_close_long.""" bond_amount = str(500 * 10**18) - normalized_time_remaining = str(9 * 10**17) - shares_returned = hyperdrivepy.calculate_close_long(POOL_CONFIG, POOL_INFO, bond_amount, normalized_time_remaining) + maturity_time = str(9 * 10**17 + 10) + current_time = str(9 * 10**17) + shares_returned = hyperdrivepy.calculate_close_long( + POOL_CONFIG, POOL_INFO, bond_amount, maturity_time, current_time + ) assert int(shares_returned) > 0 def test_calculate_open_short(): """Test for calculate_open_short.""" short_amount = str(50 * 10**18) - spot_price = hyperdrivepy.get_spot_price(POOL_CONFIG, POOL_INFO) + spot_price = hyperdrivepy.calculate_spot_price(POOL_CONFIG, POOL_INFO) open_vault_share_price = str(9 * 10**17) base_required = hyperdrivepy.calculate_open_short( POOL_CONFIG, POOL_INFO, short_amount, spot_price, open_vault_share_price @@ -202,38 +205,45 @@ def test_calculate_close_short(): short_amount = str(50 * 10**18) open_vault_share_price = str(8 * 10**17) close_vault_share_price = str(9 * 10**17) - normalized_time_remaining = str(9 * 10**17) + maturity_time = str(9 * 10**17 + 10) + current_time = str(9 * 10**17) shares_received = hyperdrivepy.calculate_close_short( - POOL_CONFIG, POOL_INFO, short_amount, open_vault_share_price, close_vault_share_price, normalized_time_remaining + POOL_CONFIG, + POOL_INFO, + short_amount, + open_vault_share_price, + close_vault_share_price, + maturity_time, + current_time, ) assert int(shares_received) > 0 def test_max_long(): - """Test get_max_long.""" + """Test calculate_max_long.""" budget = "1000000000000000000" # 1 base checkpoint_exposure = "10000" max_iterations = 20 - max_long = hyperdrivepy.get_max_long(POOL_CONFIG, POOL_INFO, budget, checkpoint_exposure, max_iterations) + max_long = hyperdrivepy.calculate_max_long(POOL_CONFIG, POOL_INFO, budget, checkpoint_exposure, max_iterations) assert int(max_long) > 0 # should == "1000000000000000000", or 1 base def test_max_long_fail_conversion(): - """Test get_max_long.""" + """Test calculate_max_long.""" max_iterations = 20 # bad string inputs budget = "asdf" checkpoint_exposure = "100" with pytest.raises(ValueError, match="Failed to convert budget string to U256"): - hyperdrivepy.get_max_long(POOL_CONFIG, POOL_INFO, budget, checkpoint_exposure, max_iterations) + hyperdrivepy.calculate_max_long(POOL_CONFIG, POOL_INFO, budget, checkpoint_exposure, max_iterations) budget = "1.23" checkpoint_exposure = "100" with pytest.raises(ValueError, match="Failed to convert budget string to U256"): - hyperdrivepy.get_max_long(POOL_CONFIG, POOL_INFO, budget, checkpoint_exposure, max_iterations) + hyperdrivepy.calculate_max_long(POOL_CONFIG, POOL_INFO, budget, checkpoint_exposure, max_iterations) budget = "1000000000000000000" # 1 base checkpoint_exposure = "asdf" with pytest.raises(ValueError, match="Failed to convert checkpoint_exposure string to I256"): - hyperdrivepy.get_max_long(POOL_CONFIG, POOL_INFO, budget, checkpoint_exposure, max_iterations) + hyperdrivepy.calculate_max_long(POOL_CONFIG, POOL_INFO, budget, checkpoint_exposure, max_iterations) def test_max_short(): @@ -244,7 +254,7 @@ def test_max_short(): checkpoint_exposure = str(0) conservative_price = None max_iterations = 20 - max_short = hyperdrivepy.get_max_short( + max_short = hyperdrivepy.calculate_max_short( POOL_CONFIG, POOL_INFO, budget, @@ -257,7 +267,7 @@ def test_max_short(): def test_max_short_fail_conversion(): - """Test get_max_short.""" + """Test calculate_max_short.""" open_vault_share_price = str(int(1 * 10**18)) # 1 base checkpoint_exposure = str(0) conservative_price = None @@ -265,7 +275,7 @@ def test_max_short_fail_conversion(): # bad string inputs budget = "asdf" with pytest.raises(ValueError, match="Failed to convert budget string to U256"): - hyperdrivepy.get_max_short( + hyperdrivepy.calculate_max_short( POOL_CONFIG, POOL_INFO, budget, @@ -276,7 +286,7 @@ def test_max_short_fail_conversion(): ) budget = "1.23" with pytest.raises(ValueError, match="Failed to convert budget string to U256"): - hyperdrivepy.get_max_short( + hyperdrivepy.calculate_max_short( POOL_CONFIG, POOL_INFO, budget, @@ -288,7 +298,7 @@ def test_max_short_fail_conversion(): budget = "10000000000000000000000" # 10k base open_vault_share_price = "asdf" with pytest.raises(ValueError, match="Failed to convert open_vault_share_price string to U256"): - hyperdrivepy.get_max_short( + hyperdrivepy.calculate_max_short( POOL_CONFIG, POOL_INFO, budget,