# Limit Orders

The `LimitOrders.sol` contract is a settlement contract for executing orders from our off-chain limit order service. Users approve the contract to spend their tokens and provide signatures of their `Order` to an off-chain API. An order execution bot monitors markets and executes them against this contract when allowed. Orders are executed on the contract by providing the Order parameters and an EIP712 Typed Data Signature of the Order, using the contracts domain information, to the `executeOrder` function.  The functions to execute orders on the contract are permissioned in the initial version as a security precaution.

## Data Structures

```solidity
struct Order {
    uint256 marketId;
    address recipient;
    address referrer;
    uint256 amount;
    uint256 minAmountOut;
    uint256 maxFee;
    uint256 submitted;
    uint256 deadline;
    address user;
}
```

| Field        | Type    | Description                                                                                                                                                                                                            |
| ------------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| marketId     | uint256 | ID of the market that the order is for                                                                                                                                                                                 |
| recipient    | address | Address that will receive the payout of the order                                                                                                                                                                      |
| referrer     | address | Referrer address for the order. May receive a fee if set on the Teller contract for the market.                                                                                                                        |
| amount       | uint256 | Amount of quote tokens to spend on the order, in quote token decimals                                                                                                                                                  |
| minAmountOut | uint256 | Minimum amount of payout tokens to receive from the order, in payout token decimals                                                                                                                                    |
| maxFee       | uint256 | Maximum fee in the quote token that the user is willing to process the order, in quote token decimals                                                                                                                  |
| submitted    | uint256 | Timestamp that the order was submitted at. Used by the off-chain service to give priority to equivalent orders. API will not accept an order where the submitted date is a certain amount older than the current time. |
| deadline     | uint256 | Timestamp that the order is valid until. The order will not be executable after this time.                                                                                                                             |
| user         | address | Address that the order proceeds will come from and that is required to sign the order.                                                                                                                                 |

```solidity
enum Status {
    Open,
    Executed,
    Cancelled
}
```

## Public Variables and View Methods

#### DOMAIN\_SEPARATOR

```solidity
function DOMAIN_SEPARATOR() external view returns (bytes32)
```

The current domain separator hashed used to verify EIP712 signatures.

#### aggregator

```solidity
function aggregator() external view returns (contract IBondAggregator)
```

The Bond Aggregator contract (i.e. top-level contract of the bond system) that this settlement contract supports.

#### authority

```solidity
function authority() external view returns (contract Authority)
```

The Authority contract that determine access to permissioned functions on the contract.

#### chainId

```solidity
function chainId() external view returns (uint256)
```

Currently cached value for the chain ID.

#### getDigest

```solidity
function getDigest(LimitOrders.Order order_) external view returns (bytes32)
```

Returns the hash digest of the provided order. Used to calculate a unique order identifier and verify EIP712 signatures of Orders.

#### orderStatus

```solidity
function orderStatus(bytes32) external view returns (enum LimitOrders.Status)turns
```

Returns the `Status` of an order (identified by its hash digest).

## State-Mutating Methods

#### cancelOrder

```solidity
function cancelOrder(LimitOrders.Order order_) external nonpayable
```

Cancels an order on the settlement contract. A cancelled order cannot be executed, even by a permissioned executor.

**Parameters**

| Name    | Type              | Description                                                       |
| ------- | ----------------- | ----------------------------------------------------------------- |
| order\_ | LimitOrders.Order | Order parameters. Specified in the Data Structures section above. |

#### executeOrder

```solidity
function executeOrder(LimitOrders.Order order_, bytes signature_, uint256 fee_) external nonpayable
```

Execute a single order. Permissioned.

**Parameters**

| Name        | Type              | Description                                                                                                                                                                               |
| ----------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| order\_     | LimitOrders.Order | Order parameters. Specified in the Data Structures section above.                                                                                                                         |
| signature\_ | bytes             | ECDSA signature of the EIP712 hash digest of the `order_` by `order_.user`                                                                                                                |
| fee\_       | uint256           | Amount of quote tokens to charge as a fee. Must be less than or equal to `order_.maxFee`. Determined by the executor based on network gas fees and token prices at the time of execution. |

#### executeOrders

```solidity
function executeOrders(LimitOrders.Order[] orders_, bytes[] signatures_, uint256[] fees_) external nonpayable
```

Execute multiple orders in one transaction. Permissioned.

**Parameters**

