# Clearinghouse

All basic exchange functions are kept in the clearinghouse contract. This includes open and close position, and liquidation functions.

{% hint style="info" %}
Find parameters like `marketID` in [Contracts](/nekodex-playground/docs-for-devs/contracts.md#metadata)
{% endhint %}

## `openPosition`

Open a position or update the size of an existing position (you can only have one position in a given asset).

#### Parameters

<pre class="language-solidity"><code class="lang-solidity">struct OpenPositionParams {
        uint256 <a data-footnote-ref href="#user-content-fn-1">marketId</a>;
        address maker; // Maker type, e.g. spotHedgeMaker
        bool isBaseToQuote;
        bool isExactInput;
        uint256 amount;
        uint256 oppositeAmountBound; // Control slippage
        uint256 deadline; // Timestamp
        bytes makerData; // @param makerData Encoded calls are custom data defined by maker
    }
</code></pre>

#### Returns

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

## `closePosition`

Close a position. Note you can also close using `openPosition` and opening an equal size position of the opposite direction (e.g. long closes short).

#### Parameters

```solidity
struct ClosePositionParams {
        uint256 marketId;
        address maker;
        uint256 oppositeAmountBound;
        uint256 deadline;
        bytes makerData;
    }
```

#### Returns

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

## `liquidate`

Liquidate a position. Contract will test the position for eligibility before liquidation.

#### Parameters

```solidity
struct LiquidatePositionParams {
        uint256 marketId;
        address liquidator;
        address trader;
        uint256 positionSize;
    }
```

#### Returns

```solidity
returns (int256 liquidatedAccountBaseDelta, int256 liquidatedAccountQuoteDelta)
```

#### Event

```solidity
event Liquidated(
        uint256 indexed marketId,
        address indexed liquidator,
        address indexed trader,
        int256 positionSizeDelta,
        int256 positionNotionalDelta,
        uint256 price,
        uint256 penalty,
        uint256 liquidationFeeToLiquidator,
        uint256 liquidationFeeToProtocol
    )
```

## `setAuthorization`

Delegate open and close permissions; delegation is currently limited to order gateway contracts only.

## `isLiquidatable`

#### Parameters

```solidity
struct isLiquidatableParams {
        uint256 marketId;
        address trader;
        uint256 price;
    }
```

#### Returns

```solidity
bool
```

[^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/clearinghouse.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.
