MultiRewards

Git Source

Inherits: ReentrancyGuard, Pausable, IMultiRewards

Fork of https://github.com/curvefi/multi-rewards with hooks on stake/withdraw of LP tokens

State Variables

stakingToken

The token that users stake to earn rewards

This is the base token that users deposit into the contract

ERC20 public immutable stakingToken;

rewardData

Stores reward-related data for each reward token

Maps reward token addresses to their Reward struct containing distribution parameters

mapping(address => Reward) public override rewardData;

rewardTokens

Array of all reward token addresses

Used to iterate through all reward tokens when updating or claiming rewards

address[] public rewardTokens;

userRewardPerTokenPaid

Tracks the reward per token paid to each user for each reward token

Maps user address to reward token address to amount already paid Used to calculate new rewards since last claim

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

rewards

Tracks the unclaimed rewards for each user for each reward token

Maps user address to reward token address to unclaimed amount

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

_totalSupply

The total amount of staking tokens in the contract

Used to calculate rewards per token

uint256 internal _totalSupply;

_balances

Maps user addresses to their staked token balance

Internal mapping used to track individual stake amounts

mapping(address => uint256) internal _balances;

Functions

updateReward

Updates the reward for the given account before executing the function body.

modifier updateReward(address account);

Parameters

NameTypeDescription
accountaddressaddress The account to update the reward for.

constructor

Constructs the MultiRewards contract.

constructor(address _stakingToken);

Parameters

NameTypeDescription
_stakingTokenaddressaddress The token that users stake to earn rewards.

totalSupply

Returns the total amount of staked tokens in the contract

function totalSupply() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256uint256 The total supply of staked tokens

balanceOf

Returns the balance of staked tokens for the given account

function balanceOf(address account) external view returns (uint256 _balance);

Parameters

NameTypeDescription
accountaddressThe account to get the balance for

Returns

NameTypeDescription
_balanceuint256The balance of staked tokens

lastTimeRewardApplicable

Calculates the last time reward is applicable for a given rewards token

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

Parameters

NameTypeDescription
_rewardsTokenaddressThe address of the rewards token

Returns

NameTypeDescription
<none>uint256The timestamp when the reward was last applicable

rewardPerToken

Calculates the reward per token for a given rewards token

function rewardPerToken(address _rewardsToken) public view returns (uint256);

Parameters

NameTypeDescription
_rewardsTokenaddressThe address of the rewards token

Returns

NameTypeDescription
<none>uint256The reward amount per token

earned

Calculates the earned rewards for a given account and rewards token

function earned(address account, address _rewardsToken)
    public
    view
    returns (uint256);

Parameters

NameTypeDescription
accountaddressThe address of the account
_rewardsTokenaddressThe address of the rewards token

Returns

NameTypeDescription
<none>uint256The amount of rewards earned

getRewardForDuration

Calculates the total reward for the duration of a given rewards token

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

Parameters

NameTypeDescription
_rewardsTokenaddressThe address of the rewards token

Returns

NameTypeDescription
<none>uint256The total reward amount for the duration of a given rewards token

stake

Stakes tokens into the contract

Transfers amount of staking tokens from the user to this contract

function stake(uint256 amount)
    external
    nonReentrant
    whenNotPaused
    updateReward(msg.sender);

Parameters

NameTypeDescription
amountuint256The amount of tokens to stake

onStake

Hook called in the stake function after transfering staking token in

function onStake(uint256 amount) internal virtual;

Parameters

NameTypeDescription
amountuint256The amount of staking token transferred in to the contract

withdraw

Withdraws staked tokens from the contract

Transfers amount of staking tokens back to the user

function withdraw(uint256 amount)
    public
    nonReentrant
    updateReward(msg.sender);

Parameters

NameTypeDescription
amountuint256The amount of tokens to withdraw

onWithdraw

Hook called in withdraw function before transferring staking token out

function onWithdraw(uint256 amount) internal virtual;

Parameters

NameTypeDescription
amountuint256The amount of staking token to be transferred out of the contract

getRewardForUser

Claims all pending rewards for a specified user

Iterates through all reward tokens and transfers any accrued rewards to the user

function getRewardForUser(address _user)
    public
    nonReentrant
    updateReward(_user);

Parameters

NameTypeDescription
_useraddressThe address of the user to claim rewards for

onReward

Hook called in getRewardForUser function

function onReward() internal virtual;

getReward

Claims all pending rewards for the caller

Transfers all accrued rewards to the caller

function getReward() public;

exit

Withdraws all staked tokens and claims pending rewards

Combines withdraw and getReward operations

function exit() external;

_addReward

Adds a reward token to the contract.

function _addReward(
    address _rewardsToken,
    address _rewardsDistributor,
    uint256 _rewardsDuration
) internal;

Parameters

NameTypeDescription
_rewardsTokenaddressaddress The address of the reward token.
_rewardsDistributoraddressaddress The address of the rewards distributor.
_rewardsDurationuint256uint256 The duration of the rewards period.

_notifyRewardAmount

Notifies the contract that reward tokens is being sent to the contract.

function _notifyRewardAmount(address _rewardsToken, uint256 reward)
    internal
    whenNotPaused
    updateReward(address(0));

Parameters

NameTypeDescription
_rewardsTokenaddressaddress The address of the reward token.
rewarduint256uint256 The amount of reward tokens is being sent to the contract.

_recoverERC20

Recovers ERC20 tokens sent to the contract.

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

function _recoverERC20(address to, address tokenAddress, uint256 tokenAmount)
    internal;

Parameters

NameTypeDescription
toaddressaddress The address to send the tokens to.
tokenAddressaddressaddress The address of the token to withdraw.
tokenAmountuint256uint256 The amount of tokens to withdraw.

_setRewardsDuration

Updates the reward duration for a reward token.

function _setRewardsDuration(address _rewardsToken, uint256 _rewardsDuration)
    internal;

Parameters

NameTypeDescription
_rewardsTokenaddressaddress The address of the reward token.
_rewardsDurationuint256uint256 The new duration of the rewards period.