Commit f00adc75 authored by Noah Zinsmeister's avatar Noah Zinsmeister

refactor range selection and use in migrate

improve migrate
parent 63907b7b
import { TransactionResponse } from '@ethersproject/providers' import { TransactionResponse } from '@ethersproject/providers'
import { Currency, TokenAmount, Percent, ETHER } from '@uniswap/sdk-core' import { Currency, TokenAmount, Percent, ETHER, Price } from '@uniswap/sdk-core'
import React, { useCallback, useContext, useState } from 'react' import React, { useCallback, useContext, useState } from 'react'
import { Link2, AlertTriangle } from 'react-feather' import { Link2, AlertTriangle } from 'react-feather'
import ReactGA from 'react-ga' import ReactGA from 'react-ga'
...@@ -105,6 +105,48 @@ export function FeeSelector({ ...@@ -105,6 +105,48 @@ export function FeeSelector({
) )
} }
// currencyA is the base token
export function RangeSelector({
priceLower,
priceUpper,
onLowerRangeInput,
onUpperRangeInput,
currencyA,
currencyB,
}: {
priceLower?: Price
priceUpper?: Price
onLowerRangeInput: (typedValue: string) => void
onUpperRangeInput: (typedValue: string) => void
currencyA?: Currency | null
currencyB?: Currency | null
}) {
return (
<RowBetween>
<StepCounter
value={priceLower?.toSignificant(5) ?? ''}
onUserInput={onLowerRangeInput}
width="48%"
label={
priceLower && currencyA && currencyB
? `${priceLower.toSignificant(4)} ${currencyB.symbol} / 1 ${currencyA.symbol}`
: '-'
}
/>
<StepCounter
value={priceUpper?.toSignificant(5) ?? ''}
onUserInput={onUpperRangeInput}
width="48%"
label={
priceUpper && currencyA && currencyB
? `${priceUpper.toSignificant(4)} ${currencyB?.symbol} / 1 ${currencyA?.symbol}`
: '-'
}
/>
</RowBetween>
)
}
// the order of displayed base currencies from left to right is always in sort order // the order of displayed base currencies from left to right is always in sort order
// currencyA is treated as the preferred base currency // currencyA is treated as the preferred base currency
export function RateToggle({ export function RateToggle({
...@@ -538,28 +580,15 @@ export default function AddLiquidity({ ...@@ -538,28 +580,15 @@ export default function AddLiquidity({
</TYPE.main> </TYPE.main>
</RowBetween> </RowBetween>
)} )}
<RowBetween>
<StepCounter <RangeSelector
value={priceLower?.toSignificant(5) ?? ''} priceLower={priceLower}
onUserInput={onLowerRangeInput} priceUpper={priceUpper}
width="48%" onLowerRangeInput={onLowerRangeInput}
label={ onUpperRangeInput={onUpperRangeInput}
priceLower && currencyA && currencyB currencyA={currencyA}
? '1 ' + currencyA?.symbol + ' / ' + priceLower?.toSignificant(4) + ' ' + currencyB?.symbol currencyB={currencyB}
: '-'
}
/>
<StepCounter
value={priceUpper?.toSignificant(5) ?? ''}
onUserInput={onUpperRangeInput}
width="48%"
label={
priceUpper && currencyA && currencyB
? '1 ' + currencyA?.symbol + ' / ' + priceUpper?.toSignificant(4) + ' ' + currencyB?.symbol
: '-'
}
/> />
</RowBetween>
{outOfRange ? ( {outOfRange ? (
<YellowCard padding="8px 12px" borderRadius="12px"> <YellowCard padding="8px 12px" borderRadius="12px">
......
...@@ -275,9 +275,9 @@ export default function AddLiquidity({ ...@@ -275,9 +275,9 @@ export default function AddLiquidity({
(currencyA: Currency) => { (currencyA: Currency) => {
const newCurrencyIdA = currencyId(currencyA) const newCurrencyIdA = currencyId(currencyA)
if (newCurrencyIdA === currencyIdB) { if (newCurrencyIdA === currencyIdB) {
history.push(`/add/${currencyIdB}/${currencyIdA}`) history.push(`/add/v2/${currencyIdB}/${currencyIdA}`)
} else { } else {
history.push(`/add/${newCurrencyIdA}/${currencyIdB}`) history.push(`/add/v2/${newCurrencyIdA}/${currencyIdB}`)
} }
}, },
[currencyIdB, history, currencyIdA] [currencyIdB, history, currencyIdA]
...@@ -287,12 +287,12 @@ export default function AddLiquidity({ ...@@ -287,12 +287,12 @@ export default function AddLiquidity({
const newCurrencyIdB = currencyId(currencyB) const newCurrencyIdB = currencyId(currencyB)
if (currencyIdA === newCurrencyIdB) { if (currencyIdA === newCurrencyIdB) {
if (currencyIdB) { if (currencyIdB) {
history.push(`/add/${currencyIdB}/${newCurrencyIdB}`) history.push(`/add/v2/${currencyIdB}/${newCurrencyIdB}`)
} else { } else {
history.push(`/add/${newCurrencyIdB}`) history.push(`/add/v2/${newCurrencyIdB}`)
} }
} else { } else {
history.push(`/add/${currencyIdA ? currencyIdA : 'ETH'}/${newCurrencyIdB}`) history.push(`/add/v2/${currencyIdA ? currencyIdA : 'ETH'}/${newCurrencyIdB}`)
} }
}, },
[currencyIdA, history, currencyIdB] [currencyIdA, history, currencyIdB]
......
This diff is collapsed.
...@@ -171,7 +171,7 @@ export default function Pool() { ...@@ -171,7 +171,7 @@ export default function Pool() {
as={Link} as={Link}
padding="6px 8px" padding="6px 8px"
borderRadius="12px" borderRadius="12px"
to="/add/ETH" to="/add/v2/ETH"
> >
<Text fontWeight={500} fontSize={16}> <Text fontWeight={500} fontSize={16}>
Add Liquidity Add Liquidity
......
...@@ -176,10 +176,10 @@ export function useDerivedMintInfo( ...@@ -176,10 +176,10 @@ export function useDerivedMintInfo(
[key: string]: number | undefined [key: string]: number | undefined
} = useMemo(() => { } = useMemo(() => {
return { return {
[Bound.LOWER]: poolForPosition ? tryParseTick(tokenA, tokenB, poolForPosition, lowerRangeTypedValue) : undefined, [Bound.LOWER]: tryParseTick(tokenA, tokenB, feeAmount, lowerRangeTypedValue),
[Bound.UPPER]: poolForPosition ? tryParseTick(tokenA, tokenB, poolForPosition, upperRangeTypedValue) : undefined, [Bound.UPPER]: tryParseTick(tokenA, tokenB, feeAmount, upperRangeTypedValue),
} }
}, [lowerRangeTypedValue, poolForPosition, tokenA, tokenB, upperRangeTypedValue]) }, [feeAmount, lowerRangeTypedValue, tokenA, tokenB, upperRangeTypedValue])
const { [Bound.LOWER]: tickLower, [Bound.UPPER]: tickUpper } = ticks || {} const { [Bound.LOWER]: tickLower, [Bound.UPPER]: tickUpper } = ticks || {}
const sortedTicks = useMemo( const sortedTicks = useMemo(
...@@ -194,10 +194,10 @@ export function useDerivedMintInfo( ...@@ -194,10 +194,10 @@ export function useDerivedMintInfo(
const pricesAtTicks = useMemo(() => { const pricesAtTicks = useMemo(() => {
return { return {
[Bound.LOWER]: poolForPosition ? getTickToPrice(tokenA, tokenB, ticks[Bound.LOWER]) : undefined, [Bound.LOWER]: getTickToPrice(tokenA, tokenB, ticks[Bound.LOWER]),
[Bound.UPPER]: poolForPosition ? getTickToPrice(tokenA, tokenB, ticks[Bound.UPPER]) : undefined, [Bound.UPPER]: getTickToPrice(tokenA, tokenB, ticks[Bound.UPPER]),
} }
}, [poolForPosition, ticks, tokenA, tokenB]) }, [tokenA, tokenB, ticks])
const { [Bound.LOWER]: lowerPrice, [Bound.UPPER]: upperPrice } = pricesAtTicks const { [Bound.LOWER]: lowerPrice, [Bound.UPPER]: upperPrice } = pricesAtTicks
// mark invalid range // mark invalid range
......
import { Pool, priceToClosestTick, nearestUsableTick } from '@uniswap/v3-sdk/dist/' import { priceToClosestTick, nearestUsableTick, FeeAmount, TICK_SPACINGS } 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'
export function tryParseTick( export function tryParseTick(
baseToken: Token | undefined, baseToken?: Token,
quoteToken: Token | undefined, quoteToken?: Token,
pool: Pool | undefined, feeAmount?: FeeAmount,
value?: string value?: string
): number | undefined { ): number | undefined {
if (!value || !baseToken || !quoteToken || !pool) { if (!baseToken || !quoteToken || !feeAmount || !value) {
return undefined return undefined
} }
...@@ -23,5 +23,5 @@ export function tryParseTick( ...@@ -23,5 +23,5 @@ export function tryParseTick(
const tick = priceToClosestTick(price) const tick = priceToClosestTick(price)
return nearestUsableTick(tick, pool.tickSpacing) return nearestUsableTick(tick, TICK_SPACINGS[feeAmount])
} }
import { Token, Price } from '@uniswap/sdk-core' import { Token, Price } from '@uniswap/sdk-core'
import { tickToPrice } from '@uniswap/v3-sdk' import { tickToPrice } from '@uniswap/v3-sdk'
export function getTickToPrice( export function getTickToPrice(baseToken?: Token, quoteToken?: Token, tick?: number): Price | undefined {
baseToken: Token | undefined, if (!baseToken || !quoteToken || typeof tick !== 'number') {
quoteToken: Token | undefined,
tick: number | undefined
): Price | undefined {
if (!baseToken || !quoteToken || tick === undefined) {
return undefined return undefined
} }
return tickToPrice(baseToken, quoteToken, tick) return tickToPrice(baseToken, quoteToken, tick)
......
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