# CollateralPool

## 🏦 `CollateralPool.sol`

The `CollateralPool` contract is a key component of the Scall.io protocol. It handles the **collateral management system**, which powers all perpetual option contracts.

This contract manages:

* User deposits and withdrawals of collateral tokens (e.g., USDC)
* Real-time rent deduction from traders
* Liquidation logic based on collateral balances
* Reward distribution for liquidity providers (LPs)

***

### 🧩 What It Does

* Accepts and stores **collateral deposits** from traders
* Tracks **user rent balance** and updates it per block
* Enables **safe withdrawals** based on collateral/rent levels
* Handles **liquidation** if rent coverage becomes insufficient
* Distributes **protocol rewards** to LPs and other participants

***

### 🛠️ Public & External Functions

#### `depositCollateral(uint256 amount)`

* Allows a trader to deposit collateral.
* Emits: `CollateralDeposited`

```solidity
/// @notice Allows a user to deposit collateral into their account.
/// @param _amount The amount of collateral to deposit.
function depositCollateral(uint256 _amount) external
```

#### `withdrawCollateral(uint256 amount)`

* Lets a trader withdraw unused collateral, assuming rent requirements are still met.
* Emits: `CollateralWithdrawn`

```solidity
/// @notice Allows a user to withdraw collateral if they have sufficient balance.
/// @param _amount The amount of collateral to withdraw.
function withdrawCollateral(uint256 _amount) external
```

#### `liquidateContract(address user, address market, uint256 contractId)`

* Called by any liquidator when a user’s collateral balance drops below the minimum threshold (e.g., one week of rent).
* Automatically closes the user’s contracts and redistributes a penalty to the liquidator.
* Emits: `ContractLiquidated`

```solidity
/// @notice Liquidates a user's contract if they meet the liquidation criteria.
/// @param _user The address of the user.
/// @param _index The market index.
/// @param _id The ID of the user's position.
/// @return The penalty amount awarded to the liquidator (18 decimals).
function liquidateContract(address _user, uint256 _index, uint256 _id) external returns(uint256)
```

#### `claimRewards(uint256 index, uint256 id, uint256 substractCount)`

* Allows LPs or users with eligible NFTs to claim rent rewards based on usage and time.
* Emits: `RewardsClaimed`

```solidity
/// @notice Allows a user to claim rewards for a position they own.
/// @param _index The market index.
/// @param _id The ID of the user's position.
/// @param _substractCount If the user want to claim until a specific period.
/// @return claimedRewards The reward amount received by the LP (token decimals).
function claimRewards(uint256 _index, uint256 _id) external returns(uint256)
```

***

### 👀 View Functions

These read-only functions help frontends or users query internal state:

#### `getUserInfos(address user) → uint256`

* Returns user information for a specific user.

```solidity
struct UserInfos {
    uint256 collateral;
    uint256 rent;
    uint256 lastUpdate;
}
/// @notice Returns user information for a specific user.
/// @param _user The address of the user.
/// @return UserInfos structure containing collateral, rent, and last update timestamp.
function getUserInfos(address _user) external view returns(UserInfos memory)
```

#### `getUserFees(address user) → uint256`

* Returns the current fees per second for a given user.

```solidity
/// @notice Calculates the accumulated fees for a user.
/// @param _user The address of the user.
/// @return The total fees accumulated for the user (18 decimals).
function getUserFees(address _user) external view returns(uint256)
```

#### `balanceOf(address user) → uint256`

* Returns the current balance for a given user.

```solidity
/// @notice Returns the balance of collateral for a user after deducting rent fees.
/// @param _user The address of the user.
/// @return The net balance of the user (18 decimals).
function balanceOf(address _user) public view returns(uint256)
```

#### `getRewardsForLP(uint256 index, uint256 id) → uint256`

* Returns the rewards for a given LP.

```solidity
/// @notice Calculates the rewards for a liquidity provider after deducting fees.
/// @param _index The market index.
/// @param _id The ID of the user's position in the market.
/// @return The net rewards after fees (18 decimals).
function getRewardsForLp(uint256 _index, uint256 _id) external view returns(uint256)
```

#### `canOpenContract(address user, uint256 rent) → bool`

* Determines if a user can open a contract based on their collateral and rent.

```solidity
/// @notice Determines if a user can open a contract based on their collateral and rent.
/// @param _user The address of the user.
/// @param _rent The rent amount for the new contract.
/// @return True if the user has sufficient collateral, otherwise false.
function canOpenContract(address _user, uint256 _rent) external view returns(bool)
```

#### `needLiquidation(address user) → bool`

* Checks if a user needs to be liquidated.

```solidity
/// @notice Checks if a user needs to be liquidated.
/// @param _user The address of the user.
/// @return True if liquidation is required, otherwise false.
function needLiquidation(address _user) external view returns(bool)
```

#### `getCollateralToken() → address`

* Returns the address of the collateral token used.

```solidity
/// @notice Returns the address of the collateral Token.
/// @return Address of the collateral Token.
function getCollateralToken() external view returns(address)
```

#### `getMain() → address`

* Returns the address of the main contract that oversees protocol governance.

```solidity
/// @notice Returns the address of the main contract.
/// @return Address of the main contract.
function getMain() external view returns(address)
```

***

### 🧠 How It Works

The contract stores user collateral in a mapping and tracks **rent accrual per second**. If a user’s collateral drops below one week of required rent, their positions are eligible for liquidation.

When rent is collected, the protocol distributes it across:

* Liquidity providers
* The DAO (if enabled)
* Potential rewards for liquidators

***

### 📌 Summary

| Feature          | Purpose                                         |
| ---------------- | ----------------------------------------------- |
| Deposit/Withdraw | Manage user collateral balances                 |
| Rent Tracking    | Ensure options remain funded in real-time       |
| Liquidation      | Protect LPs by auto-closing underfunded trades  |
| Rewards Claiming | Enable LPs to receive protocol-generated income |


---

# 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/collateralpool.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.
