Skip to content

Commit 332d57b

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

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-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

+35-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,40 @@ 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+
let lowerTick, upperTick
316+
317+
const minOSqthPrice = new BigNumber(minOSqthPriceInput)
318+
const maxOSqthPrice = new BigNumber(maxOSqthPriceInput)
319+
320+
if (minOSqthPrice.isLessThanOrEqualTo(0)) {
321+
lowerTick = TickMath.MIN_TICK
322+
}
323+
if (maxOSqthPrice.isLessThanOrEqualTo(0)) {
324+
upperTick = TickMath.MAX_TICK
325+
}
326+
327+
// $60 - oSQTH
328+
// $1500 - ETH
329+
// for 1 ETH how many oSQTH - 1500 / 60
330+
// "encodeSqrtRatioX96" asks for amount1, amount0
331+
const lowerPriceRange = isWethToken0 ? ethPrice.div(maxOSqthPrice) : minOSqthPrice.div(ethPrice)
332+
const upperPriceRange = isWethToken0 ? ethPrice.div(minOSqthPrice) : maxOSqthPrice.div(ethPrice)
333+
334+
const lowerPriceRangeInt = lowerPriceRange.integerValue()
335+
const upperPriceRangeInt = upperPriceRange.integerValue()
336+
337+
lowerTick = TickMath.getTickAtSqrtRatio(encodeSqrtRatioX96(lowerPriceRangeInt.toNumber(), 1))
338+
upperTick = TickMath.getTickAtSqrtRatio(encodeSqrtRatioX96(upperPriceRangeInt.toNumber(), 1))
339+
340+
return { lowerTick, upperTick }
341+
},
342+
[isWethToken0, ethPrice],
343+
)
314344

315345
return getTicksFromPriceRange
316346
}

0 commit comments

Comments
 (0)