# Order Gateway

## Overview

The order gateway has two contracts

* DelayedOrderGateway
* orderGatewayV2 (used by front end and [API](/nekodex-playground/docs-for-devs/api.md))

DelayedOrderGateway is covered here; orderGatewayV2 is limited to use by the Perp v3 frontend and API (no external functions for direct contract interaction).

The order gateways have two key roles:

1. Route orders to the optimal liquidity source
2. Prevent trades from front-running the oracle using a 3 second delay

## Workflow

1. Call `createOrder()` to generate an order, including `createdAt` and `executableAt` timestamps
2. Wait for `executableAt()` timestamp
3. Call `executeOrder()` with `orderId` to execute a trade
4. Call `cancelOrder()` with `orderID` to cancel an unexecuted order

## Contract

<pre class="language-solidity" data-overflow="wrap"><code class="lang-solidity">struct DelayedOrder {
    DelayedOrderType orderType; // Types: OpenPosition or ClosePosition
    address sender; // Sender address
    uint256 <a data-footnote-ref href="#user-content-fn-1">marketId</a>; // See annotation
    uint256 createdAt; // block.timestamp
    uint256 executableAt; // block.timestamp + orderDelaySeconds
    bytes data;
}
</code></pre>

{% code overflow="wrap" %}

```solidity
/// @notice Create an order (but do not execute)
function createOrder(
    DelayedOrderType orderType, 
    bytes calldata data
) 

emit OrderCreated(
    orderId, 
    delayedOrder.sender, 
    marketId, 
    abi.encode(delayedOrder)
);
```

{% endcode %}

{% code overflow="wrap" %}

```solidity
/// @notice Execute an order (probably first call createOrder)
function executeOrder(
        uint256 orderId,
        bytes calldata makerData
)

returns (
        int256 base, 
        int256 quote
);
```

{% endcode %}

<pre class="language-solidity" data-overflow="wrap"><code class="lang-solidity"><strong>/// @notice Cancel unexecuted order
</strong><strong>function cancelOrder(
</strong>    uint256 orderId
)

emit OrderCanceled(
    orderId
);
</code></pre>

```solidity
/// @notice There are many public view functions available
function getCurrentNonce() public view returns (uint256) {
        return _getOrderGatewayStorage().nonce;
}

function getOrdersCount() public view returns (uint256) {
    return _getOrderGatewayStorage().orderIds.length();
}

function getOrderIds(uint256 start, uint256 end) public view returns (uint256[] memory) {
    return _getOrderGatewayStorage().orderIds.valuesAt(start, end);
}

function getUserOrdersCount(address taker) public view returns (uint256) {
    return _getOrderGatewayStorage().userOrderIdsMap[taker].length();
}

function getUserOrderIds(address taker, uint256 start, uint256 end) public view returns (uint256[] memory) {
    return _getOrderGatewayStorage().userOrderIdsMap[taker].valuesAt(start, end);
}

function getOrder(uint256 orderId) public view returns (DelayedOrder memory) {
    return _getOrderGatewayStorage().ordersMap[orderId];
}
```

[^1]: See [Contracts](/nekodex-playground/docs-for-devs/contracts.md#metadata)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nekodex.org/nekodex-playground/docs-for-devs/contracts/order-gateway.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
