Commit 0923cf4a authored by Tina's avatar Tina Committed by GitHub

fix: Auto-slippage logic (#6167)

* range auto slippage from [.1% to 5%] and multiply gas by 110% instead of 10%

* remove newline

* no multiplier on gas, add comments

* consolidate constant variables
parent 801958d0
...@@ -13,10 +13,7 @@ import { InterfaceTrade } from 'state/routing/types' ...@@ -13,10 +13,7 @@ import { InterfaceTrade } from 'state/routing/types'
import useGasPrice from './useGasPrice' import useGasPrice from './useGasPrice'
import useStablecoinPrice, { useStablecoinValue } from './useStablecoinPrice' import useStablecoinPrice, { useStablecoinValue } from './useStablecoinPrice'
const V3_SWAP_DEFAULT_SLIPPAGE = new Percent(50, 10_000) // .50% const DEFAULT_AUTO_SLIPPAGE = new Percent(1, 1000) // .10%
const ONE_TENTHS_PERCENT = new Percent(10, 10_000) // .10%
const DEFAULT_AUTO_SLIPPAGE = ONE_TENTHS_PERCENT
const GAS_ESTIMATE_BUFFER = new Percent(10, 100) // 10%
// Base costs regardless of how many hops in the route // Base costs regardless of how many hops in the route
const V3_SWAP_BASE_GAS_ESTIMATE = 100_000 const V3_SWAP_BASE_GAS_ESTIMATE = 100_000
...@@ -67,8 +64,10 @@ function guesstimateGas(trade: Trade<Currency, Currency, TradeType> | undefined) ...@@ -67,8 +64,10 @@ function guesstimateGas(trade: Trade<Currency, Currency, TradeType> | undefined)
return undefined return undefined
} }
const MIN_AUTO_SLIPPAGE_TOLERANCE = new Percent(5, 1000) // 0.5% const MIN_AUTO_SLIPPAGE_TOLERANCE = DEFAULT_AUTO_SLIPPAGE
const MAX_AUTO_SLIPPAGE_TOLERANCE = new Percent(25, 100) // 25% // assuming normal gas speeds, most swaps complete within 3 blocks and
// there's rarely price movement >5% in that time period
const MAX_AUTO_SLIPPAGE_TOLERANCE = new Percent(5, 100) // 5%
/** /**
* Returns slippage tolerance based on values from current trade, gas estimates from api, and active network. * Returns slippage tolerance based on values from current trade, gas estimates from api, and active network.
...@@ -102,12 +101,12 @@ export default function useAutoSlippageTolerance( ...@@ -102,12 +101,12 @@ export default function useAutoSlippageTolerance(
// if not, use local heuristic // if not, use local heuristic
const dollarCostToUse = const dollarCostToUse =
chainId && SUPPORTED_GAS_ESTIMATE_CHAIN_IDS.includes(chainId) && trade?.gasUseEstimateUSD chainId && SUPPORTED_GAS_ESTIMATE_CHAIN_IDS.includes(chainId) && trade?.gasUseEstimateUSD
? trade.gasUseEstimateUSD.multiply(GAS_ESTIMATE_BUFFER) ? trade.gasUseEstimateUSD
: dollarGasCost?.multiply(GAS_ESTIMATE_BUFFER) : dollarGasCost
if (outputDollarValue && dollarCostToUse) { if (outputDollarValue && dollarCostToUse) {
// the rationale is that a user will not want their trade to fail for a loss due to slippage that is less than // optimize for highest possible slippage without getting MEV'd
// the cost of the gas of the failed transaction // so set slippage % such that the difference between expected amount out and minimum amount out < gas fee to sandwich the trade
const fraction = dollarCostToUse.asFraction.divide(outputDollarValue.asFraction) const fraction = dollarCostToUse.asFraction.divide(outputDollarValue.asFraction)
const result = new Percent(fraction.numerator, fraction.denominator) const result = new Percent(fraction.numerator, fraction.denominator)
if (result.greaterThan(MAX_AUTO_SLIPPAGE_TOLERANCE)) { if (result.greaterThan(MAX_AUTO_SLIPPAGE_TOLERANCE)) {
...@@ -121,6 +120,6 @@ export default function useAutoSlippageTolerance( ...@@ -121,6 +120,6 @@ export default function useAutoSlippageTolerance(
return result return result
} }
return V3_SWAP_DEFAULT_SLIPPAGE return DEFAULT_AUTO_SLIPPAGE
}, [trade, onL2, nativeGasPrice, gasEstimate, nativeCurrency, nativeCurrencyPrice, chainId, outputDollarValue]) }, [trade, onL2, nativeGasPrice, gasEstimate, nativeCurrency, nativeCurrencyPrice, chainId, 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