MiniChefV3

Git Source

Inherits: Multicall, AccessControlEnumerable, Rescuable, SelfPermit, Pausable

A contract for managing staking, rewards, and pool information for liquidity providers.

This contract handles the logic for staking LP tokens, distributing rewards, and managing pool information. It supports multiple reward tokens through external rewarder contracts and includes emergency withdrawal functionality. It does not support rebasing or fee on transfer tokens.

State Variables

REWARD_TOKEN

Address of REWARD_TOKEN contract.

IERC20 public immutable REWARD_TOKEN;

_poolInfo

Info of each MCV3 pool.

PoolInfo[] private _poolInfo;

lpToken

Address of the LP token for each MCV3 pool.

IERC20[] public lpToken;

lpSupply

Total amount of LP token staked in each MCV3 pool.

uint256[] public lpSupply;

rewarder

Address of each IRewarder contract in MCV3.

IMiniChefV3Rewarder[] public rewarder;

_userInfo

Info of each user that stakes LP tokens.

mapping(uint256 => mapping(address => UserInfo)) private _userInfo;

_pidPlusOne

PID of the LP token plus one.

mapping(address => uint256) private _pidPlusOne;

totalAllocPoint

Total allocation points. Must be the sum of all allocation points in all pools.

uint256 public totalAllocPoint;

rewardPerSecond

The amount of REWARD_TOKEN distributed per second.

uint256 public rewardPerSecond;

availableReward

The amount of REWARD_TOKEN available in this contract for distribution.

uint256 public availableReward;

MAX_REWARD_TOKEN_PER_SECOND

The maximum amount of REWARD_TOKEN that can be distributed per second.

uint256 public constant MAX_REWARD_TOKEN_PER_SECOND = 100_000_000 ether / uint256(1 weeks);

_ACC_REWARD_TOKEN_PRECISION

Precision factor to calculate accumulated reward tokens per share.

uint256 private constant _ACC_REWARD_TOKEN_PRECISION = 1e12;

TIMELOCK_ROLE

The timelock role for the contract.

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

PAUSER_ROLE

The pauser role for the contract.

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

Functions

constructor

Constructs the MiniChefV3 contract with a specified reward token and admin address.

constructor(IERC20 rewardToken_, address admin, address pauser) payable;

Parameters

NameTypeDescription
rewardToken_IERC20The ERC20 token to be used as the reward token.
adminaddressThe address that will be granted the default admin role.
pauseraddress

poolLength

Returns the number of MCV3 pools.

function poolLength() public view returns (uint256 pools);

pidOfLPToken

Retrieves the pool ID of a given LP token.

Returns the pool ID by looking up the LP token address in the _pidPlusOne mapping.

function pidOfLPToken(IERC20 lpToken_) external view returns (uint256 pid);

Parameters

NameTypeDescription
lpToken_IERC20The LP token to query the pool ID for.

Returns

NameTypeDescription
piduint256The pool ID of the given LP token.

isLPTokenAdded

Checks if an LP token has been added to any pool.

The unchecked block is used here because the subtraction is safe from underflow. The condition above ensures that pidPlusOne is greater than zero, so subtracting one will not underflow.

Returns true if the LP token exists in the _pidPlusOne mapping.

function isLPTokenAdded(IERC20 lpToken_) external view returns (bool added);

Parameters

NameTypeDescription
lpToken_IERC20The LP token to check.

Returns

NameTypeDescription
addedboolTrue if the LP token has been added to a pool.

getUserInfo

Retrieves user information for a specific pool.

Returns the user's staked amount, reward debt, and unpaid rewards for a given pool.

function getUserInfo(uint256 pid, address user) external view returns (UserInfo memory info);

Parameters

NameTypeDescription
piduint256The pool ID to retrieve user information for.
useraddressThe user address to retrieve information for.

Returns

NameTypeDescription
infoUserInfoThe user's information for the specified pool.

getPoolInfo

Retrieves pool information for a specific pool ID.

Returns the pool's accumulated reward per share, last reward time, and allocation points.

function getPoolInfo(uint256 pid) external view returns (PoolInfo memory info);

Parameters

NameTypeDescription
piduint256The pool ID to retrieve information for.

Returns

NameTypeDescription
infoPoolInfoThe pool's information.

add

Add a new LP to the pool. Can only be called by the owner. DO NOT add the same LP token more than once. Rewards will be messed up if you do.

