uniswap-v3-math's People
Forkers
0xosiris hexiaoyuan mevbandit upendra-jaiswal focusalready danipopes baoanh1310 ohmyfinger amanelis zhiwei-w-luo konqueror1 tomaszwaszczyk big-aaron jordy25519 0xcutesocks samtvlabs impactdni2 lakshya-sky shabbirhasan1 jdsaund roman-raizen utilitycoder gaon3 qiluge 22388o yzevmuniswap-v3-math's Issues
Feat: Bench `ruint` vs `rug` `Integer` vs `U256` on basic operations
Bench ruint
vsrug
Integer
vs U256
on basic arithmetic operations such as *, /, %, ..etc. Get an idea of which is optimal across the code base as far as speed, and switch all Integer types across the code base.
Remove External Call from next_initialized_tick_within_one_word()
Remove external call to fetch the word from tickBitmap[word_pos]
on chain. Additionally add bit_pos
and word
as parameters to the function to be passed from cfmms-rs
.
pub async fn next_initialized_tick_within_one_word(
tick: i32,
tick_spacing: i32,
lte: bool,
bit_pos: u8,
word: U256,
) -> Result<(i32, bool), UniswapV3MathError> {}
This will allow v3 swap simulation to batch all external calls into a single call for efficiency.
It seems that pool.ts is not implemented. As well as getAmountOut and getAmountIn, etc. Do you have any plans to implement it in the future?
getSqrtRatioAtTick function returns incorrect result
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));
Sqrt Price Math get_amount_0_delta & get_amount_1_delta return type wrong
In sqrt_price_math.rs
the following needs to be changed.
pub fn get_amount_0_delta(
sqrt_ratio_a_x_96: U256,
sqrt_ratio_b_x_96: U256,
liquidity: i128,
) -> I256 {
if liquidity < 0 {
return I256::from_raw(_get_amount_0_delta(
sqrt_ratio_b_x_96,
sqrt_ratio_a_x_96,
-liquidity as i128,
false,
));
} else {
return I256::from_raw(_get_amount_0_delta(
sqrt_ratio_a_x_96,
sqrt_ratio_b_x_96,
liquidity,
true,
));
}
}
Should be
pub fn get_amount_0_delta(
sqrt_ratio_a_x_96: U256,
sqrt_ratio_b_x_96: U256,
liquidity: i128,
) -> I256 {
if liquidity < 0 {
//Return the negation
return -I256::from_raw(_get_amount_0_delta(
sqrt_ratio_b_x_96,
sqrt_ratio_a_x_96,
-liquidity as i128,
false,
));
} else {
return I256::from_raw(_get_amount_0_delta(
sqrt_ratio_a_x_96,
sqrt_ratio_b_x_96,
liquidity,
true,
));
}
}
Further,
pub fn get_amount_1_delta(
sqrt_ratio_a_x_96: U256,
sqrt_ratio_b_x_96: U256,
liquidity: i128,
) -> I256 {
if liquidity < 0 {
return I256::from_raw(_get_amount_1_delta(
sqrt_ratio_b_x_96,
sqrt_ratio_a_x_96,
-liquidity as i128,
false,
));
} else {
return I256::from_raw(_get_amount_1_delta(
sqrt_ratio_a_x_96,
sqrt_ratio_b_x_96,
liquidity,
true,
));
}
}
Should be
pub fn get_amount_1_delta(
sqrt_ratio_a_x_96: U256,
sqrt_ratio_b_x_96: U256,
liquidity: i128,
) -> I256 {
if liquidity < 0 {
//Return the negation
return -I256::from_raw(_get_amount_1_delta(
sqrt_ratio_b_x_96,
sqrt_ratio_a_x_96,
-liquidity as i128,
false,
));
} else {
return I256::from_raw(_get_amount_1_delta(
sqrt_ratio_a_x_96,
sqrt_ratio_b_x_96,
liquidity,
true,
));
}
}
Reference:
/// @notice Helper that gets signed token0 delta
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The change in liquidity for which to compute the amount0 delta
/// @return amount0 Amount of token0 corresponding to the passed liquidityDelta between the two prices
function getAmount0Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
int128 liquidity
) internal pure returns (int256 amount0) {
unchecked {
return
liquidity < 0
? -getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
: getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
}
}
/// @notice Helper that gets signed token1 delta
/// @param sqrtRatioAX96 A sqrt price
/// @param sqrtRatioBX96 Another sqrt price
/// @param liquidity The change in liquidity for which to compute the amount1 delta
/// @return amount1 Amount of token1 corresponding to the passed liquidityDelta between the two prices
function getAmount1Delta(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
int128 liquidity
) internal pure returns (int256 amount1) {
unchecked {
return
liquidity < 0
? -getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(-liquidity), false).toInt256()
: getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, uint128(liquidity), true).toInt256();
}
}
Remove magic numbers
A contant value should be used anywhere there is a U256 value that never changes.
For example:
let mut ratio = if abs_tick.bitand(U256::from(0x1)) != U256::zero() {
U256::from("0xfffcb933bd6fad37aa2d162d1a594001")
} else {
U256::from("0x100000000000000000000000000000000")
};
if !abs_tick.bitand(U256::from(0x2)).is_zero() {
ratio = (ratio * U256::from("0xfff97272373d413259a46990580e213a")).shr(128)
}
if !abs_tick.bitand(U256::from(0x4)).is_zero() {
ratio = (ratio * U256::from("0xfff2e50f5f656932ef12357cf3c7fdcc")).shr(128)
}
if !abs_tick.bitand(U256::from(0x8)).is_zero() {
ratio = (ratio * U256::from("0xffe5caca7e10e4e61c3624eaa0941cd0")).shr(128)
}
All of these values should be changed to match this format:
pub const MIN_SQRT_RATIO: U256 = U256([285968860985, 0, 0, 0]);
pub const MAX_SQRT_RATIO: U256 = U256([
9809463991923573570,
227557619515130776,
5049738529920590081,
1,
]);
Calculate readable price from sqrt_ratio_x96 or tick.
Is there any consideration for supporting human readable price format?
Implementation of JS
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.