-
Notifications
You must be signed in to change notification settings - Fork 5
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
_distributeExcessIdle
overhaul
#688
Merged
jalextowle
merged 76 commits into
main
from
jalextowle/feature/distribute-excess-idle-overhaul-2
Dec 19, 2023
Merged
_distributeExcessIdle
overhaul
#688
jalextowle
merged 76 commits into
main
from
jalextowle/feature/distribute-excess-idle-overhaul-2
Dec 19, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…stribute-excess-idle-overhaul-2
…stribute-excess-idle-overhaul-2
Hyperdrive Gas Benchmark
This comment was automatically generated by workflow using github-action-benchmark. |
…cess-idle-overhaul-2' into jalextowle/feature/distribute-excess-idle-overhaul-2
jrhea
reviewed
Dec 18, 2023
…ding more consistent
…e` to make the rounding more consistent
…ke the rounding more consistent
…ding more consistent
…stribute-excess-idle-overhaul-2
ab14953
to
8319231
Compare
does this PR address #588 ? |
This was referenced Dec 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
_distributeExcessIdle
AlgorithmThe$\Delta z$ and withdrawal shares redeemed $\Delta w$ such that the LP share price is held constant.
_distributeExcessIdle
function is responsible for paying out as many withdrawal shares as possible with the pool's idle liquidity. This boils down to finding the share proceedsNotation
Assume that we're using the usual math notation unless specified otherwise. Functions of the form$f(x)$ are assumed to represent a quantity parameterized by the amount of shares $x$ that we are removing from the share reserves. We define some additional values to make our lives easier when we run through the calculations:
Problem Statement
We need to pay out as many withdrawal shares as possible while conserving the LP share price. This can be stated formally as:
We can solve this problem by breaking it down into two cases:
Case 1:$y_s \cdot t_s \leq y_l \cdot t_l$
In this case, the pool either completely nets out or it is net long. In this situation, we can drop the last constraint since for all$\Delta z \geq 0$ we have $y_{out}^{max}(\Delta z) \geq 0$ . We can break this down into two sub-cases:
Case 1.1:$\Delta z = I$
All of the idle can be paid out. We can solve$\tfrac{PV(0)}{l} = \tfrac{PV(I)}{l - \Delta w}$ for $\Delta w$ to get: $\Delta w = \left(1 - \tfrac{PV(I)}{PV(0)} \right) \cdot l$ .
Case 1.2:$\Delta w = w$
All of the withdrawal shares can be paid out. We solve$\tfrac{PV(0)}{l} = \tfrac{PV(\Delta z)}{l - w}$ for $\Delta z$ . Unfortunately, YieldSpace makes it hard or impossible to solve directly in this situation. Fortunately, the present value is differentiable, so we can use an iterative method like Newton's method to find a suitably close approximation to $\Delta z$ .
Our objective function$F(\Delta z)$ is given by:
The derivative$F'(\Delta z)$ is given by:
Assuming we have an initial guess$\Delta z_0$ of $\Delta z$ , we can calculate the $n$th, $n \geq 1$ , step $\Delta z$ of Newton's Method as:
Case 2:$y_s \cdot t_s > y_l \cdot t_l$
Since the pool is net short, the last constraint is actually relevant. We can start by checking if$y_{out}^{max}(I) \geq y_s \cdot t_s - y_l \cdot t_l$ . If this condition holds, then we aren't constrained by the maximum amount of bonds out, and we can proceed by working through cases 1.1 and 1.2. Otherwise, we need to find the upper bound $\Delta z_{max}$ such that $y_{out}^{max}(\Delta z_{max}) = y_s \cdot t_s - y_l \cdot t_l$ . Even though we can't solve for this analytically, we can solve this using Newton's method. Once we have the value $\Delta z_{max}$ , we can substitute this value in for $I$ in Case 1.1 and 1.2 and proceed normally from there.
Algorithm
Appendix
Identities
We define the follow identities for convenience:
Yield Space Math
Buying Bonds
We can calculate the amount of shares in$z_{in}(x, \Delta y)$ given the amount of bonds out $\Delta y$ as:
We can calculate the derivative$z_{in}'(x, \Delta y)$ as:
We can calculate the max buy$y_{out}^{max}(x)$ on Yield Space as:
We can calculate the derivative$y_{out}^{max'}(x)$ as:
We can calculate the share payment required to buy the maximum amount of bonds$z_{in}^{max}(x)$ as $z_{in}^{max}(x) = z_{in}(x, y_{out}^{max}(x))$ ; however, we can calculate this more directly since we know that $p = \left(\tfrac{\mu \cdot z_{in}^{max}(x)}{y_{out}^{max}(x)}\right)^{t_s} = 1 \implies \mu \cdot z_{in}^{max}(x) = y_{out}^{max}(x)$ :
We can calculate the derivative$z_{in}^{max'}(x)$ as:
Selling Bonds
We can calculate the amount of shares out$z_{out}(x, \Delta y)$ given the amount of bonds in $\Delta y$ as:
We can calculate the derivative$z_{out}'(x)$ as:
FIXME: This is wrong when$\zeta < 0$ . We've updated this in the code, but it would be good to call this out here. We also don't need the derivative, and we should mention that we don't need it when we forego adding it here.
We can calculate the maximum amount of bonds that can be sold as$y_{in}^{max}(x)$ on Yield Space as:
We can calculate the derivative$y_{in}^{max'}(x)$ as:
We can calculate the share proceeds of selling the maximum amount of bonds$z_{out}^{max}(x)$ as $z_{out}^{max}(x) = z_{out}(x, y_{in}^{max}(x))$ ; however, we know that the minimum effective share reserves is $z_{min}$ , so we can quickly calculate this as:
We can calculate the derivative$z_{out}^{max'}(x)$ as:
Present Value Math
Present Value Function
Using the notation we've defined, we can rigorously define the LP present value as a function of the amount of shares$x$ that we remove from the share reserves to pay out the withdrawal pool. At a high-level we can define $PV(x)$ as:
We can define$net_c(x)$ as a piecewise function as follows:
We calculate$net_c^{l}(x)$ by attempting to close as much of the net long position as possible. We mark any remaining longs in the net position to zero because the pool's spot price is 0 after making the max sell:
We calculate$net_c^{s}(x)$ by attempting to close as much of the net short position as possible. We mark any remaining shorts in the net position to 1 because the pool's spot price is 1 after making the max buy. Let $y_{net} = y_s \cdot t_s - y_l \cdot t_l$ :
Present Value Derivative
We can calculate the derivative of the present value function at a high-level as:
The derivative$net_c'(x)$ is calculated component-wise as:
The derivative$net_c^{l'}(x)$ is calculated component-wise as:
The derivative$net_c^{s'}(x)$ is calculated component-wise as: