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

Switch the liquidity shape to a triangle #2

Merged
merged 3 commits into from
Feb 29, 2024

Conversation

0xOmarA
Copy link
Member

@0xOmarA 0xOmarA commented Feb 23, 2024

I would recommend not reviewing the diff of packages/caviarnine-v1-adapter-v1/src/lib.rs but just review the latest implementation of open_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.

.expect(OVERFLOW_ERROR),
remaining_y,
);
remaining_y =
Copy link
Member

@iamyulong iamyulong Feb 27, 2024

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.

Copy link
Member Author

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:

$$ L = \sqrt{k} $$

$$ x = L \frac{ \sqrt{p_b} - \sqrt{p_a} }{ \sqrt{p_a} \times \sqrt{p_b} } $$

$$ y = L ( \sqrt{p_b} - \sqrt{p_a} ) $$

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));
Copy link
Member

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?

Copy link
Member Author

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),
Copy link
Member

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?

Copy link
Member Author

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 $L_x$ in the comments which is as follows:

$$ L_x = x\dfrac{\sqrt{p_a}\sqrt{p_b}}{\sqrt{p_b} - \sqrt{p_a}} $$

We can modify the equation above and use it to find $x$ which will be:

$$ x = L \frac{ \sqrt{p_b} - \sqrt{p_a} }{ \sqrt{p_a} \times \sqrt{p_b} } $$

You can also find these equations here as equations 4 and 5.

@0xOmarA
Copy link
Member Author

0xOmarA commented Feb 29, 2024

Merging this and will try to get a Caviarnine test case in later on.

@0xOmarA 0xOmarA merged commit 416d677 into main Feb 29, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants