generated from sushiswap/hardhat-foundation
/
BentoBoxV1.sol
1153 lines (1000 loc) · 49.7 KB
/
BentoBoxV1.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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: UNLICENSED
// The BentoBox
// ▄▄▄▄· ▄▄▄ . ▐ ▄ ▄▄▄▄▄ ▄▄▄▄· ▐▄• ▄
// ▐█ ▀█▪▀▄.▀·█▌▐█•██ ▪ ▐█ ▀█▪▪ █▌█▌▪
// ▐█▀▀█▄▐▀▀▪▄▐█▐▐▌ ▐█.▪ ▄█▀▄ ▐█▀▀█▄ ▄█▀▄ ·██·
// ██▄▪▐█▐█▄▄▌██▐█▌ ▐█▌·▐█▌.▐▌██▄▪▐█▐█▌.▐▌▪▐█·█▌
// ·▀▀▀▀ ▀▀▀ ▀▀ █▪ ▀▀▀ ▀█▄▀▪·▀▀▀▀ ▀█▄▀▪•▀▀ ▀▀
// This contract stores funds, handles their transfers, supports flash loans and strategies.
// Copyright (c) 2021 BoringCrypto - All rights reserved
// Twitter: @Boring_Crypto
// Special thanks to Keno for all his hard work and support
// Version 20-Mar-2021
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
// solhint-disable avoid-low-level-calls
// solhint-disable not-rely-on-time
// solhint-disable no-inline-assembly
// File @boringcrypto/boring-solidity/contracts/interfaces/IERC20.sol@v1.2.0
// License-Identifier: MIT
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
/// @notice EIP 2612
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
}
// File contracts/interfaces/IFlashLoan.sol
// License-Identifier: MIT
interface IFlashBorrower {
/// @notice The flashloan callback. `amount` + `fee` needs to repayed to msg.sender before this call returns.
/// @param sender The address of the invoker of this flashloan.
/// @param token The address of the token that is loaned.
/// @param amount of the `token` that is loaned.
/// @param fee The fee that needs to be paid on top for this loan. Needs to be the same as `token`.
/// @param data Additional data that was passed to the flashloan function.
function onFlashLoan(
address sender,
IERC20 token,
uint256 amount,
uint256 fee,
bytes calldata data
) external;
}
interface IBatchFlashBorrower {
/// @notice The callback for batched flashloans. Every amount + fee needs to repayed to msg.sender before this call returns.
/// @param sender The address of the invoker of this flashloan.
/// @param tokens Array of addresses for ERC-20 tokens that is loaned.
/// @param amounts A one-to-one map to `tokens` that is loaned.
/// @param fees A one-to-one map to `tokens` that needs to be paid on top for each loan. Needs to be the same token.
/// @param data Additional data that was passed to the flashloan function.
function onBatchFlashLoan(
address sender,
IERC20[] calldata tokens,
uint256[] calldata amounts,
uint256[] calldata fees,
bytes calldata data
) external;
}
// File contracts/interfaces/IWETH.sol
// License-Identifier: MIT
interface IWETH {
function deposit() external payable;
function withdraw(uint256) external;
}
// File contracts/interfaces/IStrategy.sol
// License-Identifier: MIT
interface IStrategy {
/// @notice Send the assets to the Strategy and call skim to invest them.
/// @param amount The amount of tokens to invest.
function skim(uint256 amount) external;
/// @notice Harvest any profits made converted to the asset and pass them to the caller.
/// @param balance The amount of tokens the caller thinks it has invested.
/// @param sender The address of the initiator of this transaction. Can be used for reimbursements, etc.
/// @return amountAdded The delta (+profit or -loss) that occured in contrast to `balance`.
function harvest(uint256 balance, address sender) external returns (int256 amountAdded);
/// @notice Withdraw assets. The returned amount can differ from the requested amount due to rounding.
/// @dev The `actualAmount` should be very close to the amount.
/// The difference should NOT be used to report a loss. That's what harvest is for.
/// @param amount The requested amount the caller wants to withdraw.
/// @return actualAmount The real amount that is withdrawn.
function withdraw(uint256 amount) external returns (uint256 actualAmount);
/// @notice Withdraw all assets in the safest way possible. This shouldn't fail.
/// @param balance The amount of tokens the caller thinks it has invested.
/// @return amountAdded The delta (+profit or -loss) that occured in contrast to `balance`.
function exit(uint256 balance) external returns (int256 amountAdded);
}
// File @boringcrypto/boring-solidity/contracts/libraries/BoringERC20.sol@v1.2.0
// License-Identifier: MIT
library BoringERC20 {
bytes4 private constant SIG_SYMBOL = 0x95d89b41; // symbol()
bytes4 private constant SIG_NAME = 0x06fdde03; // name()
bytes4 private constant SIG_DECIMALS = 0x313ce567; // decimals()
bytes4 private constant SIG_TRANSFER = 0xa9059cbb; // transfer(address,uint256)
bytes4 private constant SIG_TRANSFER_FROM = 0x23b872dd; // transferFrom(address,address,uint256)
/// @notice Provides a safe ERC20.transfer version for different ERC-20 implementations.
/// Reverts on a failed transfer.
/// @param token The address of the ERC-20 token.
/// @param to Transfer tokens to.
/// @param amount The token amount.
function safeTransfer(
IERC20 token,
address to,
uint256 amount
) internal {
(bool success, bytes memory data) = address(token).call(abi.encodeWithSelector(SIG_TRANSFER, to, amount));
require(success && (data.length == 0 || abi.decode(data, (bool))), "BoringERC20: Transfer failed");
}
/// @notice Provides a safe ERC20.transferFrom version for different ERC-20 implementations.
/// Reverts on a failed transfer.
/// @param token The address of the ERC-20 token.
/// @param from Transfer tokens from.
/// @param to Transfer tokens to.
/// @param amount The token amount.
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 amount
) internal {
(bool success, bytes memory data) = address(token).call(abi.encodeWithSelector(SIG_TRANSFER_FROM, from, to, amount));
require(success && (data.length == 0 || abi.decode(data, (bool))), "BoringERC20: TransferFrom failed");
}
}
// File @boringcrypto/boring-solidity/contracts/libraries/BoringMath.sol@v1.2.0
// License-Identifier: MIT
/// @notice A library for performing overflow-/underflow-safe math,
/// updated with awesomeness from of DappHub (https://github.com/dapphub/ds-math).
library BoringMath {
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
require((c = a + b) >= b, "BoringMath: Add Overflow");
}
function sub(uint256 a, uint256 b) internal pure returns (uint256 c) {
require((c = a - b) <= a, "BoringMath: Underflow");
}
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
require(b == 0 || (c = a * b) / b == a, "BoringMath: Mul Overflow");
}
function to128(uint256 a) internal pure returns (uint128 c) {
require(a <= uint128(-1), "BoringMath: uint128 Overflow");
c = uint128(a);
}
function to64(uint256 a) internal pure returns (uint64 c) {
require(a <= uint64(-1), "BoringMath: uint64 Overflow");
c = uint64(a);
}
function to32(uint256 a) internal pure returns (uint32 c) {
require(a <= uint32(-1), "BoringMath: uint32 Overflow");
c = uint32(a);
}
}
/// @notice A library for performing overflow-/underflow-safe addition and subtraction on uint128.
library BoringMath128 {
function add(uint128 a, uint128 b) internal pure returns (uint128 c) {
require((c = a + b) >= b, "BoringMath: Add Overflow");
}
function sub(uint128 a, uint128 b) internal pure returns (uint128 c) {
require((c = a - b) <= a, "BoringMath: Underflow");
}
}
/// @notice A library for performing overflow-/underflow-safe addition and subtraction on uint64.
library BoringMath64 {
function add(uint64 a, uint64 b) internal pure returns (uint64 c) {
require((c = a + b) >= b, "BoringMath: Add Overflow");
}
function sub(uint64 a, uint64 b) internal pure returns (uint64 c) {
require((c = a - b) <= a, "BoringMath: Underflow");
}
}
/// @notice A library for performing overflow-/underflow-safe addition and subtraction on uint32.
library BoringMath32 {
function add(uint32 a, uint32 b) internal pure returns (uint32 c) {
require((c = a + b) >= b, "BoringMath: Add Overflow");
}
function sub(uint32 a, uint32 b) internal pure returns (uint32 c) {
require((c = a - b) <= a, "BoringMath: Underflow");
}
}
// File @boringcrypto/boring-solidity/contracts/libraries/BoringRebase.sol@v1.2.0
// License-Identifier: MIT
struct Rebase {
uint128 elastic;
uint128 base;
}
/// @notice A rebasing library using overflow-/underflow-safe math.
library RebaseLibrary {
using BoringMath for uint256;
using BoringMath128 for uint128;
/// @notice Calculates the base value in relationship to `elastic` and `total`.
function toBase(
Rebase memory total,
uint256 elastic,
bool roundUp
) internal pure returns (uint256 base) {
if (total.elastic == 0) {
base = elastic;
} else {
base = elastic.mul(total.base) / total.elastic;
if (roundUp && base.mul(total.elastic) / total.base < elastic) {
base = base.add(1);
}
}
}
/// @notice Calculates the elastic value in relationship to `base` and `total`.
function toElastic(
Rebase memory total,
uint256 base,
bool roundUp
) internal pure returns (uint256 elastic) {
if (total.base == 0) {
elastic = base;
} else {
elastic = base.mul(total.elastic) / total.base;
if (roundUp && elastic.mul(total.base) / total.elastic < base) {
elastic = elastic.add(1);
}
}
}
/// @notice Add `elastic` to `total` and doubles `total.base`.
/// @return (Rebase) The new total.
/// @return base in relationship to `elastic`.
function add(
Rebase memory total,
uint256 elastic,
bool roundUp
) internal pure returns (Rebase memory, uint256 base) {
base = toBase(total, elastic, roundUp);
total.elastic = total.elastic.add(elastic.to128());
total.base = total.base.add(base.to128());
return (total, base);
}
/// @notice Sub `base` from `total` and update `total.elastic`.
/// @return (Rebase) The new total.
/// @return elastic in relationship to `base`.
function sub(
Rebase memory total,
uint256 base,
bool roundUp
) internal pure returns (Rebase memory, uint256 elastic) {
elastic = toElastic(total, base, roundUp);
total.elastic = total.elastic.sub(elastic.to128());
total.base = total.base.sub(base.to128());
return (total, elastic);
}
/// @notice Add `elastic` and `base` to `total`.
function add(
Rebase memory total,
uint256 elastic,
uint256 base
) internal pure returns (Rebase memory) {
total.elastic = total.elastic.add(elastic.to128());
total.base = total.base.add(base.to128());
return total;
}
/// @notice Subtract `elastic` and `base` to `total`.
function sub(
Rebase memory total,
uint256 elastic,
uint256 base
) internal pure returns (Rebase memory) {
total.elastic = total.elastic.sub(elastic.to128());
total.base = total.base.sub(base.to128());
return total;
}
/// @notice Add `elastic` to `total` and update storage.
/// @return newElastic Returns updated `elastic`.
function addElastic(Rebase storage total, uint256 elastic) internal returns (uint256 newElastic) {
newElastic = total.elastic = total.elastic.add(elastic.to128());
}
/// @notice Subtract `elastic` from `total` and update storage.
/// @return newElastic Returns updated `elastic`.
function subElastic(Rebase storage total, uint256 elastic) internal returns (uint256 newElastic) {
newElastic = total.elastic = total.elastic.sub(elastic.to128());
}
}
// File @boringcrypto/boring-solidity/contracts/BoringOwnable.sol@v1.2.0
// License-Identifier: MIT
// Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol + Claimable.sol
// Edited by BoringCrypto
contract BoringOwnableData {
address public owner;
address public pendingOwner;
}
contract BoringOwnable is BoringOwnableData {
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/// @notice `owner` defaults to msg.sender on construction.
constructor() public {
owner = msg.sender;
emit OwnershipTransferred(address(0), msg.sender);
}
/// @notice Transfers ownership to `newOwner`. Either directly or claimable by the new pending owner.
/// Can only be invoked by the current `owner`.
/// @param newOwner Address of the new owner.
/// @param direct True if `newOwner` should be set immediately. False if `newOwner` needs to use `claimOwnership`.
/// @param renounce Allows the `newOwner` to be `address(0)` if `direct` and `renounce` is True. Has no effect otherwise.
function transferOwnership(
address newOwner,
bool direct,
bool renounce
) public onlyOwner {
if (direct) {
// Checks
require(newOwner != address(0) || renounce, "Ownable: zero address");
// Effects
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
pendingOwner = address(0);
} else {
// Effects
pendingOwner = newOwner;
}
}
/// @notice Needs to be called by `pendingOwner` to claim ownership.
function claimOwnership() public {
address _pendingOwner = pendingOwner;
// Checks
require(msg.sender == _pendingOwner, "Ownable: caller != pending owner");
// Effects
emit OwnershipTransferred(owner, _pendingOwner);
owner = _pendingOwner;
pendingOwner = address(0);
}
/// @notice Only allows the `owner` to execute the function.
modifier onlyOwner() {
require(msg.sender == owner, "Ownable: caller is not the owner");
_;
}
}
// File @boringcrypto/boring-solidity/contracts/interfaces/IMasterContract.sol@v1.2.0
// License-Identifier: MIT
interface IMasterContract {
/// @notice Init function that gets called from `BoringFactory.deploy`.
/// Also kown as the constructor for cloned contracts.
/// Any ETH send to `BoringFactory.deploy` ends up here.
/// @param data Can be abi encoded arguments or anything else.
function init(bytes calldata data) external payable;
}
// File @boringcrypto/boring-solidity/contracts/BoringFactory.sol@v1.2.0
// License-Identifier: MIT
contract BoringFactory {
event LogDeploy(address indexed masterContract, bytes data, address indexed cloneAddress);
/// @notice Mapping from clone contracts to their masterContract.
mapping(address => address) public masterContractOf;
/// @notice Deploys a given master Contract as a clone.
/// Any ETH transferred with this call is forwarded to the new clone.
/// Emits `LogDeploy`.
/// @param masterContract The address of the contract to clone.
/// @param data Additional abi encoded calldata that is passed to the new clone via `IMasterContract.init`.
/// @param useCreate2 Creates the clone by using the CREATE2 opcode, in this case `data` will be used as salt.
/// @return cloneAddress Address of the created clone contract.
function deploy(
address masterContract,
bytes calldata data,
bool useCreate2
) public payable returns (address cloneAddress) {
require(masterContract != address(0), "BoringFactory: No masterContract");
bytes20 targetBytes = bytes20(masterContract); // Takes the first 20 bytes of the masterContract's address
if (useCreate2) {
// each masterContract has different code already. So clones are distinguished by their data only.
bytes32 salt = keccak256(data);
// Creates clone, more info here: https://blog.openzeppelin.com/deep-dive-into-the-minimal-proxy-contract/
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone, 0x14), targetBytes)
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
cloneAddress := create2(0, clone, 0x37, salt)
}
} else {
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone, 0x14), targetBytes)
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
cloneAddress := create(0, clone, 0x37)
}
}
masterContractOf[cloneAddress] = masterContract;
IMasterContract(cloneAddress).init{value: msg.value}(data);
emit LogDeploy(masterContract, data, cloneAddress);
}
}
// File contracts/MasterContractManager.sol
// License-Identifier: UNLICENSED
contract MasterContractManager is BoringOwnable, BoringFactory {
event LogWhiteListMasterContract(address indexed masterContract, bool approved);
event LogSetMasterContractApproval(address indexed masterContract, address indexed user, bool approved);
event LogRegisterProtocol(address indexed protocol);
/// @notice masterContract to user to approval state
mapping(address => mapping(address => bool)) public masterContractApproved;
/// @notice masterContract to whitelisted state for approval without signed message
mapping(address => bool) public whitelistedMasterContracts;
/// @notice user nonces for masterContract approvals
mapping(address => uint256) public nonces;
bytes32 private constant DOMAIN_SEPARATOR_SIGNATURE_HASH = keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)");
// See https://eips.ethereum.org/EIPS/eip-191
string private constant EIP191_PREFIX_FOR_EIP712_STRUCTURED_DATA = "\x19\x01";
bytes32 private constant APPROVAL_SIGNATURE_HASH =
keccak256("SetMasterContractApproval(string warning,address user,address masterContract,bool approved,uint256 nonce)");
// solhint-disable-next-line var-name-mixedcase
bytes32 private immutable _DOMAIN_SEPARATOR;
// solhint-disable-next-line var-name-mixedcase
uint256 private immutable DOMAIN_SEPARATOR_CHAIN_ID;
constructor() public {
uint256 chainId;
assembly {
chainId := chainid()
}
_DOMAIN_SEPARATOR = _calculateDomainSeparator(DOMAIN_SEPARATOR_CHAIN_ID = chainId);
}
function _calculateDomainSeparator(uint256 chainId) private view returns (bytes32) {
return keccak256(abi.encode(DOMAIN_SEPARATOR_SIGNATURE_HASH, keccak256("BentoBox V1"), chainId, address(this)));
}
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() public view returns (bytes32) {
uint256 chainId;
assembly {
chainId := chainid()
}
return chainId == DOMAIN_SEPARATOR_CHAIN_ID ? _DOMAIN_SEPARATOR : _calculateDomainSeparator(chainId);
}
/// @notice Other contracts need to register with this master contract so that users can approve them for the BentoBox.
function registerProtocol() public {
masterContractOf[msg.sender] = msg.sender;
emit LogRegisterProtocol(msg.sender);
}
/// @notice Enables or disables a contract for approval without signed message.
function whitelistMasterContract(address masterContract, bool approved) public onlyOwner {
// Checks
require(masterContract != address(0), "MasterCMgr: Cannot approve 0");
// Effects
whitelistedMasterContracts[masterContract] = approved;
emit LogWhiteListMasterContract(masterContract, approved);
}
/// @notice Approves or revokes a `masterContract` access to `user` funds.
/// @param user The address of the user that approves or revokes access.
/// @param masterContract The address who gains or loses access.
/// @param approved If True approves access. If False revokes access.
/// @param v Part of the signature. (See EIP-191)
/// @param r Part of the signature. (See EIP-191)
/// @param s Part of the signature. (See EIP-191)
// F4 - Check behaviour for all function arguments when wrong or extreme
// F4: Don't allow masterContract 0 to be approved. Unknown contracts will have a masterContract of 0.
// F4: User can't be 0 for signed approvals because the recoveredAddress will be 0 if ecrecover fails
function setMasterContractApproval(
address user,
address masterContract,
bool approved,
uint8 v,
bytes32 r,
bytes32 s
) public {
// Checks
require(masterContract != address(0), "MasterCMgr: masterC not set"); // Important for security
// If no signature is provided, the fallback is executed
if (r == 0 && s == 0 && v == 0) {
require(user == msg.sender, "MasterCMgr: user not sender");
require(masterContractOf[user] == address(0), "MasterCMgr: user is clone");
require(whitelistedMasterContracts[masterContract], "MasterCMgr: not whitelisted");
} else {
// Important for security - any address without masterContract has address(0) as masterContract
// So approving address(0) would approve every address, leading to full loss of funds
// Also, ecrecover returns address(0) on failure. So we check this:
require(user != address(0), "MasterCMgr: User cannot be 0");
// C10 - Protect signatures against replay, use nonce and chainId (SWC-121)
// C10: nonce + chainId are used to prevent replays
// C11 - All signatures strictly EIP-712 (SWC-117 SWC-122)
// C11: signature is EIP-712 compliant
// C12 - abi.encodePacked can't contain variable length user input (SWC-133)
// C12: abi.encodePacked has fixed length parameters
bytes32 digest =
keccak256(
abi.encodePacked(
EIP191_PREFIX_FOR_EIP712_STRUCTURED_DATA,
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
APPROVAL_SIGNATURE_HASH,
approved
? keccak256("Give FULL access to funds in (and approved to) BentoBox?")
: keccak256("Revoke access to BentoBox?"),
user,
masterContract,
approved,
nonces[user]++
)
)
)
);
address recoveredAddress = ecrecover(digest, v, r, s);
require(recoveredAddress == user, "MasterCMgr: Invalid Signature");
}
// Effects
masterContractApproved[masterContract][user] = approved;
emit LogSetMasterContractApproval(masterContract, user, approved);
}
}
// File @boringcrypto/boring-solidity/contracts/BoringBatchable.sol@v1.2.0
// License-Identifier: MIT
contract BaseBoringBatchable {
/// @dev Helper function to extract a useful revert message from a failed call.
/// If the returned data is malformed or not correctly abi encoded then this call can fail itself.
function _getRevertMsg(bytes memory _returnData) internal pure returns (string memory) {
// If the _res length is less than 68, then the transaction failed silently (without a revert message)
if (_returnData.length < 68) return "Transaction reverted silently";
assembly {
// Slice the sighash.
_returnData := add(_returnData, 0x04)
}
return abi.decode(_returnData, (string)); // All that remains is the revert string
}
/// @notice Allows batched call to self (this contract).
/// @param calls An array of inputs for each call.
/// @param revertOnFail If True then reverts after a failed call and stops doing further calls.
/// @return successes An array indicating the success of a call, mapped one-to-one to `calls`.
/// @return results An array with the returned data of each function call, mapped one-to-one to `calls`.
// F1: External is ok here because this is the batch function, adding it to a batch makes no sense
// F2: Calls in the batch may be payable, delegatecall operates in the same context, so each call in the batch has access to msg.value
// C3: The length of the loop is fully under user control, so can't be exploited
// C7: Delegatecall is only used on the same contract, so it's safe
function batch(bytes[] calldata calls, bool revertOnFail) external payable returns (bool[] memory successes, bytes[] memory results) {
successes = new bool[](calls.length);
results = new bytes[](calls.length);
for (uint256 i = 0; i < calls.length; i++) {
(bool success, bytes memory result) = address(this).delegatecall(calls[i]);
require(success || !revertOnFail, _getRevertMsg(result));
successes[i] = success;
results[i] = result;
}
}
}
contract BoringBatchable is BaseBoringBatchable {
/// @notice Call wrapper that performs `ERC20.permit` on `token`.
/// Lookup `IERC20.permit`.
// F6: Parameters can be used front-run the permit and the user's permit will fail (due to nonce or other revert)
// if part of a batch this could be used to grief once as the second call would not need the permit
function permitToken(
IERC20 token,
address from,
address to,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.permit(from, to, amount, deadline, v, r, s);
}
}
// File contracts/BentoBox.sol
// License-Identifier: UNLICENSED
/// @title BentoBox
/// @author BoringCrypto, Keno
/// @notice The BentoBox is a vault for tokens. The stored tokens can be flash loaned and used in strategies.
/// Yield from this will go to the token depositors.
/// Rebasing tokens ARE NOT supported and WILL cause loss of funds.
/// Any funds transfered directly onto the BentoBox will be lost, use the deposit function instead.
contract BentoBoxV1 is MasterContractManager, BoringBatchable {
using BoringMath for uint256;
using BoringMath128 for uint128;
using BoringERC20 for IERC20;
using RebaseLibrary for Rebase;
// ************** //
// *** EVENTS *** //
// ************** //
event LogDeposit(IERC20 indexed token, address indexed from, address indexed to, uint256 amount, uint256 share);
event LogWithdraw(IERC20 indexed token, address indexed from, address indexed to, uint256 amount, uint256 share);
event LogTransfer(IERC20 indexed token, address indexed from, address indexed to, uint256 share);
event LogFlashLoan(address indexed borrower, IERC20 indexed token, uint256 amount, uint256 feeAmount, address indexed receiver);
event LogStrategyTargetPercentage(IERC20 indexed token, uint256 targetPercentage);
event LogStrategyQueued(IERC20 indexed token, IStrategy indexed strategy);
event LogStrategySet(IERC20 indexed token, IStrategy indexed strategy);
event LogStrategyInvest(IERC20 indexed token, uint256 amount);
event LogStrategyDivest(IERC20 indexed token, uint256 amount);
event LogStrategyProfit(IERC20 indexed token, uint256 amount);
event LogStrategyLoss(IERC20 indexed token, uint256 amount);
// *************** //
// *** STRUCTS *** //
// *************** //
struct StrategyData {
uint64 strategyStartDate;
uint64 targetPercentage;
uint128 balance; // the balance of the strategy that BentoBox thinks is in there
}
// ******************************** //
// *** CONSTANTS AND IMMUTABLES *** //
// ******************************** //
// V2 - Can they be private?
// V2: Private to save gas, to verify it's correct, check the constructor arguments
IERC20 private immutable wethToken;
IERC20 private constant USE_ETHEREUM = IERC20(0);
uint256 private constant FLASH_LOAN_FEE = 50; // 0.05%
uint256 private constant FLASH_LOAN_FEE_PRECISION = 1e5;
uint256 private constant STRATEGY_DELAY = 0; // yeehaw
uint256 private constant MAX_TARGET_PERCENTAGE = 95; // 95%
uint256 private constant MINIMUM_SHARE_BALANCE = 1000; // To prevent the ratio going off
// ***************** //
// *** VARIABLES *** //
// ***************** //
// Balance per token per address/contract in shares
mapping(IERC20 => mapping(address => uint256)) public balanceOf;
// Rebase from amount to share
mapping(IERC20 => Rebase) public totals;
mapping(IERC20 => IStrategy) public strategy;
mapping(IERC20 => IStrategy) public pendingStrategy;
mapping(IERC20 => StrategyData) public strategyData;
// ******************* //
// *** CONSTRUCTOR *** //
// ******************* //
constructor(IERC20 wethToken_) public {
wethToken = wethToken_;
}
// ***************** //
// *** MODIFIERS *** //
// ***************** //
/// Modifier to check if the msg.sender is allowed to use funds belonging to the 'from' address.
/// If 'from' is msg.sender, it's allowed.
/// If 'from' is the BentoBox itself, it's allowed. Any ETH, token balances (above the known balances) or BentoBox balances
/// can be taken by anyone.
/// This is to enable skimming, not just for deposits, but also for withdrawals or transfers, enabling better composability.
/// If 'from' is a clone of a masterContract AND the 'from' address has approved that masterContract, it's allowed.
modifier allowed(address from) {
if (from != msg.sender && from != address(this)) {
// From is sender or you are skimming
address masterContract = masterContractOf[msg.sender];
require(masterContract != address(0), "BentoBox: no masterContract");
require(masterContractApproved[masterContract][from], "BentoBox: Transfer not approved");
}
_;
}
// ************************** //
// *** INTERNAL FUNCTIONS *** //
// ************************** //
/// @dev Returns the total balance of `token` this contracts holds,
/// plus the total amount this contract thinks the strategy holds.
function _tokenBalanceOf(IERC20 token) internal view returns (uint256 amount) {
amount = token.balanceOf(address(this)).add(strategyData[token].balance);
}
// ************************ //
// *** PUBLIC FUNCTIONS *** //
// ************************ //
/// @dev Helper function to represent an `amount` of `token` in shares.
/// @param token The ERC-20 token.
/// @param amount The `token` amount.
/// @param roundUp If the result `share` should be rounded up.
/// @return share The token amount represented in shares.
function toShare(
IERC20 token,
uint256 amount,
bool roundUp
) external view returns (uint256 share) {
share = totals[token].toBase(amount, roundUp);
}
/// @dev Helper function represent shares back into the `token` amount.
/// @param token The ERC-20 token.
/// @param share The amount of shares.
/// @param roundUp If the result should be rounded up.
/// @return amount The share amount back into native representation.
function toAmount(
IERC20 token,
uint256 share,
bool roundUp
) external view returns (uint256 amount) {
amount = totals[token].toElastic(share, roundUp);
}
/// @notice Deposit an amount of `token` represented in either `amount` or `share`.
/// @param token_ The ERC-20 token to deposit.
/// @param from which account to pull the tokens.
/// @param to which account to push the tokens.
/// @param amount Token amount in native representation to deposit.
/// @param share Token amount represented in shares to deposit. Takes precedence over `amount`.
/// @return amountOut The amount deposited.
/// @return shareOut The deposited amount repesented in shares.
function deposit(
IERC20 token_,
address from,
address to,
uint256 amount,
uint256 share
) public payable allowed(from) returns (uint256 amountOut, uint256 shareOut) {
// Checks
require(to != address(0), "BentoBox: to not set"); // To avoid a bad UI from burning funds
// Effects
IERC20 token = token_ == USE_ETHEREUM ? wethToken : token_;
Rebase memory total = totals[token];
// If a new token gets added, the tokenSupply call checks that this is a deployed contract. Needed for security.
require(total.elastic != 0 || token.totalSupply() > 0, "BentoBox: No tokens");
if (share == 0) {
// value of the share may be lower than the amount due to rounding, that's ok
share = total.toBase(amount, false);
// Any deposit should lead to at least the minimum share balance, otherwise it's ignored (no amount taken)
if (total.base.add(share.to128()) < MINIMUM_SHARE_BALANCE) {
return (0, 0);
}
} else {
// amount may be lower than the value of share due to rounding, in that case, add 1 to amount (Always round up)
amount = total.toElastic(share, true);
}
// In case of skimming, check that only the skimmable amount is taken.
// For ETH, the full balance is available, so no need to check.
// During flashloans the _tokenBalanceOf is lower than 'reality', so skimming deposits will mostly fail during a flashloan.
require(
from != address(this) || token_ == USE_ETHEREUM || amount <= _tokenBalanceOf(token).sub(total.elastic),
"BentoBox: Skim too much"
);
balanceOf[token][to] = balanceOf[token][to].add(share);
total.base = total.base.add(share.to128());
total.elastic = total.elastic.add(amount.to128());
totals[token] = total;
// Interactions
// During the first deposit, we check that this token is 'real'
if (token_ == USE_ETHEREUM) {
// X2 - If there is an error, could it cause a DoS. Like balanceOf causing revert. (SWC-113)
// X2: If the WETH implementation is faulty or malicious, it will block adding ETH (but we know the WETH implementation)
IWETH(address(wethToken)).deposit{value: amount}();
} else if (from != address(this)) {
// X2 - If there is an error, could it cause a DoS. Like balanceOf causing revert. (SWC-113)
// X2: If the token implementation is faulty or malicious, it may block adding tokens. Good.
token.safeTransferFrom(from, address(this), amount);
}
emit LogDeposit(token, from, to, amount, share);
amountOut = amount;
shareOut = share;
}
/// @notice Withdraws an amount of `token` from a user account.
/// @param token_ The ERC-20 token to withdraw.
/// @param from which user to pull the tokens.
/// @param to which user to push the tokens.
/// @param amount of tokens. Either one of `amount` or `share` needs to be supplied.
/// @param share Like above, but `share` takes precedence over `amount`.
function withdraw(
IERC20 token_,
address from,
address to,
uint256 amount,
uint256 share
) public allowed(from) returns (uint256 amountOut, uint256 shareOut) {
// Checks
require(to != address(0), "BentoBox: to not set"); // To avoid a bad UI from burning funds
// Effects
IERC20 token = token_ == USE_ETHEREUM ? wethToken : token_;
Rebase memory total = totals[token];
if (share == 0) {
// value of the share paid could be lower than the amount paid due to rounding, in that case, add a share (Always round up)
share = total.toBase(amount, true);
} else {
// amount may be lower than the value of share due to rounding, that's ok
amount = total.toElastic(share, false);
}
balanceOf[token][from] = balanceOf[token][from].sub(share);
total.elastic = total.elastic.sub(amount.to128());
total.base = total.base.sub(share.to128());
// There have to be at least 1000 shares left to prevent reseting the share/amount ratio (unless it's fully emptied)
require(total.base >= MINIMUM_SHARE_BALANCE || total.base == 0, "BentoBox: cannot empty");
totals[token] = total;
// Interactions
if (token_ == USE_ETHEREUM) {
// X2, X3: A revert or big gas usage in the WETH contract could block withdrawals, but WETH9 is fine.
IWETH(address(wethToken)).withdraw(amount);
// X2, X3: A revert or big gas usage could block, however, the to address is under control of the caller.
(bool success, ) = to.call{value: amount}("");
require(success, "BentoBox: ETH transfer failed");
} else {
// X2, X3: A malicious token could block withdrawal of just THAT token.
// masterContracts may want to take care not to rely on withdraw always succeeding.
token.safeTransfer(to, amount);
}
emit LogWithdraw(token, from, to, amount, share);
amountOut = amount;
shareOut = share;
}
/// @notice Transfer shares from a user account to another one.
/// @param token The ERC-20 token to transfer.
/// @param from which user to pull the tokens.
/// @param to which user to push the tokens.
/// @param share The amount of `token` in shares.
// Clones of master contracts can transfer from any account that has approved them
// F3 - Can it be combined with another similar function?
// F3: This isn't combined with transferMultiple for gas optimization
function transfer(
IERC20 token,
address from,
address to,
uint256 share
) public allowed(from) {
// Checks
require(to != address(0), "BentoBox: to not set"); // To avoid a bad UI from burning funds
// Effects
balanceOf[token][from] = balanceOf[token][from].sub(share);
balanceOf[token][to] = balanceOf[token][to].add(share);
emit LogTransfer(token, from, to, share);
}
/// @notice Transfer shares from a user account to multiple other ones.
/// @param token The ERC-20 token to transfer.
/// @param from which user to pull the tokens.
/// @param tos The receivers of the tokens.
/// @param shares The amount of `token` in shares for each receiver in `tos`.
// F3 - Can it be combined with another similar function?
// F3: This isn't combined with transfer for gas optimization
function transferMultiple(
IERC20 token,
address from,
address[] calldata tos,
uint256[] calldata shares
) public allowed(from) {
// Checks
require(tos[0] != address(0), "BentoBox: to[0] not set"); // To avoid a bad UI from burning funds
// Effects
uint256 totalAmount;
uint256 len = tos.length;
for (uint256 i = 0; i < len; i++) {
address to = tos[i];
balanceOf[token][to] = balanceOf[token][to].add(shares[i]);
totalAmount = totalAmount.add(shares[i]);
emit LogTransfer(token, from, to, shares[i]);
}
balanceOf[token][from] = balanceOf[token][from].sub(totalAmount);
}
/// @notice Flashloan ability.
/// @param borrower The address of the contract that implements and conforms to `IFlashBorrower` and handles the flashloan.
/// @param receiver Address of the token receiver.
/// @param token The address of the token to receive.
/// @param amount of the tokens to receive.
/// @param data The calldata to pass to the `borrower` contract.
// F5 - Checks-Effects-Interactions pattern followed? (SWC-107)
// F5: Not possible to follow this here, reentrancy has been reviewed
// F6 - Check for front-running possibilities, such as the approve function (SWC-114)
// F6: Slight grieving possible by withdrawing an amount before someone tries to flashloan close to the full amount.
function flashLoan(
IFlashBorrower borrower,
address receiver,
IERC20 token,
uint256 amount,
bytes calldata data
) public {
uint256 fee = amount.mul(FLASH_LOAN_FEE) / FLASH_LOAN_FEE_PRECISION;
token.safeTransfer(receiver, amount);
borrower.onFlashLoan(msg.sender, token, amount, fee, data);
require(_tokenBalanceOf(token) >= totals[token].addElastic(fee.to128()), "BentoBox: Wrong amount");
emit LogFlashLoan(address(borrower), token, amount, fee, receiver);
}
/// @notice Support for batched flashloans. Useful to request multiple different `tokens` in a single transaction.
/// @param borrower The address of the contract that implements and conforms to `IBatchFlashBorrower` and handles the flashloan.
/// @param receivers An array of the token receivers. A one-to-one mapping with `tokens` and `amounts`.
/// @param tokens The addresses of the tokens.
/// @param amounts of the tokens for each receiver.
/// @param data The calldata to pass to the `borrower` contract.
// F5 - Checks-Effects-Interactions pattern followed? (SWC-107)
// F5: Not possible to follow this here, reentrancy has been reviewed
// F6 - Check for front-running possibilities, such as the approve function (SWC-114)
// F6: Slight grieving possible by withdrawing an amount before someone tries to flashloan close to the full amount.
function batchFlashLoan(
IBatchFlashBorrower borrower,