Commit 9eaa22f6 authored by Jack Short's avatar Jack Short Committed by GitHub

chore: converting useStablecoinPrice to useUsdPrice in autoslippage (#7543)

parent 1c924828
...@@ -11,7 +11,8 @@ import { useMemo } from 'react' ...@@ -11,7 +11,8 @@ import { useMemo } from 'react'
import { ClassicTrade } from 'state/routing/types' import { ClassicTrade } from 'state/routing/types'
import useGasPrice from './useGasPrice' import useGasPrice from './useGasPrice'
import useStablecoinPrice, { useStablecoinAmountFromFiatValue, useStablecoinValue } from './useStablecoinPrice' import { useStablecoinAmountFromFiatValue, useStablecoinValue } from './useStablecoinPrice'
import { useUSDPrice } from './useUSDPrice'
const DEFAULT_AUTO_SLIPPAGE = new Percent(5, 1000) // 0.5% const DEFAULT_AUTO_SLIPPAGE = new Percent(5, 1000) // 0.5%
...@@ -77,30 +78,31 @@ export default function useClassicAutoSlippageTolerance(trade?: ClassicTrade): P ...@@ -77,30 +78,31 @@ export default function useClassicAutoSlippageTolerance(trade?: ClassicTrade): P
const { chainId } = useWeb3React() const { chainId } = useWeb3React()
const onL2 = chainId && L2_CHAIN_IDS.includes(chainId) const onL2 = chainId && L2_CHAIN_IDS.includes(chainId)
const outputDollarValue = useStablecoinValue(trade?.outputAmount) const outputDollarValue = useStablecoinValue(trade?.outputAmount)
const nativeGasPrice = useGasPrice()
const nativeGasPrice = useGasPrice()
const gasEstimate = guesstimateGas(trade) const gasEstimate = guesstimateGas(trade)
const gasEstimateUSD = useStablecoinAmountFromFiatValue(trade?.gasUseEstimateUSD) ?? null const gasEstimateUSD = useStablecoinAmountFromFiatValue(trade?.gasUseEstimateUSD) ?? null
const nativeCurrency = useNativeCurrency(chainId) const nativeCurrency = useNativeCurrency(chainId)
const nativeCurrencyPrice = useStablecoinPrice((trade && nativeCurrency) ?? undefined)
return useMemo(() => {
if (!trade || onL2) return DEFAULT_AUTO_SLIPPAGE
const nativeGasCost = const nativeGasCost =
nativeGasPrice && typeof gasEstimate === 'number' nativeGasPrice && typeof gasEstimate === 'number'
? JSBI.multiply(nativeGasPrice, JSBI.BigInt(gasEstimate)) ? JSBI.multiply(nativeGasPrice, JSBI.BigInt(gasEstimate))
: undefined : undefined
const dollarGasCost = const gasCostUSD = useUSDPrice(
nativeCurrency && nativeGasCost && nativeCurrencyPrice trade && nativeCurrency && nativeGasCost ? CurrencyAmount.fromRawAmount(nativeCurrency, nativeGasCost) : undefined
? nativeCurrencyPrice.quote(CurrencyAmount.fromRawAmount(nativeCurrency, nativeGasCost)) )
: undefined const gasCostStablecoinAmount = useStablecoinAmountFromFiatValue(gasCostUSD.data)
return useMemo(() => {
if (!trade || onL2) return DEFAULT_AUTO_SLIPPAGE
// if valid estimate from api and using api trade, use gas estimate from api // if valid estimate from api and using api trade, use gas estimate from api
// NOTE - dont use gas estimate for L2s yet - need to verify accuracy // NOTE - dont use gas estimate for L2s yet - need to verify accuracy
// if not, use local heuristic // if not, use local heuristic
const dollarCostToUse = const dollarCostToUse =
chainId && SUPPORTED_GAS_ESTIMATE_CHAIN_IDS.includes(chainId) && gasEstimateUSD ? gasEstimateUSD : dollarGasCost chainId && SUPPORTED_GAS_ESTIMATE_CHAIN_IDS.includes(chainId) && gasEstimateUSD
? gasEstimateUSD
: gasCostStablecoinAmount
if (outputDollarValue && dollarCostToUse) { if (outputDollarValue && dollarCostToUse) {
// optimize for highest possible slippage without getting MEV'd // optimize for highest possible slippage without getting MEV'd
...@@ -119,15 +121,5 @@ export default function useClassicAutoSlippageTolerance(trade?: ClassicTrade): P ...@@ -119,15 +121,5 @@ export default function useClassicAutoSlippageTolerance(trade?: ClassicTrade): P
} }
return DEFAULT_AUTO_SLIPPAGE return DEFAULT_AUTO_SLIPPAGE
}, [ }, [trade, onL2, chainId, gasEstimateUSD, gasCostStablecoinAmount, outputDollarValue])
trade,
onL2,
nativeGasPrice,
gasEstimate,
nativeCurrency,
nativeCurrencyPrice,
chainId,
gasEstimateUSD,
outputDollarValue,
])
} }
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