Uniswap / v3-sdk

🛠 An SDK for building applications on top of Uniswap V3
MIT License
544 stars 416 forks source link

fix nextInitializedTickWithinOneWord #164

Closed z-hao-wang closed 1 year ago

z-hao-wang commented 1 year ago

The calculation didn't match solidity code. When tick is negative, we need to round up. for example, -1233.5 will be come -1233

NoahZinsmeister commented 1 year ago

hi @z-hao-wang - thanks for the pr. in solidity, tick is only ever an integer - calling this function with a fractional tick in JS is meaningless - i don't think your fix applies in that case, right?

z-hao-wang commented 1 year ago

Hi @NoahZinsmeister The fix is about negative numbers. the rounding was incorrect when number is negative

in solidity -1000/7 = -142 in js -1000/7 = -142.85714285714286 Math.floor(-142.85714285714286) = -143

In this case it doesn't match solidity

NoahZinsmeister commented 1 year ago

i'm confused - taking -1000/7 as tick/tickSpacing, the solidity code (https://github.com/Uniswap/v3-core/blob/05c10bf6d547d6121622ac51c457f93775e1df09/contracts/libraries/TickBitmap.sol#L49) produces -143, and Math.floor(-1000/7) also produces -143

the code in both cases is meant to round consistently toward negative infinity

z-hao-wang commented 1 year ago

Ohh you are right. Now I understand it produces same results. Sorry for the confusion. will close this PR.