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

Hyperdrive upgrade to v0.2.0 #26

Merged
merged 15 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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 .github/workflows/build_linux_wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
uses: actions/checkout@master
with:
repository: delvtech/hyperdrive
ref: "v0.0.16"
ref: "v0.2.0"
path: "./hyperdrive"
ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
uses: actions/checkout@master
with:
repository: delvtech/hyperdrive
ref: "v0.0.16"
slundqui marked this conversation as resolved.
Show resolved Hide resolved
ref: "v0.2.0"
path: "./hyperdrive"
ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: actions/checkout@master
with:
repository: delvtech/hyperdrive
ref: "v0.0.16"
ref: "v0.2.0"
path: "./hyperdrive"
ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@master
with:
repository: delvtech/hyperdrive
ref: "v0.0.16"
ref: "v0.2.0"
path: "./hyperdrive"
ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@master
with:
repository: delvtech/hyperdrive
ref: "v0.0.16"
ref: "v0.2.0"
path: "./hyperdrive"
ssh-key: ${{ secrets.HYPERDRIVE_ACCESS_KEY }}

Expand Down
12 changes: 11 additions & 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pypechain. From the pyperdrive project root, run:

```shell
pip install --upgrade -r requirements-dev.txt
pypechain hyperdrive/out/IHyperdrive.sol/IHyperdrive.json --output_dir crates/pyperdrive/python/pypechain_types
pypechain hyperdrive/out/IHyperdrive.sol/IHyperdrive.json --output_dir crates/pyperdrive/python/pypechain/pypechain_types
```

