Controlling Slippage

Protect swaps from sandwich attacks and price movement.

For Solidity implementation, see Controlling Slippage.

Why

You need slippage protection when:

  • Swapping tokens - DEX prices can move between quote and execution

  • Multi-step operations - Swap + deposit combos need end-to-end protection

  • Large trades - Bigger trades have higher slippage impact

  • Protecting against MEV - Sandwich bots exploit unprotected swaps

Without slippage protection, you might receive significantly fewer tokens than expected. Gearbox provides native slippage controls that work across any sequence of operations.

What

Gearbox uses a two-step pattern:

  1. storeExpectedBalances - Record expected minimum balances BEFORE operations

  2. compareBalances - Verify actual balances meet expectations AFTER operations

If the final balance is less than expected, the entire multicall reverts.

Why not use DEX slippage parameters?

  • Multi-step operations (swap → deposit) can't use single slippage value

  • Some protocols (ERC4626 vaults) have no built-in slippage protection

  • Gearbox slippage works at the account level, across any operation sequence

How

Multiple Tokens

Check slippage on multiple output tokens:

Negative Deltas (Expected to Spend)

You can also specify tokens you expect to decrease:

Gotchas

Keep Slippage Checks Close to Operations

Place storeExpectedBalances immediately before the first external call, and compareBalances immediately after the last:

compareBalances Fails Without storeExpectedBalances

Calling compareBalances without a prior storeExpectedBalances in the same multicall reverts.

Auto-Compare at Multicall End

If you call storeExpectedBalances but forget compareBalances, the check happens automatically at the end of the multicall. However, it's better to be explicit.

BalanceDelta Type

The amount in BalanceDelta is the change you expect, not the final balance:

Slippage Check Works on balanceOf

The check reads actual token balances via balanceOf. This means:

  • Internal transfers (like addCollateral) affect the balance

  • Token rebases affect the balance

  • Any balance change, regardless of source, is included

Cannot Reuse Stored Balances

After compareBalances runs, the stored expectations are cleared. For multiple swap sequences, call storeExpectedBalances again:

See Also

Last updated