-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathVaultFlipToCake.sol
333 lines (265 loc) · 12.3 KB
/
VaultFlipToCake.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;
/*
___ _ _
| _ )_ _ _ _ _ _ _ _ | | | |
| _ \ || | ' \| ' \ || | |_| |_|
|___/\_,_|_||_|_||_\_, | (_) (_)
|__/
*
* MIT License
* ===========
*
* Copyright (c) 2020 BunnyFinance
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/
import "@openzeppelin/contracts/math/Math.sol";
import "@pancakeswap/pancake-swap-lib/contracts/math/SafeMath.sol";
import "@pancakeswap/pancake-swap-lib/contracts/token/BEP20/SafeBEP20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import "../library/RewardsDistributionRecipientUpgradeable.sol";
import {PoolConstant} from "../library/PoolConstant.sol";
import "../interfaces/IStrategy.sol";
import "../interfaces/IMasterChef.sol";
import "../interfaces/IBunnyMinter.sol";
import "./VaultController.sol";
contract VaultFlipToCake is VaultController, IStrategy, RewardsDistributionRecipientUpgradeable, ReentrancyGuardUpgradeable {
using SafeMath for uint;
using SafeBEP20 for IBEP20;
/* ========== CONSTANTS ============= */
address private constant CAKE = 0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82;
IMasterChef private constant CAKE_MASTER_CHEF = IMasterChef(0x73feaa1eE314F8c655E354234017bE2193C9E24E);
PoolConstant.PoolTypes public constant override poolType = PoolConstant.PoolTypes.FlipToCake;
/* ========== STATE VARIABLES ========== */
IStrategy private _rewardsToken;
uint public periodFinish;
uint public rewardRate;
uint public rewardsDuration;
uint public lastUpdateTime;
uint public rewardPerTokenStored;
mapping(address => uint) public userRewardPerTokenPaid;
mapping(address => uint) public rewards;
uint private _totalSupply;
mapping(address => uint) private _balances;
uint public override pid;
mapping(address => uint) private _depositedAt;
/* ========== MODIFIERS ========== */
modifier updateReward(address account) {
rewardPerTokenStored = rewardPerToken();
lastUpdateTime = lastTimeRewardApplicable();
if (account != address(0)) {
rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
}
_;
}
/* ========== EVENTS ========== */
event RewardAdded(uint reward);
event RewardsDurationUpdated(uint newDuration);
/* ========== INITIALIZER ========== */
function initialize(uint _pid, address _token) external initializer {
__VaultController_init(IBEP20(_token));
__RewardsDistributionRecipient_init();
__ReentrancyGuard_init();
_stakingToken.safeApprove(address(CAKE_MASTER_CHEF), uint(- 1));
pid = _pid;
rewardsDuration = 4 hours;
rewardsDistribution = msg.sender;
setMinter(0x8cB88701790F650F273c8BB2Cc4c5f439cd65219);
setRewardsToken(0xEDfcB78e73f7bA6aD2D829bf5D462a0924da28eD);
}
/* ========== VIEWS ========== */
function totalSupply() external view override returns (uint) {
return _totalSupply;
}
function balance() override external view returns (uint) {
return _totalSupply;
}
function balanceOf(address account) external view override returns (uint) {
return _balances[account];
}
function sharesOf(address account) external view override returns (uint) {
return _balances[account];
}
function principalOf(address account) external view override returns (uint) {
return _balances[account];
}
function depositedAt(address account) external view override returns (uint) {
return _depositedAt[account];
}
function withdrawableBalanceOf(address account) public view override returns (uint) {
return _balances[account];
}
function rewardsToken() external view override returns (address) {
return address(_rewardsToken);
}
function priceShare() external view override returns (uint) {
return 1e18;
}
function lastTimeRewardApplicable() public view returns (uint) {
return Math.min(block.timestamp, periodFinish);
}
function rewardPerToken() public view returns (uint) {
if (_totalSupply == 0) {
return rewardPerTokenStored;
}
return
rewardPerTokenStored.add(
lastTimeRewardApplicable().sub(lastUpdateTime).mul(rewardRate).mul(1e18).div(_totalSupply)
);
}
function earned(address account) override public view returns (uint) {
return _balances[account].mul(rewardPerToken().sub(userRewardPerTokenPaid[account])).div(1e18).add(rewards[account]);
}
function getRewardForDuration() external view returns (uint) {
return rewardRate.mul(rewardsDuration);
}
/* ========== MUTATIVE FUNCTIONS ========== */
function deposit(uint amount) override public {
_deposit(amount, msg.sender);
}
function depositAll() override external {
deposit(_stakingToken.balanceOf(msg.sender));
}
function withdraw(uint amount) override public nonReentrant updateReward(msg.sender) {
require(amount > 0, "VaultFlipToCake: amount must be greater than zero");
_totalSupply = _totalSupply.sub(amount);
_balances[msg.sender] = _balances[msg.sender].sub(amount);
uint cakeHarvested = _withdrawStakingToken(amount);
uint withdrawalFee;
if (canMint()) {
uint depositTimestamp = _depositedAt[msg.sender];
withdrawalFee = _minter.withdrawalFee(amount, depositTimestamp);
if (withdrawalFee > 0) {
uint performanceFee = withdrawalFee.div(100);
_minter.mintForV2(address(_stakingToken), withdrawalFee.sub(performanceFee), performanceFee, msg.sender, depositTimestamp);
amount = amount.sub(withdrawalFee);
}
}
_stakingToken.safeTransfer(msg.sender, amount);
emit Withdrawn(msg.sender, amount, withdrawalFee);
_harvest(cakeHarvested);
}
function withdrawAll() external override {
uint _withdraw = withdrawableBalanceOf(msg.sender);
if (_withdraw > 0) {
withdraw(_withdraw);
}
getReward();
}
function getReward() public override nonReentrant updateReward(msg.sender) {
uint reward = rewards[msg.sender];
if (reward > 0) {
rewards[msg.sender] = 0;
uint before = IBEP20(CAKE).balanceOf(address(this));
_rewardsToken.withdraw(reward);
uint cakeBalance = IBEP20(CAKE).balanceOf(address(this)).sub(before);
uint performanceFee;
if (canMint()) {
performanceFee = _minter.performanceFee(cakeBalance);
_minter.mintForV2(CAKE, 0, performanceFee, msg.sender, _depositedAt[msg.sender]);
}
IBEP20(CAKE).safeTransfer(msg.sender, cakeBalance.sub(performanceFee));
emit ProfitPaid(msg.sender, cakeBalance, performanceFee);
}
}
function harvest() public override {
uint cakeHarvested = _withdrawStakingToken(0);
_harvest(cakeHarvested);
}
/* ========== RESTRICTED FUNCTIONS ========== */
function setMinter(address newMinter) override public onlyOwner {
VaultController.setMinter(newMinter);
if (newMinter != address(0)) {
IBEP20(CAKE).safeApprove(newMinter, 0);
IBEP20(CAKE).safeApprove(newMinter, uint(- 1));
}
}
function setRewardsToken(address newRewardsToken) public onlyOwner {
require(address(_rewardsToken) == address(0), "VaultFlipToCake: rewards token already set");
_rewardsToken = IStrategy(newRewardsToken);
IBEP20(CAKE).safeApprove(newRewardsToken, 0);
IBEP20(CAKE).safeApprove(newRewardsToken, uint(- 1));
}
function notifyRewardAmount(uint reward) public override onlyRewardsDistribution {
_notifyRewardAmount(reward);
}
function setRewardsDuration(uint _rewardsDuration) external onlyOwner {
require(periodFinish == 0 || block.timestamp > periodFinish, "VaultFlipToCake: reward duration can only be updated after the period ends");
rewardsDuration = _rewardsDuration;
emit RewardsDurationUpdated(rewardsDuration);
}
/* ========== PRIVATE FUNCTIONS ========== */
function _deposit(uint amount, address _to) private nonReentrant notPaused updateReward(_to) {
require(amount > 0, "VaultFlipToCake: amount must be greater than zero");
_totalSupply = _totalSupply.add(amount);
_balances[_to] = _balances[_to].add(amount);
_depositedAt[_to] = block.timestamp;
_stakingToken.safeTransferFrom(msg.sender, address(this), amount);
uint cakeHarvested = _depositStakingToken(amount);
emit Deposited(_to, amount);
_harvest(cakeHarvested);
}
function _depositStakingToken(uint amount) private returns (uint cakeHarvested) {
uint before = IBEP20(CAKE).balanceOf(address(this));
CAKE_MASTER_CHEF.deposit(pid, amount);
cakeHarvested = IBEP20(CAKE).balanceOf(address(this)).sub(before);
}
function _withdrawStakingToken(uint amount) private returns (uint cakeHarvested) {
uint before = IBEP20(CAKE).balanceOf(address(this));
CAKE_MASTER_CHEF.withdraw(pid, amount);
cakeHarvested = IBEP20(CAKE).balanceOf(address(this)).sub(before);
}
function _harvest(uint cakeAmount) private {
uint _before = _rewardsToken.sharesOf(address(this));
_rewardsToken.deposit(cakeAmount);
uint amount = _rewardsToken.sharesOf(address(this)).sub(_before);
if (amount > 0) {
_notifyRewardAmount(amount);
emit Harvested(amount);
}
}
function _notifyRewardAmount(uint reward) private updateReward(address(0)) {
if (block.timestamp >= periodFinish) {
rewardRate = reward.div(rewardsDuration);
} else {
uint remaining = periodFinish.sub(block.timestamp);
uint leftover = remaining.mul(rewardRate);
rewardRate = reward.add(leftover).div(rewardsDuration);
}
// Ensure the provided reward amount is not more than the balance in the contract.
// This keeps the reward rate in the right range, preventing overflows due to
// very high values of rewardRate in the earned and rewardsPerToken functions;
// Reward + leftover must be less than 2^256 / 10^18 to avoid overflow.
uint _balance = _rewardsToken.sharesOf(address(this));
require(rewardRate <= _balance.div(rewardsDuration), "VaultFlipToCake: reward rate must be in the right range");
lastUpdateTime = block.timestamp;
periodFinish = block.timestamp.add(rewardsDuration);
emit RewardAdded(reward);
}
/* ========== SALVAGE PURPOSE ONLY ========== */
// @dev rewardToken(CAKE) must not remain balance in this contract. So dev should be able to salvage reward token transferred by mistake.
function recoverToken(address tokenAddress, uint tokenAmount) external override onlyOwner {
require(tokenAddress != address(_stakingToken), "VaultFlipToCake: cannot recover underlying token");
IBEP20(tokenAddress).safeTransfer(owner(), tokenAmount);
emit Recovered(tokenAddress, tokenAmount);
}
}