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 initial price #38

Merged
merged 2 commits into from
Apr 18, 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 @@ -11,6 +11,7 @@ interface IProps {
decimal: number
placeholder?: string
style?: CSSProperties
onBlur?: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>
}

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

Expand Down Expand Up @@ -75,6 +77,7 @@ export const AmountInput: React.FC<IProps> = ({
disableUnderline={true}
placeholder={placeholder}
onChange={allowOnlyDigitsAndTrimUnnecessaryZeros}
onBlur={onBlur}
/>
)
}
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 @@ -99,9 +99,28 @@ export const PoolInit: React.FC<IPoolInit> = ({
changeRangeHandler(leftRange, rightRange)
}, [midPrice])

const validateMidPriceInput = (midPriceInput: string) => {
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 @@ -134,6 +153,9 @@ export const PoolInit: React.FC<IPoolInit> = ({
decimal={isXtoY ? xDecimal : yDecimal}
className={classes.midPrice}
placeholder='0.0'
onBlur={e => {
setMidPriceInput(validateMidPriceInput(e.target.value))
}}
/>

<Grid
Expand Down
Loading