-
Notifications
You must be signed in to change notification settings - Fork 1
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
Switch the liquidity shape to a triangle #2
Conversation
.expect(OVERFLOW_ERROR), | ||
remaining_y, | ||
); | ||
remaining_y = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have some visualization for curve y = sqrt(tick_to_price(x + 1)) - sqrt(tick_to_price(x))
? Trying to relate this to the triangular shape.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have graphed it out here: https://www.desmos.com/calculator/krrqdfifeg and here https://docs.google.com/spreadsheets/d/1LiD_XfjvgXuGStU2KPWhMJglLYYnuy6zJq2fgvg84Mg/edit?usp=sharing
Let me provide more context on the triangle shape and why it was chosen: Caviar based their calculations around the fact that at every price point along an xy = k
curve the k
is equal and doesn't change. So, they started with this premise and started working backwards to determine how much should be added to each bin such that the k
in each bin is equal. They also used the fact that all bins at a lower price than the current price contain only Y and all bins above the current price contain only X.
They chose an arbitrary K
to be base their calculation on, let's say 500 and used the equations below to determine how much should each bin have:
In the end, after calculating the amount they got that they should be a triangle.
I follow a very similar approach in the code here where the code doesn't actually say "get me a triangle formation", rather the code is contributing the amount of resources that we need to get an equal K in all of the bins and doesn't care what shape that gives us (but it results in a triangle as seen in the second link I shared above)
.and_then(|value| value.checked_sqrt()) | ||
.unwrap(); | ||
|
||
assert!(standard_deviation <= dec!(0.0001)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonder if it's possible to get some test vectors from Caviarnine and assert_eq
the bin contributions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They've already confirmed that that graphs here are what they expect.
|
||
nominator.checked_div(denominator) | ||
}) | ||
.expect(OVERFLOW_ERROR), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any documentation for the division above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, this is derived from the equation that I named
We can modify the equation above and use it to find
You can also find these equations here as equations 4 and 5.
Merging this and will try to get a Caviarnine test case in later on. |
I would recommend not reviewing the diff of
packages/caviarnine-v1-adapter-v1/src/lib.rs
but just review the latest implementation ofopen_liquidity_position
. For this case, the change is quite large that the diff might not be a good thing to look at.For tests it would be good to look at the diff. I've added tests to ensure that the liquidity (L = sqrt(K)) in the bins is equal by calculating the standard deviation since they're not strictly equal.