| Name         | Type                 | Description                                                                                                                                                                                                                   |
| ------------ | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| orders\_     | LimitOrders.Order\[] | Array of Order parameters. Specified in the Data Structures section above.                                                                                                                                                    |
| signatures\_ | bytes\[]             | Array of ECDSA signatures of the EIP712 hash digest of the corresponding `order_` by `order_.user`                                                                                                                            |
| fees\_       | uint256\[]           | Array of quote token amounts to charge as a fee on the corresponding `order_`. Must be less than or equal to `order_.maxFee`. Determined by the executor based on network gas fees and token prices at the time of execution. |

#### reinstateOrder

```solidity
function reinstateOrder(LimitOrders.Order order_) external nonpayable
```

Reinstates a previously cancelled order.

**Parameters**

| Name    | Type              | Description |
| ------- | ----------------- | ----------- |
| order\_ | LimitOrders.Order | undefined   |

#### updateDomainSeparator

```solidity
function updateDomainSeparator() external nonpayable
```

Updates the cached domain separator in the event that the chainId changes. Saves gas on future digest computations.

## Events

#### AuthorityUpdated

```solidity
event AuthorityUpdated(address indexed user, contract Authority indexed newAuthority)
```

**Parameters**

| Name                   | Type               | Description |
| ---------------------- | ------------------ | ----------- |
| user `indexed`         | address            | undefined   |
| newAuthority `indexed` | contract Authority | undefined   |

#### OrderCancelled

```solidity
event OrderCancelled(bytes32 digest)
```

**Parameters**

| Name   | Type    | Description |
| ------ | ------- | ----------- |
| digest | bytes32 | undefined   |

#### OrderExecuted

```solidity
event OrderExecuted(bytes32 digest)
```

**Parameters**

| Name   | Type    | Description |
| ------ | ------- | ----------- |
| digest | bytes32 | undefined   |

#### OrderReinstated

```solidity
event OrderReinstated(bytes32 digest)
```

**Parameters**

| Name   | Type    | Description |
| ------ | ------- | ----------- |
| digest | bytes32 | undefined   |

#### OwnerUpdated

```solidity
event OwnerUpdated(address indexed user, address indexed newOwner)
```

**Parameters**

| Name               | Type    | Description |
| ------------------ | ------- | ----------- |
| user `indexed`     | address | undefined   |
| newOwner `indexed` | address | undefined   |

## Errors

#### LimitOrders\_AlreadyExecuted

```solidity
error LimitOrders_AlreadyExecuted(bytes32 digest)
```

**Parameters**

| Name   | Type    | Description |
| ------ | ------- | ----------- |
| digest | bytes32 | undefined   |

#### LimitOrders\_InvalidFee

```solidity
error LimitOrders_InvalidFee(uint256 fee, uint256 maxFee)
```

**Parameters**

| Name   | Type    | Description |
| ------ | ------- | ----------- |
| fee    | uint256 | undefined   |
| maxFee | uint256 | undefined   |

#### LimitOrders\_InvalidParams

```solidity
error LimitOrders_InvalidParams()
```

#### LimitOrders\_InvalidSignature

```solidity
error LimitOrders_InvalidSignature(bytes signature)
```

**Parameters**

| Name      | Type  | Description |
| --------- | ----- | ----------- |
| signature | bytes | undefined   |

#### LimitOrders\_InvalidUpdate

```solidity
error LimitOrders_InvalidUpdate()
```

#### LimitOrders\_InvalidUser

```solidity
error LimitOrders_InvalidUser()
```

#### LimitOrders\_MarketClosed

```solidity
error LimitOrders_MarketClosed(uint256 marketId)
```

**Parameters**

| Name     | Type    | Description |
| -------- | ------- | ----------- |
| marketId | uint256 | undefined   |

#### LimitOrders\_NotAuthorized

```solidity
error LimitOrders_NotAuthorized()
```

#### LimitOrders\_OrderCancelled

```solidity
error LimitOrders_OrderCancelled(bytes32 digest)
```

**Parameters**

| Name   | Type    | Description |
| ------ | ------- | ----------- |
| digest | bytes32 | undefined   |

#### LimitOrders\_OrderExpired

```solidity
error LimitOrders_OrderExpired(uint256 deadline)
```

**Parameters**

| Name     | Type    | Description |
| -------- | ------- | ----------- |
| deadline | uint256 | undefined   |


---

# 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://dev.bondprotocol.finance/smart-contracts/bond-system/limit-orders.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.
