forked from slime-fi/slime-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlimeFactory.sol
547 lines (448 loc) · 20.2 KB
/
SlimeFactory.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
* @title MC
* @dev Implements voting process along with vote delegation
*/
import './SafeMath.sol';
import './IBEP20.sol';
import './BEP20.sol';
import './SlimeToken.sol';
pragma solidity 0.6.12;
/**
* @title SafeBEP20
* @dev Wrappers around BEP20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeBEP20 for IBEP20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeBEP20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(
IBEP20 token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IBEP20 token,
address from,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IBEP20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(
IBEP20 token,
address spender,
uint256 value
) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
// solhint-disable-next-line max-line-length
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
'SafeBEP20: approve from non-zero to non-zero allowance'
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(
IBEP20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IBEP20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(
value,
'SafeBEP20: decreased allowance below zero'
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IBEP20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, 'SafeBEP20: low-level call failed');
if (returndata.length > 0) {
// Return data is optional
// solhint-disable-next-line max-line-length
require(abi.decode(returndata, (bool)), 'SafeBEP20: BEP20 operation did not succeed');
}
}
}
// referral
interface SlimeFriends {
function setSlimeFriend(address farmer, address referrer) external;
function getSlimeFriend(address farmer) external view returns (address);
}
contract IRewardDistributionRecipient is Ownable {
address public rewardReferral;
address public rewardVote;
function setRewardReferral(address _rewardReferral) external onlyOwner {
rewardReferral = _rewardReferral;
}
}
/**
* @dev Implementation of the {IBEP20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {BEP20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-BEP20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin guidelines: functions revert instead
* of returning `false` on failure. This behavior is nonetheless conventional
* and does not conflict with the expectations of BEP20 applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IBEP20-approve}.
*/
// MasterChef is the master of Cake. He can make Cake and he is a fair guy.
//
// Note that it's ownable and the owner wields tremendous power. The ownership
// will be transferred to a governance smart contract once CAKE is sufficiently
// distributed and the community can show to govern itself.
//
// Have fun reading it. Hopefully it's bug-free. God bless.
contract SlimeFactory is IRewardDistributionRecipient {
using SafeMath for uint256;
using SafeBEP20 for IBEP20;
// Info of each user.
struct UserInfo {
uint256 amount; // How many LP tokens the user has provided.
uint256 rewardDebt; // Reward debt. See explanation below.
//
// We do some fancy math here. Basically, any point in time, the amount of CAKEs
// entitled to a user but is pending to be distributed is:
//
// pending reward = (user.amount * pool.accCakePerShare) - user.rewardDebt
//
// Whenever a user deposits or withdraws LP tokens to a pool. Here's what happens:
// 1. The pool's `accCakePerShare` (and `lastRewardBlock`) gets updated.
// 2. User receives the pending reward sent to his/her address.
// 3. User's `amount` gets updated.
// 4. User's `rewardDebt` gets updated.
}
// Info of each pool.
struct PoolInfo {
IBEP20 lpToken; // Address of LP token contract.
uint256 allocPoint; // How many allocation points assigned to this pool. CAKEs to distribute per block.
uint256 lastRewardBlock; // Last block number that CAKEs distribution occurs.
uint256 accCakePerShare; // Accumulated CAKEs per share, times 1e12. See below.
uint256 fee;
}
// The TOKEN!
SlimeToken public st;
// Dev address.aqui va el dinero para la falopa del dev
address public devaddr;
address public divPoolAddress;
// CAKE tokens created per block.
uint256 public slimesPerBlock;
// no se para que pero bueno. ahi está.
uint256 public BONUS_MULTIPLIER = 1;
// The migrator contract. It has a lot of power. Can only be set through governance (owner).
// Info of each pool.
PoolInfo[] public poolInfo;
// Info of each user that stakes LP tokens.
mapping (uint256 => mapping (address => UserInfo)) public userInfo;
// Total allocation poitns. Must be the sum of all allocation points in all pools.
uint256 public totalAllocPoint = 0;
// The block number when This mining starts.
uint256 public startBlock;
uint256 public bonusEndBlock;
// fee sum 10%
uint256 public divreferralfee = 15;
uint256 public divPoolFee = 30;
uint256 public divdevfee = 10;
uint256 public MAX_FEE_ALLOWED = 50;
uint256 public stakepoolId = 0;
event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
event EmergencyWithdraw(address indexed user, uint256 indexed pid, uint256 amount);
event RewardPaid(address indexed user, uint256 reward);
event ReferralPaid(address indexed user,address indexed userTo, uint256 reward);
event Burned(uint256 reward);
mapping (uint256 => bool) public enablemethod;
constructor(
SlimeToken _st,
address _devaddr,
address _divPoolAddress,
uint256 _slimesPerBlock,
uint256 _startBlock,
uint256 _bonusEndBlock
) public {
st = _st;
devaddr = _devaddr;
divPoolAddress = _divPoolAddress;
slimesPerBlock = _slimesPerBlock;
startBlock = _startBlock;
bonusEndBlock = _bonusEndBlock;
totalAllocPoint = 0;
enablemethod[0]= false;
enablemethod[1]= false;
enablemethod[2]= true;
}
function updateMultiplier(uint256 multiplierNumber) public onlyOwner {
BONUS_MULTIPLIER = multiplierNumber;
}
function poolLength() external view returns (uint256) {
return poolInfo.length;
}
// Add a new lp to the pool. Can only be called by the owner.
// XXX DO NOT add the same LP token more than once. Rewards will be messed up if you do.
function add(uint256 _allocPoint, IBEP20 _lpToken, bool _withUpdate, uint256 __lastRewardBlock,uint256 __fee) public onlyOwner {
// if _fee == 10 then 100% of dev and treasury fee is applied, if _fee = 5 then 50% discount, if 0 , no fee
require(__fee<=10);
if (_withUpdate) {
massUpdatePools();
}
uint256 lastRewardBlock = __lastRewardBlock == 0 ? block.number > startBlock ? block.number : startBlock : __lastRewardBlock;
totalAllocPoint = totalAllocPoint.add(_allocPoint);
poolInfo.push(PoolInfo({
lpToken: _lpToken,
allocPoint: _allocPoint,
lastRewardBlock: lastRewardBlock,
accCakePerShare: 0,
fee:__fee
}));
}
// Update the given pool's CAKE allocation point. Can only be called by the owner. if update lastrewardblock, need update pools
function set(uint256 _pid, uint256 _allocPoint, bool _withUpdate, uint256 __lastRewardBloc,uint256 __fee) public onlyOwner {
// if _fee == 10 then 100% of dev and treasury fee is applied, if _fee = 5 then 50% discount, if 0 , no fee
require(__fee<=10);
if (_withUpdate) {
massUpdatePools();
}
totalAllocPoint = totalAllocPoint.sub(poolInfo[_pid].allocPoint).add(_allocPoint);
poolInfo[_pid].allocPoint = _allocPoint;
if(__lastRewardBloc>0)
poolInfo[_pid].lastRewardBlock = __lastRewardBloc;
poolInfo[_pid].fee = __fee;
}
// Return reward multiplier over the given _from to _to block.
function getMultiplier(uint256 _from, uint256 _to) public view returns (uint256) {
if (_to <= bonusEndBlock) {
return _to.sub(_from).mul(BONUS_MULTIPLIER);
} else if (_from >= bonusEndBlock) {
return _to.sub(_from);
} else {
return bonusEndBlock.sub(_from).mul(BONUS_MULTIPLIER).add(
_to.sub(bonusEndBlock)
);
}
}
// View function to see pending tokens on frontend.
function pendingReward(uint256 _pid, address _user) external view returns (uint256) {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
uint256 accCakePerShare = pool.accCakePerShare;
uint256 lpSupply = pool.lpToken.balanceOf(address(this));
if (block.number > pool.lastRewardBlock && lpSupply != 0) {
uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
uint256 cakeReward = multiplier.mul(slimesPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
accCakePerShare = accCakePerShare.add(cakeReward.mul(1e12).div(lpSupply));
}
return user.amount.mul(accCakePerShare).div(1e12).sub(user.rewardDebt);
}
// Update reward variables for all pools. Be careful of gas spending!
function massUpdatePools() public {
uint256 length = poolInfo.length;
for (uint256 pid = 0; pid < length; ++pid) {
updatePool(pid);
}
}
// Update reward variables of the given pool to be up-to-date.
function updatePool(uint256 _pid) public {
PoolInfo storage pool = poolInfo[_pid];
if (block.number <= pool.lastRewardBlock) {
return;
}
uint256 lpSupply = pool.lpToken.balanceOf(address(this));
if (lpSupply == 0) {
pool.lastRewardBlock = block.number;
return;
}
uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
uint256 cakeReward = multiplier.mul(slimesPerBlock).mul(pool.allocPoint).div(totalAllocPoint);
//st.mint(devaddr, cakeReward.div(5));
st.mint(address(this), cakeReward);
//treasury and dev
st.mint(divPoolAddress, cakeReward.mul(divPoolFee).div(1000));
st.mint(devaddr, cakeReward.mul(divdevfee).div(1000));
pool.accCakePerShare = pool.accCakePerShare.add(cakeReward.mul(1e12).div(lpSupply));
pool.lastRewardBlock = block.number;
}
/** Harvest all pools where user has pending balance at same time! Be careful of gas spending! **/
function massHarvest(uint256[] memory idsx) public {
require(enablemethod[0]);
uint256 idxlength = idsx.length;
address nulladdress = address(0);
for (uint256 i = 0; i < idxlength; i++) {
deposit(idsx[i],0,nulladdress);
}
}
/** Harvest & stake to stakepool all pools where user has pending balance at same time! Be careful of gas spending! **/
function massStake(uint256[] memory idsx) public {
require(enablemethod[1]);
uint256 idxlength = idsx.length;
for (uint256 i = 0; i < idxlength; i++) {
stakeReward(idsx[i]);
}
}
// Deposit LP tokens to MasterChef for CAKE allocation.
function deposit(uint256 _pid, uint256 _amount,address referrer) public {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
// anti -backdoor
require((block.number >= pool.lastRewardBlock || _amount==0) ,"pool didnt start yet");
updatePool(_pid);
if (_amount>0 && rewardReferral != address(0) && referrer != address(0)) {
SlimeFriends(rewardReferral).setSlimeFriend (msg.sender, referrer);
}
if (user.amount > 0) {
uint256 pending = user.amount.mul(pool.accCakePerShare).div(1e12).sub(user.rewardDebt);
if(pending > 0) {
payRefFees(pending);
safeStransfer(msg.sender, pending);
emit RewardPaid(msg.sender, pending);
}
}
if (_amount > 0) {
pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
if(pool.fee > 0){
// if pool.fee = 10 ==> 35 * 10/10000 = 3,5% fee
// if pool.fee = 5 ==> 35 * 5/10000 = 1,75 fee
// if pool.fee = 0 ==> 35 * 0/10000 = 0 fee
uint256 treasuryfee = _amount.mul(pool.fee).mul(divPoolFee).div(10000);
uint256 devfee = _amount.mul(pool.fee).mul(divdevfee).div(10000);
pool.lpToken.safeTransfer(divPoolAddress, treasuryfee);
pool.lpToken.safeTransfer(devaddr, devfee);
user.amount = user.amount.add(_amount).sub(treasuryfee).sub(devfee);
}else{
user.amount = user.amount.add(_amount);
}
}
user.rewardDebt = user.amount.mul(pool.accCakePerShare).div(1e12);
emit Deposit(msg.sender, _pid, _amount);
}
// user can choose autoStake reward to stake pool instead just harvest
function stakeReward(uint256 _pid) public {
require(enablemethod[2] && _pid!=stakepoolId);
UserInfo storage user = userInfo[_pid][msg.sender];
if (user.amount > 0) {
PoolInfo storage pool = poolInfo[_pid];
updatePool(_pid);
uint256 pending = user.amount.mul(pool.accCakePerShare).div(1e12).sub(user.rewardDebt);
if(pending > 0) {
payRefFees(pending);
safeStransfer(msg.sender, pending);
emit RewardPaid(msg.sender, pending);
deposit(stakepoolId,pending,address(0));
}
user.rewardDebt = user.amount.mul(pool.accCakePerShare).div(1e12);
}
}
// Withdraw LP tokens from MasterChef.
function withdraw(uint256 _pid, uint256 _amount) public {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
require(user.amount >= _amount, "withdraw: not good");
updatePool(_pid);
uint256 pending = user.amount.mul(pool.accCakePerShare).div(1e12).sub(user.rewardDebt);
if(pending > 0) {
safeStransfer(msg.sender, pending);
emit RewardPaid(msg.sender, pending);
}
if(_amount > 0) {
user.amount = user.amount.sub(_amount);
pool.lpToken.safeTransfer(address(msg.sender), _amount);
}
user.rewardDebt = user.amount.mul(pool.accCakePerShare).div(1e12);
emit Withdraw(msg.sender, _pid, _amount);
}
function payRefFees( uint256 pending ) internal
{
uint256 toReferral =pending.mul(divreferralfee).div(1000);// 2%
address referrer = address(0);
if (rewardReferral != address(0)) {
referrer = SlimeFriends(rewardReferral).getSlimeFriend (msg.sender);
}
if (referrer != address(0)) { // send commission to referrer
st.mint(referrer, toReferral);
emit ReferralPaid(msg.sender, referrer,toReferral);
}
}
// Withdraw without caring about rewards. EMERGENCY ONLY.
function emergencyWithdraw(uint256 _pid) public {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
pool.lpToken.safeTransfer(address(msg.sender), user.amount);
emit EmergencyWithdraw(msg.sender, _pid, user.amount);
user.amount = 0;
user.rewardDebt = 0;
}
function changeSlimiesPerBlock(uint256 _slimesPerBlock) public onlyOwner {
slimesPerBlock = _slimesPerBlock;
}
function safeStransfer(address _to, uint256 _amount) internal {
uint256 sbal = st.balanceOf(address(this));
if (_amount > sbal) {
st.transfer(_to, sbal);
} else {
st.transfer(_to, _amount);
}
}
function updateFees(uint256 _devFee,uint256 _refFee,uint256 _divPoolFee ) public onlyOwner{
require(_devFee <= MAX_FEE_ALLOWED && _refFee <= MAX_FEE_ALLOWED && _divPoolFee <= MAX_FEE_ALLOWED);
divdevfee = _devFee;
divreferralfee = _refFee;
divPoolFee = _divPoolFee;
}
function setdivPoolAddress(address _divPoolAddress) public onlyOwner {
divPoolAddress =_divPoolAddress;
}
// Update dev address by the previous dev.
function devAddress(address _devaddr) public onlyOwner{
devaddr = _devaddr;
}
//set what will be the stake pool
function setStakePoolId(uint256 _id) public onlyOwner {
stakepoolId =_id;
}
function enableMethod(uint256 _id,bool enabled) public onlyOwner
{
enablemethod[_id]= enabled;
}
}