## Disclaimer
Expand Down
2 changes: 1 addition & 1 deletion crates/pyperdrive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pyperdrive"
edition = "2021"
version = "0.1.0"
version = "0.2.0"
authors = [
"Dylan Paiton",
"Matt Brown",
Expand Down
2 changes: 1 addition & 1 deletion crates/pyperdrive/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pyperdrive"
version = "0.1.0"
version = "0.2.0"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
Expand Down
200 changes: 169 additions & 31 deletions crates/pyperdrive/python/pyperdrive/hyperdrive_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,56 @@
# pylint: disable=too-many-arguments


def get_max_spot_price(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
) -> str:
"""Get the pool's max spot price.

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 things like reserve levels and share prices.

Returns
-------
str (FixedPoint)
max_spot_price = 1/1 + curve_fee * (1 / (spot_price - 1))
"""
return _get_interface(pool_config, pool_info).get_max_spot_price()


def get_spot_price_after_long(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
long_amount: str,
) -> str:
"""Get the spot price after opening the long on YieldSpace
and before calculating the fees.

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 things like reserve levels and share prices.
long_amount : str (FixedPoint)
The long amount.

Returns
-------
str (FixedPoint)
The spot price after opening the long.
"""
return _get_interface(pool_config, pool_info).get_spot_price_after_long(long_amount)


def get_solvency(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
Expand Down Expand Up @@ -137,7 +187,9 @@ def get_short_deposit(
# the underlying rust code uses current market share price if this is 0
# zero value is used because the smart contract will return 0 if the checkpoint hasn't been minted
open_share_price = "0"
return _get_interface(pool_config, pool_info).get_short_deposit(short_amount, spot_price, open_share_price)
slundqui marked this conversation as resolved.
Show resolved Hide resolved
return _get_interface(pool_config, pool_info).get_short_deposit(
short_amount, spot_price, open_share_price
)


def to_checkpoint(
Expand Down Expand Up @@ -195,7 +247,9 @@ 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).get_max_long(
budget, checkpoint_exposure, maybe_max_iterations
)


def get_max_short(
Expand Down Expand Up @@ -242,13 +296,72 @@ def get_max_short(
)


def get_out_for_in(
def calculate_bonds_out_given_shares_in_down(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
amount_in: str,
) -> str:
"""Calculates the amount of bonds a user will receive from the pool by
providing a specified amount of shares. We underestimate the amount of
bonds.

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 things like reserve levels and share prices.
amount_in : str (FixedPoint)
The amount of shares going into the pool.

Returns
-------
str (FixedPoint)
The amount of bonds out.
"""
return _get_interface(
pool_config, pool_info
).calculate_bonds_out_given_shares_in_down(amount_in)


def calculate_shares_in_given_bonds_out_up(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
amount_in: str,
) -> str:
"""Calculates the amount of shares a user must provide the pool to receive
a specified amount of bonds. We overestimate the amount of shares in.

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 things like reserve levels and share prices.
amount_in : str (FixedPoint)
The amount of bonds to target.

Returns
-------
str (FixedPoint)
The amount of shares in to reach the target.
"""
return _get_interface(
pool_config, pool_info
).calculate_shares_in_given_bonds_out_up(amount_in)


def calculate_shares_in_given_bonds_out_down(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
amount_in: str,
shares_in: bool,
) -> str:
"""Gets the amount of an asset for a given amount in of the other.
"""Calculates the amount of shares a user must provide the pool to receive
a specified amount of bonds. We underestimate the amount of shares in.

Arguments
---------
Expand All @@ -259,26 +372,26 @@ def get_out_for_in(
Current state information of the hyperdrive contract.
Includes things like reserve levels and share prices.
amount_in : str (FixedPoint)
The amount of asset going into the pool.
shares_in : bool
True if the asset in is shares, False if it is bonds.
The amount out will be the opposite type.
The amount of bonds to target.

Returns
-------
str (FixedPoint)
The amount out.
The amount of shares in to reach the target.
"""
return _get_interface(pool_config, pool_info).get_out_for_in(amount_in, shares_in)
return _get_interface(
pool_config, pool_info
).calculate_shares_in_given_bonds_out_down(amount_in)


def get_out_for_in_safe(
def calculate_shares_out_given_bonds_in_down(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
amount_in: str,
shares_in: bool,
) -> str:
"""Gets the amount of an asset for a given amount in of the other.
"""Calculates the amount of shares a user will receive from the pool by
providing a specified amount of bonds. We underestimate the amount of
shares out.

Arguments
---------
Expand All @@ -289,26 +402,52 @@ def get_out_for_in_safe(
Current state information of the hyperdrive contract.
Includes things like reserve levels and share prices.
amount_in : str (FixedPoint)
The amount of asset going into the pool.
shares_in : bool
True if the asset in is shares, False if it is bonds.
The amount out will be the opposite type.
The amount of bonds in.

Returns
-------
str (FixedPoint)
The amount of shares out.
"""
return _get_interface(
pool_config, pool_info
).calculate_shares_out_given_bonds_in_down(amount_in)


def calculate_max_buy(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
) -> str:
"""
Calculates the maximum amount of bonds that can be purchased with the
specified reserves. We round so that the max buy amount is
underestimated.

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 things like reserve levels and share prices.

Returns
-------
str (FixedPoint)
The amount out.
The maximum buy amount.
"""
return _get_interface(pool_config, pool_info).get_out_for_in_safe(amount_in, shares_in)
return _get_interface(pool_config, pool_info).calculate_max_buy()


def get_in_for_out(
def calculate_max_sell(
pool_config: types.PoolConfigType,
pool_info: types.PoolInfoType,
amount_out: str,
shares_out: bool,
minimum_share_reserves: str,
) -> str:
"""Gets the amount of an asset for a given amount out of the other.
"""Calculates the maximum amount of bonds that can be sold with the
specified reserves. We round so that the max sell amount is
underestimated.

Arguments
---------
Expand All @@ -318,15 +457,14 @@ def get_in_for_out(
pool_info : PoolInfo
Current state information of the hyperdrive contract.
Includes things like reserve levels and share prices.
amount_out : str (FixedPoint)
The amount of asset the user expects to receive from the pool.
shares_out : bool
True if the asset out is shares, False if it is bonds.
The amount in will be the opposite type.
minimum_share_reserves: str (FixedPoint)
The minimum share reserves to target

Returns
-------
str (FixedPoint)
The amount in as a string representation of a Solidity uint256 value.
The maximum buy amount.
"""
return _get_interface(pool_config, pool_info).get_in_for_out(amount_out, shares_out)
return _get_interface(pool_config, pool_info).calculate_max_sell(
minimum_share_reserves
)
Loading
Loading