Assessment reports>GTE -- Perp>Threat Model>_matchIncomingOrder

Function: _matchIncomingOrder(PerpBook ds, Order matchedOrder, Order incomingOrder, bool amountIsBase)

This internal function handles a single match between an incoming taker order and a matched maker order during the _matchIncomingBid or _matchIncomingAsk functions' execution. This function executes the maker fill logic and performs cleanup or updates to the matched maker order. This function prevents self-trade by removing an order if the maker is the same user.

Additionally, this function

  • calculates the matched baseDelta and quoteDelta,

  • executes maker-side fill logic,

  • updates or removes the maker order in the order book, and

  • returns the MatchData, which contains matchedAmount, baseDelta, quoteDelta, and takerFee.

Branches and code coverage

Intended branches

  • The order book contains only the opposite order of the same account. The order was removed, and matchedAmount, baseDelta, quoteDelta, and takerFee are zero.

  • matchedOrder is reduceOnly.

  • amountIsBase is true for the incomingOrder. The baseDelta, quoteDelta, and matchedAmount are calculated correctly.

  • amountIsBase is false for the incomingOrder. The baseDelta, quoteDelta and matchedAmount are calculated correctly.

  • When baseDelta > 0, the makerFee, maker’s position, and margin are updated correctly.

  • Fully matches matchedOrder, and it is removed from the order book.

  • Partially matches matchedOrder, and the amount is updated.

Zellic © 2025Back to top ↑