forked from autofarmnetwork/autofarm-v2-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StratVLEV2.sol
800 lines (644 loc) · 24.4 KB
/
StratVLEV2.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import "./helpers/ERC20.sol";
import "./libraries/Address.sol";
import "./libraries/SafeERC20.sol";
import "./helpers/Ownable.sol";
import "./helpers/ReentrancyGuard.sol";
import "./helpers/Pausable.sol";
import "./interfaces/IPancakeRouter01.sol";
import "./interfaces/IPancakeRouter02.sol";
interface IVenusDistribution {
function claimVenus(address holder) external;
function enterMarkets(address[] memory _vtokens) external;
function exitMarket(address _vtoken) external;
function getAssetsIn(address account)
external
view
returns (address[] memory);
function getAccountLiquidity(address account)
external
view
returns (
uint256,
uint256,
uint256
);
}
interface IWBNB is IERC20 {
function deposit() external payable;
function withdraw(uint256 wad) external;
}
interface IVBNB {
function mint() external payable;
function redeem(uint256 redeemTokens) external returns (uint256);
function redeemUnderlying(uint256 redeemAmount) external returns (uint256);
function borrow(uint256 borrowAmount) external returns (uint256);
function repayBorrow() external payable;
// function getAccountSnapshot(address account)
// external
// view
// returns (
// uint256,
// uint256,
// uint256,
// uint256
// );
function balanceOfUnderlying(address owner) external returns (uint256);
function borrowBalanceCurrent(address account) external returns (uint256);
}
interface IVToken is IERC20 {
function underlying() external returns (address);
function mint(uint256 mintAmount) external returns (uint256);
function redeem(uint256 redeemTokens) external returns (uint256);
function redeemUnderlying(uint256 redeemAmount) external returns (uint256);
function borrow(uint256 borrowAmount) external returns (uint256);
function repayBorrow(uint256 repayAmount) external returns (uint256);
function balanceOfUnderlying(address owner) external returns (uint256);
function borrowBalanceCurrent(address account) external returns (uint256);
}
contract StratVLEV2 is Ownable, ReentrancyGuard, Pausable {
using SafeERC20 for IERC20;
using Address for address;
using SafeMath for uint256;
bool public wantIsWBNB = false;
address public wantAddress;
address public vTokenAddress;
address[] public venusMarkets;
address public uniRouterAddress;
address public constant wbnbAddress =
0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;
address public constant venusAddress =
0xcF6BB5389c92Bdda8a3747Ddb454cB7a64626C63;
address public constant earnedAddress = venusAddress;
address public constant venusDistributionAddress =
0xfD36E2c2a6789Db23113685031d7F16329158384;
address public autoFarmAddress;
address public AUTOAddress;
address public govAddress; // timelock contract
uint256 public sharesTotal = 0;
uint256 public lastEarnBlock = 0;
uint256 public controllerFee = 60;
uint256 public constant controllerFeeMax = 10000; // 100 = 1%
uint256 public constant controllerFeeUL = 300; // 3% upperlimit
uint256 public buyBackRate = 150;
uint256 public constant buyBackRateMax = 10000; // 100 = 1%
uint256 public constant buyBackRateUL = 800; // 8% upperlimit
address public buyBackAddress = 0x000000000000000000000000000000000000dEaD;
address public rewardsAddress;
uint256 public entranceFeeFactor = 9990; // < 0.1% entrance fee - goes to pool
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 deleverAmtFactorMax = 50; // 0.5% is the max amt to delever for deleverageOnce()
uint256 public constant deleverAmtFactorMaxUL = 500;
uint256 public deleverAmtFactorSafe = 20; // 0.2% is the safe amt to delever for deleverageOnce()
uint256 public constant deleverAmtFactorSafeUL = 500;
uint256 public slippageFactor = 950; // 5% default slippage tolerance
uint256 public constant slippageFactorUL = 995;
address[] public venusToWantPath;
address[] public earnedToAUTOPath;
/**
* @dev Variables that can be changed to config profitability and risk:
* {borrowRate} - What % of our collateral do we borrow per leverage level.
* {borrowDepth} - How many levels of leverage do we take.
* {BORROW_RATE_MAX} - A limit on how much we can push borrow risk.
* {BORROW_DEPTH_MAX} - A limit on how many steps we can leverage.
*/
uint256 public borrowRate = 585;
uint256 public borrowDepth = 3;
uint256 public constant BORROW_RATE_MAX = 595;
uint256 public constant BORROW_RATE_MAX_HARD = 599;
uint256 public constant BORROW_DEPTH_MAX = 6;
bool onlyGov = true;
uint256 public supplyBal = 0; // Cached want supplied to venus
uint256 public borrowBal = 0; // Cached want borrowed from venus
uint256 public supplyBalTargeted = 0; // Cached targetted want supplied to venus to achieve desired leverage
uint256 public supplyBalMin = 0;
/**
* @dev Events that the contract emits
*/
constructor(
address _govAddress,
address _autoFarmAddress,
address _AUTOAddress,
address _wantAddress,
address _vTokenAddress,
address _uniRouterAddress,
address[] memory _venusToWantPath,
address[] memory _earnedToAUTOPath,
uint256 _controllerFee,
address _rewardsAddress,
uint256 _buyBackRate,
uint256 _entranceFeeFactor,
uint256 _withdrawFeeFactor
) public {
govAddress = _govAddress;
autoFarmAddress = _autoFarmAddress;
AUTOAddress = _AUTOAddress;
wantAddress = _wantAddress;
if (wantAddress == wbnbAddress) {
wantIsWBNB = true;
}
venusToWantPath = _venusToWantPath;
earnedToAUTOPath = _earnedToAUTOPath;
controllerFee = _controllerFee;
rewardsAddress = _rewardsAddress;
buyBackRate = _buyBackRate;
entranceFeeFactor = _entranceFeeFactor;
withdrawFeeFactor = _withdrawFeeFactor;
vTokenAddress = _vTokenAddress;
venusMarkets = [vTokenAddress];
uniRouterAddress = _uniRouterAddress;
transferOwnership(autoFarmAddress);
_resetAllowances();
IVenusDistribution(venusDistributionAddress).enterMarkets(venusMarkets);
}
event SetSettings(
uint256 _entranceFeeFactor,
uint256 _withdrawFeeFactor,
uint256 _controllerFee,
uint256 _buyBackRate,
uint256 _slippageFactor,
uint256 _deleverAmtFactorMax,
uint256 _deleverAmtFactorSafe
);
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");
_;
}
function _supply(uint256 _amount) internal {
if (wantIsWBNB) {
IVBNB(vTokenAddress).mint{value: _amount}();
} else {
IVToken(vTokenAddress).mint(_amount);
}
}
function _removeSupply(uint256 _amount) internal {
IVToken(vTokenAddress).redeemUnderlying(_amount);
}
function _borrow(uint256 _amount) internal {
IVToken(vTokenAddress).borrow(_amount);
}
function _repayBorrow(uint256 _amount) internal {
if (wantIsWBNB) {
IVBNB(vTokenAddress).repayBorrow{value: _amount}();
} else {
IVToken(vTokenAddress).repayBorrow(_amount);
}
}
function deposit(address _userAddress, uint256 _wantAmt)
public
onlyOwner
nonReentrant
whenNotPaused
returns (uint256)
{
updateBalance();
uint256 sharesAdded = _wantAmt;
if (wantLockedTotal() > 0 && sharesTotal > 0) {
sharesAdded = _wantAmt
.mul(sharesTotal)
.mul(entranceFeeFactor)
.div(wantLockedTotal())
.div(entranceFeeFactorMax);
}
sharesTotal = sharesTotal.add(sharesAdded);
IERC20(wantAddress).safeTransferFrom(
address(msg.sender),
address(this),
_wantAmt
);
_farm(true);
return sharesAdded;
}
function farm(bool _withLev) public nonReentrant {
_farm(_withLev);
}
function _farm(bool _withLev) internal {
if (wantIsWBNB) {
_unwrapBNB(); // WBNB -> BNB. Venus accepts BNB, not WBNB.
}
_leverage(_withLev);
updateBalance();
deleverageUntilNotOverLevered(); // It is possible to still be over-levered after depositing.
}
/**
* @dev Repeatedly supplies and borrows bnb following the configured {borrowRate} and {borrowDepth}
* into the vToken contract.
*/
function _leverage(bool _withLev) internal {
if (_withLev) {
for (uint256 i = 0; i < borrowDepth; i++) {
uint256 amount = venusWantBal();
_supply(amount);
amount = amount.mul(borrowRate).div(1000);
_borrow(amount);
}
}
_supply(venusWantBal()); // Supply remaining want that was last borrowed.
}
function leverageOnce() public onlyAllowGov {
_leverageOnce();
}
function _leverageOnce() internal {
updateBalance(); // Updates borrowBal & supplyBal & supplyBalTargeted & supplyBalMin
uint256 borrowAmt = supplyBal.mul(borrowRate).div(1000).sub(borrowBal);
if (borrowAmt > 0) {
_borrow(borrowAmt);
_supply(venusWantBal());
}
updateBalance(); // Updates borrowBal & supplyBal & supplyBalTargeted & supplyBalMin
}
/**
* @dev Redeem to the desired leverage amount, then use it to repay borrow.
* If already over leverage, redeem max amt redeemable, then use it to repay borrow.
*/
function deleverageOnce() public onlyAllowGov {
_deleverageOnce();
}
function _deleverageOnce() internal {
updateBalance(); // Updates borrowBal & supplyBal & supplyBalTargeted & supplyBalMin
if (supplyBal <= 0) {
return;
}
uint256 deleverAmt;
uint256 deleverAmtMax = supplyBal.mul(deleverAmtFactorMax).div(10000); // 0.5%
if (supplyBal <= supplyBalMin) {
// If very over levered, delever 0.2% at a time
deleverAmt = supplyBal.mul(deleverAmtFactorSafe).div(10000);
} else if (supplyBal <= supplyBalTargeted) {
deleverAmt = supplyBal.sub(supplyBalMin);
} else {
deleverAmt = supplyBal.sub(supplyBalTargeted);
}
if (deleverAmt > deleverAmtMax) {
deleverAmt = deleverAmtMax;
}
_removeSupply(deleverAmt);
if (wantIsWBNB) {
_unwrapBNB(); // WBNB -> BNB
_repayBorrow(address(this).balance);
} else {
_repayBorrow(wantLockedInHere());
}
updateBalance(); // Updates borrowBal & supplyBal & supplyBalTargeted & supplyBalMin
}
/**
* @dev Redeem the max possible, use it to repay borrow
*/
function deleverageUntilNotOverLevered() public {
// updateBalance(); // To be more accurate, call updateBalance() first to cater for changes due to interest rates
// If borrowRate slips below targetted borrowRate, withdraw the max amt first.
// Further actual deleveraging will take place later on.
// (This can happen in when net interest rate < 0, and supplied balance falls below targeted.)
while (supplyBal > 0 && supplyBal <= supplyBalTargeted) {
_deleverageOnce();
}
}
/**
* @dev Incrementally alternates between paying part of the debt and withdrawing part of the supplied
* collateral. Continues to do this untill all want tokens is withdrawn. For partial deleveraging,
* this continues until at least _minAmt of want tokens is reached.
*/
function _deleverage(uint256 _minAmt) internal {
updateBalance(); // Updates borrowBal & supplyBal & supplyBalTargeted & supplyBalMin
deleverageUntilNotOverLevered();
if (wantIsWBNB) {
_wrapBNB(); // WBNB -> BNB
}
uint256 supplyRemovableMax = supplyBal.sub(supplyBalMin);
if (_minAmt < supplyRemovableMax) {
// If _minAmt to deleverage is less than supplyRemovableMax, just remove _minAmt
supplyRemovableMax = _minAmt;
}
_removeSupply(supplyRemovableMax);
uint256 wantBal = wantLockedInHere();
// Recursively repay borrowed + remove more from supplied
while (wantBal < borrowBal) {
// If only partially deleveraging, when sufficiently deleveraged, do not repay anymore
if (wantBal >= _minAmt) {
return;
}
_repayBorrow(wantBal);
updateBalance(); // Updates borrowBal & supplyBal & supplyBalTargeted & supplyBalMin
supplyRemovableMax = supplyBal.sub(supplyBalMin);
if (_minAmt < supplyRemovableMax) {
// If _minAmt to deleverage is less than supplyRemovableMax, just remove _minAmt
supplyRemovableMax = _minAmt;
}
_removeSupply(supplyRemovableMax);
wantBal = wantLockedInHere();
}
// When sufficiently deleveraged, do not repay
if (wantBal >= _minAmt) {
return;
}
// Make a final repayment of borrowed
_repayBorrow(borrowBal);
// remove all supplied
uint256 vTokenBal = IERC20(vTokenAddress).balanceOf(address(this));
IVToken(vTokenAddress).redeem(vTokenBal);
}
/**
* @dev Updates the risk profile and rebalances the vault funds accordingly.
* @param _borrowRate percent to borrow on each leverage level.
* @param _borrowDepth how many levels to leverage the funds.
*/
function rebalance(uint256 _borrowRate, uint256 _borrowDepth)
external
onlyAllowGov
{
require(_borrowRate <= BORROW_RATE_MAX, "!rate");
require(_borrowDepth <= BORROW_DEPTH_MAX, "!depth");
borrowRate = _borrowRate;
borrowDepth = _borrowDepth;
updateBalance(); // Updates borrowBal & supplyBal & supplyBalTargeted & supplyBalMin
deleverageUntilNotOverLevered();
}
function earn() external nonReentrant whenNotPaused {
if (onlyGov) {
require(msg.sender == govAddress, "!gov");
}
IVenusDistribution(venusDistributionAddress).claimVenus(address(this));
uint256 earnedAmt = IERC20(venusAddress).balanceOf(address(this));
earnedAmt = distributeFees(earnedAmt);
earnedAmt = buyBack(earnedAmt);
if (venusAddress != wantAddress) {
_safeSwap(
uniRouterAddress,
earnedAmt,
slippageFactor,
venusToWantPath,
address(this),
block.timestamp.add(600)
);
}
lastEarnBlock = block.number;
_farm(false); // Supply wantToken without leverage, to cater for net -ve interest rates.
}
function buyBack(uint256 _earnedAmt) internal returns (uint256) {
if (buyBackRate <= 0) {
return _earnedAmt;
}
uint256 buyBackAmt = _earnedAmt.mul(buyBackRate).div(buyBackRateMax);
_safeSwap(
uniRouterAddress,
buyBackAmt,
slippageFactor,
earnedToAUTOPath,
buyBackAddress,
block.timestamp.add(600)
);
return _earnedAmt.sub(buyBackAmt);
}
function distributeFees(uint256 _earnedAmt) internal returns (uint256) {
if (_earnedAmt > 0) {
if (controllerFee > 0) {
uint256 fee =
_earnedAmt.mul(controllerFee).div(controllerFeeMax);
IERC20(venusAddress).safeTransfer(rewardsAddress, fee);
return _earnedAmt.sub(fee);
}
}
return _earnedAmt;
}
function withdraw(address _userAddress, uint256 _wantAmt)
external
onlyOwner
nonReentrant
returns (uint256)
{
uint256 sharesRemoved =
_wantAmt.mul(sharesTotal).div(wantLockedTotal());
if (sharesRemoved > sharesTotal) {
sharesRemoved = sharesTotal;
}
sharesTotal = sharesTotal.sub(sharesRemoved);
uint256 wantBal = IERC20(wantAddress).balanceOf(address(this));
if (wantBal < _wantAmt) {
_deleverage(_wantAmt.sub(wantBal));
if (wantIsWBNB) {
_wrapBNB(); // wrap BNB -> WBNB before sending it back to user
}
wantBal = IERC20(wantAddress).balanceOf(address(this));
}
if (wantBal < _wantAmt) {
_wantAmt = wantBal;
}
if (withdrawFeeFactor < withdrawFeeFactorMax) {
_wantAmt = _wantAmt.mul(withdrawFeeFactor).div(
withdrawFeeFactorMax
);
}
IERC20(wantAddress).safeTransfer(autoFarmAddress, _wantAmt);
_farm(false);
return sharesRemoved;
}
/**
* @dev Pauses the strat.
*/
function pause() public onlyAllowGov {
_pause();
}
/**
* @dev Unpauses the strat.
*/
function unpause() external onlyAllowGov {
_unpause();
_resetAllowances();
}
function _resetAllowances() internal {
IERC20(venusAddress).safeApprove(uniRouterAddress, uint256(0));
IERC20(venusAddress).safeIncreaseAllowance(
uniRouterAddress,
uint256(-1)
);
IERC20(wantAddress).safeApprove(uniRouterAddress, uint256(0));
IERC20(wantAddress).safeIncreaseAllowance(
uniRouterAddress,
uint256(-1)
);
if (!wantIsWBNB) {
IERC20(wantAddress).safeApprove(vTokenAddress, uint256(0));
IERC20(wantAddress).safeIncreaseAllowance(
vTokenAddress,
uint256(-1)
);
}
}
function resetAllowances() public onlyAllowGov {
_resetAllowances();
}
/**
* @dev Updates want locked in Venus after interest is accrued to this very block.
* To be called before sensitive operations.
*/
function updateBalance() public {
supplyBal = IVToken(vTokenAddress).balanceOfUnderlying(address(this)); // a payable function because of acrueInterest()
borrowBal = IVToken(vTokenAddress).borrowBalanceCurrent(address(this));
supplyBalTargeted = borrowBal.mul(1000).div(borrowRate);
supplyBalMin = borrowBal.mul(1000).div(BORROW_RATE_MAX_HARD);
}
function wantLockedTotal() public view returns (uint256) {
return wantLockedInHere().add(supplyBal).sub(borrowBal);
}
function wantLockedInHere() public view returns (uint256) {
uint256 wantBal = IERC20(wantAddress).balanceOf(address(this));
if (wantIsWBNB) {
uint256 bnbBal = address(this).balance;
return bnbBal.add(wantBal);
} else {
return wantBal;
}
}
/**
* @dev Returns balance of want. If wantAddress is WBNB, returns BNB balance, not WBNB balance.
*/
function venusWantBal() public view returns (uint256) {
if (wantIsWBNB) {
return address(this).balance;
}
return IERC20(wantAddress).balanceOf(address(this));
}
function setSettings(
uint256 _entranceFeeFactor,
uint256 _withdrawFeeFactor,
uint256 _controllerFee,
uint256 _buyBackRate,
uint256 _slippageFactor,
uint256 _deleverAmtFactorMax,
uint256 _deleverAmtFactorSafe
) public 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;
require(
_deleverAmtFactorMax <= deleverAmtFactorMaxUL,
"_deleverAmtFactorMax too high"
);
deleverAmtFactorMax = _deleverAmtFactorMax;
require(
_deleverAmtFactorSafe <= deleverAmtFactorSafeUL,
"_deleverAmtFactorSafe too high"
);
deleverAmtFactorSafe = _deleverAmtFactorSafe;
emit SetSettings(
_entranceFeeFactor,
_withdrawFeeFactor,
_controllerFee,
_buyBackRate,
_slippageFactor,
_deleverAmtFactorMax,
_deleverAmtFactorSafe
);
}
function setGov(address _govAddress) public onlyAllowGov {
govAddress = _govAddress;
emit SetGov(_govAddress);
}
function setOnlyGov(bool _onlyGov) public onlyAllowGov {
onlyGov = _onlyGov;
emit SetOnlyGov(_onlyGov);
}
function setUniRouterAddress(address _uniRouterAddress)
public
onlyAllowGov
{
uniRouterAddress = _uniRouterAddress;
_resetAllowances();
emit SetUniRouterAddress(_uniRouterAddress);
}
function setBuyBackAddress(address _buyBackAddress) public onlyAllowGov {
buyBackAddress = _buyBackAddress;
emit SetBuyBackAddress(_buyBackAddress);
}
function setRewardsAddress(address _rewardsAddress) public onlyAllowGov {
rewardsAddress = _rewardsAddress;
emit SetRewardsAddress(_rewardsAddress);
}
function inCaseTokensGetStuck(
address _token,
uint256 _amount,
address _to
) public onlyAllowGov {
require(_token != earnedAddress, "!safe");
require(_token != wantAddress, "!safe");
require(_token != vTokenAddress, "!safe");
IERC20(_token).safeTransfer(_to, _amount);
}
function _wrapBNB() internal {
// BNB -> WBNB
uint256 bnbBal = address(this).balance;
if (bnbBal > 0) {
IWBNB(wbnbAddress).deposit{value: bnbBal}(); // BNB -> WBNB
}
}
function _unwrapBNB() internal {
// WBNB -> BNB
uint256 wbnbBal = IERC20(wbnbAddress).balanceOf(address(this));
if (wbnbBal > 0) {
IWBNB(wbnbAddress).withdraw(wbnbBal);
}
}
/**
* @dev We should not have significant amts of BNB in this contract if any at all.
* In case we do (eg. Venus returns all users' BNB to this contract or for any other reason),
* We can wrap all BNB, allowing users to withdraw() as per normal.
*/
function wrapBNB() public onlyAllowGov {
require(wantIsWBNB, "!wantIsWBNB");
_wrapBNB();
}
function _safeSwap(
address _uniRouterAddress,
uint256 _amountIn,
uint256 _slippageFactor,
address[] memory _path,
address _to,
uint256 _deadline
) internal {
uint256[] memory amounts =
IPancakeRouter02(_uniRouterAddress).getAmountsOut(_amountIn, _path);
uint256 amountOut = amounts[amounts.length.sub(1)];
IPancakeRouter02(_uniRouterAddress).swapExactTokensForTokens(
_amountIn,
amountOut.mul(_slippageFactor).div(1000),
_path,
_to,
_deadline
);
}
receive() external payable {}
}