# MarketPool

## `MarketPool.sol`

The `MarketPool` contract is a **core component of Scall.io**, responsible for managing **perpetual options**, **liquidity deposits**, and **strike price pools** for a specific market (e.g., BTC/USDC or ETH/USDC).

Each deployed `MarketPool` instance handles one market and uses a **Chainlink price feed** to determine valid strike prices and market conditions.

***

### 🧩 What It Does

* Accepts deposits from **Liquidity Providers (LPs)** who want to earn yield at specific strike prices.
* Allows **traders** to open and close **Call or Put perpetual options**.
* Tracks individual **LP positions** and **open option contracts** through NFTs.

***

### 📤 Public Functions

Here are the key functions users and external contracts can call:

#### `deposit(bool isCall, uint256 strikeIndex, uint256 amount)`

* Called by LPs to **provide liquidity** at a specific strike price.
* Mints an LP NFT representing their position.
* Emits: `Deposit` event

```solidity
/// @notice Allows a user to deposit assets and open an LP position
/// @dev Mints an NFT for the LP position and updates liquidity information
/// @param _isCall Specifies if the option is a call or a put
/// @param _strikeIndex The strike Index from getIntervals()
/// @param _amount The amount of assets to deposit (token decimal)
/// @return id The unique ID assigned to the newly created LP position.
function deposit(bool _isCall, uint256 _amount) external returns(uint256)
```

#### `withdraw(uint256 lpId)`

* Allows LPs to **withdraw their deposited liquidity** (if not in use).
* Burns the associated LP NFT.
* Emits: `Withdraw` event

```solidity
/// @notice Allows a user to withdraw assets from an LP position
/// @dev Claims rewards for the LP position before withdrawal and burns the NFT if fully withdrawn
/// @param _id The ID of the LP position to withdraw from
/// @return tokenAtoTransfer The amount of token A withdrawn by the LP (token decimals).
/// @return tokenBtoTransfer The amount of token B withdrawn by the LP (token decimals).
/// @return claimedRewards The additional reward amount received by the LP (token decimals).
function withdraw(uint256 _id) external returns(uint256, uint256, uint256)
```

#### `openContract(bool isCall, uint256 strikeIndex, uint256 amount)`

* Called by traders to **open a new perpetual option**.
* Requires sufficient collateral in the CollateralPool.
* Mints a contract NFT.
* Emits: `ContractOpened` event

```solidity
/// @notice Opens a new option contract using liquidity provided in the pool
/// @dev Mints an NFT for the option contract and updates liquidity information
/// @param _isCall Specifies if the option is a call or a put
/// @param _strikeIndex The strike Index from getIntervals()
/// @param _amount The amount of assets for the option contract
function openContract(bool _isCall, uint256 _strikeIndex, uint256 _amount) external
```

#### `closeContract(uint256 contractId)`

* Allows traders to **manually close** their perpetual options at any time.
* Burns the contract NFT.
* Emits: `ContractClosed` event

```solidity
/// @notice Closes an open contract position, settles based on the current price, and burns the contract NFT.
/// @dev Checks if the caller is the contract owner. Determines if the position is a call or put and settles the contract based on the current price relative to the strike price.
/// @param _id The unique ID of the contract to close.
function closeContract(uint256 _id) external
```

***

### 👀 View Functions

Here are helpful read-only functions to query data:

<table><thead><tr><th width="374">Function</th><th>	Description</th></tr></thead><tbody><tr><td>getMain()</td><td>Returns the address of the <code>Main</code> contract.</td></tr><tr><td>getERC721_Contract()</td><td>Returns the address of the ERC721 contract representing trader positions.</td></tr><tr><td>getERC721_LP()</td><td>Returns the address of the ERC721 contract representing LP positions.</td></tr><tr><td>getTokenA()</td><td>Returns the address of the asset token (e.g., BTC).</td></tr><tr><td>getTokenB()</td><td>Returns the address of the collateral token (e.g., USDC).</td></tr><tr><td>getPriceFeed()</td><td>Returns the address of the Chainlink price feed used for indexing.</td></tr><tr><td>getIntervalLength()</td><td>Returns the strike price interval setting.</td></tr><tr><td>getRange()</td><td>Returns the range of strike prices around the market price.</td></tr><tr><td>getYield()</td><td>Returns the current yield (APR) configured for the market.</td></tr><tr><td>getContractInfos(uint256 _id)</td><td>Returns all stored data for a given trader contract ID.</td></tr><tr><td>getLpInfos(uint256 _id)</td><td>Returns all stored data for a given LP position ID.</td></tr><tr><td>getStrikeInfos(uint256 _strike)</td><td>Returns aggregated info about liquidity at a specific strike.</td></tr><tr><td>getStrikeHistory(uint256 _strike, uint256 _index)</td><td>Returns historical data for a strike (useful for rewards calculations or snapshots).</td></tr><tr><td>getPrice()</td><td>Fetches the current price from the Chainlink oracle.</td></tr><tr><td>getIntervals()</td><td>Returns the list of active strike price intervals.</td></tr><tr><td>getRewards(uint256 _id)</td><td>Returns the claimable rewards for a given LP NFT ID.</td></tr></tbody></table>

***

### 🔐 Roles & Interactions

This contract interacts with:

* `CollateralPool.sol` for rent collection and liquidation triggers
* `Main.sol` to access governance parameters
* Chainlink price feeds for accurate strike pricing
* Custom ERC721 contracts for on-chain position tracking


---

# 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://scall-io.gitbook.io/scall.io/developers/contracts/marketpool.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.