function add(uint32 allocPoint, IERC20 lpToken_, IMiniChefV3Rewarder rewarder_) public onlyRole(TIMELOCK_ROLE);

Parameters

NameTypeDescription
allocPointuint32AP of the new pool.
lpToken_IERC20Address of the LP ERC-20 token.
rewarder_IMiniChefV3RewarderAddress of the rewarder delegate.

set

Update the given pool's REWARD_TOKEN allocation point and IRewarder contract. Can only be called by the owner.

function set(
    uint256 pid,
    uint32 allocPoint,
    IERC20 lpToken_,
    IMiniChefV3Rewarder rewarder_,
    bool overwrite
)
    public
    onlyRole(TIMELOCK_ROLE);

Parameters

NameTypeDescription
piduint256The index of the pool. See _poolInfo.
allocPointuint32New AP of the pool.
lpToken_IERC20Address of the LP ERC-20 token.
rewarder_IMiniChefV3RewarderAddress of the rewarder delegate.
overwriteboolTrue if rewarder_ should be set. Otherwise rewarder_ is ignored.

rescue

function rescue(IERC20 token, address to, uint256 amount) external onlyRole(DEFAULT_ADMIN_ROLE);

setRewardPerSecond

Sets the reward per second to be distributed. Can only be called by the owner.

function setRewardPerSecond(uint256 rewardPerSecond_) public onlyRole(TIMELOCK_ROLE);

Parameters

NameTypeDescription
rewardPerSecond_uint256The amount of reward token to be distributed per second.

commitReward

Commits REWARD_TOKEN to the contract for distribution.

function commitReward(uint256 amount) external;

Parameters

NameTypeDescription
amountuint256The amount of REWARD_TOKEN to commit.

pendingReward

View function to see pending REWARD_TOKEN on frontend.

function pendingReward(uint256 pid, address user_) external view returns (uint256 pending);

Parameters

NameTypeDescription
piduint256The index of the pool. See _poolInfo.
user_addressAddress of user.

Returns

NameTypeDescription
pendinguint256REWARD_TOKEN reward for a given user.

updatePool

Update reward variables of the given pool.

function updatePool(uint256 pid) public returns (PoolInfo memory pool);

Parameters

NameTypeDescription
piduint256The index of the pool. See _poolInfo.

Returns

NameTypeDescription
poolPoolInfoReturns the pool that was updated.

deposit

Deposit LP tokens to MCV3 for REWARD_TOKEN allocation.

Deposits can be paused in case of emergencies by the admin or pauser roles.

function deposit(uint256 pid, uint256 amount, address to) public whenNotPaused;

Parameters

NameTypeDescription
piduint256The index of the pool. See _poolInfo.
amountuint256LP token amount to deposit.
toaddressThe receiver of amount deposit benefit.

harvestAndWithdraw

Withdraw LP tokens from MCV3.

+= is slightly more gas efficient (5300) than the alternative (5365) using solidity 0.8.18. The rule only applies to non-mapping storage variables.

function harvestAndWithdraw(uint256 pid, uint256 amount, address to) public;

Parameters

NameTypeDescription
piduint256The index of the pool. See _poolInfo.
amountuint256LP token amount to withdraw.
toaddressReceiver of the LP tokens.

harvest

Harvest proceeds for transaction sender to to.

unchecked is used as the subtraction is guaranteed to not underflow because rewardAmount is always less than or equal to availableReward_.

function harvest(uint256 pid, address to) public;

Parameters

NameTypeDescription
piduint256The index of the pool. See _poolInfo.
toaddressReceiver of REWARD_TOKEN rewards.

emergencyWithdraw

Withdraw without caring about rewards. EMERGENCY ONLY.

unchecked is used as the subtraction is guaranteed to not underflow because rewardAmount is always less than or equal to availableReward_.

function emergencyWithdraw(uint256 pid, address to) public;

Parameters

NameTypeDescription
piduint256The index of the pool. See _poolInfo.
toaddressReceiver of the LP tokens.

pause

Pauses the contract. Only callable by PAUSER_ROLE or DEFAULT_ADMIN_ROLE.

function pause() external;

unpause

Unpauses the contract. Only callable by DEFAULT_ADMIN_ROLE.

function unpause() external onlyRole(DEFAULT_ADMIN_ROLE);

Events

Deposit

