Skip to main content

Order Flow Router

The Order Flow Router is a key component of Euler V2's swap and multiply position infrastructure. It acts as an off-chain meta-aggregator and strategy engine, finding the best way to swap assets and building safe, ready-to-use payloads for EVC batch calls. This enables users to open, close, and manage complex positions with a single transaction.

Overview

Euler V2's seamless integration of token swaps within lending is made possible by combining deferred liquidity checks with swap periphery contracts, all orchestrated by the Order Flow Router. For example, opening a multiply position involves:

  • Margin deposit
  • Borrowing the full short amount (enabled by deferred liquidity checks)
  • Swapping to the collateral asset
  • Depositing the result

Closing a multiply position reverses this flow: collateral is withdrawn, swapped to the debt asset, and the debt is repaid — all in one batch, with the Order Flow Router providing the optimal swap route and payload.

Architecture: Strategies and Pipelines

At its core, the Order Flow Router uses configurable strategies organized into pipelines. When a quote request comes in, it is processed through the pipeline, where each strategy is applied in turn until a valid response is found. Strategies can also modify requests and trigger recursive runs through the pipeline, allowing for highly flexible and robust routing.

Some key strategies include:

  • strategyBalmySDK: A meta-aggregator that queries multiple sources (like 1Inch, LI.FI, Pendle) and compares results.
  • strategyCombinedUniswap: For swap-and-repay operations, most of the trade is routed through an aggregator, with the remainder swapped on Uniswap's exact output function to precisely clear debt.
  • strategyERC4626Wrapper: Handles direct deposits/redemptions into ERC4626 vaults, trading the underlying through aggregators. Similar wrappers exist for Curve LPs, Midas assets, and more.
  • strategyConnect2: Stitches two trades together through an intermediary asset.

Pipelines are defined per chain and can be customized for specific asset pairs or use cases.

Quotes and Payloads

The Order Flow Router API returns quotes that include:

  • Expected trade details (amounts sold/bought, routes, slippage, etc.)
  • All data needed to call the EVK periphery swapping contracts: Swapper and SwapVerifier

These payloads are ready to be used as items in an EVC batch, ensuring safe and efficient execution. The Swapper contract executes the swap, while the SwapVerifier checks the result and either deposits the bought asset or repays debt.

Swap-to-Repay: Handling Debt Closures

Closing borrow positions by selling collateral is uniquely challenging. Most swap providers only support sell orders (fixed input, variable output), but repaying debt requires a buy order (fixed output). The exact debt amount is only known at execution time due to interest accrual.

The Order Flow Router addresses this with two main strategies:

  • strategyCombinedUniswap: Executes most of the trade via an aggregator, then uses Uniswap's exact output to clear the remaining debt precisely.
  • strategyBalmySDK: Where Uniswap is unavailable, approximates a buy order by binary searching sell quotes to reach the debt amount, factoring in slippage and a buffer for accruing debt. Any small remainder is deposited into the user's debt vault.

Supported Aggregators and Custom Routes

The Order Flow Router supports a wide range of DEX aggregators and swap providers, including:

  • 0x
  • 1Inch
  • Enso
  • KyberSwap
  • LI.FI
  • Magpie
  • Odos
  • Oku
  • OKX
  • Ooga Booga
  • OpenOcean
  • ParaSwap
  • Pendle
  • Uniswap

For assets not well supported by aggregators, direct strategies are available for:

  • ERC4626 vaults
  • Curve LP NG
  • Midas m-tokens
  • Idle CDO Tranches

Additional Resources