tunnl
  • Introduction
  • Roadmap
  • Orderbook Exchange
    • Getting Started
    • Userflow
    • UI/UX
    • Matching Engine and Orderbook
    • On-Chain Execution
    • Margin and Shorts
    • Oracles
    • Decentralized Leverage
    • Liquidations
    • Interest and Lending Rate
    • Fees
    • Token Details
  • Aggregated USDC
  • Aggregated Swaps
    • Getting Started
    • Fees
  • MPC Vaults
  • TUN Token
    • Profit Share and Buy Back
    • Token Allocations
    • Token Emissions
    • Governance
Powered by GitBook
On this page
  • Matching Logic
  • Balance Checks
  1. Orderbook Exchange

Matching Engine and Orderbook

PreviousUI/UXNextOn-Chain Execution

Last updated 2 months ago

When you place an order on the front end it is immediately pushed through to our Matching Engine. The Matching Engine is responsible for matching trades that come in, checking appropriate balances and trade parameters, and placing orders into the order book if there are no trades that match.

The matching engine is also responsible for additional security checks and procedures.

Matching Logic

When an order comes into the matching engine the order parameters will tell the matching engine what side of the book the trade is in (Buy or Sell), what type of trade it is (Market, Limit, or Stop), if the trade is a regular spot trade, if its a short or on margin, the size of the trade and the price of the trade.

Depending on all of the parameters the trade will go through a variety of different checks, balances, and procedures.

Balance Checks

On every order, the Matching Engine will check each user's asset balance in the smart contract to ensure that they have the available balance to complete that trade. In the case of , the matching engine will instead check the "Total Portfolio Balance", "Initial Margin Requirement", and "Aggregate Maintenance Margin Requirement" parameters.

Only after all of these parameters pass will a trade be matched. All matched orders will be batched together and subsequently passed to the to execute in one trade.

Margin and Shorts
Exchange smart contract