Fermi DEX- Docs
  • ๐Ÿ‘‹Intro to Fermi
  • Overview
    • ๐Ÿ‘๏ธโ€๐Ÿ—จ๏ธVision
    • ๐Ÿ’กWhat we do
    • โœจFermi DEX: Features
    • ๐Ÿˆโ€โฌ›Background
  • Fermi DEX Usage Guide
    • Fermi DEX: How it works
      • ๐Ÿ“ชPosting a Limit Order
      • โœ…Finalizing Orders
      • ๐Ÿ”„Withdrawing Funds
    • ๐Ÿ› ๏ธGetting set up
      • ๐Ÿ“Getting tokens
      • ๐Ÿ”ซTrobleshooting
    • ๐ŸชTransfer Hooks Redefined
    • Custom settlement periods
  • Technical Overview
    • Overview v0.2 (DEPRECATED)
    • Technical Overview v1
      • Market Addresses (Devnet)
      • Key Structs
      • Instructions
      • Events
      • SDK
    • ๐ŸšงArchitecture
      • ๐Ÿ‘ฝJust In Time Liquidity
      • ๐ŸคMatching Logic
      • ๐Ÿ›ธLiquidity Abstraction
      • ๐ŸŒŠLiquidity Management Programs
    • ๐ŸงชTesting Locally
    • ๐Ÿ•น๏ธDemo - Fermi v0.2
    • ๐Ÿ“ฝ๏ธDemo - Fermi v1
    • ๐Ÿ“‡Github, Devnet Programs and PDAs
    • ๐Ÿ‘ฉโ€๐ŸŽคActors and Incentives
  • Use cases & Benefits
    • ๐Ÿ–‡๏ธLower Slippage than traditional DEXes
    • โ›๏ธPortfolio Margining Strategies
    • ๐ŸMarket Making on Fermi DEX
  • About us
    • ๐Ÿ‘‹Meet the Team
    • ๐Ÿ›ฃ๏ธRoadmap
    • ๐Ÿš“Next Steps
Powered by GitBook
On this page
  • Order stages
  • Object flow diagram
  • Accounting
  • Technical Notes
  1. Technical Overview

Architecture

The architecture of Fermi DEX resembles Openbook/Serum at a high level, with many fundamental changes - such as updated internal accounting, PDA structure and ownership.

PreviousSDKNextJust In Time Liquidity

Last updated 1 year ago

Fermi DEX's on-chain programs are built from a heavily modified, anchor-compatible fork of Serum. To enable all the new capital efficiency oriented features, the internal accounting was redesigned from the ground up. On this page we provide a high level overview, and we dive into specific concepts in the following pages.

Order stages

An order placed on Fermi DEX follows a slightly different path than an order on a traditional on-chain CLOB, like OpenBook. In some ways, the JIT liquidity abstraction mechanisms provide a similar user feel to centralised exchanges, however, Fermi DEX executes entirely on decentralised rails.

As shown, newly placed orders are stored in the Orderbook (i.e., bids and asks PDA). When a match is found (see Matching Logic), the order is added to the eventQueue, with the "filled" status. Thereafter, any user may choose to atomically finalise a filled transaction in the eventbook, using finaliseMatch.

At this stage, just-in-time liquidity is debited from both counterparties to the trade. If successful, the traded balances are credited to the parties, and the trade is changed to a "finalised" status.

Object flow diagram

The following diagram shows the flow of an order in a bit more detail. As seen, limit orders are first tested against the best offer from the takers. If it matched, the trade moves directly to the Filled stage, bypassing the orderbook. Otherwise, the order (or any unfilled portion of the order) is posted on the orderbook, to be matched against future orders.

Accounting

At the order placement stage, the program checks whether you have enough liquidity to fill this trade, were it matched immediately. This check is performed against a cross-market abstracted total of liquidity that you have supplied to the protocol so far. If liquidity supplied is insufficient, the program will attempt to "approve" the required liquidity from your wallet. Later, if the trade is filled, either counterparty can call "finaliseBid" and finaliseAsk" to make the just-in-time transfers from both counterparties. If the trade is finalised, the traded tokens will instead be available for immediate withdrawal. If one of the counterparties' fails to supply liquidity, the others funds will be unlocked and available for instant withdrawal. Thus, there is no period during which any deposited liquidity is inaccessible on Fermi DEX.

Note: Since matching is not a complete guarantee of trade finalisation, orders are NOT popped from the orderbook if matched, but their status is set to filled. Only upon order finalisation are the orders removed from the orderbook. This means, you may see crossed bids and asks in the orderbooks - they indicate there are matched orders which are still pending finalisation.

Technical Notes

Here we discuss a few interesting quirks of designing a JIT based orderbook system. Feel free to skip if this background doesn't interest you. From a birds eye perspective, here are some of the key changes that enable JIT and liquidity abstractions:

  • Updated NewOrder method, and underlyingnew_bid , new_ask methods under the Orderbook implementation. Significant changes to the internal accounting were made to enable JIT. In addition to interactions with the updated OO accounts, we ensured that any deposits update the unlocked balances, instead of the locked balances.

  • New FinaliseOrder Method. Analogous to 'cranking' in Openbook, FinaliseOrder is the step in the trade lifecycle where Just-In-Time liquidity is implemented. It accepts two events with the same orderId, and atomically executes the match by JIT'ing liquidity from both counterparties to each other's OOAbs balances.

Further, we will also implement ZeroCopy on the following accounts: EventsQueue, Bids, Asks, OO : To scale the amount of information these queues hold, we implemented zero-copy. We dive deeper on how each of these changes work together to produce the features of Fermi DEX in the following sections.

๐Ÿšง
๐ŸคMatching Logic
๐Ÿ‘ฝJust In Time Liquidity
๐Ÿ›ธLiquidity Abstraction
Simplified schematic of order stages
What happens when you place an order on Fermi DEX?