Matching Engine
An overview into nftperp v2's Fusion AMM Matching Engine
Last updated
An overview into nftperp v2's Fusion AMM Matching Engine
Last updated
The matching engine is the heart of any trading platform, determining how orders are executed and matched. In v2's Fusion AMM design, the matching engine plays a pivotal role in ensuring our traders get the best possible execution by intelligently routing orders either through the AMM or the DLOB.
The Fusion AMM's matching engine is designed to provide optimal order execution by determining the best path for each order type:
Market Orders: These orders are executed immediately at the best available price. When a market order is received, the matching engine evaluates the size of the order and the available liquidity in the orderbook. The engine then loops through the available liquidity and:
Fills the market order with the orderbook liquidity if matching limit orders are on the given price/liquidity tick.
If there is no matching liquidity on the price, the AMM liquidity for that price level is used.
This loop continues until the order is filled. A market order can be broken down into multiple sub-trades, which can be a combination of orderbook and AMM fills rather than strictly one or the other. The chart below visualizes the orderbook state combining AMM and Limit Orderbook in an Orderbook:
In a scenario in which there is a liquidity of 200 ETH in the AMM and some in the order book, they will both combine. The AMM will fill liquidity across all prices, as visualized in blue above, while the order book will have liquidity in price points where a maker puts their order (20.3 and 20.4 ETH visualized in black on the buy side above).
In such an order book state, A market sell order of size 25 ETH will match against 10 ETH on the limit orderbook at 20.4 ETH, then with 3 ETH on the AMM at 20.4 ETH, then 10 ETH of the limit order book at 20.3 ETH, and 2 ETH of AMM at 20.3 ETH. In this trade, the order book makers were the counterparty of 20 ETH, and AMM LPs were the counterparty of 5 ETH. As more orders take place, AMM LPs hold different positions. They may withdraw their liquidity at a given time and make their impermanent position permanent.
Limit Orders: These specify a price at which the trader is willing to buy or sell, placed on the DLOB. They are stored with a tick for a price (similar to Uniswap v3) and are executed when a matching market order or another limit order meets the specified price. Limit orders in the Fusion AMM are entirely on-chain, ensuring transparency and security.
Conditional Orders (Stop-Loss, Take-Profit): Conditional orders are special orders executed when certain market conditions are met. Conditional orders are treated like market orders and are matched with the DLOB or AMM based on the matching engine's logic.
Order types are covered more extensively here.
Conditional orders are executed on-chain by a network of keepers when the price moves and the order conditions are met. Of note, conditional orders are charged an extra 5 bps fee, with 5 bps going to the keeper that executes the order.
If you are interested in becoming a keeper, head to the Developer Section.