// SPDX-License-Identifier: MIT pragma solidity 0.8.18; import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol"; import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol"; import {Pausable} from "@openzeppelin/contracts/security/Pausable.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {Address} from "@openzeppelin/contracts/utils/Address.sol"; /** * @dev Implementation of a staking contract for MasaToken. This contract allows * users to stake their MasaTokens for a specified period and earn interest * based on the staking period. The contract includes functionalities for * staking, unstaking, and querying staked balances and earned interest. */ contract MasaStaking is ReentrancyGuard, AccessControl, Pausable { using SafeERC20 for IERC20; /** * @dev Struct to store information about each stake including amount, * start timestamp, period, and interest rate. */ struct Stake { uint256 amount; // The amount of tokens staked. uint256 startTime; // The timestamp when the stake was initiated. uint256 unlockTime; // The timestamp when the stake was unlocked. uint256 period; // The period of the stake. uint256 interestRate; // The interest rate applicable to the stake. } /** * @dev Struct to store details about each stake including amount, * start timestamp, unlock timestamp, period, interest rate, and eligibility * for unlocking and claiming. Used for querying stake details. */ struct StakeDetails { Stake stake; bool canUnlock; bool canClaim; } /* ========== STATE VARIABLES =========================================== */ uint256 public constant INTEREST_PRECISSION = 1_000_000; IERC20 public immutable masaToken; uint256 public immutable secondsForPeriod; // seconds for each period uint256 public cooldownPeriod; // period for unstaking mapping(address => Stake[]) public stakes; mapping(uint256 => uint256) public interestRates; // period => interest rate // array with all the periods uint256[] public periods; uint256 public rewardsNotReserved; // rewards not reserved for staking uint256 public rewardsReserved; // rewards reserved for staking (not yet claimed) uint256 public totalStaked; mapping(uint256 => uint256) public totalStakedForPeriod; /// @dev State variable to control the availability of staking functionality. bool public stakingEnabled; /* ========== EVENTS ==================================================== */ event StakingEnabled(address indexed by); event StakingDisabled(address indexed by); event InterestRateUpdated(address indexed by, uint256 period, uint256 rate); event CooldownPeriodUpdated(address indexed by, uint256 cooldownPeriod); event Staked( address indexed user, uint256 amount, uint256 startTime, uint256 period, uint256 interestRate, uint256 index ); event Unlocked(address indexed user, uint256 amount, uint256 index); event Claimed( address indexed user, uint256 amount, uint256 reward, uint256 index ); /* ========== INITIALIZE ================================================ */ /** * @dev Sets the initial values for the MasaToken address and initializes * roles and default interest rates for different periods. * @param _masaTokenAddress The address of the MasaToken contract. * @param _admin The address of the admin account. * @param _secondsForPeriod The number of seconds for each staking period. * @param _cooldownPeriod The period for unstaking in seconds. */ constructor( address _masaTokenAddress, address _admin, uint256 _secondsForPeriod, uint256 _cooldownPeriod ) { require(_secondsForPeriod > 0, "Invalid seconds for period"); masaToken = IERC20(_masaTokenAddress); secondsForPeriod = _secondsForPeriod; cooldownPeriod = _cooldownPeriod; // set staking enabled default stakingEnabled = true; _setupRole(DEFAULT_ADMIN_ROLE, _admin); } /* ========== RESTRICTED FUNCTIONS ====================================== */ /** * @notice Disables the staking functionality. * @dev This function can only be called by an account with the DEFAULT_ADMIN_ROLE. * It sets the `stakingEnabled` state variable to false. */ function disableStaking() external onlyRole(DEFAULT_ADMIN_ROLE) { require(stakingEnabled, "Staking is already disabled"); stakingEnabled = false; emit StakingDisabled(msg.sender); } /** * @notice Enables the staking functionality. * @dev This function can only be called by an account with the DEFAULT_ADMIN_ROLE. * It sets the `stakingEnabled` state variable to true. */ function enableStaking() external onlyRole(DEFAULT_ADMIN_ROLE) { require(!stakingEnabled, "Staking is already enabled"); stakingEnabled = true; emit StakingEnabled(msg.sender); } /** * @dev Pauses all staking and unstaking functions. Only callable by accounts * with the DEFAULT_ADMIN_ROLE. */ function pause() external onlyRole(DEFAULT_ADMIN_ROLE) { _pause(); emit Paused(msg.sender); } /** * @dev Unpauses all staking and unstaking functions. Only callable by accounts * with the DEFAULT_ADMIN_ROLE. */ function unpause() external onlyRole(DEFAULT_ADMIN_ROLE) { _unpause(); emit Unpaused(msg.sender); } /** * @dev Updates the interest rate for a specific staking period. * Only callable by accounts with the DEFAULT_ADMIN_ROLE. * @param _period The staking period to update the interest rate for. * @param _rate The new interest rate for the specified staking period. */ function setInterestRate( uint256 _period, uint256 _rate ) external onlyRole(DEFAULT_ADMIN_ROLE) { require(interestRates[_period] != _rate, "Rate is the same"); interestRates[_period] = _rate; // Add the period to the periods array if it doesn't already exist bool periodExists = false; uint periodsLength = periods.length; for (uint256 i = 0; i < periodsLength; i++) { if (periods[i] == _period) { periodExists = true; break; } } if (!periodExists) { periods.push(_period); } emit InterestRateUpdated(msg.sender, _period, _rate); } /** * @dev Updates the cooldown period for unstaking. * Only callable by accounts with the DEFAULT_ADMIN_ROLE. * @param _cooldownPeriod The new cooldown period in seconds. */ function setCooldownPeriod( uint256 _cooldownPeriod ) external onlyRole(DEFAULT_ADMIN_ROLE) { require( cooldownPeriod != _cooldownPeriod, "Cooldown period is the same" ); cooldownPeriod = _cooldownPeriod; emit CooldownPeriodUpdated(msg.sender, _cooldownPeriod); } /* ========== MUTATIVE FUNCTIONS ======================================== */ /** * @dev Allows the admin to add MasaToken to the reward pool. * @param _amount The amount of MasaToken to add to the reward pool. */ function addRewards(uint256 _amount) external nonReentrant whenNotPaused { require(_amount > 0, "Invalid amount"); rewardsNotReserved += _amount; masaToken.safeTransferFrom(msg.sender, address(this), _amount); } /** * @dev Allows users to stake a specified amount of MasaToken for a specified period. Stakes are recorded * in the stakes mapping, and users are added to the allStakers array if they haven't staked before. * @param _amount The amount of MasaToken to be staked. * @param _period The period for which the MasaToken is staked, in months. */ function stake( uint256 _amount, uint256 _period ) external nonReentrant whenNotPaused { require(stakingEnabled, "Staking is currently disabled"); require(interestRates[_period] > 0, "Invalid staking period"); require(_amount > 0, "Invalid amount"); // Calculate the reward based on the staked amount and the interest rate uint256 reward = (_amount * interestRates[_period]) / (100 * INTEREST_PRECISSION); require( rewardsNotReserved >= reward, "Not enough rewards to reserve for staking" ); totalStaked += _amount; totalStakedForPeriod[_period] += _amount; // decrease the rewards not reserved for staking rewardsNotReserved -= reward; // increase the rewards reserved for staking rewardsReserved += reward; stakes[msg.sender].push( Stake({ amount: _amount, startTime: block.timestamp, unlockTime: 0, period: _period, interestRate: interestRates[_period] }) ); masaToken.safeTransferFrom(msg.sender, address(this), _amount); emit Staked( msg.sender, _amount, block.timestamp, _period, interestRates[_period], stakes[msg.sender].length - 1 ); } /** * @dev Allows users to unlock a specific stake identified by its index after the staking period has fully elapsed. * @param _index The index of the stake within the user's array of stakes to be unlocked. */ function unlock(uint256 _index) external nonReentrant whenNotPaused { require(_index < stakes[msg.sender].length, "Invalid index"); Stake memory stakeData = stakes[msg.sender][_index]; // Ensure the current timestamp is beyond the unlock timestamp require( canUnlockStake(msg.sender, _index), "Staking period has not yet elapsed" ); stakes[msg.sender][_index].unlockTime = block.timestamp; emit Unlocked(msg.sender, stakeData.amount, _index); } /** * @dev Allows users to claim a specific stake identified by its index after the cooldown period has fully elapsed. * @param _index The index of the stake within the user's array of stakes to be claimed. */ function claim(uint256 _index) external nonReentrant whenNotPaused { require(_index < stakes[msg.sender].length, "Invalid index"); Stake memory stakeData = stakes[msg.sender][_index]; // Ensure the current timestamp is beyond the cooldown period require( canClaimStake(msg.sender, _index), "Cooldown period has not yet elapsed" ); // Calculate the reward based on the staked amount and the interest rate uint256 reward = (stakeData.amount * stakeData.interestRate) / (100 * INTEREST_PRECISSION); // Remove the stake from the user's stakes array _removeStake(msg.sender, _index); // decrease the rewards reserved for staking rewardsReserved -= reward; // Transfer the staked amount plus the reward back to the user masaToken.safeTransfer(msg.sender, stakeData.amount + reward); emit Claimed(msg.sender, stakeData.amount, reward, _index); } /* ========== VIEWS ===================================================== */ /** * @dev Returns the periods for which interest rates have been set. * @return An array of periods. */ function getPeriods() external view returns (uint256[] memory) { return periods; } /** * @dev Returns the number of stakes a user has made. * @param _user The address of the user. * @return The number of stakes. */ function getUserStakeCount(address _user) external view returns (uint256) { return stakes[_user].length; } /** * @dev Returns details of a specific stake for a user, including whether it can be unstaked. * @param _user The address of the user. * @param _index The index of the stake in the user's stakes array. * @return StakeDetails The details of the stake. */ function getUserStake( address _user, uint256 _index ) external view returns (StakeDetails memory) { require(_index < stakes[_user].length, "Invalid index"); Stake memory stakeData = stakes[_user][_index]; return StakeDetails({ stake: stakeData, canUnlock: canUnlockStake(_user, _index), canClaim: canClaimStake(_user, _index) }); } /** * @dev Returns the total staked balance of a user by summing up the amounts in all their stakes. * @param _user The address of the user whose total staked balance is being queried. * @return uint256 The total staked balance of the user. */ function getUserStakedBalance( address _user ) external view returns (uint256) { uint256 totalBalance = 0; for (uint256 i = 0; i < stakes[_user].length; i++) { totalBalance += stakes[_user][i].amount; } return totalBalance; } /** * @dev Returns the principal amounts and interest earned for each stake of a user * This function calculates the interest based on the elapsed time since each stake was made. * @param _user The address of the user whose stakes and interest are being queried. * @return principalAmounts An array of the principal amounts for each stake of the user. * @return interestEarned An array of the interest earned for each stake of the user. */ function getUserStakesWithInterest( address _user ) external view returns ( uint256[] memory principalAmounts, uint256[] memory interestEarned ) { uint256 stakeCount = stakes[_user].length; principalAmounts = new uint256[](stakeCount); interestEarned = new uint256[](stakeCount); for (uint256 i = 0; i < stakeCount; i++) { Stake memory stakeData = stakes[_user][i]; principalAmounts[i] = stakeData.amount; // Calculate seconds elapsed since the stake started uint256 secondsElapsed = block.timestamp - stakeData.startTime; // Calculate the fraction of the staking period that has elapsed uint256 secondsInPeriod = stakeData.period * secondsForPeriod; if (secondsElapsed > secondsInPeriod) { secondsElapsed = secondsInPeriod; } // Multiply by 1e18 for precision uint256 elapsedFraction = secondsInPeriod > 0 ? (secondsElapsed * 1e18) / secondsInPeriod : 0; // Calculate prorated interest based on the elapsed fraction of the period uint256 interestFraction = (stakeData.amount * stakeData.interestRate); // Note: We divide by 100 and then multiply by elapsedFraction and divide by 1e18 for precision interestEarned[i] = (interestFraction * elapsedFraction) / (100 * INTEREST_PRECISSION * 1e18); } return (principalAmounts, interestEarned); } /** * @dev Determines if a stake is eligible for unlocking based on the current timestamp. * @param _user The address of the user querying unlock eligibility. * @param _index The index of the stake within the user's array of stakes. * @return canUnlock True if the stake can be unlocked, false otherwise. */ function canUnlockStake( address _user, uint256 _index ) public view returns (bool canUnlock) { require(_index < stakes[_user].length, "Invalid index"); Stake memory stakeData = stakes[_user][_index]; if (stakeData.unlockTime > 0) { canUnlock = false; } else { uint256 secondsInPeriod = stakeData.period * secondsForPeriod; uint256 unlockTime = stakeData.startTime + secondsInPeriod; canUnlock = block.timestamp >= unlockTime; } return canUnlock; } /** * @dev Determines if a stake is eligible for claiming based on the current timestamp. * @param _user The address of the user querying claim eligibility. * @param _index The index of the stake within the user's array of stakes. * @return canClaim True if the stake can be claimed, false otherwise. */ function canClaimStake( address _user, uint256 _index ) public view returns (bool canClaim) { require(_index < stakes[_user].length, "Invalid index"); Stake memory stakeData = stakes[_user][_index]; canClaim = stakeData.unlockTime > 0 && block.timestamp >= stakeData.unlockTime + cooldownPeriod; return canClaim; } /* ========== PRIVATE FUNCTIONS ========================================= */ /** * @dev Removes a stake from the stakes array for a user by index. * This is a private function used internally by the unstake function. * @param _user The address of the user from whom the stake is removed. * @param _index The index of the stake within the user's stakes array to remove. */ function _removeStake(address _user, uint256 _index) private { require(_index < stakes[_user].length, "Invalid index"); totalStaked -= stakes[_user][_index].amount; totalStakedForPeriod[stakes[_user][_index].period] -= stakes[_user][ _index ].amount; stakes[_user][_index] = stakes[_user][stakes[_user].length - 1]; stakes[_user].pop(); } /// @notice Transfer native tokens. /// @param _amount Token amount /// @param _receiver Receiver address function rescueNativeToken( uint256 _amount, address _receiver ) external onlyRole(DEFAULT_ADMIN_ROLE) { Address.sendValue(payable(_receiver), _amount); } /// @notice Transfer tokens. /// @param _token Token contract address /// @param _amount Token amount /// @param _receiver Receiver address function rescueERC20Token( address _token, uint256 _amount, address _receiver ) external onlyRole(DEFAULT_ADMIN_ROLE) { // Ensure the contract doesn't allow rescuing the staking token in a way that affects the staked or reserved funds if (_token == address(masaToken)) { uint256 masaBalance = masaToken.balanceOf(address(this)); uint256 totalProtected = rewardsReserved + totalStaked; // This is the amount that must remain in the contract uint256 notAllocated = masaBalance - totalProtected - rewardsNotReserved; require( masaBalance - _amount >= totalProtected, "Operation would affect staked or reserved funds" ); // If rescuing the masaToken, adjust the not reserved rewards if (_amount > notAllocated) { rewardsNotReserved -= (_amount - notAllocated); } } // Perform the transfer IERC20(_token).safeTransfer(_receiver, _amount); } }