Skip to content

Commit 822a7fa

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

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

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

+23-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Box, Typography, Divider, InputAdornment } from '@material-ui/core'
33
import { ToggleButtonGroup, ToggleButton } from '@material-ui/lab'
44
import { makeStyles, createStyles } from '@material-ui/core/styles'
55
import { useAtomValue } from 'jotai'
6-
import bn from 'bignumber.js'
6+
import BigNumber from 'bignumber.js'
77
import { TickMath } from '@uniswap/v3-sdk'
88

99
import { AltPrimaryButton } from '@components/Button'
@@ -144,8 +144,8 @@ const LpSettings: React.FC<{ onComplete: () => void; squeethToMint: string }> =
144144
const textClasses = useTextStyles()
145145

146146
const squeethPrice = getWSqueethPositionValue(1)
147-
const collatRatioVal = new bn(collatRatio).div(100).toNumber()
148-
const slippageAmountVal = new bn(slippageAmount).div(100).toNumber()
147+
const collatRatioVal = new BigNumber(collatRatio).div(100).toNumber()
148+
const slippageAmountVal = new BigNumber(slippageAmount).div(100).toNumber()
149149

150150
useAppEffect(() => {
151151
if (usingDefaultPriceRange) {
@@ -154,42 +154,42 @@ 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 BigNumber(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 () => {
183174
try {
184-
await openLpPosition(new bn(squeethToMint), lowerTick, upperTick, 0, collatRatioVal, slippageAmountVal, 0, () => {
185-
console.log('successfully deposited')
186-
onComplete()
187-
})
175+
await openLpPosition(
176+
new BigNumber(squeethToMint),
177+
lowerTick,
178+
upperTick,
179+
0,
180+
collatRatioVal,
181+
slippageAmountVal,
182+
0,
183+
() => {
184+
console.log('successfully deposited')
185+
onComplete()
186+
},
187+
)
188188
} catch (e) {
189189
console.log('transaction failed')
190190
console.log(e)
191191
}
192-
}, [squeethToMint, lowerTick, upperTick, collatRatio, slippageAmount, openLpPosition])
192+
}, [squeethToMint, lowerTick, upperTick, collatRatioVal, slippageAmountVal, openLpPosition, onComplete])
193193

194194
return (
195195
<>

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

+33-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,38 @@ 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 =
329+
lowerPriceRangeInt.isFinite() && !lowerPriceRangeInt.isZero()
330+
? TickMath.getTickAtSqrtRatio(encodeSqrtRatioX96(lowerPriceRangeInt.toNumber(), 1))
331+
: TickMath.MIN_TICK
332+
333+
const upperTick =
334+
upperPriceRangeInt.isFinite() && !upperPriceRangeInt.isZero()
335+
? TickMath.getTickAtSqrtRatio(encodeSqrtRatioX96(upperPriceRangeInt.toNumber(), 1))
336+
: TickMath.MAX_TICK
337+
338+
return { lowerTick, upperTick }
339+
},
340+
[isWethToken0, ethPrice],
341+
)
314342

315343
return getTicksFromPriceRange
316344
}

0 commit comments

Comments
 (0)