Skip to content

Commit

Permalink
hyperdrivepy cleanup & new methods (#103)
Browse files Browse the repository at this point in the history
# Description
- adds the `as_base` option to
`calculate_pool_deltas_after_add_liquidity` in hyperdrive-rs
- adds the following bindings:
  - `calculate_pool_deltas_after_open_long`
  - `calculate_add_liquidity`
  - `calculate_pool_deltas_after_add_liquidity`
- reorganizes hyperdrivepy to more closely match hyperdrive-rs

# Review Checklists

Please check each item **before approving** the pull request. While
going
through the checklist, it is recommended to leave comments on items that
are
referenced in the checklist to make sure that they are reviewed.

- [ ] **Testing**
    - [ ] Are there new or updated unit or integration tests?
    - [ ] Do the tests cover the happy paths?
    - [ ] Do the tests cover the unhappy paths?
- [ ] Are there an adequate number of fuzz tests to ensure that we are
          covering the full input space?
- [ ] If matching Solidity behavior, are there differential fuzz tests
that
          ensure that Rust matches Solidity?
  • Loading branch information
dpaiton committed May 17, 2024
1 parent 3b61bf6 commit 4c8da98
Show file tree
Hide file tree
Showing 24 changed files with 703 additions and 411 deletions.
21 changes: 14 additions & 7 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [

[workspace.package]
name="hyperdrive-rs"
version="0.15.4"
version="0.15.5"
authors = [
"Alex Towle <alex@delv.tech>",
"Dylan Paiton <dylan@delv.tech>",
Expand Down
2 changes: 1 addition & 1 deletion bindings/hyperdrivepy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ fixed-point-macros = { path = "../../crates/fixed-point-macros" }
hyperdrive-math = { path = "../../crates/hyperdrive-math" }
hyperdrive-wrappers = { path = "../../crates/hyperdrive-wrappers" }

pyo3 = { version = "0.19.0", features = ["abi3-py37"] }
pyo3 = { version = "0.19.0", features = ["abi3-py37", "multiple-pymethods"] }
2 changes: 1 addition & 1 deletion bindings/hyperdrivepy/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "hyperdrivepy"
version = "0.15.4"
version = "0.15.5"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
Expand Down
101 changes: 100 additions & 1 deletion bindings/hyperdrivepy/python/hyperdrivepy/hyperdrive_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def calculate_solvency(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
) -> str:
"""Get the pool's solvency.
"""Calculate the pool's solvency.
Arguments
---------
Expand Down Expand Up @@ -217,6 +217,32 @@ def calculate_open_long(
return _get_interface(pool_config, pool_info).calculate_open_long(base_amount)


def calculate_pool_deltas_after_open_long(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
base_amount: str,
) -> str:
"""Calculate the bond deltas to be applied to the pool after opening a long.
Arguments
---------
pool_config: PoolConfig
Static configuration for the hyperdrive contract.
Set at deploy time.
pool_info: PoolInfo
Current state information of the hyperdrive contract.
Includes attributes like reserve levels and share prices.
base_amount: str (FixedPoint)
The amount of base used to open a long.
Returns
-------
str (FixedPoint)
The amount of bonds to remove from the pool reserves.
"""
return _get_interface(pool_config, pool_info).calculate_pool_deltas_after_open_long(base_amount)


def calculate_close_long(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
Expand Down Expand Up @@ -657,3 +683,76 @@ def calculate_idle_share_reserves_in_base(
The idle share reserves in base of the pool.
"""
return _get_interface(pool_config, pool_info).calculate_idle_share_reserves_in_base()


def calculate_add_liquidity(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
contribution: str,
min_lp_share_price: str,
min_apr: str,
max_apr: str,
as_base: str,
) -> str:
"""Calculates the lp_shares for a given contribution when adding liquidity.
Arguments
---------
pool_config: PoolConfig
Static configuration for the hyperdrive contract.
Set at deploy time.
pool_info: PoolInfo
Current state information of the hyperdrive contract.
Includes attributes like reserve levels and share prices.
contribution: str (FixedPoint)
The amount of liquidity, in base or shares, to add to the pool.
min_lp_share_price: str (FixedPoint)
The minimum allowable LP share price.
The call will return an error if this condition is not met.
min_apr: str (FixedPoint)
The minimum apr after contribution is added.
The call will return an error if this condition is not met.
max_apr: str (FixedPoint)
The maximum apr after contribution is added.
The call will return an error if this condition is not met.
as_base: str (bool)
The unit of currency for the contribution.
If true, then the contribution is in base. Otherwise, it is shares.
Returns
-------
str (FixedPoint)
The amount of LP shares provided by the pool for the given contribution.
"""
return _get_interface(pool_config, pool_info).calculate_add_liquidity(
contribution, min_lp_share_price, min_apr, max_apr, as_base.lower()
)


def calculate_pool_deltas_after_add_liquidity(
pool_config: types.PoolConfigType, pool_info: types.PoolInfoType, contribution: str, as_base: str
) -> str:
"""Calculate the deltas to be applied to the pool after adding liquidity.
Arguments
---------
pool_config: PoolConfig
Static configuration for the hyperdrive contract.
Set at deploy time.
pool_info: PoolInfo
Current state information of the hyperdrive contract.
Includes attributes like reserve levels and share prices.
contribution: str (FixedPoint)
The amount of liquidity, in base or shares, to add to the pool.
as_base: str (bool)
The unit of currency for the contribution.
If true, then the contribution is in base. Otherwise, it is shares.
Returns
-------
Tuple(str, str, str) (FixedPoint, FixedPoint, FixedPoint)
The deltas for share reserves, share adjustment, and bond reserves, respectively.
"""
return _get_interface(pool_config, pool_info).calculate_pool_deltas_after_add_liquidity(
contribution, as_base.lower()
)
2 changes: 1 addition & 1 deletion bindings/hyperdrivepy/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="hyperdrivepy",
version="0.15.4",
version="0.15.5",
packages=["hyperdrivepy"],
package_dir={"": "python"},
rust_extensions=[
Expand Down
Loading

0 comments on commit 4c8da98

Please sign in to comment.