Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add validation for starting price in pool init #664

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/Inputs/SimpleInput/SimpleInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface IProps {
placeholder?: string
style?: CSSProperties
globalPrice?: number
onBlur?: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>
}

export const AmountInput: React.FC<IProps> = ({
Expand All @@ -22,7 +23,8 @@ export const AmountInput: React.FC<IProps> = ({
decimal,
placeholder,
style,
globalPrice
globalPrice,
onBlur
}) => {
const classes = useStyles()

Expand Down Expand Up @@ -95,6 +97,7 @@ export const AmountInput: React.FC<IProps> = ({
</Tooltip>
) : null
}
onBlur={onBlur}
/>
)
}
Expand Down
10 changes: 5 additions & 5 deletions src/components/NewPosition/NewPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ export const NewPosition: React.FC<INewPosition> = ({

const [minimumSliderIndex, setMinimumSliderIndex] = useState<number>(0)

const concentrationArray = useMemo(
() => getConcentrationArray(tickSpacing, 2, midPrice.index).sort((a, b) => a - b),
[tickSpacing]
)

const setRangeBlockerInfo = () => {
if (tokenAIndex === null || tokenBIndex === null) {
return 'Select tokens to set price range.'
Expand Down Expand Up @@ -428,11 +433,6 @@ export const NewPosition: React.FC<INewPosition> = ({
}
}

const concentrationArray = useMemo(
() => getConcentrationArray(tickSpacing, 2, midPrice.index).sort((a, b) => a - b),
[tickSpacing, midPrice.index]
)

return (
<Grid container className={classes.wrapper} direction='column'>
<Link to='/pool' style={{ textDecoration: 'none', maxWidth: 'fit-content' }}>
Expand Down
24 changes: 23 additions & 1 deletion src/components/NewPosition/PoolInit/PoolInit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,28 @@ export const PoolInit: React.FC<IPoolInit> = ({
changeRangeHandler(leftRange, rightRange)
}, [midPrice])

const validateMidPriceInput = (midPriceInput: string) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks quite complex. Iry it more this way:

const validateMidPriceInput = (midPriceInput, tickSpacing, isXtoY, xDecimal, yDecimal) => {
  const minTick = getMinTick(tickSpacing);
  const maxTick = getMaxTick(tickSpacing);

  const minPrice = isXtoY 
    ? calcPrice(minTick, isXtoY, xDecimal, yDecimal)
    : calcPrice(maxTick, isXtoY, xDecimal, yDecimal);
  const maxPrice = isXtoY 
    ? calcPrice(maxTick, isXtoY, xDecimal, yDecimal)
    : calcPrice(minTick, isXtoY, xDecimal, yDecimal);

  const numericMidPriceInput = parseFloat(midPriceInput);
  const validatedMidPrice = Math.min(Math.max(numericMidPriceInput, minPrice), maxPrice);

  return toMaxNumericPlaces(validatedMidPrice, 5);
}

const minTick = getMinTick(tickSpacing)
const maxTick = getMaxTick(tickSpacing)

const minPrice = isXtoY
? calcPrice(minTick, isXtoY, xDecimal, yDecimal)
: calcPrice(maxTick, isXtoY, xDecimal, yDecimal)
const maxPrice = isXtoY
? calcPrice(maxTick, isXtoY, xDecimal, yDecimal)
: calcPrice(minTick, isXtoY, xDecimal, yDecimal)

const numericMidPriceInput = parseFloat(midPriceInput)
const validatedMidPrice = Math.min(Math.max(numericMidPriceInput, minPrice), maxPrice)

return toMaxNumericPlaces(validatedMidPrice, 5)
}

useEffect(() => {
if (currentPairReversed !== null) {
setMidPriceInput((1 / +midPriceInput).toString())
const validatedMidPrice = validateMidPriceInput((1 / +midPriceInput).toString())

setMidPriceInput(validatedMidPrice)
changeRangeHandler(rightRange, leftRange)
}
}, [currentPairReversed])
Expand Down Expand Up @@ -136,6 +155,9 @@ export const PoolInit: React.FC<IPoolInit> = ({
className={classes.midPrice}
placeholder='0.0'
globalPrice={globalPrice}
onBlur={e => {
setMidPriceInput(validateMidPriceInput(e.target.value))
}}
/>

<Grid
Expand Down
Loading