Order Flow Router
Overview
One of the key features of Euler V2 is the seamless integration of token swaps within the lending functionality, allowing users to easily open and close multiplied positions. This is achieved by combining deferred liquidity checks with swap periphery contracts, called using off-chain swap payloads fetched from swap aggregators.
A multiply position, for example, is created by combining a margin deposit, a borrow of the full short amount (enabled through deferred liquidity checks), a swap to the collateral asset, and a deposit. Similarly, closing a multiply position involves withdrawing collateral into the swapper periphery (again enabled by deferred liquidity checks), swapping to the debt asset, and repaying the debt.
The Euler Order Flow API is an off-chain backend service responsible for finding the best way to swap a pair of assets and building a safe payload that can be incorporated into an EVC batch. Its main features include:
- Configurable strategies that can handle any asset pair on any chain.
- A meta-aggregator of swap providers, querying multiple sources to find the best quote.
- The ability to handle custom routes directly on-chain, such as depositing or redeeming vault shares or LP tokens.
- Configurable fallbacks to ensure uninterrupted service.
- Providing safe payloads for calling Euler swap periphery contracts, ready to be used in EVC batch calls.
Architecture
The core concept in the Order Flow Router is the use of strategies
configured into pipelines
. An incoming quote request is processed through the pipeline, where strategies are applied sequentially until one provides a valid response. Strategies can also modify the request and trigger a recursive run through the pipeline. The pipelines are defined per chain
Some of the main strategies are:
strategyBalmySDK
: Meta-aggregator strategy built on top of Balmy SDK, querying selected aggregators and comparing results.strategyCombinedUniswap
: A strategy for swap and repay operations, where most assets are traded through an aggregator and a small remainder is swapped using Uniswap's exact output function.strategyERC4626Wrapper
: A strategy that redeems or deposits into ERC4626 vaults directly and trades the underlying through aggregators. Similar strategies exist for Curve LP tokens, Midas assets, and more.strategyConnect2
- A strategy that stitches 2 trades together through an intermediary asset.
For example, the following pipeline is configured to query Pendle and LI.FI for Pendle PT token swaps, and 1Inch and LI.FI for all other tokens. It also includes a repay wrapper strategy, which processes a regular exact-input quote and wraps it into a repay operation on the receiver vault.
const pipeline = [
{
strategy: StrategyRepayWrapper.name(),
match: {
isRepay: true,
swapperModes: [SwapperMode.EXACT_IN],
},
},
{
strategy: StrategyBalmySDK.name(),
config: {
sourcesFilter: {
includeSources: ["pendle", "li-fi"],
},
},
match: { isPendlePT: true },
},
{
strategy: StrategyBalmySDK.name(),
config: {
sourcesFilter: {
includeSources: ["1inch", "li-fi"],
},
},
},
]
Quotes and payloads
Quotes returned from the API include expected trade details such as amounts sold and bought, routes, slippage, and more. They also contain all the necessary data to call the EVK periphery swapping contracts: Swapper
and SwapVerifier
.
-
Swapper: A multicall contract that receives sold tokens and executes payloads from aggregators. It is considered untrusted and should not hold any balance beyond transient execution.
-
SwapVerifier: A trusted contract that verifies amounts sold and bought, and consumes proceeds by either depositing bought assets for the user or using them to repay the user’s debt.
Both contracts are intended to be called within an EVC batch. The API provides ready-to-use payloads for batch items.
See periphery docs for details about swapping contracts.
Swap to repay
Closing borrow positions by selling collateral introduces unique challenges. Most swap providers only support sell orders, where the sold amount is fixed but the bought amount is uncertain (subject to price impact and slippage). However, repaying a debt requires a buy order where the bought amount must match the debt exactly.
Moreover, the exact debt amount isn’t known at the time of transaction construction, since it accrues over time and is only determinable during execution. True buy orders (fixed bought amount) are only available on certain DEXes like Uniswap and Balancer.
The API provides 2 strategies to handle repays with collateral.
-
strategyCombinedUniswap
: Executes two swaps — most of the trade routes through an aggregator for the best price, and a small remainder swaps on Uniswap’s exact output. The Uniswap leg amount is calculated on-chain to clear the debt fully without leftover dust. -
strategyBalmySDK
: Where Uniswap is unavailable, this strategy approximates a buy order by binary searching sell quotes to reach the debt amount. It factors in user-provided slippage and a small extra buffer for accruing debt. Upon execution, a small remainder of the debt asset (if any) is deposited into the user’s debt vault.
Supported aggregators and custom routes
Supported DEX aggregators and swap providers (alphabetically):
- 0x
- 1Inch
- Enso
- KyberSwap
- LI.FI
- Magpie
- Odos
- Oku
- OKX
- Ooga Booga
- OpenOcean
- ParaSwap
- Pendle
- Uniswap
For assets not well supported by aggregators, direct interaction strategies are available for:
- ERC4626 vaults
- Curve LP NG
- Midas m-tokens
- Idle CDO Tranches
For detailed routing configuration, please refer to per-chain config files.