0xKitsune/uniswap-v3-math

getSqrtRatioAtTick function returns incorrect result

Code0x2 opened this issue · 2 comments

The function in tick_math.rs returns incorrect result here

if (ratio.shr(32) + (ratio % (U256::one().shl(32)))).is_zero() {
    Ok(U256::zero())
} else {
    Ok(U256::one())
}

Function can only return one or zero, which is wrong. UniswapV3 code is ratio >> 32 and adding the value instead
sqrtPriceX96 = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));

Thanks for catching this! The lib still needs to be tested, feel free to make a PR fixing this if you would like, otherwise, Ill patch it in the interim.

Sure, fixed it for myself anyways so might as well