# Position Management

### Overview

The Position Management system is a core component of Curvance's lending protocol, enabling advanced leverage and deleverage operations for user positions. It provides a structured way for users to manage complex DeFi positions across multiple asset types while maintaining protocol safety.

### Architecture Design

<figure><img src="https://4091724113-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsJ5CLOxFjcZYTs5pEZoN%2Fuploads%2FyA5yBpjL3UeQVWSv6ERb%2FBlank%20diagram(20).png?alt=media&#x26;token=95421388-6742-449e-8214-37b6ca3b7cb4" alt=""><figcaption></figcaption></figure>

### Core Components

#### PositionManagementBase

The abstract base contract provides the core functionality for all position management implementations. It includes:

* Common state variables and constants.
* Core leverage/deleverage logic.
* Safety checks and fee calculations.
* Market status queries and calculations.
* Standard integration points with other protocol components.

#### Specialized Implementations

Each implementation extends the base functionality to support specific asset types:

* **PositionManagementSimple:** Basic implementation for standard tokens with simple swap requirements.
* **PositionManagementPendlePT**: Specialized for Pendle Principal Tokens, handling their unique yield token mechanics.
* **PositionManagementPendleLP**: Manages positions for Pendle liquidity provider tokens.
* **PositionManagementVelodrome:** Handles Velodrome AMM-specific operations..
* **PositionManagementAerodrome:** Extends Velodrome implementation for Aerodrome protocol

### Key Data Structures

#### LeverageStruct

```solidity
struct LeverageAction {
    IBorrowableCToken borrowableCToken;
    uint256 borrowAssets;
    ICToken cToken;
    SwapperLib.Swap swapAction;
    bytes auxData;
}
```

Encapsulates all data needed for a leverage operation, including which token to borrow, how much to borrow, and which position token to leverage against.

#### DeleverageStruct

```solidity
struct DeleverageStruct {
    ICToken cToken;
    uint256 collateralAssets;
    IBorrowableCToken borrowableCToken;
    uint256 repayAssets;
    SwapperLib.Swap[] swapAction;
    bytes auxData;
}
```

Contains data for deleverage operations, specifying which collateral to liquidate and how to route funds to repay debt.

### Integration Points

The Position Management system interacts with multiple components of the Curvance ecosystem:

* **MarketManager:** For checking account status, debt limits, and collateral values.
* **CentralRegistry:** For protocol-wide configuration and permissions.
* **CTokens:** Position tokens that serve as collateral.
* **BorrowableCTokens:** Debt tokens that users borrow from.
* **SwapperLib:** For executing token swaps during leverage/deleverage operations.
* **External Protocols:** Direct integrations with Pendle, Velodrome, and other DeFi protocols.

### Security Features

The Position Management architecture implements several security features:

* **Slippage Protection:** Ensures executed trades don't lose value beyond user-specified thresholds.
* **Sanity Checks:** Validates all operations against user account status and market constraints.
* **Maximum Leverage Limits:** Prevents users from taking on excessive risk.
* **Fee Calculations:** Ensures protocol takes appropriate fees for providing leverage services.
* **Reentrancy Protection:** Guards against reentrancy attacks during complex operations.

### Protocol Interactions

During position management operations, the system follows these general steps:

* **Validation:** Check that the requested operation is valid for the user's account.
* **Calculation:** Determine limits, risks, and required amounts.
* **Execution:** Perform necessary token transfers, borrows, or repayments.
* **Swapping:** Convert between token types as needed using appropriate swap routes.
* **Position Update:** Update the user's collateral and debt positions.
* **Fee Handling:** Calculate and collect protocol fees.

This architecture enables Curvance to support complex leverage strategies across diverse asset types while maintaining protocol security and risk parameters.
