Fieres Dex Apps
  • Fieres SWAP AMM
  • Concepts
    • How Fieres swap works
    • Ecosystem Participants
    • Smart contracts
    • Glossary
  • Core Concepts
    • Swaps
    • Pools
  • Advanced Topics
    • Fees
    • Pricing
  • STAKING
    • Staking Feature
    • Contract Overview
    • Contract Mechanisms
  • FIERES TOKEN MINTER
    • Fieres Token Minter
    • Step-by-Step Guide
    • Token Categories
  • IDO Pools
    • IDO Launchpad
    • Steps for Creating an IDO Pool on Fieres DexAPPS
Powered by GitBook
On this page
  • Stake Accumulation
  • Reward Calculation Mechanism:
  • Example Scenario:
  • Functions
  • Events
  1. STAKING

Contract Mechanisms

Stake Accumulation

Stake accumulation in the contract refers to the process by which users' staked assets are collected, tracked, and managed over time. This involves several key components and functionalities:

Lockup Structs and Arrays: The contract defines Lockup structs, each representing a different staking option with unique terms (like duration and fees). Lockup contains information like the total amount staked in that particular lockup (totalStaked), which is crucial for tracking the total value locked in each staking option.

User Stakes Mapping: The contract maintains a mapping (userStakes) that maps each user's address to an array of Stake structs. Each Stake struct holds details about an individual staking transaction, such as the amount staked, the stake type, and the duration.

UserStaked Mapping: Alongside userStakes, there is another mapping called userStaked that keeps track of overall information about each user's staking in the contract. This includes the total amount the user has staked across all lockup types. Stake

Accumulation Process: When a user decides to stake tokens, they call the deposit function, specifying the stake type. The contract calculates any applicable deposit fees and subtracts them from the staked amount. The remaining amount is then added to the user's total staked amount in userStaked and to the specific Lockup's total staked amount. A new Stake struct is created (or updated) for the user in the userStakes mapping, containing details of this specific staking action. If it's the user's first time staking, the contract increments the totalUsers counter.

Events and Updates: Upon successful completion of the deposit, an event (Deposit) is emitted with details of the transaction. This process not only adds the user's stake to the total pool but also ensures that the staking details are recorded for future reward calculations and tracking of individual user stakes.

Stake Tracking: The contract's userInfo function provides a way to view a user's staking details, including the total amount staked and the division between available and locked stakes. This information is essential for both users and the contract to manage staking positions and calculate rewards.

Reward Calculation Mechanism:

Accumulated Token Per Share (accTokenPerShare): This variable represents the accumulated rewards per share, up to the last block that was accounted for in the contract. It is updated in the _updatePool function, which calculates new rewards and distributes them proportionally to all staked tokens.

Calculating New Rewards: New rewards are calculated based on the number of blocks that have passed since the last update and the reward rate per block for a specific lockup type. This is done in the _updatePool function using the _getMultiplier function, which finds the number of blocks between the last reward block and the current block. The reward for each block (defined in each lockup's rate) is then multiplied by this block count to find the total new rewards.

Updating accTokenPerShare: Once the total new rewards are calculated, the contract updates accTokenPerShare. It does this by dividing the new rewards by the total staked amount (in the specific lockup) and then adding this value to the existing accTokenPerShare.

Calculating User's Pending Rewards: When a user interacts with the contract (deposit, withdraw, or claim rewards), the contract calculates the pending rewards for that user. This is done by taking the user's staked amount, multiplying it by the current accTokenPerShare, and then subtracting the user's rewardDebt. rewardDebt is a user-specific value that tracks how much reward has already been accounted for that user, preventing double-counting of rewards.

Updating User's rewardDebt: After calculating pending rewards and possibly distributing them, the contract updates the user's rewardDebt. This is set to the user's staked amount multiplied by the current accTokenPerShare. This update ensures that the next time rewards are calculated, the user will only get rewards for new accumulations since their last interaction.

Example Scenario:

A user stakes tokens: Their stake increases the total staked amount in a lockup, and their rewardDebt is set based on the current accTokenPerShare.

Time passes, rewards accumulate: With each block, new rewards are added, and accTokenPerShare increases.

User claims rewards: The contract calculates pending rewards (user's stake * current accTokenPerShare - user's rewardDebt). Rewards are distributed to the user, and their rewardDebt is updated.

If a user stakes more or withdraws: Similar calculations are performed to ensure accurate and fair distribution of rewards based on their current stake.

Functions

deposit(uint8 _stakeType):

Purpose: Allows users to deposit Ether into a specific stake type. Parameters: _stakeType - the type of stake being deposited into. Functionality: Validates the deposit amount and stake type. Updates pool rewards for the stake type. Calculates pending rewards for the user and transfers them if any. Applies deposit fee, if applicable, and handles the fee distribution. Updates user and pool staking information. Emits a Deposit event.

withdraw(uint256 _amount, uint8 _stakeType):

Purpose: Allows users to withdraw their staked Ether along with any rewards. Parameters: _amount - the amount of Ether to withdraw, _stakeType - the type of stake being withdrawn from. Functionality: Validates the withdrawal amount and stake type. Updates pool rewards for the stake type. Calculates and transfers pending rewards. Handles withdrawal fees if applicable. Updates user and pool staking information. Emits a Withdraw event.

claimReward(uint8 _stakeType):

Purpose: Allows users to claim rewards for a specific stake type without withdrawing their staked tokens. Parameters: _stakeType - the type of stake for which to claim rewards. Functionality: Updates pool rewards for the stake type. Calculates and transfers pending rewards to the user. Updates user's reward debt.

_updatePool(uint8 _stakeType):

Purpose: Updates reward variables of the given pool to be up-to-date. Parameters: _stakeType - the type of stake being updated. Functionality: Checks if an update is needed based on the last reward block. Calculates new rewards and updates the accumulated token per share (accTokenPerShare). Updates the last reward block for the lockup.

_addStake(uint8 _stakeType, address _account, uint256 _duration, uint256 _amount):

Purpose: Internal function to add a new stake for a user. Parameters: _stakeType, _account, _duration, _amount - details about the stake being added. Functionality: Calculates the end time for the stake based on the duration. Ensures the number of stakes doesn't exceed the maximum allowed. Adds the new stake to the user's stakes array.

Events

Deposit(address indexed user, uint256 stakeType, uint256 amount):

Triggered when: A user deposits Ether into a stake. Purpose: Logs the details of the deposit. Parameters: user - the address of the user, stakeType - the type of stake, amount - the amount of Ether deposited.

Withdraw(address indexed user, uint256 stakeType, uint256 amount):

Triggered when: A user withdraws their staked Ether. Purpose: Logs the details of the withdrawal. Parameters: user - the address of the user, stakeType - the type of stake, amount - the amount of Ether withdrawn.

Other Events:

AdminTokenRecovered, NewStartAndEndBlocks, LockupUpdated, NewPoolLimit, RewardsStop, RewardsAdded, WalletAUpadted, DurationUpdated are other events in the contract used to log various administrative actions and updates to the contract's parameters.

PreviousContract OverviewNextFieres Token Minter

Last updated 1 year ago