Controlling Slippage

Protect against price movement during swaps and other operations.

For SDK implementation, see Controlling Slippage.

Why

Slippage protection is critical for:

  • Swaps - Ensure minimum output from DEX trades

  • Deposits/Withdrawals - Protect against unfavorable rates

  • Multi-step strategies - Verify final position matches expectations

Without slippage protection, MEV bots can sandwich your transactions, extracting value through price manipulation.

What

Two operations work together for slippage control:

Operation
Description

storeExpectedBalances

Record expected token balances (current + delta)

compareBalances

Verify current balances meet expectations, revert if not

The pattern: Store expectations before swaps, compare after swaps.

How

Basic Slippage Check

Multiple Token Checks

Check multiple tokens in one comparison:

Complete Strategy with Slippage Protection

8-step leveraged yield farming with full slippage protection:

Gotchas

Expected Balances Must Not Already Be Set

storeExpectedBalances reverts if already called without a compareBalances:

Compare Reverts If Not Stored

compareBalances reverts if no expected balances were stored:

Delta Calculation

The delta is added to CURRENT balance, not starting balance:

Available in All Multicalls

Unlike most operations, slippage checks work in:

  • openCreditAccount

  • multicall

  • closeCreditAccount

  • botMulticall

  • liquidateCreditAccount

Gas Optimization

For simple swaps, you can rely on the DEX's slippage parameter:

Use storeExpectedBalances/compareBalances for:

  • Multi-hop routes where intermediate tokens aren't checked

  • Complex strategies with multiple operations

  • When you need to check multiple tokens atomically

See Also

Last updated