@@ -12,6 +12,7 @@ import {
12
12
squeethPoolContractAtom ,
13
13
} from '../contracts/atoms'
14
14
import useAppCallback from '@hooks/useAppCallback'
15
+ import { useETHPrice } from '@hooks/useETHPrice'
15
16
import { addressAtom } from '../wallet/atoms'
16
17
import { Contract } from 'web3-eth-contract'
17
18
import { useHandleTransaction } from '../wallet/hooks'
@@ -306,11 +307,40 @@ export const useGetTickPrices = () => {
306
307
}
307
308
308
309
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
+ )
314
344
315
345
return getTicksFromPriceRange
316
346
}
0 commit comments