Skip to main content

Swaps

Swaps are a core building block of the Euler V2 protocol, enabling users to efficiently manage leverage, rebalance positions, and handle liquidations — all within a single, gas-optimized transaction. This guide explains how swaps work in Euler, the contracts involved, and best practices for secure and efficient execution.

Why Swaps Matter

Swapping is essential for:

  • Opening leveraged positions: Borrow an asset, swap it for collateral, and deposit — all in one batch.
  • Closing or rebalancing positions: Withdraw collateral, swap for the debt asset, and repay your loan in a single transaction.
  • Liquidations: Collateral can be swapped for the liability asset to repay debt and close unhealthy positions.

Traditionally, these workflows required multiple manual steps or specialized smart contracts. Euler's native swap integration streamlines the process, making it seamless and efficient.

How Swaps Are Executed: The EVC Batch System

Swaps in Euler are executed as part of a batch of operations via the Ethereum Vault Connector (EVC). This means you can combine withdrawals, swaps, deposits, and repayments into a single atomic transaction, reducing gas costs and risk.

The typical flow:

  1. Withdraw or borrow the asset you want to swap.
  2. Send it to a periphery contract (the Swapper).
  3. Execute the swap on an external DEX via the Swapper contract.
  4. Deposit or repay with the swapped asset.
  5. Verify the result with the SwapVerifier contract.

The Swapper and SwapVerifier Contracts

  • Swapper:
    • An untrusted, flexible contract that executes swaps using external DEXs or aggregators.
    • Can be replaced or customized; acts as a black box from the protocol's perspective.
    • No access control—anyone can interact with it or withdraw tokens it holds.
  • SwapVerifier:
    • A trusted, fully audited contract that verifies the outcome of swaps.
    • Ensures the amount received (or repaid) matches expectations, enforcing slippage and price impact limits.
    • Always use SwapVerifier after Swapper in your EVC batch for security.

Swapping Modes

Euler supports three main swap modes:

  • Exact Input: Swap a known amount of input token for as much output token as possible. Useful for most DEX aggregator payloads.
  • Exact Output: Swap as little input as needed to receive a specific amount of output token. Any unused input is redeposited for the user.
  • Target Debt (Swap-to-Repay): Swap just enough to repay a target debt amount (often zero, to close a position). Any surplus is deposited for the user.

Handlers and Meta-Aggregators

  • Handlers: Internal modules in the Swapper contract that interface with external swap providers (e.g., 1Inch, Uniswap V2/V3). The handler type is specified in the swap parameters.
  • Meta-aggregator API: Off-chain, open-source service that queries multiple aggregators, finds the best route, and generates a transaction payload for your EVC batch. This enables automated, optimal swap execution.

Common Swap Flows

  • Open Leverage:
    1. Borrow asset to Swapper
    2. Swapper executes swap (exact input)
    3. Deposit output as collateral
    4. SwapVerifier checks result
  • Swap Collateral:
    1. Withdraw collateral to Swapper
    2. Swapper swaps for target collateral asset
    3. Deposit output as collateral
    4. SwapVerifier checks result
  • Swap-to-Repay:
    1. Withdraw collateral to Swapper
    2. Swapper swaps for liability asset (target debt mode)
    3. SwapVerifier checks that debt is repaid

Security and Best Practices

  • Always use SwapVerifier after Swapper in your EVC batch to ensure the swap result is as expected.
  • Be aware of trust boundaries: Swapper is untrusted; SwapVerifier is trusted and audited.
  • Handle slippage: Set reasonable slippage limits in your batch to avoid failed transactions or unexpected losses.
  • Use meta-aggregators for optimal routing and payload generation.