Modules
Staking

Staking Pool System Documentation

Overview

The Staking Pool System is designed to allow users to stake tokens within the Synnq ecosystem and earn rewards over time. This system manages the total staked tokens, tracks individual stakers' contributions, and calculates rewards based on the duration of staking. It ensures that users are rewarded fairly for their participation in the network.


Components and Responsibilities

1. Stake Information (StakeInfo)

StakeInfo is a structure that holds details about each user's stake. It records the amount staked, the time when staking began, and the rewards earned over time.

Key Attributes:

  • Amount: The number of tokens staked by the user.
  • Start Time: The timestamp when the staking began.
  • Reward: The reward earned based on the staking duration and amount.

2. Staking Pool (StakingPool)

The StakingPool struct is responsible for managing the overall staking process. It keeps track of the total tokens staked in the pool, manages individual stakers' information, and calculates rewards. It also handles the storage and retrieval of the staking pool data, ensuring persistence and consistency.

Key Functionalities:

  • Stake Tokens: Allows users to stake their tokens in the pool, updating their stake information and the total pool balance.
  • Unstake Tokens: Enables users to withdraw their staked tokens along with the earned rewards. The system calculates the reward based on the staking duration and amount.
  • Calculate Rewards: Computes the rewards for a user based on the staked amount and how long the tokens have been staked.
  • Save Staking Pool: Persists the current state of the staking pool to storage, ensuring that all staking data is saved securely.
  • Load Staking Pool: Retrieves the staking pool data from storage, restoring the state of the pool after a system restart or crash.

Use Cases and Workflows

Staking Tokens

  1. User Staking:

    • A user decides to stake a certain number of tokens.
    • The system checks if the amount is valid (greater than 0).
    • A StakeInfo object is created, recording the amount staked and the start time.
    • The total staked amount in the pool is updated, and the user's staking information is stored.
    • The updated staking pool state is saved to persistent storage.
  2. Confirmation:

    • The system logs the staking action, confirming that the user has staked the specified amount of tokens.

Unstaking Tokens

  1. User Unstaking:

    • A user wishes to unstake their tokens.
    • The system retrieves the user's staking information from the pool.
    • The elapsed time since staking is calculated, and the reward is computed based on the staked amount and duration.
    • The user's staked tokens and earned rewards are returned to them, and their staking record is removed from the pool.
    • The total staked amount in the pool is decremented, and the updated state is saved to storage.
  2. Confirmation:

    • The system logs the unstaking action, detailing the amount unstaked and the rewards earned.

Calculating Rewards

  1. Reward Calculation:

    • Rewards are calculated using a fixed annual interest rate (e.g., 10%).
    • The reward formula is based on the proportion of the year that the tokens were staked, adjusted by the annual rate.
  2. Example Calculation:

    • If a user stakes 100 tokens for 6 months, their reward is calculated as follows:

      [\text{Reward} = 100 \times 0.1 \times \left(\frac{6}{12}\right) = 5 \text{ tokens}]

Saving and Loading the Staking Pool

  1. Saving State:

    • After any significant change (e.g., staking or unstaking), the staking pool's state is saved to persistent storage.
    • This ensures that the system can recover its state in the event of a crash or restart.
  2. Loading State:

    • When the system starts, it loads the staking pool from storage.
    • If no existing state is found, a new empty staking pool is initialized.

Error Handling and Security

The Staking Pool System includes error handling for common issues, such as invalid staking amounts or attempts to unstake without an active stake. It uses thread-safe structures and secure storage to prevent data corruption and ensure the integrity of the staking process.