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

Function: postFillOrder(byte[32] asset, address account, PostFillOrderArgs args)

This function attempts to open or adjust a position by executing an order. It matches the order against existing orders in the order book using the specified parameters. The user can specify how much to buy or sell, at what price limit, and whether to allow partial fills.

If the order is filled, the user's position is updated: either opened, increased, or reduced (if opposite direction). The function supports both FILL_OR_KILL (fills fully or reverts) and IMMEDIATE_OR_CANCEL (partially fills).

Inputs

  • asset

    • Control: Full control.

    • Constraints: Market should exist.

    • Impact: Determines which asset market the fill operation applies to.

  • account

    • Control: Full control.

    • Constraints: The caller should be the account itself or an approved operator.

    • Impact: Position, margin, and balance updates are applied to this account.

  • args.amount

    • Control: Full control.

    • Constraints: Must be greater than zero.

    • Impact: Sets how much the user wants to buy or sell.

  • args.price

    • Control: Full control.

    • Constraints: Must be greater than zero, price % tickSize == 0.

    • Impact: Defines the worst price the user is willing to accept for the trade.

  • args.amountIsBase

    • Control: Full control.

    • Constraints: Boolean — affects interpretation of args.amount.

    • Impact: Defines whether amount is in base asset units or quote.

  • args.fillOrderType

    • Control: Full control.

    • Constraints: Must be valid enum value.

    • Impact: Sets how the order should be filled, either completely or partially.

  • args.subaccount

    • Control: Full control.

    • Constraints: N/A.

    • Impact: Determines which subaccount to apply the trade to.

  • args.leverage

    • Control: Full control.

    • Constraints: (1e18 * 1e18) / leverage should be less than settings.initMarginReq.

    • Impact: Controls how much borrowed funds are used — affects margin and liquidation risk.

  • args.settlement

    • Control: Full control.

    • Constraints: Must be a valid settlement type (INSTANT, ACCOUNT). For settlement INSTANT, immediately returns, or for ACCOUNT, keeps funds inside protocol.

    • Impact: Defines where the resulting funds go after the position is closed.

Branches and code coverage

Intended branches

  • Opens a new long position using BUY.

  • Opens a new short position using SELL.

  • Increases an existing long.

  • Increases an existing short.

  • Decreases a position by filling with the opposite side (partial close).

  • Fully closes a position with an opposite-side fill.

  • Opens a new position after closing the previous one.

  • Executes fill against multiple matching maker orders.

  • IMMEDIATE_OR_CANCEL partially fills.

  • Position updates correctly on increase.

  • Position decreases correctly.

  • Position amount and openNotional update correctly on open.

  • Realized PNL is correctly applied on partial close.

  • Full close resets the position to zero and cleans storage.

  • Margin is correctly calculated.

  • Funding is settled before fill occurs.

  • Reverses open with exact 2× amount.

  • Opens new position with opposite side.

  • Reverses open from long to short with amountIsBase == false.

  • Realized PNL is correctly calculated for the closed part of the position.

  • Margin is released correctly from the closed position before applying to the new one.

  • Reverses open. Price and openNotional are recalculated correctly for the new position.

  • Fees are charged properly.

Negative behavior

  • Price deviates too much from the current fair market value.

  • price == 0.

  • asset is not supported.

  • price % tickSize != 0.

  • FILL_OR_KILL is partly filled.

  • FILL_OR_KILL is not filled at all — no orders.

  • Reverts if reverse open causes undercollateralization.

  • For BUY, order book contains only higher prices.

  • For SELL, order book contains only smaller prices.

  • User does not have enough funds to post margin.

  • Order book is empty.

Zellic © 2025Back to top ↑