Skip to content

Commit

Permalink
univ3 - min - current price + fix calc token0/token1
Browse files Browse the repository at this point in the history
  • Loading branch information
shendel committed Sep 25, 2024
1 parent c6e1cd3 commit e96d750
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
99 changes: 83 additions & 16 deletions src/front/shared/pages/Exchange/QuickSwap/univ3/MintPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function MintPosition(props) {
slippage,
intl,
owner,

} = props

console.log('>>>> MIN POSITION', props)
Expand Down Expand Up @@ -111,6 +112,54 @@ function MintPosition(props) {
}, [token0Address, token1Address])

const [ activeFee, setActiveFee ] = useState(0)

const [ poolInfo, setPoolInfo ] = useState<any>(false)
const [ isPoolFetching, setIsPoolFetching ] = useState(false)

useEffect(() => {
if (poolsByFee[activeFee]) {
// Fetching pool info
setIsPoolFetching(true)
console.log('>>> FETCHING POOL INFO')
actions.uniswap.getUserPoolLiquidityV3({
owner,
baseCurrency,
chainId,
poolAddress: poolsByFee[activeFee],
}).then(({ pool }) => {
console.log('>>> POOL INFO', pool)
setPoolInfo(pool)
setIsPoolFetching(false)
}).catch((err) => {
console.log('>ERR getUserPoolLiquidityV3', err)
})
} else {
setIsPoolFetching(false)
}
}, [ activeFee, poolsByFee ])

useEffect(() => {
console.log('>>> CHANGED POOL INFO OR SIDE')
if (poolInfo) {
const {
currentPrice,
currentPrice: {
buyOneOfToken0,
buyOneOfToken1,
},
} = poolInfo
console.log('>>> POOL PRICE', currentPrice)
setStartPrice(
Number(
(viewSide == VIEW_SIDE.A_TO_B)
? buyOneOfToken1
: buyOneOfToken0
)
)
} else {
setStartPrice(0)
}
}, [ poolInfo, viewSide ])

const isWrappedToken0 = actions.uniswap.isWrappedToken({ chainId, tokenAddress: token0Address })
const isWrappedToken1 = actions.uniswap.isWrappedToken({ chainId, tokenAddress: token1Address })
Expand Down Expand Up @@ -266,47 +315,49 @@ function MintPosition(props) {

/* @to-do - need optimize code size */
const calcAmount = (amount, token) => {
console.log('>> CALC AMOUNT', viewSide, token, amount)
if (viewSide == VIEW_SIDE.A_TO_B) {
if (token == TOKEN._0) {
const _amount1 = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: amount,
const perTokenPrice = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: 1,
price: startPrice,
priceHigh: token0HighPrice,
priceLow: token0LowerPrice,
}).toNumber()
const _amount1 = new BigNumber(amount).dividedBy(perTokenPrice).toNumber()
setAmount0(amount)
setAmount1(_amount1)
}
if (token == TOKEN._1) {
const perTokenPrice = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: 1,
const _amount0 = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: amount,
price: startPrice,
priceHigh: token0HighPrice,
priceLow: token0LowerPrice,
}).toNumber()
const _amount0 = new BigNumber(amount).dividedBy(perTokenPrice).toNumber()
console.log('>>> _amount0', _amount0)
setAmount0(_amount0)
setAmount1(amount)
}
} else {
if (token == TOKEN._1) {
const _amount0 = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: amount,
const perTokenPrice = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: 1,
price: startPrice,
priceHigh: token1HighPrice,
priceLow: token1LowerPrice,
}).toNumber()
setAmount1(amount)
const _amount0 = new BigNumber(amount).dividedBy(perTokenPrice).toNumber()
setAmount0(_amount0)
setAmount1(amount)
}
if (token == TOKEN._0) {
const perTokenPrice = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: 1,
const _amount1 = actions.uniswap.addLiquidityV3CalcAmount({
amountIn: amount,
price: startPrice,
priceHigh: token1HighPrice,
priceLow: token1LowerPrice,
}).toNumber()
const _amount1 = new BigNumber(amount).dividedBy(perTokenPrice).toNumber()
setAmount1(_amount1)
setAmount0(amount)
}
Expand Down Expand Up @@ -464,7 +515,7 @@ function MintPosition(props) {
</div>
</div>
)}
{!poolsByFee[activeFee] && (
{!poolsByFee[activeFee] ? (
<div>
<div>
<FormattedMessage
Expand All @@ -488,6 +539,22 @@ function MintPosition(props) {
)}
/>
</div>
) : (
<PriceInput
price={startPrice}
tokenA={getTokenSymbolFromViewSideA()}
tokenB={getTokenSymbolFromViewSideB()}
disabled={true}
label={(
<FormattedMessage
id="uni_mint_current_price"
defaultMessage="Current {symbol} price"
values={{
symbol: getTokenSymbolFromViewSideB(),
}}
/>
)}
/>
)}
<div>
<h4>
Expand All @@ -498,13 +565,13 @@ function MintPosition(props) {
</h4>
{(viewSide == VIEW_SIDE.A_TO_B) ? (
<>
{!startPriceIsLower && renderDepositToken0()}
{!startPriceIsHigh && renderDepositToken1()}
{!startPriceIsLower && renderDepositToken1()}
{!startPriceIsHigh && renderDepositToken0()}
</>
) : (
<>
{!startPriceIsLower && renderDepositToken1()}
{!startPriceIsHigh && renderDepositToken0()}
{!startPriceIsLower && renderDepositToken0()}
{!startPriceIsHigh && renderDepositToken1()}
</>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type PriceInputProps = {
price: number
label: any,
disabled?: boolean
onChange: (amount:number) => void
onChange?: (amount:number) => void
onBlur?: () => void
tokenA: string,
tokenB: string,
Expand Down
19 changes: 0 additions & 19 deletions src/front/shared/redux/actions/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,19 +476,6 @@ const returnSwapMethod = (params) => {
}
}

const createPoolV3 = async (params) => {
}

const addLiquidityPositionsV3 = async (params) => {
}
/*
const getSqrtRatioAtTick = (tick) => {
//const ret = new BigNumber(1.0001).exponentiatedBy(tick).multipliedBy( new BigNumber(2).exponentiatedBy(96) )
//console.log(ret, ret.toString())
// sqrt(1.0001^tick) * 2^96
}
*/

const getClosestLowTick = (tick, tickSpacing) => {
return Math.floor(tick / tickSpacing) * tickSpacing;
}
Expand Down Expand Up @@ -535,7 +522,6 @@ const getTickAtSqrtRatio = (sqrtPriceX96) => {
const tQ96 = new BigNumber(sqrtPriceX96).dividedBy(Q96).toNumber()
console.log(tQ96)
let tick = Math.floor(Math.log(tQ96**2)/Math.log(1.0001))
console.log('>>> getTickAtSqrtRatio', tick)
return tick
}

Expand Down Expand Up @@ -777,8 +763,6 @@ const getSqrtRatioAtTick = (tick) => {
return result;
}

window.getSqrtRatioAtTick = getSqrtRatioAtTick

const addLiquidityV3CalcAmount = (params) => {
const {
amountIn,
Expand Down Expand Up @@ -861,9 +845,6 @@ const getTokenAmountsV3 = (params) => {
sqrtRatioB,
}
}
window.getTokenAmountsV3 = getTokenAmountsV3

window.getUserPoolLiquidityV3 = getUserPoolLiquidityV3

const isWrappedToken = ({ chainId, tokenAddress }) => {
const wrappedAddress = constants.ADDRESSES.WrapperCurrency[chainId]
Expand Down

0 comments on commit e96d750

Please sign in to comment.