# Orderbook

## Overview

The `Orderbook` contains modified order-book functionality, matching lenders to borrower within the `BloomPool` .

## Contract API

### `openDepth`

```solidity
function openDepth(uint256 amount) external;
```

Returns the current total depth of open orders in terms of `asset` tokens.

### `amountOpen`

```solidity
function amountOpen(address account) external view returns (uint256);
```

Returns the total amount of underlying `asset`s in open orders for a users `account`.

### `lendOrder`

```solidity
function lendOrder(uint256 amount) external;
```

Opens a lend order for a user.

{% hint style="info" %}
Users have the right at anytime to cancel their lend order and withdraw their assets.
{% endhint %}

{% hint style="warning" %}
Assets will be transferred to the `BloomPool` contract when executing this function, but no receipt token will be given. `TBY`s are only minted after orders have been matched and market makers have performed their swaps.
{% endhint %}

### `killOpenOrder`

```solidity
function killOpenOrder(uint256 amount) external;
```

Allows users to cancel their open lend order and withdraw their underlying assets.

### `matchedDepth`

```solidity
function matchedDepth() external view returns (uint256);
```

Returns the current total depth of matched orders.

### `amountMatched`

```solidity
function amountMatched(address account) external view returns (uint256 amount);
```

Returns the total `amount` of underlying assets in matched orders for a users `account`.

### `matchedOrder`

```solidity
function matchedOrder(address account, uint256 index) external view returns (MatchOrder memory);
```

Returns the matched order details for a users `account` in the form of a `MatchOrder` struct.&#x20;

```solidity
/**
 * @notice Struct to store the details of a lend order that has been matched.
 * @param lCollateral The amount of underlying assets the lender used as collateral.
 * @param bCollateral The amount of underlying assets the borrower used as collateral.
 * @param borrower The address of the borrower who filled the order.
*/
struct MatchOrder {
   uint128 lCollateral;
   uint128 bCollateral;
   address borrower;
}
```

### `matchedOrderCount`

```solidity
function matchedOrderCount(address account) external view returns (uint256);
```

Returns the number of matched orders for a users `account`.

### `killMatchOrder`

```solidity
function killMatchOrder(uint256 amount) external returns (uint256 totalRemoved);
```

Allows users to cancel their match lend orders and withdraw their underlying assets.&#x20;

Returns the `totalRemoved` amount in terms of `asset` tokens.

### `killBorrowerMatch`

```solidity
function killBorrowerMatch(address lender) external returns (uint256 lenderAmount, uint256 borrowerReturn);
```

Allows borrowers to cancel their match orders and withdraw their underlying assets

Returns the `lenderAmount` and `borrowerReturn` in terms of underlying assets that were returned to the various users.

### `fillOrder`

{% hint style="info" %}
This is a permissioned function. Only KYCed borrowers can call this function.
{% endhint %}

```solidity
function fillOrder(address account, uint256 amount) external returns (uint256 filled);
```

Allows borrowers to fill a single `account`s lend order with a specified `amount` of underlying assets.

Returns the amount of liquidity filled in terms of `asset` tokens.

### `fillOrders`

{% hint style="info" %}
This is a permissioned function. Only KYCed borrowers can call this function.
{% endhint %}

```solidity
function fillOrders(address[] calldata accounts, uint256 amount) external returns (uint256 filled);
```

Allows borrowers to fill a multiple `account`s lend orders with a specified `amount` of underlying assets.

Returns the amount of liquidity filled in terms of `asset` tokens.

### `idleCapital`

```solidity
function idleCapital(address account) external view returns (uint256);
```

Returns the idle capital of the borrower.

### `withdrawIdleCapital`

```solidity
function withdrawIdleCapital(uint256 amount) external;
```

Allows borrowers to withdraw idle capital from the system.


---

# 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.bloom.garden/technical-docs/smart-contracts/bloompool/orderbook.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.
