# Under the Hood (Smart Contract API)

## Under the Hood: Where the Magic Reside

### Opening Positions: Like Opening Pandora's Box

#### `openPosition()`: The Point of No Return

Welcome to the function that could either enrich you or teach humility.

**Function Signature:**

```solidity
function openPosition(
    uint256 _collateralAmount, // Your tribute to the crypto market
    uint256 _sizeDelta, // Size of your ambition
    bool _isLong // True for bullish, False for bearish
) external nonReentrant
```

#### Parameters Explained:

* **`_collateralAmount`**: Your USDC contribution (range: 1-1000). Consider it your offering to market volatility.
* **`_sizeDelta`**: Desired position size. The larger the number, the quicker you might face liquidation.
* **`_isLong`**:
  * `true`: "DOGE is destined to hit $1!"
  * `false`: "I've read this story before."

#### Requirements (Rules for Risk Management):

1. No existing positions (one risk at a time).
2. Leverage must be between 10x-200x.
3. Collateral must be within the valid range.
4. Necessary USDC balance (check wallet, not dreams).

#### Events Emitted:

* **`PositionOpened(user, size, collateral, entryPrice, isLong)`**: Signifying a new contender in the crypto market.

***

## Closing Positions: The Walk of Shame (or Glory)

It's decision time: ramen or lobster?

### Functions Overview

#### `closePosition()`

**What Happens:**\
Calculates your PnL (Profit and Loss, aka "Pain and Suffering").

* **Profit:** Congratulations, you've overcome the odds!
* **Loss:** Join the rest; consolation t-shirts available.

**Requirements:**

* Must have an active position.
* Contract must have sufficient USDC if your position is profitable.

#### `getPosition() - The Reality Check Function`

Gets your position and current PnL. Warning: May cause regret.

**Returns:** Struct `PositionWithPnL`

* `uint256 size`: Size of your investment.
* `uint256 collateral`: Your collateral.
* `uint256 entryPrice`: The haunting entry price.
* `bool isLong`: Your market stance.
* `bool isActive`: If you're still in the game.
* `int256 pnl`: The number that makes you laugh or cry.

#### Liquidation System (Where Dreams Go to Die)

**`liquidatePosition() - The Grim Reaper Function`**

Activates when your position crashes disastrously.

**Function:** `liquidatePosition(address _user) external nonReentrant`

### How Liquidation Works

#### Standard Liquidation

* **Loss Threshold**: Your position loses 90% of its value.
* **Liquidator's Role**: A liquidator calls the liquidation function.
  * **Reward**: They earn a 5% reward for executing the liquidation.
* **Remaining Value**: You receive any value left (if anything).
* **Reflection**: It's a time to reconsider life decisions.

#### batchLiquidate() - Mass Destruction Mode

* **Purpose**: To liquidate multiple positions simultaneously during market downturns.
* **Function Signature**:

  ```solidity
  function batchLiquidate(uint256 maxLiquidations) external nonReentrant returns (uint256)
  ```
* **Ideal For**:
  * Market crash days
  * Liquidation bot operators
  * Those who find humor in misfortune

#### isLiquidatable() - The Danger Detector

* **Functionality**: Determines if a position is at risk of liquidation.
* **Function Signature**:

  ```solidity
  function isLiquidatable(address _user) public view returns (bool)
  ```
* **Outcome**: Returns `true` if liquidation is imminent.

###
