StakingDelegateRewards

Git Source

Inherits: IStakingDelegateRewards, AccessControlEnumerable

Contract for managing staking rewards with functionality to update balances, notify new rewards, and recover tokens.

Inherits from IStakingDelegateRewards and AccessControlEnumerable.

State Variables

_DEFAULT_DURATION

Default duration of rewards period in seconds (7 days).

uint256 private constant _DEFAULT_DURATION = 7 days;

TIMELOCK_ROLE

Role identifier used for protecting functions with timelock access.

bytes32 public constant TIMELOCK_ROLE = keccak256("TIMELOCK_ROLE");

_REWARDS_TOKEN

Address of the token used for rewards, immutable.

address private immutable _REWARDS_TOKEN;

_STAKING_DELEGATE

Address of the staking delegate, immutable.

address private immutable _STAKING_DELEGATE;

periodFinish

Mapping of staking tokens to the period end timestamp.

mapping(address => uint256) public periodFinish;

rewardRate

Mapping of staking tokens to their respective reward rate.

mapping(address => uint256) public rewardRate;

rewardsDuration

Mapping of staking tokens to their rewards duration.

mapping(address => uint256) public rewardsDuration;

lastUpdateTime

Mapping of staking tokens to the last update time for rewards.

mapping(address => uint256) public lastUpdateTime;

rewardPerTokenStored

Mapping of staking tokens to the accumulated reward per token.

mapping(address => uint256) public rewardPerTokenStored;

leftOver

Mapping of staking tokens to the leftover rewards.

mapping(address => uint256) public leftOver;

userRewardPerTokenPaid

Mapping of staking tokens and users to the paid-out reward per token.

mapping(address => mapping(address => uint256)) public userRewardPerTokenPaid;

rewards

Mapping of staking tokens and users to their respective rewards.

mapping(address => mapping(address => uint256)) public rewards;

rewardDistributors

Mapping of staking tokens to their reward distributors.

mapping(address => address) public rewardDistributors;

rewardReceiver

Mapping of users to their designated reward receivers.

mapping(address => address) public rewardReceiver;

Functions

constructor

Constructor that sets the rewards token and staking delegate addresses.

constructor(address rewardsToken_, address stakingDelegate_, address admin, address timeLock) payable;

Parameters

NameTypeDescription
rewardsToken_addressThe ERC20 token to be used as the rewards token.
stakingDelegate_addressThe address of the staking delegate contract.
adminaddress
timeLockaddress

getReward

Claims reward for a given staking token.

function getReward(address stakingToken) external;

Parameters

NameTypeDescription
stakingTokenaddressThe address of the staking token.

getReward

Claims reward for a given user and staking token.

function getReward(address user, address stakingToken) external;

Parameters

NameTypeDescription
useraddressThe address of the user to claim rewards for.
stakingTokenaddressThe address of the staking token.

setRewardReceiver

Sets the reward receiver who will receive your rewards instead.

This can be set to the zero address to receive rewards directly.

function setRewardReceiver(address receiver) external;

Parameters

NameTypeDescription
receiveraddressThe address of the reward receiver.

notifyRewardAmount

Notifies a new reward amount for a given staking token.

function notifyRewardAmount(address stakingToken, uint256 reward) external;

Parameters

NameTypeDescription
stakingTokenaddressThe address of the staking token to notify the reward for.
rewarduint256The amount of the new reward.

updateUserBalance

Updates the balance of a user for a given staking token.

function updateUserBalance(
    address user,
    address stakingToken,
    uint256 currentUserBalance,
    uint256 currentTotalDeposited
)
    external;

Parameters

NameTypeDescription
useraddressThe address of the user to update the balance for.
stakingTokenaddressThe address of the staking token.
currentUserBalanceuint256The current balance of staking token of the user.
currentTotalDepositeduint256The current total deposited amount of the staking token.

addStakingToken

Adds a new staking token to the contract.

function addStakingToken(address stakingToken, address rewardDistributioner) external;

Parameters

NameTypeDescription
stakingTokenaddressThe address of the staking token to add.
rewardDistributioneraddressThe address allowed to notify new rewards for the staking token.

setRewardsDuration

Sets the duration of the rewards period for a given staking token.

function setRewardsDuration(address stakingToken, uint256 rewardsDuration_) external onlyRole(TIMELOCK_ROLE);

Parameters

NameTypeDescription
stakingTokenaddressThe address of the staking token to set the rewards duration for.
rewardsDuration_uint256The new duration of the rewards period.

recoverERC20

Allows recovery of ERC20 tokens other than the staking and rewards tokens.

Added to support recovering LP Rewards from other systems such as BAL to be distributed to holders

function recoverERC20(address tokenAddress, address to, uint256 tokenAmount) external onlyRole(DEFAULT_ADMIN_ROLE);

Parameters

NameTypeDescription
tokenAddressaddressThe address of the token to recover.
toaddressThe address to send the recovered tokens to.
tokenAmountuint256The amount of tokens to recover.

