Stakinator Overview
This document provides a comprehensive guide to the Stakinator facet in the Diamond Vaultinator framework.
Table of Contents
Introduction
Features
Architecture
Usage
Staking Pools
Reward Calculation
Security Considerations
Best Practices
Integration Examples
Introduction
The Stakinator facet extends the Diamond Vaultinator with comprehensive staking functionality, allowing vault owners to stake tokens, earn rewards, and participate in various staking strategies. It provides a flexible system for creating and managing staking pools with customizable parameters.
Features
Multiple Staking Pools: Support for multiple token staking pools with different reward rates and lock periods
Flexible Locking: Customizable lock periods for staked tokens
Reward Distribution: Automated reward calculation and distribution
Emergency Withdrawals: Safety mechanism for emergency situations
Role-Based Access Control: Secure management of staking pools and parameters
Vault Integration: Seamless integration with the Diamond Vaultinator vault system
Architecture
The Stakinator facet follows the Diamond pattern and consists of:
IStakinatorFacet: Interface defining all functions and events
StakinatorFacet: Implementation of the interface
StakinatorStorage: Dedicated storage structure for staking data
Storage Structure
Access Control
The Stakinator facet uses the following role:
STAKING_MANAGER_ROLE: Required for creating and updating staking pools and managing system parameters
Usage
Initialization
Before using the Stakinator facet, it must be initialized:
This function:
Sets the reward token address
Grants the
STAKING_MANAGER_ROLEto the adminMarks the storage as initialized
Creating Staking Pools
Staking pools can be created by accounts with the STAKING_MANAGER_ROLE:
Parameters:
_token: Address of the token to be staked_rewardRate: Annual reward rate in basis points (e.g., 500 = 5%)_lockPeriod: Duration in seconds that tokens must remain staked
Staking Tokens
Vault owners or approved operators can stake tokens:
Parameters:
_vaultId: ID of the vault_token: Address of the token to stake_amount: Amount to stake_lockPeriod: Optional custom lock period (0 for default pool period)
Managing Stakes
Users can increase their stakes, withdraw after the lock period, or claim rewards:
Viewing Information
The facet provides several view functions to check staking information:
Staking Pools
Pool Parameters
Each staking pool has the following parameters:
Token: The ERC20 token that can be staked
Reward Rate: Annual percentage yield in basis points
Lock Period: Minimum duration tokens must remain staked
Total Staked: Total amount of tokens staked in the pool
Updating Pools
Pool parameters can be updated by accounts with the STAKING_MANAGER_ROLE:
When updating a pool, all pending rewards are calculated and stored before applying the new parameters.
Reward Calculation
Rewards are calculated based on:
Staked Amount: The amount of tokens staked
Reward Rate: Annual percentage yield in basis points
Time Elapsed: Duration since the last reward calculation
The formula used is:
Where:
10000is the basis points denominator (100%)31536000is the number of seconds in a year (365 days)
Security Considerations
Lock Period Enforcement
The lock period is strictly enforced. Users cannot withdraw staked tokens before the lock period expires unless they use the emergency withdrawal function, which may forfeit rewards.
Emergency Withdrawals
The emergency withdrawal function provides a safety mechanism for users to withdraw their staked tokens in case of emergencies, even if the lock period has not expired. However, this may result in forfeiting accumulated rewards.
Access Control
All administrative functions are protected by role-based access control. Only accounts with the appropriate roles can create or update staking pools or change system parameters.
Best Practices
Setting Appropriate Reward Rates
When setting reward rates, consider:
The value of the staked token
The value of the reward token
Market conditions and competitive rates
Sustainability of the reward system
Lock Periods
Choose lock periods that balance:
User flexibility
Protocol stability
Reward economics
Common lock periods range from 7 days to 365 days.
Reward Token Management
Ensure that:
The reward token contract is secure and well-audited
Sufficient reward tokens are available for distribution
The reward distribution is sustainable over time