Skip to content

Commit

Permalink
univ3 - fix mint new pool with different decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
shendel committed Oct 4, 2024
1 parent 3bd5a3e commit a12ac02
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
28 changes: 8 additions & 20 deletions src/front/shared/pages/Exchange/QuickSwap/univ3/MintPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ function MintPosition(props) {
price,
Decimal0: (token == TOKEN._0) ? token0.decimals : token1.decimals,
Decimal1: (token == TOKEN._0) ? token1.decimals : token0.decimals,
isLowerPrice: (token == TOKEN._0),
isLowerPrice: (token == TOKEN._0) ? false : true,
})

console.log('>>> calcPriceByTick', token, priceInfo)
const {
price: {
buyOneOfToken0,
Expand Down Expand Up @@ -386,25 +386,13 @@ function MintPosition(props) {
const amount0Wei = toWei(TOKEN._0, amount0).toString()
const amount1Wei = toWei(TOKEN._1, amount1).toString()

const {
tick: tickCurrent,
sqrtPriceX96: _sqrtPriceX96
} = actions.uniswap.getPriceRoundedToTick({
fee: activeFee,
const sqrtPriceX96 = actions.uniswap.calculateSqrtPriceX96({
Decimal0: (viewSide == VIEW_SIDE.A_TO_B) ? token1.decimals : token0.decimals,
Decimal1: (viewSide == VIEW_SIDE.A_TO_B) ? token0.decimals : token1.decimals,
price: startPrice,
Decimal0: (viewSide == VIEW_SIDE.A_TO_B) ? token0.decimals : token1.decimals,
Decimal1: (viewSide == VIEW_SIDE.B_TO_A) ? token1.decimals : token0.decimals,
isLowerPrice: true,
})
const sqrtPriceX96 = _sqrtPriceX96.toString()
/*
const sqrtPriceX96 = actions.uniswap.priceToSqrtPriceX96(
startPrice,
(viewSide == VIEW_SIDE.A_TO_B) ? token1.decimals : token0.decimals,
(viewSide == VIEW_SIDE.A_TO_B) ? token0.decimals : token1.decimals
)
console.log('>>> SQRT PRICE', startPrice, sqrtPriceX96.toString())
*/
const tickCurrent = actions.uniswap.getTickAtSqrtRatio(sqrtPriceX96)

console.log('>>> FEE', activeFee, token0LowerPrice, token0HighPrice)
const { tick: _tickLower } = actions.uniswap.getPriceRoundedToTick({
fee: activeFee,
Expand Down Expand Up @@ -436,7 +424,7 @@ function MintPosition(props) {
amount1Wei,
fee: activeFee,
poolExists: (poolsByFee[activeFee]) ? true : false,
sqrtPriceX96,
sqrtPriceX96, //: __sqrtPriceX96,
tickLower,
tickUpper,
slippagePercent: slippage,
Expand Down
36 changes: 33 additions & 3 deletions src/front/shared/redux/actions/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,13 @@ const getPriceRoundedToTick = (params) => {
isLowerPrice = true,
fee = 3000
} = params

console.log('>>> GET PRICE ROUNDED TO TICK', params)
const roundedSqrt = priceToSqrtPriceX96(price, Decimal0, Decimal1)
const roundedSqrt_ = calculateSqrtPriceX96({
Decimal0,
Decimal1,
price
})
const roundedTick = getTickAtSqrtRatio(roundedSqrt)
const closestTick = (isLowerPrice)
? getClosestHighTick(roundedTick, TICK_SPACING_BY_FEE[fee])
Expand All @@ -513,8 +518,11 @@ const priceToSqrtPriceX96 = (amount, decimals0, decimals1) => {
const amount1Wei = new BigNumber(1).multipliedBy(10**decimals1)

const Q96 = new BigNumber(2).exponentiatedBy(96)
return new BigNumber(Math.sqrt(amount1Wei.dividedBy(amount0Wei).toNumber()) * 2 ** 96);
const ret = new BigNumber(Math.sqrt(amount1Wei.dividedBy(amount0Wei).toNumber()) * 2 ** 96);

const SqrtPriceX96 = new BigNumber(Math.sqrt(amount1Wei.toNumber() / amount0Wei.toNumber()) * 2 ** 96)
console.log('>>> priceToSqrtPriceX96', amount, decimals0, decimals1, SqrtPriceX96.toString(), ret.toString())
return ret
}

const getTickAtSqrtRatio = (sqrtPriceX96) => {
Expand All @@ -526,6 +534,25 @@ const getTickAtSqrtRatio = (sqrtPriceX96) => {
return tick
}

const calculateSqrtPriceX96 = (params) => {
const {
Decimal0,
Decimal1,
price, // 1 token0 = price token1
} = params

const priceRatio = price * (10 ** (Decimal1 - Decimal0))
const sqrtPriceX96 = new BigNumber(
new BigNumber(priceRatio)
.sqrt()
.multipliedBy(new BigNumber(2).pow(96))
.integerValue(3)
.toString()
)

const roundedTick = getTickAtSqrtRatio(sqrtPriceX96.toString())
return getSqrtRatioAtTick(roundedTick).toString()
}

const mintPositionV3 = async (params) => {
const {
Expand Down Expand Up @@ -1231,7 +1258,8 @@ const removeLiquidityV3 = async (params) => {
token1Amount,
deadline
]

console.log('>>>> decreaseLiquidity params', decreaseLiquidityParams)
console.log('>>>> liq', delLiquidity, liquidity, token0Amount, token1Amount)
const hasWrappedToken = isWrappedToken({ chainId, tokenAddress: token0.address }) || isWrappedToken({ chainId, tokenAddress: token1.address })

const sweepToken0Params = [
Expand Down Expand Up @@ -1705,4 +1733,6 @@ export default {
getTokensInfoV3,
getPriceRoundedToTick,
priceToSqrtPriceX96,
calculateSqrtPriceX96,
getTickAtSqrtRatio,
}

0 comments on commit a12ac02

Please sign in to comment.