Emitted when a user deposits LP tokens into a pool.

event Deposit(address indexed user, uint256 indexed pid, uint256 amount, address indexed to);

Parameters

NameTypeDescription
useraddressThe address of the user making the deposit.
piduint256The pool ID into which the deposit is made.
amountuint256The amount of LP tokens deposited.
toaddressThe address receiving the deposit.

Withdraw

Emitted when a user withdraws LP tokens from a pool.

event Withdraw(address indexed user, uint256 indexed pid, uint256 amount, address indexed to);

Parameters

NameTypeDescription
useraddressThe address of the user making the withdrawal.
piduint256The pool ID from which the withdrawal is made.
amountuint256The amount of LP tokens withdrawn.
toaddressThe address receiving the withdrawn LP tokens.

EmergencyWithdraw

Emitted when a user performs an emergency withdrawal of LP tokens from a pool.

event EmergencyWithdraw(address indexed user, uint256 indexed pid, uint256 amount, address indexed to);

Parameters

NameTypeDescription
useraddressThe address of the user making the emergency withdrawal.
piduint256The pool ID from which the emergency withdrawal is made.
amountuint256The amount of LP tokens emergency withdrawn.
toaddressThe address receiving the emergency withdrawn LP tokens.

Harvest

Emitted when a user harvests reward tokens from a pool.

event Harvest(address indexed user, uint256 indexed pid, uint256 amount);

Parameters

NameTypeDescription
useraddressThe address of the user performing the harvest.
piduint256The pool ID from which the harvest is performed.
amountuint256The amount of reward tokens harvested.

LogPoolAddition

Emitted when a new pool is added to the MiniChef contract.

event LogPoolAddition(
    uint256 indexed pid, uint256 allocPoint, IERC20 indexed lpToken, IMiniChefV3Rewarder indexed rewarder
);

Parameters

NameTypeDescription
piduint256The pool ID of the newly added pool.
allocPointuint256The number of allocation points assigned to the new pool.
lpTokenIERC20The address of the LP token for the new pool.
rewarderIMiniChefV3RewarderThe address of the rewarder contract for the new pool.

LogSetPool

Emitted when a pool's configuration is updated.

event LogSetPool(uint256 indexed pid, uint256 allocPoint, IMiniChefV3Rewarder indexed rewarder, bool overwrite);

Parameters

NameTypeDescription
piduint256The pool ID of the updated pool.
allocPointuint256The new number of allocation points assigned to the pool.
rewarderIMiniChefV3RewarderThe address of the rewarder contract for the pool.
overwriteboolIndicates whether the update overwrites the existing rewarder contract.

LogUpdatePool

Emitted when a pool's rewards are updated.

event LogUpdatePool(uint256 indexed pid, uint64 lastRewardTime, uint256 lpSupply, uint256 accRewardPerShare);

Parameters

NameTypeDescription
piduint256The pool ID of the updated pool.
lastRewardTimeuint64The last timestamp when the pool's rewards were calculated.
lpSupplyuint256The total amount of LP tokens staked in the pool.
accRewardPerShareuint256The accumulated reward tokens per share, scaled to precision.

LogRewardPerSecond

Emitted when the reward per second is updated.

event LogRewardPerSecond(uint256 rewardPerSecond);

Parameters

NameTypeDescription
rewardPerSeconduint256The new amount of reward tokens distributed per second.

LogRewardCommitted

Emitted when a reward amount is committed for distribution.

event LogRewardCommitted(uint256 amount);

Parameters

NameTypeDescription
amountuint256The amount of reward tokens committed.

LogRewarderEmergencyWithdrawFaulty

Emitted when an emergency withdrawal from a rewarder contract is faulty.

event LogRewarderEmergencyWithdrawFaulty(address indexed user, uint256 indexed pid, uint256 amount, address indexed to);

Parameters

NameTypeDescription
useraddressThe address of the user performing the emergency withdrawal.
piduint256The pool ID from which the emergency withdrawal is made.
amountuint256The amount of tokens emergency withdrawn.
toaddressThe address receiving the emergency withdrawn tokens.

Structs

UserInfo

struct UserInfo {
    uint256 amount;
    uint256 rewardDebt;
    uint256 unpaidRewards;
}

PoolInfo

struct PoolInfo {
    uint160 accRewardPerShare;
    uint64 lastRewardTime;
    uint32 allocPoint;
}