This repository contains the dynamic_slippage_config_json
that is used to estimate slippage at the /swap
endpoint.
- Slippage threshold is not a one-size-fit-all solution -- Swapping different types of tokens require users to change slippage thresholds to cater to specific profiles.
- Many different factors can contribute to slippage -- one of them is time, especially when users can take time to accept a quote/sign the transaction.
- Based on the categories of the tokens being traded, Dynamic Slippage will use its category min max to calculate the optimal slippage.
- Dynamic Slippage is also implemented at the
/swap
endpoint, this will allow slippage to be estimated after "Swap Click" instead of during quoting, which will bring the estimation closer to the actual swap execution.
In the backend
If dynamicSlippage: true
is passed to the /swap
endpoint, the backend will
- Simulate a swap based on the exact quote.
- Use the
quotedAmount
andsimulatedAmount
to calculate asimulatedSlippage
. - Use the
amplificationRatio
and multiply it to thesimulatedSlippage
. - With the latest
simulatedSlippage
value, we will apply some math to it.
- Depending on the input/output tokens' categories, we will use the worse of category between the 2 tokens
- This will dictate the
categoryMin
andcategoryMax
- Take the
results = min(simulatedSlippage, userMin, categoryMin)
- Take the
finalSlippage = max(results, userMax, categoryMax)
- Finally, the
/swap
endpoint will return the response already using thefinalSlippage
based on the the simulation and heuristics.
Internal test stuff
Validate dynamic slippage config
cargo test