StakingRewards

Address:0x07f40d1df737c4fc9f30d76d99607fb063cdd02215f228182a4c595e24be86b6

Summary of the StakingContract Code

The provided code defines the StakingContract, which facilitates staking and rewards distribution for users on a StarkNet-based blockchain. Below is a detailed summary of its components and functionalities:

Purpose

The StakingContract manages the staking of tokens (staking_token) by users and distributes rewards (reward_token) based on the staked amounts and predefined reward rates and durations. It supports functionalities such as staking, withdrawing, checking and claiming rewards, setting reward amounts and durations, and enforcing ownership controls.

Key Components

  1. Interfaces:

    • IStakingContract Interface:

      • Specifies methods for setting reward amount and duration, staking tokens, withdrawing tokens, checking rewards, and claiming rewards.

      • Methods:

        • set_reward_amount(ref self: TContractState, amount: u256): Sets the reward amount per duration cycle.

        • set_reward_duration(ref self: TContractState, duration: u256): Sets the duration for which rewards will be distributed.

        • stake(ref self: TContractState, amount: u256): Allows users to stake a specified amount of tokens.

        • withdraw(ref self: TContractState, amount: u256): Allows users to withdraw their staked tokens.

        • get_rewards(self: @TContractState, account: ContractAddress) -> u256: Retrieves the total rewards accrued by a specified account.

        • claim_rewards(ref self: TContractState): Allows users to claim their accrued rewards.

  2. Error Handling:

    • Errors Module:

      • Defines constants for error messages related to null rewards, insufficient rewards, null amounts, null duration, unfinished duration, ownership verification, and insufficient balance.

  3. Storage Structure:

    • Storage Struct:

      • staking_token: Represents the token used for staking.

      • reward_token: Represents the token used for distributing rewards.

      • owner: Stores the contract owner's address.

      • reward_rate: Tracks the rate at which rewards are distributed.

      • duration: Duration for which rewards are distributed.

      • current_reward_per_staked_token: Current rate of rewards per staked token.

      • finish_at: Timestamp indicating when the current reward distribution cycle ends.

      • last_updated_at: Timestamp of the last state-modifying action (staking, withdrawal, or rewards claimed).

      • last_user_reward_per_staked_token: Maps user addresses to the last recorded reward per staked token.

      • unclaimed_rewards: Maps user addresses to their unclaimed rewards.

      • total_distributed_rewards: Tracks the total amount of rewards distributed.

      • total_supply: Total amount of tokens staked in the contract.

      • balance_of: Maps user addresses to their staked token balances.

  4. Events:

    • Event Enum:

      • Defines events (Deposit, Withdrawal, RewardsFinished) triggered during staking, withdrawal, and reward distribution operations.

      • Events are emitted to notify external entities about significant contract operations.

  5. Constructor:

    • Constructor Function:

      • Initializes the contract with addresses for the staking token and reward token.

      • Sets the contract owner as the caller deploying the contract.

  6. ABI Implementation:

    • ABI Implementation for IStakingContract:

      • Implements the methods specified in IStakingContract to manage staking, reward setting, and distribution functionalities.

      • Includes validations, updates to storage variables, and emission of corresponding events.

      • Ensures that only the contract owner can perform certain operations, such as setting reward amounts and durations.

  7. Private Functions:

    • PrivateFunctions Trait:

      • Implements internal helper functions used for reward calculation, state updates, and ownership verification.

      • Functions include update_rewards, distribute_user_rewards, send_rewards_finished_event, compute_current_reward_per_staked_token, compute_new_rewards, last_time_applicable, min, and only_owner.

      • These functions manage the distribution of rewards, computation of reward rates, and validation of ownership for state-modifying operations.

Key Points

  • Functionality: The contract allows users to stake tokens, withdraw tokens, check accrued rewards, and claim rewards based on predefined reward rates and durations.

  • Ownership: Ownership controls are enforced to restrict certain operations (e.g., setting reward parameters) to the contract owner.

  • Event Handling: Events are used to signal important contract operations, facilitating external monitoring and integration with other contracts or applications.

  • Reward Calculation: Rewards are calculated based on the amount of tokens staked, duration, and reward rate, ensuring fairness and transparency in reward distribution.

  • Error Handling: Error messages are defined to handle scenarios such as insufficient balances, invalid durations, and unauthorized operations, enhancing contract robustness and reliability.

This design ensures that the StakingContract operates efficiently, securely managing token staking and reward distribution while providing clear interfaces for user interaction and contract management.

Last updated