# Dynamic Staking

## Overview

The **Dynamic Staking** contract is a sophisticated staking mechanism built on the Ethereum blockchain. It allows users to stake CELT tokens in a dynamic environment where profits and losses are calculated over cycles. The contract supports creating new staking pools, staking tokens, withdrawing tokens, and distributing profits or losses.

> Legacy dynamic pools are bridged via the `DynamicPoolWrapper`. This allows existing pools to continue providing reserves for house games while receiving a proportional share of new revenue.

**Note:** Users cannot create new stakes in these pools. All new liquidity provision must be routed through the primary [Liquidity Pool (V2)](/smartcontracts-description/liquidity-pool-v2.md).

### Key Features

* **Dynamic Staking Pools:** Supports the creation of new pools for staking tokens.
* **Profit and Loss Calculation:** Calculates profits and losses at the end of each cycle and distributes them accordingly.
* **Invitee Staking:** Tracks the total stakes made by invitees of a staker.
* **Non-Reentrant:** Ensures that functions cannot be re-entered, mitigating potential security risks.
* **Access Control:** Utilizes roles for managing permissions within the contract.

### Key Components

| Component              | Description                                                                    |
| ---------------------- | ------------------------------------------------------------------------------ |
| **ERC20 Token**        | The contract interacts with ERC20 tokens for staking and rewards.              |
| **DynamicStakingPool** | Represents each staking pool within the contract.                              |
| **Core**               | The core logic for token management and access control.                        |
| **Calculation Window** | A predefined time window for calculating profits/losses and distributing them. |

## Stake Tokens

Users can stake tokens by invoking the `stake` function. The amount to be staked must be greater than or equal to the minimum allowed amount.

```solidity
function stake(address staker, uint256 amount) external onlyRole(CORE) nonReentrant
```

**Requirements:**

* Not within the calculation window (`DS04` error).
* The amount is at least the minimum allowed amount (`DS01` error).
* The amount is divisible by 2 (`DS09` error).

## Withdraw Tokens

Tokens can be withdrawn from a pool by calling the `withdraw` function. This is only allowed during the calculation time and if the pool is fully distributed.

```solidity
function withdraw(address pool) external
```

**Requirements:**

* Must be calculation time (`DS03` error).
* Pool must be active and successfully distributed in the current cycle (`DS08`, `DS10`, `DS11` errors).

## Calculate Profit or Loss

This function calculates and distributes the profit or loss for each cycle. It must be called at least once per cycle based on the active pool count.

```solidity
function calculateProfit(uint256 offset, uint256 count) external nonReentrant
```

**Requirement:** Must be calculation time (`DS03` error).

## Reserve Funds

Allows game contracts to reserve funds from the staking contract.

```solidity
function reserveFunds(uint256 amount) external onlyRole(GAME)
```

**Requirements:**

* Not within the calculation window (`DS04` error).
* Sufficient balance to reserve (`DS06` error).

## Adjust Settings

The contract allows adjusting the minimum allowed staking amount and the calculation window through the following functions, controlled by the `TIMELOCK` role.

* `setMinAllowedAmount(uint256 _amount)`: Adjusts the minimum staking amount.
* `setCalculatingWindow(uint256 _window)`: Adjusts the calculation window.

## Events

Various events are emitted for tracking activities within the contract, such as `PoolOpened`, `PoolClosed`, `Staked`, `Withdraw`, `NewMinAllowedAmount`, and `NewCalculationWindow`.

## Conclusion

The **Dynamic Staking** smart contract introduces a flexible and dynamic approach to staking on the Ethereum blockchain. By leveraging calculation cycles, dynamic pools, and robust access control, it offers a comprehensive staking solution for users.


---

# 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://docs.celestium.digital/smartcontracts-description/dynamic-staking.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.
