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

Adjusts the time stretch calculation to hold the ratio of reserves constant #716

Merged
merged 2 commits into from
Jan 8, 2024

Conversation

jalextowle
Copy link
Contributor

@jalextowle jalextowle commented Jan 6, 2024

Fixes: #715.

The gist of this PR is that all we need to do to maintain an approximately constant inventory across pool's with similar initial conditions but different position durations is to solve for a time stretch that implies the same ratio of reserves.

To derive this, we use the spot price calculation:

$$ p = \left( \tfrac{\mu \cdot z_e}{y} \right)^{t_s} $$

We start by solving for the benchmark time stretch $t_{s_b}$ using the existing calculateTimeStretch function. Then we use this time stretch to solve for the ratio of reserves $ratio = \tfrac{\mu \cdot z}{y}$ using calculateInitialSharePrice. For simplicity we use a benchmark of $z_e = 1$ and $\mu = 1$, but this won't effect the ending result since the ratio will be the same regardless of the starting initial share price and effective share reserves. Finally, we solve for the target spot price $p_{target}$ from the position duration $d_{position}$ and target APR $r_{target}$:

$$ p_{target} = \frac{1}{1 + r_{target} \cdot \tfrac{d_{position}}{365\text{ days}}} $$

Finally, we use the spot price calculation to solve for the adjusted time stretch:

$$ p_{target} = \left( \frac{\mu \cdot z_e}{y} \right)^{t_s} \implies t_s = \frac{\ln{p_{target}}}{\ln{\tfrac{\mu \cdot z_e}{y}}} $$

NOTE: Any logarithm will do, but we use the natural logarithm since we have an efficient fixed point implementation in Solidity.

@jalextowle jalextowle requested a review from jrhea January 6, 2024 00:28
@coveralls
Copy link
Collaborator

Coverage Status

coverage: 95.057%. remained the same
when pulling ff99411 on jalextowle/fix/time-stretch-adjustment
into 62cec39 on main.

@jalextowle jalextowle merged commit f4aeeee into main Jan 8, 2024
8 checks passed
@jalextowle jalextowle deleted the jalextowle/fix/time-stretch-adjustment branch January 8, 2024 17:05
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.

Small longs fail due with a "negative interest" error when the pool duration is 1 week
3 participants