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

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

This function executes a market fill intended to close an existing position for a given account. It attempts to match an opposing order from the order book based on the provided parameters. If the fillOrderType is FILL_OR_KILL, the entire requested amount must be filled or the transaction reverts. If it is IMMEDIATE_OR_CANCEL, it fills as much as possible and cancels the rest.

Inputs

  • asset

    • Control: Full control.

    • Constraints: Must be a valid, supported market.

    • Impact: Determines which asset the order is for.

  • account

    • Control: Full control.

    • Constraints: Must own the subaccount and have an active position in the selected market. Must match msg.sender or be authorized to act on behalf of the user.

    • Impact: Specifies whose position is being closed. Position and collateral changes will apply to this address.

  • args.amount

    • Control: Full control.

    • Constraints: Must be greater than zero — the amount of the order in base or quote units depending on amountIsBase.

    • Impact: The amount of the order to fill (in base or quote units depending on amountIsBase).

  • args.price

    • Control: Full control.

    • Constraints: For BUY, the maximum price the user is willing to pay. For SELL, the minimum price the user is willing to accept.

    • Impact: Prevents the order from filling at worse prices than the user expects.

  • args.side

    • Control: Full control.

    • Constraints: Must be opposite to the existing position side.

    • Impact: Determines the direction of the closing order.

  • args.amountIsBase

    • Control: Full control.

    • Constraints: Boolean. If true, the amount is in base asset — if false, in quote asset. Only amountIsBase is allowed.

    • Impact: Affects how the order amount is interpreted and matched.

  • args.fillOrderType

    • Control: Full control.

    • Constraints: Must be a valid enum value (FILL_OR_KILL, IMMEDIATE_OR_CANCEL). For FILL_OR_KILL, fills fully or reverts — or for IMMEDIATE_OR_CANCEL, partial fill is allowed.

    • Impact: Affects how the order is executed.

  • args.subaccount

    • Control: Full control.

    • Constraints: Must be owned by the account. Must have an open position in this market.

    • Impact: Identifies which subaccount's position will be closed.

  • args.leverage

    • Control: Full control.

    • Constraints: the margin per unit of quote amount should not less than the initMarginReq.

    • Impact: Leverage to apply if position is reopened.

  • 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 the protocol.

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

Branches and code coverage

Intended branches

  • Closes entire position (full match).

  • Closes partial position (partially filled).

  • side to close is correct.

  • Filled price is only below or equal to the provided price for BUY.

  • Filled price fills only above or equal to the provided price for SELL.

  • Fill order type FILL_OR_KILL fully fills.

  • Fill order type IMMEDIATE_OR_CANCEL partially fills.

  • No matching liquidity and IMMEDIATE_OR_CANCEL.

  • amountIsBase == true.

  • Realized PNL is correctly computed on close.

  • Partially closed position updates parameters of the position.

  • Fully closed position fully resets.

  • Settlement type is INSTANT.

  • Settlement type is ACCOUNT.

  • Margin is reduced proportionally on partial close.

  • Margin is fully released if position is closed.

  • Closes across multiple prices.

  • Protocol reserves are updated.

  • Closes a position close to liquidation.

Negative behavior

  • Closes with an amount greater than the position amount.

  • Closes a position that does not exist.

  • Incorrect side.

  • Fill order type FILL_OR_KILL partly fills.

  • There is no matching liquidity in the book and FILL_OR_KILL.

  • amount == 0.

  • Invalid asset.

  • price == 0.

  • Subaccount does not belong to account.

  • Position is already liquidated.

  • Order book contains only the user’s own opposite order.

  • Very large amount.

  • amountIsBase == false.

Zellic © 2025Back to top ↑