getRewardForDuration

Calculates the total reward for a given duration for a staking token.

function getRewardForDuration(address stakingToken) external view returns (uint256);

Parameters

NameTypeDescription
stakingTokenaddressThe address of the staking token.

Returns

NameTypeDescription
<none>uint256The total reward for the given duration.

rewardToken

Returns the address of the rewards token.

function rewardToken() external view returns (address);

Returns

NameTypeDescription
<none>addressThe address of the rewards token.

stakingDelegate

Returns the address of the staking delegate.

function stakingDelegate() external view returns (address);

Returns

NameTypeDescription
<none>addressThe address of the staking delegate.

lastTimeRewardApplicable

Calculates the last time a reward was applicable for the given staking token.

function lastTimeRewardApplicable(address stakingToken) public view returns (uint256);

Parameters

NameTypeDescription
stakingTokenaddressThe address of the staking token.

Returns

NameTypeDescription
<none>uint256The last applicable timestamp for rewards.

rewardPerToken

Calculates the accumulated reward per token stored.

function rewardPerToken(address stakingToken) external view returns (uint256);

Parameters

NameTypeDescription
stakingTokenaddressThe address of the staking token.

Returns

NameTypeDescription
<none>uint256The accumulated reward per token.

_rewardPerToken

function _rewardPerToken(address stakingToken, uint256 currentTotalDeposited) internal view returns (uint256);

earned

Calculates the amount of reward earned by an account for a given staking token.

function earned(address account, address stakingToken) external view returns (uint256);

Parameters

NameTypeDescription
accountaddressThe address of the user's account.
stakingTokenaddressThe address of the staking token.

Returns

NameTypeDescription
<none>uint256The amount of reward earned.

_earned

function _earned(
    address account,
    address stakingToken,
    uint256 userBalance,
    uint256 rewardPerToken_
)
    internal
    view
    returns (uint256);

_getReward

Updates the reward state for a given user and staking token. If there are any rewards to be paid out, they are sent to the receiver that was set by the user. (Defaults to the user's address if not set)

function _getReward(address user, address stakingToken) internal;

Parameters

NameTypeDescription
useraddressThe address of the user to update rewards for.
stakingTokenaddressThe address of the staking token.

_updateReward

function _updateReward(address account, address stakingToken) internal;

_updateReward

Updates reward state for a given user and staking token.

function _updateReward(
    address account,
    address stakingToken,
    uint256 currentUserBalance,
    uint256 currentTotalDeposited
)
    internal;

Parameters

NameTypeDescription
accountaddressThe address of the user to update rewards for.
stakingTokenaddressThe address of the staking token.
currentUserBalanceuint256
currentTotalDepositeduint256

Events

RewardAdded

Emitted when rewards are added for a staking token.

event RewardAdded(address indexed stakingToken, uint256 rewardAmount, uint256 rewardRate, uint256 start, uint256 end);

Parameters

NameTypeDescription
stakingTokenaddressThe staking token for which rewards are added.
rewardAmountuint256The amount of rewards added.
rewardRateuint256The rate at which rewards will be distributed.
startuint256The start time of the reward period.
enduint256The end time of the reward period.

StakingTokenAdded

Emitted when a staking token is added to the rewards program.

event StakingTokenAdded(address indexed stakingToken, address rewardDistributioner);

Parameters

NameTypeDescription
stakingTokenaddressThe staking token that was added.
rewardDistributioneraddressThe address authorized to distribute rewards for the staking token.

UserBalanceUpdated

Emitted when a user's balance is updated for a staking token.

event UserBalanceUpdated(address indexed user, address indexed stakingToken);

Parameters

NameTypeDescription
useraddressThe user whose balance was updated.
stakingTokenaddressThe staking token for which the balance was updated.

RewardPaid

Emitted when rewards are paid out to a user for a staking token.

event RewardPaid(address indexed user, address indexed stakingToken, uint256 reward, address receiver);

Parameters

NameTypeDescription
useraddressThe user who received the rewards.
stakingTokenaddressThe staking token for which the rewards were paid.
rewarduint256The amount of rewards paid.
receiveraddressThe address that received the rewards.

RewardsDurationUpdated

Emitted when the rewards duration is updated for a staking token.

event RewardsDurationUpdated(address indexed stakingToken, uint256 newDuration);

Parameters

NameTypeDescription
stakingTokenaddressThe staking token for which the duration was updated.
newDurationuint256The new duration for rewards.

Recovered

Emitted when tokens are recovered from the contract.

event Recovered(address token, uint256 amount);

Parameters

NameTypeDescription
tokenaddressThe address of the token that was recovered.
amountuint256The amount of the token that was recovered.

RewardReceiverSet

Emitted when a user sets a reward receiver address.

event RewardReceiverSet(address indexed user, address receiver);

Parameters

NameTypeDescription
useraddressThe user who set the reward receiver.
receiveraddressThe address set as the reward receiver.