Commit d3c04b72 authored by Justin Domingue's avatar Justin Domingue Committed by GitHub

fix: stabilize fiat value by fixing max hop and increasing USDC out to 100k (#1651)

* stablizie fiat value by fixing max hop and increasing USDC out to 100k

* move maxHops and singleHopOnly to parameter of useV2TradeExactOut

* remove single hop param
parent 307a995a
...@@ -7,7 +7,7 @@ import { useActiveWeb3React } from './web3' ...@@ -7,7 +7,7 @@ import { useActiveWeb3React } from './web3'
// USDC amount used when calculating spot price for a given currency. // USDC amount used when calculating spot price for a given currency.
// The amount is large enough to filter low liquidity pairs. // The amount is large enough to filter low liquidity pairs.
const usdcCurrencyAmount = CurrencyAmount.fromRawAmount(USDC, 10_000e6) const usdcCurrencyAmount = CurrencyAmount.fromRawAmount(USDC, 100_000e6)
/** /**
* Returns the price in USDC of the input currency * Returns the price in USDC of the input currency
...@@ -16,7 +16,9 @@ const usdcCurrencyAmount = CurrencyAmount.fromRawAmount(USDC, 10_000e6) ...@@ -16,7 +16,9 @@ const usdcCurrencyAmount = CurrencyAmount.fromRawAmount(USDC, 10_000e6)
export default function useUSDCPrice(currency?: Currency): Price<Currency, Token> | undefined { export default function useUSDCPrice(currency?: Currency): Price<Currency, Token> | undefined {
const { chainId } = useActiveWeb3React() const { chainId } = useActiveWeb3React()
const v2USDCTrade = useV2TradeExactOut(currency, chainId === ChainId.MAINNET ? usdcCurrencyAmount : undefined) const v2USDCTrade = useV2TradeExactOut(currency, chainId === ChainId.MAINNET ? usdcCurrencyAmount : undefined, {
maxHops: 2,
})
const v3USDCTrade = useBestV3TradeExactOut(currency, chainId === ChainId.MAINNET ? usdcCurrencyAmount : undefined) const v3USDCTrade = useBestV3TradeExactOut(currency, chainId === ChainId.MAINNET ? usdcCurrencyAmount : undefined)
return useMemo(() => { return useMemo(() => {
......
...@@ -36,15 +36,14 @@ const MAX_HOPS = 3 ...@@ -36,15 +36,14 @@ const MAX_HOPS = 3
*/ */
export function useV2TradeExactIn( export function useV2TradeExactIn(
currencyAmountIn?: CurrencyAmount<Currency>, currencyAmountIn?: CurrencyAmount<Currency>,
currencyOut?: Currency currencyOut?: Currency,
{ maxHops = MAX_HOPS } = {}
): Trade<Currency, Currency, TradeType.EXACT_INPUT> | null { ): Trade<Currency, Currency, TradeType.EXACT_INPUT> | null {
const allowedPairs = useAllCommonPairs(currencyAmountIn?.currency, currencyOut) const allowedPairs = useAllCommonPairs(currencyAmountIn?.currency, currencyOut)
const [singleHopOnly] = useUserSingleHopOnly()
return useMemo(() => { return useMemo(() => {
if (currencyAmountIn && currencyOut && allowedPairs.length > 0) { if (currencyAmountIn && currencyOut && allowedPairs.length > 0) {
if (singleHopOnly) { if (maxHops === 1) {
return ( return (
Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops: 1, maxNumResults: 1 })[0] ?? Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops: 1, maxNumResults: 1 })[0] ??
null null
...@@ -52,7 +51,7 @@ export function useV2TradeExactIn( ...@@ -52,7 +51,7 @@ export function useV2TradeExactIn(
} }
// search through trades with varying hops, find best trade out of them // search through trades with varying hops, find best trade out of them
let bestTradeSoFar: Trade<Currency, Currency, TradeType.EXACT_INPUT> | null = null let bestTradeSoFar: Trade<Currency, Currency, TradeType.EXACT_INPUT> | null = null
for (let i = 1; i <= MAX_HOPS; i++) { for (let i = 1; i <= maxHops; i++) {
const currentTrade: Trade<Currency, Currency, TradeType.EXACT_INPUT> | null = const currentTrade: Trade<Currency, Currency, TradeType.EXACT_INPUT> | null =
Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops: i, maxNumResults: 1 })[0] ?? Trade.bestTradeExactIn(allowedPairs, currencyAmountIn, currencyOut, { maxHops: i, maxNumResults: 1 })[0] ??
null null
...@@ -65,7 +64,7 @@ export function useV2TradeExactIn( ...@@ -65,7 +64,7 @@ export function useV2TradeExactIn(
} }
return null return null
}, [allowedPairs, currencyAmountIn, currencyOut, singleHopOnly]) }, [allowedPairs, currencyAmountIn, currencyOut, maxHops])
} }
/** /**
...@@ -73,15 +72,14 @@ export function useV2TradeExactIn( ...@@ -73,15 +72,14 @@ export function useV2TradeExactIn(
*/ */
export function useV2TradeExactOut( export function useV2TradeExactOut(
currencyIn?: Currency, currencyIn?: Currency,
currencyAmountOut?: CurrencyAmount<Currency> currencyAmountOut?: CurrencyAmount<Currency>,
{ maxHops = MAX_HOPS } = {}
): Trade<Currency, Currency, TradeType.EXACT_OUTPUT> | null { ): Trade<Currency, Currency, TradeType.EXACT_OUTPUT> | null {
const allowedPairs = useAllCommonPairs(currencyIn, currencyAmountOut?.currency) const allowedPairs = useAllCommonPairs(currencyIn, currencyAmountOut?.currency)
const [singleHopOnly] = useUserSingleHopOnly()
return useMemo(() => { return useMemo(() => {
if (currencyIn && currencyAmountOut && allowedPairs.length > 0) { if (currencyIn && currencyAmountOut && allowedPairs.length > 0) {
if (singleHopOnly) { if (maxHops === 1) {
return ( return (
Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops: 1, maxNumResults: 1 })[0] ?? Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops: 1, maxNumResults: 1 })[0] ??
null null
...@@ -89,7 +87,7 @@ export function useV2TradeExactOut( ...@@ -89,7 +87,7 @@ export function useV2TradeExactOut(
} }
// search through trades with varying hops, find best trade out of them // search through trades with varying hops, find best trade out of them
let bestTradeSoFar: Trade<Currency, Currency, TradeType.EXACT_OUTPUT> | null = null let bestTradeSoFar: Trade<Currency, Currency, TradeType.EXACT_OUTPUT> | null = null
for (let i = 1; i <= MAX_HOPS; i++) { for (let i = 1; i <= maxHops; i++) {
const currentTrade = const currentTrade =
Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops: i, maxNumResults: 1 })[0] ?? Trade.bestTradeExactOut(allowedPairs, currencyIn, currencyAmountOut, { maxHops: i, maxNumResults: 1 })[0] ??
null null
...@@ -100,5 +98,5 @@ export function useV2TradeExactOut( ...@@ -100,5 +98,5 @@ export function useV2TradeExactOut(
return bestTradeSoFar return bestTradeSoFar
} }
return null return null
}, [currencyIn, currencyAmountOut, allowedPairs, singleHopOnly]) }, [currencyIn, currencyAmountOut, allowedPairs, maxHops])
} }
...@@ -19,6 +19,7 @@ import { AppDispatch, AppState } from '../index' ...@@ -19,6 +19,7 @@ import { AppDispatch, AppState } from '../index'
import { useCurrencyBalances } from '../wallet/hooks' import { useCurrencyBalances } from '../wallet/hooks'
import { Field, replaceSwapState, selectCurrency, setRecipient, switchCurrencies, typeInput } from './actions' import { Field, replaceSwapState, selectCurrency, setRecipient, switchCurrencies, typeInput } from './actions'
import { SwapState } from './reducer' import { SwapState } from './reducer'
import { useUserSingleHopOnly } from 'state/user/hooks'
export function useSwapState(): AppState['swap'] { export function useSwapState(): AppState['swap'] {
return useSelector<AppState, AppState['swap']>((state) => state.swap) return useSelector<AppState, AppState['swap']>((state) => state.swap)
...@@ -126,6 +127,8 @@ export function useDerivedSwapInfo( ...@@ -126,6 +127,8 @@ export function useDerivedSwapInfo(
} { } {
const { account } = useActiveWeb3React() const { account } = useActiveWeb3React()
const [singleHopOnly] = useUserSingleHopOnly()
const { const {
independentField, independentField,
typedValue, typedValue,
...@@ -147,8 +150,12 @@ export function useDerivedSwapInfo( ...@@ -147,8 +150,12 @@ export function useDerivedSwapInfo(
const isExactIn: boolean = independentField === Field.INPUT const isExactIn: boolean = independentField === Field.INPUT
const parsedAmount = tryParseAmount(typedValue, (isExactIn ? inputCurrency : outputCurrency) ?? undefined) const parsedAmount = tryParseAmount(typedValue, (isExactIn ? inputCurrency : outputCurrency) ?? undefined)
const bestV2TradeExactIn = useV2TradeExactIn(isExactIn ? parsedAmount : undefined, outputCurrency ?? undefined) const bestV2TradeExactIn = useV2TradeExactIn(isExactIn ? parsedAmount : undefined, outputCurrency ?? undefined, {
const bestV2TradeExactOut = useV2TradeExactOut(inputCurrency ?? undefined, !isExactIn ? parsedAmount : undefined) maxHops: singleHopOnly ? 1 : undefined,
})
const bestV2TradeExactOut = useV2TradeExactOut(inputCurrency ?? undefined, !isExactIn ? parsedAmount : undefined, {
maxHops: singleHopOnly ? 1 : undefined,
})
const bestV3TradeExactIn = useBestV3TradeExactIn(isExactIn ? parsedAmount : undefined, outputCurrency ?? undefined) const bestV3TradeExactIn = useBestV3TradeExactIn(isExactIn ? parsedAmount : undefined, outputCurrency ?? undefined)
const bestV3TradeExactOut = useBestV3TradeExactOut(inputCurrency ?? undefined, !isExactIn ? parsedAmount : undefined) const bestV3TradeExactOut = useBestV3TradeExactOut(inputCurrency ?? undefined, !isExactIn ? parsedAmount : undefined)
......
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