Abstract
This ERC proposes a standardized interface for on-chain swap liquidity. It defines methods to calculate price, swap tokens, retrieve trading limits, and query capabilities of liquidity pools.
The standard aims to simplify access to defi liquidity sources – to improve composability and make routing (solving), MEV capture (searching), and other defi functions easier.
Motivation
The number and variety of DEXs grew rapidly. However, the lack of a standard interface to DEX, and other defi, liquidity creates imporant challenges:
- Lost of duplicate effort —> Barriers to entry: Routers, searchers and solvers have to understand the internals of every liquidity source. Resulting in significant duplication of effort across teams. This also makes it hard for new entrants to compete with incumbents – leading to centralisation in routing, solving, searching and market making.
- Limits to DEX innovation: It's difficult for new DEXs to get order flow, as they must convince multiple solver, router, and searcher teams to integrate them.
- Risk of centralisation in defi: The ecosystem risks centralization as larger teams with more resources can more easily integrate new liquidity sources, potentially marginalizing smaller teams and new entrants.
By providing a standardized interface for liquidity sources, this ERC aims to:
- Foster innovation in DEX design by enabling new protocols to compete based on their merits rather than integration efforts. Allowing each DEX to take composability into their own hands.
- Significantly improve the efficiency and composability of the DeFi ecosystem.
- Reduce the barrier to entry for new solvers and routers.
Specification
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
Interface
Retrieves tokens for a given pool. This function is OPTIONAL. MUST return all tokens that are tradeable by the pool.
Rationale
A short overview of functions needed in order routing
This ERC aims to make it easy for anyone to discover and use defi liquidity. Especially solvers, searchers, and aggregators (here collectively termed “routers”).
Here are common and important pieces of different routing algorithms, that inspired the design of this ERC:Marginal Price:
The marginal price is the first derivative of the swap (amount out) function at a particular trade amount. The spot price, i.e. the current price, is the marginal price at trade amount 0. Having easy access to the marginal price at different trade amounts is essential to:
- Filter pools: Pools with the best spot price have the best "instantaneous swap rate" for the token pair – making them a good candidate to include in a swap route. Second derivatives help judge even better which pool will provide the highest amount out for the swap (area under the swap curve).
- Determine swap amounts: Marginal prices are also very good for sizing steps in iterative algorithms: Swap on the pool with the best marginal price until it reaches the marginal price of the second best pool, then split between both until you reach the marginal price of the third best pool etc.
- Curve interpolation: Solvers can also use marginal prices to construct analytical interpolations of price functions that might not have simple analytical representations. Sample at multiple points and then build an interpolation. Analytical interpolation are useful to find analytical solutions to trade and split amounts – before using iterative approaches for fine-tuning the splits.
- Price after swap equivalence: Besides discontinuous price functions and discrete gas costs, optimal solutions to route splitting must equalise the marginal prices of all pools included in the split.
- Convex optimisation: Framing the optimal routing problem as a convex optimisation problem, which has gained more popularity recently, also relies on fast and accurate access to marginal prices.
- From these uses in different routing approaches we derived the need for:
- price: A price function that can provide the spot price (marginal price at 0 amount in) and the marginal price after any trade amount.
- priceDerivative: Derivatives of marginal prices, i.e. second derivatives of the swap function, are very useful for choosing pools, and for analytically interpolating the swap function.
- swap to price: A direct analytical derivation of the trade amount needed to move the liquidity pool to a specific marginal price speeds up routers, by avoiding iterative queries to the price function.
Other rationale
The design of this interface aims to balance flexibility and standardization:
- Separation of price and swap functions: So you can query price without state changes.
- Optional price function: In some protocols, providing a spot price is not easy or not possible.
- Capabilities query: Enables dynamic discovery of pool features for flexible integration.
- Gas estimation: Including gas estimates in the swap function aids solvers and routers to route optimally.
- Optional functions for testing: The getTokens and getPoolIds functions simplify the testing process without being essential for core functionality.
Backwards Compatibility
This ERC is designed to be compatible with existing liquidity pool implementations. Pools can implement this interface alongside their existing interfaces without breaking changes.
Reference Implementation
[TODO: Add reference implementation]
Security Considerations
None of the above methods are supposed to be called onchain. This interface is purely for indexing liquidity and efficiently simulating over it's properties to arrive at the most efficient trade amount during solving.
It is an interface designed purely for reading. As such it presents to risk to the protocol.
Risk of faulty swap amounts
If the above functions are implemented incorrectly, and if solvers do not check for the expected amount out in their routers – then routers can unintentionally lose users funds by routing inefficiently.
Routers MUST verify expected out amounts from swaps against protocols to avoid this risk.
Copyright
Copyright and related rights waived via CC0.