Stakinator Overview

This document provides a comprehensive guide to the Stakinator facet in the Diamond Vaultinator framework.

Table of Contents

  1. Introduction

  2. Features

  3. Architecture

  4. Usage

  5. Staking Pools

  6. Reward Calculation

  7. Security Considerations

  8. Best Practices

  9. 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:

  1. IStakinatorFacet: Interface defining all functions and events

  2. StakinatorFacet: Implementation of the interface

  3. 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_ROLE to the admin

  • Marks 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:

  1. Staked Amount: The amount of tokens staked

  2. Reward Rate: Annual percentage yield in basis points

  3. Time Elapsed: Duration since the last reward calculation

The formula used is:

Where:

  • 10000 is the basis points denominator (100%)

  • 31536000 is 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

Integration Examples

Basic Staking Example

Staking with Custom Lock Period

Emergency Withdrawal