Commit cccf1495 authored by Ian Lapham's avatar Ian Lapham Committed by GitHub

fix: update tick parsing to handle min/max prices (#1613)

* update tick parsing to handle min/max prices

* remove fee tier step
parent 604ea495
import { priceToClosestTick, nearestUsableTick, FeeAmount, TICK_SPACINGS } from '@uniswap/v3-sdk/dist/' import {
priceToClosestTick,
nearestUsableTick,
FeeAmount,
TICK_SPACINGS,
encodeSqrtRatioX96,
TickMath,
} from '@uniswap/v3-sdk/dist/'
import { Price, Token } from '@uniswap/sdk-core' import { Price, Token } from '@uniswap/sdk-core'
import { tryParseAmount } from 'state/swap/hooks' import { tryParseAmount } from 'state/swap/hooks'
import JSBI from 'jsbi'
export function tryParseTick( export function tryParseTick(
baseToken?: Token, baseToken?: Token,
...@@ -12,8 +20,8 @@ export function tryParseTick( ...@@ -12,8 +20,8 @@ export function tryParseTick(
return undefined return undefined
} }
// base token fixed at 1 unit, quote token amount based on typed input
const amount = tryParseAmount(value, quoteToken) const amount = tryParseAmount(value, quoteToken)
const amountOne = tryParseAmount('1', baseToken) const amountOne = tryParseAmount('1', baseToken)
if (!amount || !amountOne) return undefined if (!amount || !amountOne) return undefined
...@@ -21,8 +29,19 @@ export function tryParseTick( ...@@ -21,8 +29,19 @@ export function tryParseTick(
// parse the typed value into a price // parse the typed value into a price
const price = new Price(baseToken, quoteToken, amountOne.quotient, amount.quotient) const price = new Price(baseToken, quoteToken, amountOne.quotient, amount.quotient)
// this function is agnostic to the base, will always return the correct tick let tick: number
const tick = priceToClosestTick(price)
// check price is within min/max bounds, if outside return min/max
const sqrtRatioX96 = encodeSqrtRatioX96(price.numerator, price.denominator)
if (JSBI.greaterThanOrEqual(sqrtRatioX96, TickMath.MAX_SQRT_RATIO)) {
tick = TickMath.MAX_TICK
} else if (JSBI.lessThanOrEqual(sqrtRatioX96, TickMath.MIN_SQRT_RATIO)) {
tick = TickMath.MIN_TICK
} else {
// this function is agnostic to the base, will always return the correct tick
tick = priceToClosestTick(price)
}
return nearestUsableTick(tick, TICK_SPACINGS[feeAmount]) return nearestUsableTick(tick, TICK_SPACINGS[feeAmount])
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment