forked from Dracula-Protocol/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRewardPool.sol
173 lines (149 loc) · 6.04 KB
/
RewardPool.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "./IRewardDistributor.sol";
import "./DraculaToken.sol";
/// @title A reward pool that does not mint
/// @dev The rewards are transferred to the pool by calling `fundPool`.
/// Only the reward distributor can notify.
contract RewardPool is IRewardDistributor, ReentrancyGuard {
using SafeMath for uint256;
using SafeERC20 for IERC20;
using SafeERC20 for DraculaToken;
DraculaToken public dracula;
IERC20 public rewardToken;
uint256 public rewardsDuration;
uint256 public periodFinish;
uint256 public rewardRate;
uint256 public lastUpdateTime;
uint256 public rewardPerTokenStored;
uint256 public burnRate = 1; // default 1%
uint256 public totalStaked;
mapping(address => uint256) private stakedBalances;
mapping(address => uint256) public userRewardPerTokenPaid;
mapping(address => uint256) public rewards;
event RewardAdded(uint256 reward);
event Staked(address indexed user, uint256 amount);
event Withdrawn(address indexed user, uint256 amount);
event RewardPaid(address indexed user, uint256 reward);
modifier updateReward(address account) {
rewardPerTokenStored = rewardPerToken();
lastUpdateTime = lastTimeRewardApplicable();
if (account != address(0)) {
rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
}
_;
}
constructor(
address _rewardToken,
DraculaToken _dracula,
uint256 _rewardsDuration,
address _rewardDistributor) public
IRewardDistributor(_rewardDistributor)
{
rewardToken = IERC20(_rewardToken);
dracula = _dracula;
rewardsDuration = _rewardsDuration;
}
function balanceOf(address account) external view returns (uint256) {
return stakedBalances[account];
}
function setBurnRate(uint256 _burnRate) external onlyOwner {
require(_burnRate <= 10, "Invalid burn rate value");
burnRate = _burnRate;
}
function lastTimeRewardApplicable() public view returns (uint256) {
return Math.min(block.timestamp, periodFinish);
}
function rewardPerToken() public view returns (uint256) {
if (totalStaked == 0) {
return rewardPerTokenStored;
}
return
rewardPerTokenStored.add(
lastTimeRewardApplicable()
.sub(lastUpdateTime)
.mul(rewardRate)
.mul(1e18)
.div(totalStaked)
);
}
function rewardForDuration() external view returns (uint256) {
return rewardRate.mul(rewardsDuration);
}
/// @notice Calculate the earned rewards for an account
/// @return amount earned by specified account
function earned(address account) public view returns (uint256) {
return
stakedBalances[account]
.mul(rewardPerToken().sub(userRewardPerTokenPaid[account]))
.div(1e18)
.add(rewards[account]);
}
/// @notice Stake specified amount
function stake(uint256 amount) external nonReentrant updateReward(msg.sender) {
require(amount > 0, "Cannot stake 0");
totalStaked = totalStaked.add(amount);
stakedBalances[msg.sender] = stakedBalances[msg.sender].add(amount);
dracula.safeTransferFrom(msg.sender, address(this), amount);
emit Staked(msg.sender, amount);
}
/// @notice Withdraw specified amount and collect rewards
function unstake(uint256 amount) external {
withdraw(amount);
getReward();
}
/// @notice Claims reward for the sender account
function getReward() public nonReentrant updateReward(msg.sender) {
uint256 reward = rewards[msg.sender];
if (reward > 0) {
rewards[msg.sender] = 0;
rewardToken.safeTransfer(msg.sender, reward);
emit RewardPaid(msg.sender, reward);
}
}
/// @notice Withdraw specified amount
/// @dev A configurable percentage is burnt on withdrawal
function withdraw(uint256 amount) internal nonReentrant updateReward(msg.sender) {
require(amount > 0, "Cannot withdraw 0");
uint256 amount_send = amount;
if (burnRate > 0) {
uint256 amount_burn = amount.mul(burnRate).div(100);
amount_send = amount.sub(amount_burn);
require(amount == amount_send + amount_burn, "Burn value invalid");
dracula.burn(amount_burn);
}
totalStaked = totalStaked.sub(amount);
stakedBalances[msg.sender] = stakedBalances[msg.sender].sub(amount);
dracula.safeTransfer(msg.sender, amount_send);
emit Withdrawn(msg.sender, amount_send);
}
/// @notice Transfers reward amount to pool and updates reward rate
/// @dev Should be called by external mechanism
function fundPool(uint256 reward)
external
override
onlyRewardDistributor
updateReward(address(0))
{
// overflow fix according to https://sips.synthetix.io/sips/sip-77
require(reward < uint(-1) / 1e18, "the notified reward cannot invoke multiplication overflow");
rewardToken.safeTransferFrom(msg.sender, address(this), reward);
if (block.timestamp >= periodFinish) {
rewardRate = reward.div(rewardsDuration);
} else {
uint256 remaining = periodFinish.sub(block.timestamp);
uint256 leftover = remaining.mul(rewardRate);
rewardRate = reward.add(leftover).div(rewardsDuration);
}
lastUpdateTime = block.timestamp;
periodFinish = block.timestamp.add(rewardsDuration);
emit RewardAdded(reward);
}
}