forked from autofarmnetwork/autofarm-v2-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStratX2.sol
More file actions
520 lines (431 loc) · 15.2 KB
/
Copy pathStratX2.sol
File metadata and controls
520 lines (431 loc) · 15.2 KB
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
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import "./helpers/ERC20.sol";
import "./libraries/Address.sol";
import "./libraries/SafeERC20.sol";
import "./libraries/EnumerableSet.sol";
import "./helpers/Ownable.sol";
import "./interfaces/IPancakeswapFarm.sol";
import "./interfaces/IPancakeRouter01.sol";
import "./interfaces/IPancakeRouter02.sol";
interface IWBNB is IERC20 {
function deposit() external payable;
function withdraw(uint256 wad) external;
}
import "./helpers/ReentrancyGuard.sol";
import "./helpers/Pausable.sol";
abstract contract StratX2 is Ownable, ReentrancyGuard, Pausable {
// Maximises yields in pancakeswap
using SafeMath for uint256;
using SafeERC20 for IERC20;
bool public isCAKEStaking; // only for staking CAKE using pancakeswap's native CAKE staking contract.
bool public isSameAssetDeposit;
bool public isAutoComp; // this vault is purely for staking. eg. WBNB-AUTO staking vault.
address public farmContractAddress; // address of farm, eg, PCS, Thugs etc.
uint256 public pid; // pid of pool in farmContractAddress
address public wantAddress;
address public token0Address;
address public token1Address;
address public earnedAddress;
address public uniRouterAddress; // uniswap, pancakeswap etc
address public wbnbAddress;
address public autoFarmAddress;
address public AUTOAddress;
address public govAddress; // timelock contract
bool public onlyGov = true;
uint256 public lastEarnBlock = 0;
uint256 public wantLockedTotal = 0;
uint256 public sharesTotal = 0;
uint256 public controllerFee = 0; // 70;
uint256 public constant controllerFeeMax = 10000; // 100 = 1%
uint256 public constant controllerFeeUL = 300;
uint256 public buyBackRate = 0; // 250;
uint256 public constant buyBackRateMax = 10000; // 100 = 1%
uint256 public constant buyBackRateUL = 800;
address public buyBackAddress = 0x000000000000000000000000000000000000dEaD;
address public rewardsAddress;
uint256 public entranceFeeFactor = 9990; // < 0.1% entrance fee - goes to pool + prevents front-running
uint256 public constant entranceFeeFactorMax = 10000;
uint256 public constant entranceFeeFactorLL = 9950; // 0.5% is the max entrance fee settable. LL = lowerlimit
uint256 public withdrawFeeFactor = 10000; // 0.1% withdraw fee - goes to pool
uint256 public constant withdrawFeeFactorMax = 10000;
uint256 public constant withdrawFeeFactorLL = 9950; // 0.5% is the max entrance fee settable. LL = lowerlimit
uint256 public slippageFactor = 950; // 5% default slippage tolerance
uint256 public constant slippageFactorUL = 995;
address[] public earnedToAUTOPath;
address[] public earnedToToken0Path;
address[] public earnedToToken1Path;
address[] public token0ToEarnedPath;
address[] public token1ToEarnedPath;
event SetSettings(
uint256 _entranceFeeFactor,
uint256 _withdrawFeeFactor,
uint256 _controllerFee,
uint256 _buyBackRate,
uint256 _slippageFactor
);
event SetGov(address _govAddress);
event SetOnlyGov(bool _onlyGov);
event SetUniRouterAddress(address _uniRouterAddress);
event SetBuyBackAddress(address _buyBackAddress);
event SetRewardsAddress(address _rewardsAddress);
modifier onlyAllowGov() {
require(msg.sender == govAddress, "!gov");
_;
}
// Receives new deposits from user
function deposit(address _userAddress, uint256 _wantAmt)
public
virtual
onlyOwner
nonReentrant
whenNotPaused
returns (uint256)
{
IERC20(wantAddress).safeTransferFrom(
address(msg.sender),
address(this),
_wantAmt
);
uint256 sharesAdded = _wantAmt;
if (wantLockedTotal > 0 && sharesTotal > 0) {
sharesAdded = _wantAmt
.mul(sharesTotal)
.mul(entranceFeeFactor)
.div(wantLockedTotal)
.div(entranceFeeFactorMax);
}
sharesTotal = sharesTotal.add(sharesAdded);
if (isAutoComp) {
_farm();
} else {
wantLockedTotal = wantLockedTotal.add(_wantAmt);
}
return sharesAdded;
}
function farm() public virtual nonReentrant {
_farm();
}
function _farm() internal virtual {
require(isAutoComp, "!isAutoComp");
uint256 wantAmt = IERC20(wantAddress).balanceOf(address(this));
wantLockedTotal = wantLockedTotal.add(wantAmt);
IERC20(wantAddress).safeIncreaseAllowance(farmContractAddress, wantAmt);
if (isCAKEStaking) {
IPancakeswapFarm(farmContractAddress).enterStaking(wantAmt); // Just for CAKE staking, we dont use deposit()
} else {
IPancakeswapFarm(farmContractAddress).deposit(pid, wantAmt);
}
}
function _unfarm(uint256 _wantAmt) internal virtual {
if (isCAKEStaking) {
IPancakeswapFarm(farmContractAddress).leaveStaking(_wantAmt); // Just for CAKE staking, we dont use withdraw()
} else {
IPancakeswapFarm(farmContractAddress).withdraw(pid, _wantAmt);
}
}
function withdraw(address _userAddress, uint256 _wantAmt)
public
virtual
onlyOwner
nonReentrant
returns (uint256)
{
require(_wantAmt > 0, "_wantAmt <= 0");
uint256 sharesRemoved = _wantAmt.mul(sharesTotal).div(wantLockedTotal);
if (sharesRemoved > sharesTotal) {
sharesRemoved = sharesTotal;
}
sharesTotal = sharesTotal.sub(sharesRemoved);
if (withdrawFeeFactor < withdrawFeeFactorMax) {
_wantAmt = _wantAmt.mul(withdrawFeeFactor).div(
withdrawFeeFactorMax
);
}
if (isAutoComp) {
_unfarm(_wantAmt);
}
uint256 wantAmt = IERC20(wantAddress).balanceOf(address(this));
if (_wantAmt > wantAmt) {
_wantAmt = wantAmt;
}
if (wantLockedTotal < _wantAmt) {
_wantAmt = wantLockedTotal;
}
wantLockedTotal = wantLockedTotal.sub(_wantAmt);
IERC20(wantAddress).safeTransfer(autoFarmAddress, _wantAmt);
return sharesRemoved;
}
// 1. Harvest farm tokens
// 2. Converts farm tokens into want tokens
// 3. Deposits want tokens
function earn() public virtual nonReentrant whenNotPaused {
require(isAutoComp, "!isAutoComp");
if (onlyGov) {
require(msg.sender == govAddress, "!gov");
}
// Harvest farm tokens
_unfarm(0);
if (earnedAddress == wbnbAddress) {
_wrapBNB();
}
// Converts farm tokens into want tokens
uint256 earnedAmt = IERC20(earnedAddress).balanceOf(address(this));
earnedAmt = distributeFees(earnedAmt);
earnedAmt = buyBack(earnedAmt);
if (isCAKEStaking || isSameAssetDeposit) {
lastEarnBlock = block.number;
_farm();
return;
}
IERC20(earnedAddress).safeApprove(uniRouterAddress, 0);
IERC20(earnedAddress).safeIncreaseAllowance(
uniRouterAddress,
earnedAmt
);
if (earnedAddress != token0Address) {
// Swap half earned to token0
_safeSwap(
uniRouterAddress,
earnedAmt.div(2),
slippageFactor,
earnedToToken0Path,
address(this),
block.timestamp.add(600)
);
}
if (earnedAddress != token1Address) {
// Swap half earned to token1
_safeSwap(
uniRouterAddress,
earnedAmt.div(2),
slippageFactor,
earnedToToken1Path,
address(this),
block.timestamp.add(600)
);
}
// Get want tokens, ie. add liquidity
uint256 token0Amt = IERC20(token0Address).balanceOf(address(this));
uint256 token1Amt = IERC20(token1Address).balanceOf(address(this));
if (token0Amt > 0 && token1Amt > 0) {
IERC20(token0Address).safeIncreaseAllowance(
uniRouterAddress,
token0Amt
);
IERC20(token1Address).safeIncreaseAllowance(
uniRouterAddress,
token1Amt
);
IPancakeRouter02(uniRouterAddress).addLiquidity(
token0Address,
token1Address,
token0Amt,
token1Amt,
0,
0,
address(this),
block.timestamp.add(600)
);
}
lastEarnBlock = block.number;
_farm();
}
function buyBack(uint256 _earnedAmt) internal virtual returns (uint256) {
if (buyBackRate <= 0) {
return _earnedAmt;
}
uint256 buyBackAmt = _earnedAmt.mul(buyBackRate).div(buyBackRateMax);
if (earnedAddress == AUTOAddress) {
IERC20(earnedAddress).safeTransfer(buyBackAddress, buyBackAmt);
} else {
IERC20(earnedAddress).safeIncreaseAllowance(
uniRouterAddress,
buyBackAmt
);
_safeSwap(
uniRouterAddress,
buyBackAmt,
slippageFactor,
earnedToAUTOPath,
buyBackAddress,
block.timestamp.add(600)
);
}
return _earnedAmt.sub(buyBackAmt);
}
function distributeFees(uint256 _earnedAmt)
internal
virtual
returns (uint256)
{
if (_earnedAmt > 0) {
// Performance fee
if (controllerFee > 0) {
uint256 fee =
_earnedAmt.mul(controllerFee).div(controllerFeeMax);
IERC20(earnedAddress).safeTransfer(rewardsAddress, fee);
_earnedAmt = _earnedAmt.sub(fee);
}
}
return _earnedAmt;
}
function convertDustToEarned() public virtual whenNotPaused {
require(isAutoComp, "!isAutoComp");
require(!isCAKEStaking, "isCAKEStaking");
// Converts dust tokens into earned tokens, which will be reinvested on the next earn().
// Converts token0 dust (if any) to earned tokens
uint256 token0Amt = IERC20(token0Address).balanceOf(address(this));
if (token0Address != earnedAddress && token0Amt > 0) {
IERC20(token0Address).safeIncreaseAllowance(
uniRouterAddress,
token0Amt
);
// Swap all dust tokens to earned tokens
_safeSwap(
uniRouterAddress,
token0Amt,
slippageFactor,
token0ToEarnedPath,
address(this),
block.timestamp.add(600)
);
}
// Converts token1 dust (if any) to earned tokens
uint256 token1Amt = IERC20(token1Address).balanceOf(address(this));
if (token1Address != earnedAddress && token1Amt > 0) {
IERC20(token1Address).safeIncreaseAllowance(
uniRouterAddress,
token1Amt
);
// Swap all dust tokens to earned tokens
_safeSwap(
uniRouterAddress,
token1Amt,
slippageFactor,
token1ToEarnedPath,
address(this),
block.timestamp.add(600)
);
}
}
function pause() public virtual onlyAllowGov {
_pause();
}
function unpause() public virtual onlyAllowGov {
_unpause();
}
function setSettings(
uint256 _entranceFeeFactor,
uint256 _withdrawFeeFactor,
uint256 _controllerFee,
uint256 _buyBackRate,
uint256 _slippageFactor
) public virtual onlyAllowGov {
require(
_entranceFeeFactor >= entranceFeeFactorLL,
"_entranceFeeFactor too low"
);
require(
_entranceFeeFactor <= entranceFeeFactorMax,
"_entranceFeeFactor too high"
);
entranceFeeFactor = _entranceFeeFactor;
require(
_withdrawFeeFactor >= withdrawFeeFactorLL,
"_withdrawFeeFactor too low"
);
require(
_withdrawFeeFactor <= withdrawFeeFactorMax,
"_withdrawFeeFactor too high"
);
withdrawFeeFactor = _withdrawFeeFactor;
require(_controllerFee <= controllerFeeUL, "_controllerFee too high");
controllerFee = _controllerFee;
require(_buyBackRate <= buyBackRateUL, "_buyBackRate too high");
buyBackRate = _buyBackRate;
require(
_slippageFactor <= slippageFactorUL,
"_slippageFactor too high"
);
slippageFactor = _slippageFactor;
emit SetSettings(
_entranceFeeFactor,
_withdrawFeeFactor,
_controllerFee,
_buyBackRate,
_slippageFactor
);
}
function setGov(address _govAddress) public virtual onlyAllowGov {
govAddress = _govAddress;
emit SetGov(_govAddress);
}
function setOnlyGov(bool _onlyGov) public virtual onlyAllowGov {
onlyGov = _onlyGov;
emit SetOnlyGov(_onlyGov);
}
function setUniRouterAddress(address _uniRouterAddress)
public
virtual
onlyAllowGov
{
uniRouterAddress = _uniRouterAddress;
emit SetUniRouterAddress(_uniRouterAddress);
}
function setBuyBackAddress(address _buyBackAddress)
public
virtual
onlyAllowGov
{
buyBackAddress = _buyBackAddress;
emit SetBuyBackAddress(_buyBackAddress);
}
function setRewardsAddress(address _rewardsAddress)
public
virtual
onlyAllowGov
{
rewardsAddress = _rewardsAddress;
emit SetRewardsAddress(_rewardsAddress);
}
function inCaseTokensGetStuck(
address _token,
uint256 _amount,
address _to
) public virtual onlyAllowGov {
require(_token != earnedAddress, "!safe");
require(_token != wantAddress, "!safe");
IERC20(_token).safeTransfer(_to, _amount);
}
function _wrapBNB() internal virtual {
// BNB -> WBNB
uint256 bnbBal = address(this).balance;
if (bnbBal > 0) {
IWBNB(wbnbAddress).deposit{value: bnbBal}(); // BNB -> WBNB
}
}
function wrapBNB() public virtual onlyAllowGov {
_wrapBNB();
}
function _safeSwap(
address _uniRouterAddress,
uint256 _amountIn,
uint256 _slippageFactor,
address[] memory _path,
address _to,
uint256 _deadline
) internal virtual {
uint256[] memory amounts =
IPancakeRouter02(_uniRouterAddress).getAmountsOut(_amountIn, _path);
uint256 amountOut = amounts[amounts.length.sub(1)];
IPancakeRouter02(_uniRouterAddress)
.swapExactTokensForTokensSupportingFeeOnTransferTokens(
_amountIn,
amountOut.mul(_slippageFactor).div(1000),
_path,
_to,
_deadline
);
}
}