Skip to content
LogoLogo

MatchingEngine

Git Source

Title: MatchingEngine

Standalone price-time matching harness. It maintains a radix-indexed limit book and emits deterministic fill instructions.

Algorithm and gas benchmark only: it does not custody or settle tokens. Live orders settle through the AsyncSwap hook and AsyncMatching.

Constants

PRICE_SCALE

uint128 public constant PRICE_SCALE = 1e18;

MAX_MATCHES_PER_CALL

uint8 public constant MAX_MATCHES_PER_CALL = 8;

State Variables

books

mapping(bytes32 market => Book) private books;

entered

bool private entered;

Functions

nonReentrant

modifier nonReentrant();

placeOrder

Submit a limit order. Crossing orders match immediately, up to eight fills; any remainder rests at its limit price.

function placeOrder(bytes32 market, bool isBuy, uint128 amount, uint128 price)
        external
        nonReentrant
        returns (uint64 orderId);

Parameters

NameTypeDescription
marketbytes32Identifier for a pair and settlement domain chosen by the caller.
isBuyboolTrue for a bid, false for an ask.
amountuint128Base quantity in the engine's fixed-point units.
priceuint128Quote per base, scaled by 1e18.

Returns

NameTypeDescription
orderIduint64

matchBook

Continue clearing a crossed book after an order hit its per-call fill cap. Work is bounded by maxMatches and anyone may call it.

function matchBook(bytes32 market, uint8 maxMatches) external nonReentrant returns (uint8 matched);

Parameters

NameTypeDescription
marketbytes32
maxMatchesuint8

Returns

NameTypeDescription
matcheduint8

cancelOrder

function cancelOrder(bytes32 market, uint64 orderId) external nonReentrant;

Parameters

NameTypeDescription
marketbytes32
orderIduint64

getOrder

function getOrder(bytes32 market, uint64 orderId) external view returns (Order memory);

Parameters

NameTypeDescription
marketbytes32
orderIduint64

Returns

NameTypeDescription
<none>Order

bestPrice

function bestPrice(bytes32 market, bool isBuy) external view returns (bool found, uint128 price);

Parameters

NameTypeDescription
marketbytes32
isBuybool

Returns

NameTypeDescription
foundbool
priceuint128

levelOrderCount

function levelOrderCount(bytes32 market, bool isBuy, uint128 price) external view returns (uint32);

Parameters

NameTypeDescription
marketbytes32
isBuybool
priceuint128

Returns

NameTypeDescription
<none>uint32

_matchIncoming

function _matchIncoming(Book storage book, bytes32 market, uint64 incomingId) private returns (uint8 matched);

Parameters

NameTypeDescription
bookBook
marketbytes32
incomingIduint64

Returns

NameTypeDescription
matcheduint8

_fill

function _fill(Book storage book, bytes32 market, uint64 makerId, uint64 takerId, uint128 quantity, uint128 price)
        private;

Parameters

NameTypeDescription
bookBook
marketbytes32
makerIduint64
takerIduint64
quantityuint128
priceuint128

_clearBestPair

function _clearBestPair(Book storage book, bytes32 market) private returns (bool);

Parameters

NameTypeDescription
bookBook
marketbytes32

Returns

NameTypeDescription
<none>bool

_clearOrders

function _clearOrders(Book storage book, bytes32 market, uint64 bidId, uint64 askId) private;

Parameters

NameTypeDescription
bookBook
marketbytes32
bidIduint64
askIduint64

_append

function _append(Book storage book, uint64 orderId) private;

Parameters

NameTypeDescription
bookBook
orderIduint64

_remove

function _remove(Book storage book, uint64 orderId) private;

Parameters

NameTypeDescription
bookBook
orderIduint64

Events

OrderPlaced

event OrderPlaced(
        bytes32 indexed market,
        uint64 indexed orderId,
        address indexed owner,
        bool isBuy,
        uint128 amount,
        uint128 price,
        uint64 sequence
    );

Parameters

NameTypeDescription
marketbytes32
orderIduint64
owneraddress
isBuybool
amountuint128
priceuint128
sequenceuint64

OrderCancelled

event OrderCancelled(bytes32 indexed market, uint64 indexed orderId, address indexed owner);

Parameters

NameTypeDescription
marketbytes32
orderIduint64
owneraddress

OrderFilled

event OrderFilled(
        bytes32 indexed market,
        uint64 indexed makerOrderId,
        uint64 indexed takerOrderId,
        uint128 amount,
        uint128 price,
        uint256 quoteAmount
    );

Parameters

NameTypeDescription
marketbytes32
makerOrderIduint64
takerOrderIduint64
amountuint128
priceuint128
quoteAmountuint256

Errors

ZeroAmount

error ZeroAmount();

ZeroPrice

error ZeroPrice();

InvalidMatchBound

error InvalidMatchBound();

NotOrderOwner

error NotOrderOwner();

OrderNotFound

error OrderNotFound();

ReentrantCall

error ReentrantCall();

Structs

Order

struct Order {
        address owner;
        uint128 price;
        uint128 remaining;
        uint64 sequence;
        uint64 previous;
        uint64 next;
        bool isBuy;
        bool active;
    }

Properties

NameTypeDescription
owneraddress
priceuint128
remaininguint128
sequenceuint64
previousuint64
nextuint64
isBuybool
activebool

Level

struct Level {
        uint64 head;
        uint64 tail;
        uint32 orderCount;
    }

Properties

NameTypeDescription
headuint64
tailuint64
orderCountuint32

Book

struct Book {
        uint64 nextOrderId;
        uint64 nextSequence;
        RadixPriceTree.Tree bids;
        RadixPriceTree.Tree asks;
        mapping(uint128 price => Level) bidLevels;
        mapping(uint128 price => Level) askLevels;
        mapping(uint64 orderId => Order) orders;
    }

Properties

NameTypeDescription
nextOrderIduint64
nextSequenceuint64
bidsRadixPriceTree.Tree
asksRadixPriceTree.Tree
bidLevelsmapping(uint128 price => Level)
askLevelsmapping(uint128 price => Level)
ordersmapping(uint64 orderId => Order)