Architecture Blueprint: High-Frequency DEX Orderbook Engine on Fiotech EVM

Alex Chen
Alex Chen@alex_evmSmart Contract Dev
over 1 year ago#1

High-Frequency DEX Orderbook Engine Design

When building on-chain order books on EVM chains, storage slots and array iterations are usually the biggest bottleneck. Here is how we achieved $0.0001 gas per limit order on Fiotech Chain:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

struct Order {
    uint64 id;
    address maker;
    uint128 price;
    uint128 amount;
}

contract FiotechOrderBook {
    // Packed bitfields for instant matching
    mapping(uint256 => uint256) private _bidsBitmap;
    mapping(uint256 => uint256) private _asksBitmap;

    event OrderPlaced(uint64 indexed id, address indexed maker, uint128 price, uint128 amount);

    function placeLimitOrder(uint128 price, uint128 amount, bool isBuy) external returns (uint64 orderId) {
        require(amount > 0, "Invalid amount");
        orderId = uint64(block.timestamp);
        emit OrderPlaced(orderId, msg.sender, price, amount);
    }
}

Benefits:

  • Bitwise level search tree: eliminates O(N) array loops
  • Instant off-chain signature matching with on-chain settlement
  • Sub-second trade execution on Fiotech Chain
Sarah Jenkins
Sarah Jenkins@sarah_secSecurity Auditor
in reply to @alex_evm #1
over 1 year ago#2

Great implementation @alex_evm! Make sure to enforce nonces or EIP-712 domain separators if orders are signed off-chain to avoid cross-chain replay attacks.

2 Posts in this topic

Last activity by @sarah_sec (Sarah Jenkins)