Skip to content

Commit 27f9b06

Browse files
committed
add comments to staking contract (#86)
1 parent ce2ae7e commit 27f9b06

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

contracts/staking/StakingRewardsV2.sol

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,34 @@ contract StakingRewardsV2 is RewardsDistributionRecipient, ReentrancyGuard {
1717

1818
/* ========== STATE VARIABLES ========== */
1919

20+
/// @notice Token to be distributed to stakers
2021
IERC20 public rewardsToken;
22+
23+
/// @notice Token to be staked in the contract
2124
IERC20 public stakingToken;
25+
26+
/// @notice Timestamp at which the staking rewards end
2227
uint256 public periodFinish = 0;
28+
29+
/// @notice Number of tokens rewarded per unit time
2330
uint256 public rewardRate = 0;
31+
32+
/// @notice Duration for which rewards are applied
2433
uint256 public rewardsDuration;
34+
2535
uint256 public lastUpdateTime;
2636
uint256 public rewardPerTokenStored;
2737

2838
mapping(address => uint256) public userRewardPerTokenPaid;
39+
40+
/// @notice Accumulated number of `rewardsToken` per address remaining to be claimed.
41+
/// Once a staker claims the rewards, it is reset to 0.
2942
mapping(address => uint256) public rewards;
3043

44+
/// @notice Total number of staked tokens
3145
uint256 private _totalSupply;
46+
47+
/// @notice Number of staked tokens per address
3248
mapping(address => uint256) private _balances;
3349

3450
/* ========== CONSTRUCTOR ========== */
@@ -47,14 +63,21 @@ contract StakingRewardsV2 is RewardsDistributionRecipient, ReentrancyGuard {
4763

4864
/* ========== VIEWS ========== */
4965

66+
// Number of total staked tokens
5067
function totalSupply() external view returns (uint256) {
5168
return _totalSupply;
5269
}
5370

71+
/**
72+
* @notice Get the number of tokens staked by the `account`
73+
* @param account The address of the account to get the balance of
74+
* @return The number of tokens staked
75+
*/
5476
function balanceOf(address account) external view returns (uint256) {
5577
return _balances[account];
5678
}
5779

80+
/// @return last time when the reward was applied.
5881
function lastTimeRewardApplicable() public view returns (uint256) {
5982
return Math.min(block.timestamp, periodFinish);
6083
}
@@ -69,16 +92,21 @@ contract StakingRewardsV2 is RewardsDistributionRecipient, ReentrancyGuard {
6992
);
7093
}
7194

95+
/**
96+
* @notice Accumulated number of `rewardsToken` remaining to be claimed by `address`.
97+
*/
7298
function earned(address account) public view returns (uint256) {
7399
return _balances[account].mul(rewardPerToken().sub(userRewardPerTokenPaid[account])).div(1e18).add(rewards[account]);
74100
}
75101

102+
/// @notice Get the number of rewardsToken to be rewarded for `rewardsDuration`
76103
function getRewardForDuration() external view returns (uint256) {
77104
return rewardRate.mul(rewardsDuration);
78105
}
79106

80107
/* ========== MUTATIVE FUNCTIONS ========== */
81108

109+
/// @notice Transfer `amount` of stakingToken from `msg.sender` to this contract for staking
82110
function stake(uint256 amount) external nonReentrant updateReward(msg.sender) {
83111
require(amount > 0, "Cannot stake 0");
84112
_totalSupply = _totalSupply.add(amount);
@@ -87,6 +115,7 @@ contract StakingRewardsV2 is RewardsDistributionRecipient, ReentrancyGuard {
87115
emit Staked(msg.sender, amount);
88116
}
89117

118+
/// @notice Unstake `amount` number of staked tokens by `msg.sender`
90119
function withdraw(uint256 amount) public nonReentrant updateReward(msg.sender) {
91120
require(amount > 0, "Cannot withdraw 0");
92121
_totalSupply = _totalSupply.sub(amount);
@@ -95,6 +124,7 @@ contract StakingRewardsV2 is RewardsDistributionRecipient, ReentrancyGuard {
95124
emit Withdrawn(msg.sender, amount);
96125
}
97126

127+
/// @notice Transfer the staking rewards from contract to `msg.sender`
98128
function getReward() public nonReentrant updateReward(msg.sender) {
99129
uint256 reward = rewards[msg.sender];
100130
if (reward > 0) {
@@ -104,13 +134,22 @@ contract StakingRewardsV2 is RewardsDistributionRecipient, ReentrancyGuard {
104134
}
105135
}
106136

137+
/// @notice Unstake all the staked tokens by `msg.sender` and claim the rewards
107138
function exit() external {
108139
withdraw(_balances[msg.sender]);
109140
getReward();
110141
}
111142

112143
/* ========== RESTRICTED FUNCTIONS ========== */
113144

145+
/**
146+
* @notice Update the number of `rewardsToken` to be rewarded to stakers.
147+
The updated reward is only applicable from the next block.
148+
If the staking period is over, ie, if the current block occurs at or after
149+
`periodFinish` has passed, a fresh reward period starts with
150+
duration=`rewardsDuration`.
151+
* @param reward number of `rewardsToken` to to be rewarded from now on.
152+
*/
114153
function notifyRewardAmount(uint256 reward) external override onlyRewardsDistribution updateReward(address(0)) {
115154
if (block.timestamp >= periodFinish) {
116155
rewardRate = reward.div(rewardsDuration);
@@ -134,6 +173,7 @@ contract StakingRewardsV2 is RewardsDistributionRecipient, ReentrancyGuard {
134173

135174
/* ========== MODIFIERS ========== */
136175

176+
/// @notice Update rewards for an address along with bookkeeping variables
137177
modifier updateReward(address account) {
138178
rewardPerTokenStored = rewardPerToken();
139179
lastUpdateTime = lastTimeRewardApplicable();

0 commit comments

Comments
 (0)