forked from autofarmnetwork/autofarm-v2-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoFarmTimelock.sol
654 lines (575 loc) · 18.9 KB
/
AutoFarmTimelock.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
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
pragma solidity >=0.6.9 <0.8.0;
pragma experimental ABIEncoderV2;
import "./interfaces/IERC20.sol";
import "./libraries/SafeERC20.sol";
import "./helpers/AccessControl.sol";
import "./helpers/ReentrancyGuard.sol";
/**
* @dev AutoFarm functions that do not require less than the min timelock
*/
interface IAutoFarm {
function add(
uint256 _allocPoint,
address _want,
bool _withUpdate,
address _strat
) external;
function set(
uint256 _pid,
uint256 _allocPoint,
bool _withUpdate
) external;
}
/**
* @dev Strategy functions that do not require timelock or have a timelock less than the min timelock
*/
interface IStrategy {
// Main want token compounding function
function earn() external;
function farm() external;
function pause() external;
function unpause() external;
function rebalance(uint256 _borrowRate, uint256 _borrowDepth) external;
function deleverageOnce() external;
function leverageOnce() external;
function wrapBNB() external; // Specifically for the Venus WBNB vault.
// In case new vaults require functions without a timelock as well, hoping to avoid having multiple timelock contracts
function noTimeLockFunc1() external;
function noTimeLockFunc2() external;
function noTimeLockFunc3() external;
}
// import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/TimelockController.sol";
/**
* @dev Contract module which acts as a timelocked controller. When set as the
* owner of an `Ownable` smart contract, it enforces a timelock on all
* `onlyOwner` maintenance operations. This gives time for users of the
* controlled contract to exit before a potentially dangerous maintenance
* operation is applied.
*
* By default, this contract is self administered, meaning administration tasks
* have to go through the timelock process. The proposer (resp executor) role
* is in charge of proposing (resp executing) operations. A common use case is
* to position this {TimelockController} as the owner of a smart contract, with
* a multisig or a DAO as the sole proposer.
*
* _Available since v3.3._
*/
contract TimelockController is AccessControl, ReentrancyGuard {
using SafeERC20 for IERC20;
bytes32 public constant TIMELOCK_ADMIN_ROLE =
keccak256("TIMELOCK_ADMIN_ROLE");
bytes32 public constant PROPOSER_ROLE = keccak256("PROPOSER_ROLE");
bytes32 public constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE");
uint256 internal constant _DONE_TIMESTAMP = uint256(1);
mapping(bytes32 => uint256) private _timestamps;
uint256 public minDelay; // seconds - to be increased in production
uint256 public minDelayReduced; // seconds - to be increased in production
address payable public devWalletAddress;
/**
* @dev Emitted when a call is scheduled as part of operation `id`.
*/
event CallScheduled(
bytes32 indexed id,
uint256 indexed index,
address target,
uint256 value,
bytes data,
bytes32 predecessor,
uint256 delay
);
/**
* @dev Emitted when a call is scheduled as part of operation `id`.
*/
event SetScheduled(
bytes32 indexed id,
uint256 indexed index,
uint256 _pid,
uint256 _allocPoint,
bool _withUpdate,
bytes32 predecessor,
uint256 delay
);
/**
* @dev Emitted when a call is performed as part of operation `id`.
*/
event CallExecuted(
bytes32 indexed id,
uint256 indexed index,
address target,
uint256 value,
bytes data
);
/**
* @dev Emitted when operation `id` is cancelled.
*/
event Cancelled(bytes32 indexed id);
/**
* @dev Emitted when the minimum delay for future operations is modified.
*/
event MinDelayChange(uint256 oldDuration, uint256 newDuration);
event MinDelayReducedChange(uint256 oldDuration, uint256 newDuration);
event SetScheduled(
bytes32 indexed id,
uint256 indexed index,
address target,
uint256 value,
bytes data,
bytes32 predecessor,
uint256 delay
);
/**
* @dev Initializes the contract with a given `minDelay`.
*/
constructor(
address payable _devWalletAddress,
uint256 _minDelay,
uint256 _minDelayReduced // address[] memory proposers, address[] memory executors
) public {
devWalletAddress = _devWalletAddress;
minDelay = _minDelay;
minDelayReduced = _minDelayReduced;
_setRoleAdmin(TIMELOCK_ADMIN_ROLE, TIMELOCK_ADMIN_ROLE);
_setRoleAdmin(PROPOSER_ROLE, TIMELOCK_ADMIN_ROLE);
_setRoleAdmin(EXECUTOR_ROLE, TIMELOCK_ADMIN_ROLE);
// deployer + self administration
_setupRole(TIMELOCK_ADMIN_ROLE, _msgSender());
_setupRole(TIMELOCK_ADMIN_ROLE, address(this));
// register proposers
// for (uint256 i = 0; i < proposers.length; ++i) {
// _setupRole(PROPOSER_ROLE, proposers[i]);
// }
_setupRole(PROPOSER_ROLE, devWalletAddress);
// // register executors
// for (uint256 i = 0; i < executors.length; ++i) {
// _setupRole(EXECUTOR_ROLE, executors[i]);
// }
_setupRole(EXECUTOR_ROLE, devWalletAddress);
emit MinDelayChange(0, minDelay);
}
/**
* @dev Modifier to make a function callable only by a certain role. In
* addition to checking the sender's role, `address(0)` 's role is also
* considered. Granting a role to `address(0)` is equivalent to enabling
* this role for everyone.
*/
modifier onlyRole(bytes32 role) {
require(
hasRole(role, _msgSender()) || hasRole(role, address(0)),
"TimelockController: sender requires permission"
);
_;
}
/**
* @dev Contract might receive/hold ETH as part of the maintenance process.
*/
receive() external payable {}
/**
* @dev Returns whether an operation is pending or not.
*/
function isOperationPending(bytes32 id) public view returns (bool pending) {
return _timestamps[id] > _DONE_TIMESTAMP;
}
/**
* @dev Returns whether an operation is ready or not.
*/
function isOperationReady(bytes32 id) public view returns (bool ready) {
// solhint-disable-next-line not-rely-on-time
return
_timestamps[id] > _DONE_TIMESTAMP &&
_timestamps[id] <= block.timestamp;
}
/**
* @dev Returns whether an operation is done or not.
*/
function isOperationDone(bytes32 id) public view returns (bool done) {
return _timestamps[id] == _DONE_TIMESTAMP;
}
/**
* @dev Returns the timestamp at with an operation becomes ready (0 for
* unset operations, 1 for done operations).
*/
function getTimestamp(bytes32 id) public view returns (uint256 timestamp) {
return _timestamps[id];
}
/**
* @dev Returns the minimum delay for an operation to become valid.
*
* This value can be changed by executing an operation that calls `updateDelay`.
*/
function getMinDelay() public view returns (uint256 duration) {
return minDelay;
}
/**
* @dev Returns the identifier of an operation containing a single
* transaction.
*/
function hashOperation(
address target,
uint256 value,
bytes calldata data,
bytes32 predecessor,
bytes32 salt
) public pure returns (bytes32 hash) {
return keccak256(abi.encode(target, value, data, predecessor, salt));
}
/**
* @dev Returns the identifier of an operation containing a batch of
* transactions.
*/
function hashOperationBatch(
address[] calldata targets,
uint256[] calldata values,
bytes[] calldata datas,
bytes32 predecessor,
bytes32 salt
) public pure returns (bytes32 hash) {
return keccak256(abi.encode(targets, values, datas, predecessor, salt));
}
/**
* @dev Schedule an operation containing a single transaction.
*
* Emits a {CallScheduled} event.
*
* Requirements:
*
* - the caller must have the 'proposer' role.
*/
function schedule(
address target,
uint256 value,
bytes calldata data,
bytes32 predecessor,
bytes32 salt,
uint256 delay
) public virtual onlyRole(PROPOSER_ROLE) {
bytes32 id = hashOperation(target, value, data, predecessor, salt);
_schedule(id, delay);
emit CallScheduled(id, 0, target, value, data, predecessor, delay);
}
/**
* @dev Schedule an operation containing a batch of transactions.
*
* Emits one {CallScheduled} event per transaction in the batch.
*
* Requirements:
*
* - the caller must have the 'proposer' role.
*/
function scheduleBatch(
address[] calldata targets,
uint256[] calldata values,
bytes[] calldata datas,
bytes32 predecessor,
bytes32 salt,
uint256 delay
) public virtual onlyRole(PROPOSER_ROLE) {
require(
targets.length == values.length,
"TimelockController: length mismatch"
);
require(
targets.length == datas.length,
"TimelockController: length mismatch"
);
bytes32 id =
hashOperationBatch(targets, values, datas, predecessor, salt);
_schedule(id, delay);
for (uint256 i = 0; i < targets.length; ++i) {
emit CallScheduled(
id,
i,
targets[i],
values[i],
datas[i],
predecessor,
delay
);
}
}
/**
* @dev Schedule an operation that is to becomes valid after a given delay.
*/
function _schedule(bytes32 id, uint256 delay) private {
require(
_timestamps[id] == 0,
"TimelockController: operation already scheduled"
);
require(delay >= minDelay, "TimelockController: insufficient delay");
// solhint-disable-next-line not-rely-on-time
_timestamps[id] = SafeMath.add(block.timestamp, delay);
}
/**
* @dev Cancel an operation.
*
* Requirements:
*
* - the caller must have the 'proposer' role.
*/
function cancel(bytes32 id) public virtual onlyRole(PROPOSER_ROLE) {
require(
isOperationPending(id),
"TimelockController: operation cannot be cancelled"
);
delete _timestamps[id];
emit Cancelled(id);
}
/**
* @dev Execute an (ready) operation containing a single transaction.
*
* Emits a {CallExecuted} event.
*
* Requirements:
*
* - the caller must have the 'executor' role.
*/
function execute(
address target,
uint256 value,
bytes calldata data,
bytes32 predecessor,
bytes32 salt
) public payable virtual onlyRole(EXECUTOR_ROLE) nonReentrant {
bytes32 id = hashOperation(target, value, data, predecessor, salt);
_beforeCall(predecessor);
_call(id, 0, target, value, data);
_afterCall(id);
}
/**
* @dev Execute an (ready) operation containing a batch of transactions.
*
* Emits one {CallExecuted} event per transaction in the batch.
*
* Requirements:
*
* - the caller must have the 'executor' role.
*/
function executeBatch(
address[] calldata targets,
uint256[] calldata values,
bytes[] calldata datas,
bytes32 predecessor,
bytes32 salt
) public payable virtual onlyRole(EXECUTOR_ROLE) nonReentrant {
require(
targets.length == values.length,
"TimelockController: length mismatch"
);
require(
targets.length == datas.length,
"TimelockController: length mismatch"
);
bytes32 id =
hashOperationBatch(targets, values, datas, predecessor, salt);
_beforeCall(predecessor);
for (uint256 i = 0; i < targets.length; ++i) {
_call(id, i, targets[i], values[i], datas[i]);
}
_afterCall(id);
}
/**
* @dev Checks before execution of an operation's calls.
*/
function _beforeCall(bytes32 predecessor) private view {
require(
predecessor == bytes32(0) || isOperationDone(predecessor),
"TimelockController: missing dependency"
);
}
/**
* @dev Checks after execution of an operation's calls.
*/
function _afterCall(bytes32 id) private {
require(
isOperationReady(id),
"TimelockController: operation is not ready"
);
_timestamps[id] = _DONE_TIMESTAMP;
}
/**
* @dev Execute an operation's call.
*
* Emits a {CallExecuted} event.
*/
function _call(
bytes32 id,
uint256 index,
address target,
uint256 value,
bytes calldata data
) private {
// solhint-disable-next-line avoid-low-level-calls
(bool success, ) = target.call{value: value}(data);
require(success, "TimelockController: underlying transaction reverted");
emit CallExecuted(id, index, target, value, data);
}
/**
* @dev Changes the minimum timelock duration for future operations.
*
* Emits a {MinDelayChange} event.
*
* Requirements:
*
* - the caller must be the timelock itself. This can only be achieved by scheduling and later executing
* an operation where the timelock is the target and the data is the ABI-encoded call to this function.
*/
function updateMinDelay(uint256 newDelay) external virtual {
require(
msg.sender == address(this),
"TimelockController: caller must be timelock"
);
emit MinDelayChange(minDelay, newDelay);
minDelay = newDelay;
}
function updateMinDelayReduced(uint256 newDelay) external virtual {
require(
msg.sender == address(this),
"TimelockController: caller must be timelock"
);
emit MinDelayReducedChange(minDelayReduced, newDelay);
minDelayReduced = newDelay;
}
function setDevWalletAddress(address payable _devWalletAddress) public {
require(
msg.sender == address(this),
"TimelockController: caller must be timelock"
);
require(tx.origin == devWalletAddress, "tx.origin != devWalletAddress");
devWalletAddress = _devWalletAddress;
}
/**
* @dev Reduced timelock functions
*/
function scheduleSet(
address _autofarmAddress,
uint256 _pid,
uint256 _allocPoint,
bool _withUpdate,
bytes32 predecessor,
bytes32 salt
) public onlyRole(EXECUTOR_ROLE) {
bytes32 id =
keccak256(
abi.encode(
_autofarmAddress,
_pid,
_allocPoint,
_withUpdate,
predecessor,
salt
)
);
require(
_timestamps[id] == 0,
"TimelockController: operation already scheduled"
);
_timestamps[id] = SafeMath.add(block.timestamp, minDelayReduced);
emit SetScheduled(
id,
0,
_pid,
_allocPoint,
_withUpdate,
predecessor,
minDelayReduced
);
}
function executeSet(
address _autofarmAddress,
uint256 _pid,
uint256 _allocPoint,
bool _withUpdate,
bytes32 predecessor,
bytes32 salt
) public payable virtual onlyRole(EXECUTOR_ROLE) nonReentrant {
bytes32 id =
keccak256(
abi.encode(
_autofarmAddress,
_pid,
_allocPoint,
_withUpdate,
predecessor,
salt
)
);
_beforeCall(predecessor);
IAutoFarm(_autofarmAddress).set(_pid, _allocPoint, _withUpdate);
_afterCall(id);
}
/**
* @dev No timelock functions
*/
function withdrawBNB() public payable {
require(msg.sender == devWalletAddress, "!devWalletAddress");
devWalletAddress.safeTransfer(address(this).balance);
}
function withdrawBEP20(address _tokenAddress) public payable {
require(msg.sender == devWalletAddress, "!devWalletAddress");
uint256 tokenBal = IERC20(_tokenAddress).balanceOf(address(this));
IERC20(_tokenAddress).safeIncreaseAllowance(devWalletAddress, tokenBal);
IERC20(_tokenAddress).safeTransfer(devWalletAddress, tokenBal);
}
function add(
address _autofarmAddress,
address _want,
bool _withUpdate,
address _strat
) public onlyRole(EXECUTOR_ROLE) {
IAutoFarm(_autofarmAddress).add(0, _want, _withUpdate, _strat); // allocPoint = 0. Schedule set (timelocked) to increase allocPoint.
}
function earn(address _stratAddress) public onlyRole(EXECUTOR_ROLE) {
IStrategy(_stratAddress).earn();
}
function farm(address _stratAddress) public onlyRole(EXECUTOR_ROLE) {
IStrategy(_stratAddress).farm();
}
function pause(address _stratAddress) public onlyRole(EXECUTOR_ROLE) {
IStrategy(_stratAddress).pause();
}
function unpause(address _stratAddress) public onlyRole(EXECUTOR_ROLE) {
IStrategy(_stratAddress).unpause();
}
function rebalance(
address _stratAddress,
uint256 _borrowRate,
uint256 _borrowDepth
) public onlyRole(EXECUTOR_ROLE) {
IStrategy(_stratAddress).rebalance(_borrowRate, _borrowDepth);
}
function deleverageOnce(address _stratAddress)
public
onlyRole(EXECUTOR_ROLE)
{
IStrategy(_stratAddress).deleverageOnce();
}
function leverageOnce(address _stratAddress)
public
onlyRole(EXECUTOR_ROLE)
{
IStrategy(_stratAddress).leverageOnce();
}
function wrapBNB(address _stratAddress) public onlyRole(EXECUTOR_ROLE) {
IStrategy(_stratAddress).wrapBNB();
}
// // In case new vaults require functions without a timelock as well, hoping to avoid having multiple timelock contracts
function noTimeLockFunc1(address _stratAddress)
public
onlyRole(EXECUTOR_ROLE)
{
IStrategy(_stratAddress).noTimeLockFunc1();
}
function noTimeLockFunc2(address _stratAddress)
public
onlyRole(EXECUTOR_ROLE)
{
IStrategy(_stratAddress).noTimeLockFunc2();
}
function noTimeLockFunc3(address _stratAddress)
public
onlyRole(EXECUTOR_ROLE)
{
IStrategy(_stratAddress).noTimeLockFunc3();
}
}