> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/reserve-protocol/reserve-index-dtf/llms.txt
> Use this file to discover all available pages before exploring further.

# UnstakingManager Contract

> Manages time-locked withdrawals from StakingVault with cancel and claim functionality

## Overview

The **UnstakingManager** contract handles time-delayed withdrawals from StakingVaults. When a user withdraws from a vault with an unstaking delay, their tokens are held in this contract until the unlock time, after which they can be claimed.

### Key Features

* **Time-Locked Withdrawals**: Holds tokens until unlock time
* **Cancel Option**: Users can cancel and re-stake before unlock
* **Simple Lock Management**: Each withdrawal creates a numbered lock
* **Immutable Configuration**: Tied to specific vault and token

## Lock Structure

<ParamField path="user" type="address">
  Address that owns the lock
</ParamField>

<ParamField path="amount" type="uint256">
  Amount of tokens locked
</ParamField>

<ParamField path="unlockTime" type="uint256">
  Timestamp when lock becomes claimable
</ParamField>

<ParamField path="claimedAt" type="uint256">
  Timestamp when lock was claimed (0 if unclaimed)
</ParamField>

## Functions

### Create Lock

Create a new time-locked withdrawal (called only by vault).

<ParamField path="user" type="address">
  Address that will own the lock
</ParamField>

<ParamField path="amount" type="uint256">
  Amount of tokens to lock
</ParamField>

<ParamField path="unlockTime" type="uint256">
  Timestamp when tokens become claimable
</ParamField>

```solidity UnstakingManager.sol theme={null}
function createLock(
    address user,
    uint256 amount,
    uint256 unlockTime
) external
```

<Warning>
  Only the associated StakingVault can call this function. It's automatically called during vault withdrawals.
</Warning>

### Claim Lock

Claim tokens from an unlocked withdrawal.

<ParamField path="lockId" type="uint256">
  ID of the lock to claim
</ParamField>

```solidity UnstakingManager.sol theme={null}
function claimLock(uint256 lockId) external
```

<Info>
  Anyone can call this function, but tokens are always sent to the lock's original owner. The lock must be past its unlock time and not already claimed.
</Info>

### Cancel Lock

Cancel a lock and re-stake the tokens back into the vault.

<ParamField path="lockId" type="uint256">
  ID of the lock to cancel
</ParamField>

```solidity UnstakingManager.sol theme={null}
function cancelLock(uint256 lockId) external
```

<Info>
  This deposits the locked tokens back into the vault on behalf of the user. This is useful if the user changes their mind about unstaking or wants to avoid the waiting period.
</Info>

## View Functions

### Get Lock Details

Retrieve information about a specific lock.

```solidity UnstakingManager.sol theme={null}
function locks(uint256 lockId) external view returns (
    address user,
    uint256 amount,
    uint256 unlockTime,
    uint256 claimedAt
)
```

### Target Token

Get the token being managed.

```solidity UnstakingManager.sol theme={null}
function targetToken() external view returns (IERC20)
```

### Vault

Get the associated StakingVault.

```solidity UnstakingManager.sol theme={null}
function vault() external view returns (IERC4626)
```

## Events

<ResponseField name="LockCreated" type="event">
  Emitted when a new lock is created

  **Parameters:**

  * `lockId` - Unique identifier for the lock
  * `user` - Address that owns the lock
  * `amount` - Amount of tokens locked
  * `unlockTime` - Timestamp when claimable
</ResponseField>

<ResponseField name="LockCancelled" type="event">
  Emitted when a lock is cancelled and tokens are re-staked

  **Parameters:**

  * `lockId` - ID of the cancelled lock
</ResponseField>

<ResponseField name="LockClaimed" type="event">
  Emitted when a lock is claimed

  **Parameters:**

  * `lockId` - ID of the claimed lock
</ResponseField>

## Errors

<ResponseField name="UnstakingManager__Unauthorized" type="error">
  Thrown when caller is not authorized for the operation
</ResponseField>

<ResponseField name="UnstakingManager__NotUnlockedYet" type="error">
  Thrown when trying to claim before unlock time
</ResponseField>

<ResponseField name="UnstakingManager__AlreadyClaimed" type="error">
  Thrown when trying to claim or cancel an already-claimed lock
</ResponseField>

## Usage Flow

### Standard Unstaking

```solidity theme={null}
// 1. User withdraws from vault (creates lock)
vault.withdraw(1000e18, receiver, owner);
// Lock created with ID = 0

// 2. Wait for unlock time
// ...

// 3. Claim tokens
unstakingManager.claimLock(0);
// Tokens sent to user
```

### Cancel and Re-stake

```solidity theme={null}
// 1. User withdraws from vault
vault.withdraw(1000e18, receiver, owner);
// Lock created with ID = 0

// 2. User changes mind before unlock time
unstakingManager.cancelLock(0);
// Tokens deposited back into vault
// Lock deleted
```

## Security Considerations

<Warning>
  The UnstakingManager is automatically deployed by the StakingVault during initialization. The vault address is immutable and set in the constructor.
</Warning>

<Info>
  Locks cannot be transferred or traded. They are permanently tied to the original user address.
</Info>

## Lock ID Sequence

<ParamField path="nextLockId" type="uint256">
  Counter that increments for each new lock, starting at 0
</ParamField>

Lock IDs are sequential starting from 0 and increment with each new lock creation. Once a lock is claimed or cancelled, its ID is not reused.
