Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max Spot Price: closeShort #645

Merged
merged 5 commits into from
Nov 1, 2023

Conversation

jalextowle
Copy link
Contributor

@jalextowle jalextowle commented Oct 31, 2023

The recent #642 only addressed the negative interest problem for openLong. closeShort has an identical problem because the trade increases the spot price and the curve fee results in the trader's effective spot price being larger than the pool's spot price.

We can derive the maximum spot price for closing a short in the same way that we derived it for openLong in #582. When a trader attempts to close a short of $y$ bonds on the curve, the curve will calculate $x(y)$ the amount of base needed to pay for $y$ bonds. $x(y)$ is given by:

$$ x(y) = x_{YS}(x) + \phi_c \cdot (1 - p_0) \cdot y $$

If we assume that the trader's realized price is $p_r$, then we can simplify $x_{YS}(x)$ to:

$$ x_{YS}(x) = p_r \cdot y $$

To ensure that the trader doesn't purchase bonds at a negative interest rate, we enforce the invariant that:

$$ x(y) \leq y $$

We can solve our inequality as:

$$ p_r \cdot y + \phi_c \cdot (1 - p_0) \cdot y \leq y \implies p_r \leq 1 - \phi_c \cdot (1 - p_0) $$

This is the max price that we'll need to use to ensure that shorts don't accidentally purchase some of their bonds at a negative interest rate.

Copy link

github-actions bot commented Oct 31, 2023

Hyperdrive Gas Benchmark

Benchmark suite Current: e9be79d Previous: 1148b95 Deviation Status
addLiquidity: min 733 gas 733 gas 0% 🟰
addLiquidity: avg 54041 gas 54072 gas -0.0573%
addLiquidity: max 96720 gas 96704 gas 0.0165% 🚨
checkpoint: min 558 gas 558 gas 0% 🟰
checkpoint: avg 47640 gas 47642 gas -0.0042%
checkpoint: max 99410 gas 99410 gas 0% 🟰
closeLong: min 755 gas 755 gas 0% 🟰
closeLong: avg 23436 gas 23397 gas 0.1667% 🚨
closeLong: max 114183 gas 114183 gas 0% 🟰
closeShort: min 802 gas 802 gas 0% 🟰
closeShort: avg 26710 gas 25012 gas 6.7887% 🚨
closeShort: max 112872 gas 111387 gas 1.3332% 🚨
initialize: min 662 gas 662 gas 0% 🟰
initialize: avg 177327 gas 177327 gas 0% 🟰
initialize: max 251633 gas 251633 gas 0% 🟰
openLong: min 757 gas 757 gas 0% 🟰
openLong: avg 57017 gas 56892 gas 0.2197% 🚨
openLong: max 218431 gas 218401 gas 0.0137% 🚨
openShort: min 712 gas 712 gas 0% 🟰
openShort: avg 56133 gas 56235 gas -0.1814%
openShort: max 217803 gas 217803 gas 0% 🟰
removeLiquidity: min 777 gas 777 gas 0% 🟰
removeLiquidity: avg 75958 gas 75957 gas 0.0013% 🚨
removeLiquidity: max 203187 gas 203187 gas 0% 🟰

This comment was automatically generated by workflow using github-action-benchmark.

@coveralls
Copy link
Collaborator

coveralls commented Oct 31, 2023

Coverage Status

coverage: 97.551%. remained the same when pulling e9be79d on jalextowle/feature/close-short-max-spot-price into 3c9bcea on main.

@jalextowle jalextowle requested a review from jrhea October 31, 2023 21:59
@jalextowle jalextowle enabled auto-merge (squash) October 31, 2023 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants