Skip to content

Commit b32a291

Browse files
committed
refactor: move most of the logic into hooks
1 parent 79f467c commit b32a291

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

packages/frontend/src/components/Lp/MintAndLp/LpSettings.tsx

+7-16
Original file line numberDiff line numberDiff line change
@@ -154,29 +154,20 @@ const LpSettings: React.FC<{ onComplete: () => void; squeethToMint: string }> =
154154
return
155155
}
156156

157-
const minPriceBN = new bn(minPrice)
158-
const maxPriceBN = new bn(maxPrice)
159-
if (minPriceBN.isLessThanOrEqualTo(0) || maxPriceBN.isLessThanOrEqualTo(0)) {
160-
return
161-
}
162-
163-
// still not sure about this
164-
// but basically the thought is that lowerPrice is derived from maxPrice since that's in denominator
165-
const lowerPrice = ethPrice.div(maxPrice).integerValue(bn.ROUND_FLOOR).toNumber()
166-
const upperPrice = ethPrice.div(minPrice).integerValue(bn.ROUND_FLOOR).toNumber()
167-
168-
const ticks = getTicksFromPriceRange(lowerPrice, upperPrice)
169-
157+
const ticks = getTicksFromPriceRange(minPrice, maxPrice)
170158
setLowerTick(ticks.lowerTick)
171159
setUpperTick(ticks.upperTick)
172160
}, [usingDefaultPriceRange, minPrice, maxPrice, ethPrice, getTicksFromPriceRange])
173161

174162
useAppEffect(() => {
175-
getDepositAmounts(new bn(squeethToMint), lowerTick, upperTick, 0, collatRatioVal, 0).then((deposits) => {
163+
async function calcDepositAmounts() {
164+
const deposits = await getDepositAmounts(new bn(squeethToMint), lowerTick, upperTick, 0, collatRatioVal, 0)
176165
if (deposits) {
177166
setDepositAmounts(deposits)
178167
}
179-
})
168+
}
169+
170+
calcDepositAmounts()
180171
}, [squeethToMint, lowerTick, upperTick, collatRatioVal, getDepositAmounts])
181172

182173
const openPosition = useAppCallback(async () => {
@@ -189,7 +180,7 @@ const LpSettings: React.FC<{ onComplete: () => void; squeethToMint: string }> =
189180
console.log('transaction failed')
190181
console.log(e)
191182
}
192-
}, [squeethToMint, lowerTick, upperTick, collatRatio, slippageAmount, openLpPosition])
183+
}, [squeethToMint, lowerTick, upperTick, collatRatioVal, slippageAmountVal, openLpPosition, onComplete])
193184

194185
return (
195186
<>

packages/frontend/src/state/lp/hooks.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
squeethPoolContractAtom,
1313
} from '../contracts/atoms'
1414
import useAppCallback from '@hooks/useAppCallback'
15+
import { useETHPrice } from '@hooks/useETHPrice'
1516
import { addressAtom } from '../wallet/atoms'
1617
import { Contract } from 'web3-eth-contract'
1718
import { useHandleTransaction } from '../wallet/hooks'
@@ -306,11 +307,35 @@ export const useGetTickPrices = () => {
306307
}
307308

308309
export const useGetTicksFromPriceRange = () => {
309-
const getTicksFromPriceRange = useCallback((minPrice: number, maxPrice: number) => {
310-
const lowerTick = TickMath.getTickAtSqrtRatio(encodeSqrtRatioX96(minPrice, 1))
311-
const upperTick = TickMath.getTickAtSqrtRatio(encodeSqrtRatioX96(maxPrice, 1))
312-
return { lowerTick, upperTick }
313-
}, [])
310+
const isWethToken0 = useAtomValue(isWethToken0Atom)
311+
const ethPrice = useETHPrice()
312+
313+
const getTicksFromPriceRange = useAppCallback(
314+
(minOSqthPriceInput: string, maxOSqthPriceInput: string) => {
315+
const minOSqthPrice = new BigNumber(minOSqthPriceInput)
316+
const maxOSqthPrice = new BigNumber(maxOSqthPriceInput)
317+
318+
// $60 - oSQTH
319+
// $1500 - ETH
320+
// for 1 ETH how many oSQTH - 1500 / 60
321+
// "encodeSqrtRatioX96" asks for amount1, amount0
322+
const lowerPriceRange = isWethToken0 ? ethPrice.div(maxOSqthPrice) : minOSqthPrice.div(ethPrice)
323+
const upperPriceRange = isWethToken0 ? ethPrice.div(minOSqthPrice) : maxOSqthPrice.div(ethPrice)
324+
325+
const lowerPriceRangeInt = lowerPriceRange.integerValue()
326+
const upperPriceRangeInt = upperPriceRange.integerValue()
327+
328+
const lowerTick = lowerPriceRangeInt.isFinite()
329+
? TickMath.getTickAtSqrtRatio(encodeSqrtRatioX96(lowerPriceRangeInt.toNumber(), 1))
330+
: TickMath.MIN_TICK
331+
const upperTick = upperPriceRangeInt.isFinite()
332+
? TickMath.getTickAtSqrtRatio(encodeSqrtRatioX96(upperPriceRangeInt.toNumber(), 1))
333+
: TickMath.MAX_TICK
334+
335+
return { lowerTick, upperTick }
336+
},
337+
[isWethToken0, ethPrice],
338+
)
314339

315340
return getTicksFromPriceRange
316341
}

0 commit comments

Comments
 (0)