Overlapping Pool #11
Replies: 2 comments
-
This is somewhat tricky. Here's real values from the swap (I extracted these by using
Notice how ticks and liquidity change here. You can plug these values into the Python functions to compute amounts in each step and find the final price. To demonstrate the idea, for the first step: import unimath
l = 1518129116516325614066
sqrt_cur = 5602223755577321903022134995689
sqrt_target = 5602783977952879635212437209189
amount0 = unimath.calc_amount0(l, sqrt_cur, sqrt_target)
amount1 = unimath.calc_amount1(l, sqrt_cur, sqrt_target)
sqrt_next = sqrt_cur + (amount1 * 2**96) // l
|
Beta Was this translation helpful? Give feedback.
-
Thank you! Perfect
…On Sat, Oct 8, 2022 at 5:55 AM Ivan Kuznetsov ***@***.***> wrote:
This is somewhat tricky.
The main swap loop iterates in small steps, each step is up to 256 ticks
(this happens here
<https://github.com/Jeiwan/uniswapv3-code/blob/main/src/UniswapV3Pool.sol#L423>).
A step is shorter than 256 ticks when there's an activated tick between the
current tick and current_tick + 256. Also, a step is shorter when
starting not from beginning of a 256-bit word. In each step, it calculates
input and output amounts between the current tick and the next tick based
on current liquidity (liquidity is added when a liquidity position is
entered, and removed when a liquidity position is left). The amounts are
calculated here
<https://github.com/Jeiwan/uniswapv3-code/blob/main/src/UniswapV3Pool.sol#L436>
.
[image: Partially overlapping liquidity positions]
<https://camo.githubusercontent.com/01d861ab5f00e06b401aba92697503dca5a0a231074cf29910ed4d7ed893b592/68747470733a2f2f756e69737761707633626f6f6b2e636f6d2f696d616765732f6d696c6573746f6e655f332f737761705f7061727469616c6c795f6f7665726c617070696e675f70726963655f72616e6765732e706e67>
(We're talking about the upper image)
There are 2 price ranges in that example: 4545-5500 and 5001-6250. Since
the current price is ~5000, the swap starts at this price. It moves to the
right until it converts all input amount (10,000 USDC) to output amount. In
the range between 5000 and 5001, only liquidity of the first position is
used. In the range between 5001 and 5500, liquidity of both positions is
used. In the range between 5500 and 6250, only liquidity of the second
position is used.
To compute the intermediate steps, you need to iteratively compute input
and output amounts (with the Python calc_amount0 and calc_amount1
functions, for example) using liquidity and price range of each step. After
the entire input amount is converted, the current price will be ~6055.578.
Here's real values from the swap (I extracted these by using console.log
and running the test):
====================================================================================
Current price: 5602223755577321903022134995689
Current tick: 85176
Current liquidity: 1518129116516325614066
Next price: 5602783977952879635212437209189
Amount swapped (input): 10734691719059038792
Amount remaining: 9989265308280940961208
====================================================================================
Current price: 5602783977952879635212437209189
Current tick: 85178
Current liquidity: 2188694397454035087752
Next price: 5622145994867546797787171334839
Amount swapped (input): 534879727103510395017
Amount remaining: 9454385581177430566191
====================================================================================
Current price: 5622145994867546797787171334839
Current tick: 85247
Current liquidity: 2188694397454035087752
Next price: 5694568356906635577050546247497
Amount swapped (input): 2000682749859344339854
Amount remaining: 7453702831318086226337
====================================================================================
Current price: 5694568356906635577050546247497
Current tick: 85503
Current liquidity: 2188694397454035087752
Next price: 5767923636470119667816132517957
Amount swapped (input): 2026454789676159292638
Amount remaining: 5427248041641926933699
====================================================================================
Current price: 5767923636470119667816132517957
Current tick: 85759
Current liquidity: 2188694397454035087752
Next price: 5842223851049321075399999157648
Amount swapped (input): 2052558815179543588298
Amount remaining: 3374689226462383345401
====================================================================================
Current price: 5842223851049321075399999157648
Current tick: 86015
Current liquidity: 2188694397454035087752
Next price: 5875617940067453351001625213169
Amount swapped (input): 922518624976419389367
Amount remaining: 2452170601485963956034
====================================================================================
Current price: 5875617940067453351001625213169
Current tick: 86129
Current liquidity: 670565280937709473686
Next price: 5917481172940347756675672283293
Amount swapped (input): 354318838422121276488
Amount remaining: 2097851763063842679546
====================================================================================
Current price: 5917481172940347756675672283293
Current tick: 86271
Current liquidity: 670565280937709473686
Next price: 5993707931238230425450970325679
Amount swapped (input): 645162224780711742771
Amount remaining: 1452689538283130936775
====================================================================================
Current price: 5993707931238230425450970325679
Current tick: 86527
Current liquidity: 670565280937709473686
Next price: 6070916613856747075276281624828
Amount swapped (input): 653472960976404125154
Amount remaining: 799216577306726811621
====================================================================================
Current price: 6070916613856747075276281624828
Current tick: 86783
Current liquidity: 670565280937709473686
Next price: 6149119869574266119161721685201
Amount swapped (input): 661890752938013161392
Amount remaining: 137325824368713650229
====================================================================================
Current price: 6149119869574266119161721685201
Current tick: 87039
Current liquidity: 670565280937709473686
Next price: 6165345094827913637987008642386
Amount swapped (input): 137325824368713650229
Amount remaining: 0
Final price: 6165345094827913637987008642386
Final tick: 87091
Final liquidity: 670565280937709473686
Notice how ticks and liquidity change here. You can plug these values into
the Python functions to compute amounts in each step and find the final
price.
—
Reply to this email directly, view it on GitHub
<#11 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD6A5OIF54M2CEF2N3OBAQDWCFAJPANCNFSM6AAAAAAQ6TUXRE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Would it be possible to see the arithmetic for the interim steps of the overlapping pool example that leads to 6055.578 price answer?
Beta Was this translation helpful? Give feedback.
All reactions