Price Impact and Slippage in DEX Swaps
Last updated:
Price impact measures how much your trade moves the market price. Slippage is the maximum deviation you'll accept between quoted and executed price. swapapi returns both values so you can protect users from unfavorable trades.
What is Price Impact?
When you trade on a DEX, you are trading against a liquidity pool. The pool uses an automated market maker (AMM) formula to determine the price. Larger trades consume more liquidity, which moves the price further from the market rate.
swapapi returns priceImpact as a decimal ratio. A value of -0.0012 means -0.12% (unfavorable — you get less than the market rate). Negative values always indicate an unfavorable price movement.
The relationship is straightforward: swapping 100 ETH will have a much higher price impact than swapping 0.1 ETH, because the larger trade consumes more of the pool's liquidity.
{ "swapPrice": 2500.0, // quoted exchange rate "priceImpact": -0.0012, // -0.12% — unfavorable "expectedAmountOut": "2500000000" // already accounts for impact }
Rule of thumb: reject any swap with a priceImpact below -0.05 (-5%). That level of impact usually means the pool has very low liquidity for this pair.
What is Slippage?
Slippage is the difference between the expected execution price (at quote time) and the actual execution price (on-chain). Between when you receive a quote and when your transaction is mined, other trades may execute against the same pool, changing the price.
The maxSlippage parameter controls how much price movement you will tolerate. It is a decimal between 0 and 1:
0.005= 0.5% tolerance (the default)0.01= 1% tolerance0.03= 3% tolerance
The API uses maxSlippage to calculate minAmountOut — the minimum acceptable output. If the actual output falls below this threshold, the transaction reverts on-chain and you keep your tokens (minus gas).
$ curl "https://api.swapapi.dev/v1/swap/1?\ tokenIn=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE&\ tokenOut=0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48&\ amount=1000000000000000000&\ maxSlippage=0.005&\ sender=0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
How minAmountOut is calculated
The API computes minAmountOut from expectedAmountOut and maxSlippage using this formula:
minAmountOut = expectedAmountOut × (1 − maxSlippage) Example: expectedAmountOut = 2,500 USDC maxSlippage = 0.005 (0.5%) minAmountOut = 2,500 × 0.995 = 2,487.50 USDC In raw units (6 decimals): expectedAmountOut = "2500000000" minAmountOut = "2487500000"
This value is encoded into the swap calldata. The on-chain router contract enforces it — if the actual output is less than minAmountOut, the entire transaction reverts.
When to reject swaps
Use the priceImpact field to decide whether to show or reject a swap. Here are recommended thresholds:
| Price Impact | Meaning | Action |
|---|---|---|
| > -0.01 | Normal | Proceed |
| -0.01 to -0.05 | Elevated | Warn the user |
| -0.05 to -0.10 | High | Likely reject |
| < -0.10 | Extreme | Definitely reject |
const { data } = await res.json(); if (data.priceImpact < -0.05) { throw new Error( `Price impact too high: ${(data.priceImpact * 100).toFixed(2)}%` ); } if (data.priceImpact < -0.01) { console.warn( `Warning: ${(data.priceImpact * 100).toFixed(2)}% price impact` ); }
Fee-on-transfer tokens
Some tokens charge a tax on every transfer — typically 1% to 10% of the amount. These are sometimes called "tax tokens" or "reflection tokens." Common examples include SafeMoon-style tokens.
The API cannot detect fee-on-transfer tokens. The expectedAmountOut will not account for the transfer tax, so the actual amount you receive in your wallet will be lower than quoted.
If you know a token charges a transfer fee, increase maxSlippage to accommodate it. For a 5% tax token, set maxSlippage to at least 0.06 (6%) to prevent reverts. A common safe range for known tax tokens is 0.10 to 0.15 (10-15%).
Protecting against MEV
MEV (Maximal Extractable Value) bots monitor the mempool for pending swap transactions. A common attack is the "sandwich" — the bot buys the token before your transaction executes (pushing the price up), then sells after your transaction (pocketing the difference).
Your maxSlippage setting is your primary defense. A lower slippage tolerance makes sandwich attacks less profitable, because the bot has less room to extract value. However, setting it too low increases the chance your transaction reverts due to normal price movement.
Recommended values:
- Major pairs (ETH/USDC, ETH/USDT):
0.005(0.5%) - Mid-cap tokens:
0.01to0.02(1-2%) - Small-cap / low liquidity:
0.02to0.03(2-3%)
The swap calldata also includes a deadline — submit the transaction within 30 seconds of receiving the quote. Stale transactions with outdated prices are more vulnerable to MEV extraction.
Best practices for production
- Always check
priceImpactbefore showing the swap to users. Display it prominently in the UI. - Set
maxSlippagebased on the token pair. Use 0.5% for major pairs, higher for smaller tokens. Let users override if they understand the risk. - Simulate with
eth_callbefore submitting. This catches reverts before you pay gas. Use thetxobject directly in a static call. - Re-fetch quotes if more than 30 seconds have passed. The calldata includes a deadline and prices move. Stale quotes waste gas on reverts.
- Display
minAmountOutto users as "Minimum received." This is the guaranteed floor — the transaction reverts if actual output falls below it.