forked from superform-xyz/SuperVaults
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperVault.sol
1004 lines (822 loc) · 38.8 KB
/
SuperVault.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: MIT
pragma solidity ^0.8.24;
import { Math } from "openzeppelin/contracts/utils/math/Math.sol";
import { Address } from "openzeppelin/contracts/utils/Address.sol";
import { EnumerableSet } from "openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import { SafeERC20 } from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import { ERC20 } from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import { IERC20 } from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import { IERC165 } from "openzeppelin/contracts/utils/introspection/IERC165.sol";
import {
SingleDirectSingleVaultStateReq,
SingleDirectMultiVaultStateReq,
MultiVaultSFData,
SingleVaultSFData,
LiqRequest
} from "superform-core/src/types/DataTypes.sol";
import { ISuperPositions } from "superform-core/src/interfaces/ISuperPositions.sol";
import { DataLib } from "superform-core/src/libraries/DataLib.sol";
import { IBaseForm } from "superform-core/src/interfaces/IBaseForm.sol";
import { IBaseRouter } from "superform-core/src/interfaces/IBaseRouter.sol";
import { ISuperformRouterPlus } from "superform-core/src/interfaces/ISuperformRouterPlus.sol";
import { ISuperRegistry } from "superform-core/src/interfaces/ISuperRegistry.sol";
import { ISuperVault, IERC1155Receiver } from "./interfaces/ISuperVault.sol";
import { ISuperformFactory } from "superform-core/src/interfaces/ISuperformFactory.sol";
import { BaseStrategy } from "tokenized-strategy/BaseStrategy.sol";
import { ISuperformFactoryMinimal } from "./interfaces/ISuperformFactoryMinimal.sol";
/// @title SuperVault
/// @notice A vault contract that manages multiple Superform positions
/// @dev Inherits from BaseStrategy and implements ISuperVault and IERC1155Receiver
/// @author Superform Labs
contract SuperVault is BaseStrategy, ISuperVault {
using EnumerableSet for EnumerableSet.UintSet;
using SafeERC20 for IERC20;
using SafeERC20 for ERC20;
using DataLib for uint256;
using Math for uint256;
//////////////////////////////////////////////////////////////
// STATE VARIABLES //
//////////////////////////////////////////////////////////////
/// @notice The chain ID of the network this contract is deployed on
uint64 public immutable CHAIN_ID;
/// @notice The address of the SuperVault Strategist
address public strategist;
/// @notice The address of the SuperVault Vault Manager
address public vaultManager;
/// @notice The address of the SuperRegistry contract
ISuperRegistry public immutable superRegistry;
/// @notice The address of the SuperformFactory contract
ISuperformFactory public immutable superformFactory;
/// @notice The ID of the ERC5115 form implementation
uint32 public ERC5115FormImplementationId;
/// @notice The total weight used for calculating proportions (10000 = 100%)
uint256 private constant TOTAL_WEIGHT = 10_000;
/// @notice The maximum allowed slippage (1% = 100)
uint256 private constant MAX_SLIPPAGE = 100;
/// @dev Tolerance constant to account for minAmountOut check in 5115
uint256 private constant TOLERANCE_CONSTANT = 10 wei;
/// @notice The number of Superforms in the vault
uint256 public numberOfSuperforms;
/// @notice The deposit limit for the vault
uint256 public depositLimit;
/// @notice Set of whitelisted Superform IDs for easy access
EnumerableSet.UintSet whitelistedSuperformIdsSet;
/// @notice Array of Superform IDs in the vault
uint256[] public superformIds;
/// @notice Array of weights for each Superform in the vault
uint256[] public weights;
address private immutable _SUPER_POSITIONS;
address private immutable _SUPERFORM_ROUTER;
address private immutable _SUPERFORM_FACTORY;
/// @notice Slot for call depth.
/// @dev Equal to bytes32(uint256(keccak256("transient.calldepth")) - 1).
bytes32 internal constant CALL_DEPTH_SLOT = 0x7a74fd168763fd280eaec3bcd2fd62d0e795027adc8183a693c497a7c2b10b5c;
//////////////////////////////////////////////////////////////
// MODIFIERS //
//////////////////////////////////////////////////////////////
/// @notice Ensures that only the Super Vaults Strategist can call the function
modifier onlySuperVaultsStrategist() {
if (strategist != msg.sender) {
revert NOT_SUPER_VAULTS_STRATEGIST();
}
_;
}
/// @notice Ensures that only the Vault Manager can call the function
modifier onlyVaultManager() {
if (vaultManager != msg.sender) {
revert NOT_VAULT_MANAGER();
}
_;
}
//////////////////////////////////////////////////////////////
// CONSTRUCTOR //
//////////////////////////////////////////////////////////////
/// @param superRegistry_ Address of the SuperRegistry contract
/// @param asset_ Address of the asset token
/// @param name_ Name of the strategy
/// @param depositLimit_ Maximum deposit limit
/// @param superformIds_ Array of Superform IDs
/// @param startingWeights_ Array of starting weights for each Superform
constructor(
address superRegistry_,
address asset_,
address strategist_,
address vaultManager_,
string memory name_,
uint256 depositLimit_,
uint256[] memory superformIds_,
uint256[] memory startingWeights_
)
BaseStrategy(asset_, name_)
{
numberOfSuperforms = superformIds_.length;
if (numberOfSuperforms == 0) {
revert ZERO_SUPERFORMS();
}
if (numberOfSuperforms != startingWeights_.length) {
revert ARRAY_LENGTH_MISMATCH();
}
if (superRegistry_ == address(0) || strategist_ == address(0) || vaultManager_ == address(0)) {
revert ZERO_ADDRESS();
}
superRegistry = ISuperRegistry(superRegistry_);
superformFactory = ISuperformFactory(superRegistry.getAddress(keccak256("SUPERFORM_FACTORY")));
if (CHAIN_ID > type(uint64).max) {
revert BLOCK_CHAIN_ID_OUT_OF_BOUNDS();
}
CHAIN_ID = uint64(block.chainid);
uint256 totalWeight;
address superform;
for (uint256 i; i < numberOfSuperforms; ++i) {
/// @dev this superVault only supports superforms that have the same asset as the vault
(superform,,) = superformIds_[i].getSuperform();
if (!superformFactory.isSuperform(superformIds_[i])) {
revert SUPERFORM_DOES_NOT_EXIST(superformIds_[i]);
}
if (IBaseForm(superform).getVaultAsset() != asset_) {
revert SUPERFORM_DOES_NOT_SUPPORT_ASSET();
}
/// @dev initial whitelist of superform IDs
_addToWhitelist(superformIds_[i]);
totalWeight += startingWeights_[i];
}
if (totalWeight != TOTAL_WEIGHT) revert INVALID_WEIGHTS();
strategist = strategist_;
vaultManager = vaultManager_;
superformIds = superformIds_;
weights = startingWeights_;
depositLimit = depositLimit_;
{
_SUPER_POSITIONS = _getAddress(keccak256("SUPER_POSITIONS"));
_SUPERFORM_ROUTER = _getAddress(keccak256("SUPERFORM_ROUTER"));
_SUPERFORM_FACTORY = _getAddress(keccak256("SUPERFORM_FACTORY"));
}
}
//////////////////////////////////////////////////////////////
// EXTERNAL FUNCTIONS //
//////////////////////////////////////////////////////////////
/// @inheritdoc ISuperVault
function setDepositLimit(uint256 depositLimit_) external override onlyVaultManager {
depositLimit = depositLimit_;
emit DepositLimitSet(depositLimit_);
}
/// @inheritdoc ISuperVault
function setStrategist(address strategist_) external override onlyManagement {
strategist = strategist_;
emit StrategistSet(strategist_);
}
/// @inheritdoc ISuperVault
function setValid5115FormImplementationId(uint32 formImplementationId_) external override onlyManagement {
if (formImplementationId_ == 0) revert ZERO_ID();
ERC5115FormImplementationId = formImplementationId_;
}
/// @inheritdoc ISuperVault
function rebalance(RebalanceArgs calldata rebalanceArgs_) external payable override onlySuperVaultsStrategist {
uint256 lenRebalanceFrom = rebalanceArgs_.superformIdsRebalanceFrom.length;
uint256 lenAmountsRebalanceFrom = rebalanceArgs_.amountsRebalanceFrom.length;
uint256 lenFinal = rebalanceArgs_.finalSuperformIds.length;
if (lenAmountsRebalanceFrom == 0) revert EMPTY_AMOUNTS_REBALANCE_FROM();
if (lenFinal == 0) revert EMPTY_FINAL_SUPERFORM_IDS();
/// @dev sanity check input arrays
if (lenRebalanceFrom != lenAmountsRebalanceFrom || lenFinal != rebalanceArgs_.weightsOfRedestribution.length) {
revert ARRAY_LENGTH_MISMATCH();
}
{
/// @dev caching to avoid multiple loads
uint256 foundCount;
uint256 nSuperforms = numberOfSuperforms;
for (uint256 i; i < lenRebalanceFrom; ++i) {
for (uint256 j; j < nSuperforms; ++j) {
if (rebalanceArgs_.superformIdsRebalanceFrom[i] == superformIds[j]) {
foundCount++;
break;
}
}
}
if (foundCount != lenRebalanceFrom) {
revert INVALID_SUPERFORM_ID_REBALANCE_FROM();
}
}
for (uint256 i = 1; i < lenRebalanceFrom; ++i) {
if (rebalanceArgs_.superformIdsRebalanceFrom[i] <= rebalanceArgs_.superformIdsRebalanceFrom[i - 1]) {
revert DUPLICATE_SUPERFORM_IDS_REBALANCE_FROM();
}
}
for (uint256 i; i < lenFinal; ++i) {
if (i >= 1 && rebalanceArgs_.finalSuperformIds[i] <= rebalanceArgs_.finalSuperformIds[i - 1]) {
revert DUPLICATE_FINAL_SUPERFORM_IDS();
}
if (!whitelistedSuperformIdsSet.contains(rebalanceArgs_.finalSuperformIds[i])) {
revert SUPERFORM_NOT_WHITELISTED();
}
}
/// @dev step 1: prepare rebalance arguments
ISuperformRouterPlus.RebalanceMultiPositionsSyncArgs memory args = _prepareRebalanceArgs(
rebalanceArgs_.superformIdsRebalanceFrom,
rebalanceArgs_.amountsRebalanceFrom,
rebalanceArgs_.finalSuperformIds,
rebalanceArgs_.weightsOfRedestribution,
rebalanceArgs_.slippage
);
address routerPlus = _getAddress(keccak256("SUPERFORM_ROUTER_PLUS"));
/// @dev step 2: execute rebalance
_setSuperPositionsApproval(routerPlus, args.ids, args.sharesToRedeem);
ISuperformRouterPlus(routerPlus).rebalanceMultiPositions(args);
/// @dev step 3: update SV data
/// @notice no issue about reentrancy as the external contracts are trusted
/// @notice updateSV emits rebalance event
_updateSVData(_SUPER_POSITIONS, rebalanceArgs_.finalSuperformIds);
}
/// @inheritdoc ISuperVault
function forwardDustToPaymaster() external override {
address paymaster = superRegistry.getAddress(keccak256("PAYMASTER"));
IERC20 token = IERC20(asset);
uint256 dust = _getAssetBalance(token);
if (dust != 0) {
token.safeTransfer(paymaster, dust);
emit DustForwardedToPaymaster(dust);
}
}
/// @inheritdoc ISuperVault
function setWhitelist(
uint256[] memory superformIds_,
bool[] memory isWhitelisted_
)
external
override
onlyVaultManager
{
uint256 length = superformIds_.length;
if (length != isWhitelisted_.length) revert ARRAY_LENGTH_MISMATCH();
if (length == 0) revert ZERO_SUPERFORMS();
for (uint256 i; i < length; ++i) {
_changeSuperformWhitelist(superformIds_[i], isWhitelisted_[i]);
}
}
/// @inheritdoc ISuperVault
function setVaultManager(address vaultManager_) external override onlyManagement {
if (vaultManager_ == address(0)) revert ZERO_ADDRESS();
vaultManager = vaultManager_;
emit VaultManagerSet(vaultManager_);
}
//////////////////////////////////////////////////////////////
// EXTERNAL VIEW/PURE FUNCTIONS //
//////////////////////////////////////////////////////////////
/// @inheritdoc ISuperVault
function getIsWhitelisted(uint256[] memory superformIds_) external view returns (bool[] memory isWhitelisted) {
uint256 length = superformIds_.length;
isWhitelisted = new bool[](length);
for (uint256 i; i < length; ++i) {
isWhitelisted[i] = whitelistedSuperformIdsSet.contains(superformIds_[i]);
}
return isWhitelisted;
}
/// @inheritdoc ISuperVault
function getWhitelist() external view override returns (uint256[] memory) {
return whitelistedSuperformIdsSet.values();
}
/// @inheritdoc ISuperVault
function getSuperVaultData() external view returns (uint256[] memory superformIds_, uint256[] memory weights_) {
return (superformIds, weights);
}
/// @inheritdoc IERC1155Receiver
function onERC1155Received(
address,
address,
uint256,
uint256,
bytes calldata
)
external
pure
override
returns (bytes4)
{
return this.onERC1155Received.selector;
}
/// @inheritdoc IERC1155Receiver
function onERC1155BatchReceived(
address,
address,
uint256[] calldata,
uint256[] calldata,
bytes calldata
)
external
pure
override
returns (bytes4)
{
return this.onERC1155BatchReceived.selector;
}
/// @notice Checks if the contract supports a given interface
/// @param interfaceId The interface identifier
/// @return bool True if the contract supports the interface
function supportsInterface(bytes4 interfaceId) external pure override returns (bool) {
return interfaceId == type(IERC165).interfaceId || interfaceId == type(IERC1155Receiver).interfaceId;
}
/// @inheritdoc BaseStrategy
function availableDepositLimit(address /*_owner*/ ) public view override returns (uint256) {
uint256 totalAssets = TokenizedStrategy.totalAssets();
uint256 _depositLimit_ = depositLimit;
return totalAssets >= _depositLimit_ ? 0 : _depositLimit_ - totalAssets;
}
//////////////////////////////////////////////////////////////
// BASESTRATEGY INTERNAL OVERRIDES //
//////////////////////////////////////////////////////////////
/// @notice Deploys funds to the underlying Superforms
/// @param amount_ The amount of funds to deploy
function _deployFunds(uint256 amount_) internal override {
bytes memory callData = numberOfSuperforms == 1
? abi.encodeWithSelector(
IBaseRouter.singleDirectSingleVaultDeposit.selector,
SingleDirectSingleVaultStateReq(_prepareSingleVaultDepositData(amount_))
)
: abi.encodeWithSelector(
IBaseRouter.singleDirectMultiVaultDeposit.selector,
SingleDirectMultiVaultStateReq(_prepareMultiVaultDepositData(amount_))
);
address router = _SUPERFORM_ROUTER;
asset.safeIncreaseAllowance(router, amount_);
/// @dev this call has to be enforced with 0 msg.value not to break the 4626 standard
(bool success, bytes memory returndata) = router.call(callData);
Address.verifyCallResult(success, returndata, "CallRevertWithNoReturnData");
if (asset.allowance(address(this), router) > 0) asset.forceApprove(router, 0);
}
/// @notice Frees funds from the underlying Superforms
/// @param amount_ The amount of funds to free
function _freeFunds(uint256 amount_) internal override {
bytes memory callData;
address router = _SUPERFORM_ROUTER;
if (numberOfSuperforms == 1) {
SingleVaultSFData memory svData = _prepareSingleVaultWithdrawData(amount_);
callData = abi.encodeWithSelector(
IBaseRouter.singleDirectSingleVaultWithdraw.selector, SingleDirectSingleVaultStateReq(svData)
);
_setSuperPositionApproval(router, svData.superformId, svData.amount);
} else {
MultiVaultSFData memory mvData = _prepareMultiVaultWithdrawData(amount_);
callData = abi.encodeWithSelector(
IBaseRouter.singleDirectMultiVaultWithdraw.selector, SingleDirectMultiVaultStateReq(mvData)
);
_setSuperPositionsApproval(router, mvData.superformIds, mvData.amounts);
}
/// @dev this call has to be enforced with 0 msg.value not to break the 4626 standard
(bool success, bytes memory returndata) = router.call(callData);
Address.verifyCallResult(success, returndata, "CallRevertWithNoReturnData");
}
/// @notice Reports the total assets of the vault
/// @return totalAssets The total assets of the vault
function _harvestAndReport() internal view override returns (uint256 totalAssets) {
uint256 totalAssetsInVaults;
uint256 _numberOfSuperforms_ = numberOfSuperforms;
uint256[] memory _superformIds_ = superformIds;
address superPositions = _SUPER_POSITIONS;
for (uint256 i; i < _numberOfSuperforms_;) {
(address superform,,) = _superformIds_[i].getSuperform();
/// @dev This contract holds superPositions, not shares
uint256 spBalance = ISuperPositions(superPositions).balanceOf(address(this), _superformIds_[i]);
totalAssetsInVaults += IBaseForm(superform).previewRedeemFrom(spBalance);
unchecked {
++i;
}
}
totalAssets = totalAssetsInVaults + _getAssetBalance(asset);
}
//////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS //
//////////////////////////////////////////////////////////////
function _prepareMultiVaultDepositData(uint256 amount_) internal view returns (MultiVaultSFData memory mvData) {
uint256 _numberOfSuperforms_ = numberOfSuperforms;
mvData.superformIds = superformIds;
mvData.amounts = new uint256[](_numberOfSuperforms_);
mvData.maxSlippages = new uint256[](_numberOfSuperforms_);
mvData.liqRequests = new LiqRequest[](_numberOfSuperforms_);
mvData.hasDstSwaps = new bool[](_numberOfSuperforms_);
mvData.retain4626s = mvData.hasDstSwaps;
mvData.receiverAddress = address(this);
mvData.receiverAddressSP = address(this);
mvData.outputAmounts = new uint256[](_numberOfSuperforms_);
bytes[] memory dataToEncode = new bytes[](_numberOfSuperforms_);
uint256[] memory _weights_ = weights;
for (uint256 i; i < _numberOfSuperforms_;) {
mvData.liqRequests[i].token = address(asset);
(address superform,,) = mvData.superformIds[i].getSuperform();
dataToEncode[i] = _prepareDepositExtraFormDataForSuperform(mvData.superformIds[i]);
/// @notice rounding down to avoid one-off issue
mvData.amounts[i] = amount_.mulDiv(_weights_[i], TOTAL_WEIGHT, Math.Rounding.Down);
mvData.outputAmounts[i] = IBaseForm(superform).previewDepositTo(mvData.amounts[i]);
mvData.maxSlippages[i] = MAX_SLIPPAGE;
unchecked {
++i;
}
}
mvData.extraFormData = abi.encode(_numberOfSuperforms_, dataToEncode);
return mvData;
}
function _prepareSingleVaultDepositData(uint256 amount_) internal view returns (SingleVaultSFData memory svData) {
svData.superformId = superformIds[0];
svData.amount = amount_;
svData.maxSlippage = MAX_SLIPPAGE;
svData.liqRequest.token = address(asset);
svData.hasDstSwap = false;
svData.retain4626 = false;
svData.receiverAddress = address(this);
svData.receiverAddressSP = address(this);
(address superform,,) = svData.superformId.getSuperform();
svData.outputAmount = IBaseForm(superform).previewDepositTo(amount_);
bytes memory dataToEncode = _prepareDepositExtraFormDataForSuperform(svData.superformId);
bytes[] memory finalDataToEncode = new bytes[](1);
finalDataToEncode[0] = dataToEncode;
svData.extraFormData = abi.encode(1, finalDataToEncode);
return svData;
}
function _prepareMultiVaultWithdrawData(uint256 amount_) internal view returns (MultiVaultSFData memory mvData) {
uint256 _numberOfSuperforms_ = numberOfSuperforms;
mvData.superformIds = superformIds;
mvData.amounts = new uint256[](_numberOfSuperforms_);
mvData.maxSlippages = new uint256[](_numberOfSuperforms_);
mvData.liqRequests = new LiqRequest[](_numberOfSuperforms_);
mvData.hasDstSwaps = new bool[](_numberOfSuperforms_);
mvData.retain4626s = mvData.hasDstSwaps;
mvData.receiverAddress = address(this);
mvData.receiverAddressSP = address(this);
mvData.outputAmounts = new uint256[](_numberOfSuperforms_);
address superPositions = _SUPER_POSITIONS;
uint256 totalAssetsInVaults;
uint256[] memory spBalances = new uint256[](_numberOfSuperforms_);
uint256[] memory assetBalances = new uint256[](_numberOfSuperforms_);
// Snapshot assets and SP balances
for (uint256 i; i < _numberOfSuperforms_;) {
(address superform,,) = mvData.superformIds[i].getSuperform();
spBalances[i] = _getSuperPositionBalance(superPositions, mvData.superformIds[i]);
assetBalances[i] = IBaseForm(superform).previewRedeemFrom(spBalances[i]);
totalAssetsInVaults += assetBalances[i];
unchecked {
++i;
}
}
// Calculate withdrawal amounts
for (uint256 i; i < _numberOfSuperforms_;) {
mvData.liqRequests[i].token = address(asset);
(address superform,,) = mvData.superformIds[i].getSuperform();
bool isERC5115 = _isERC5115Vault(mvData.superformIds[i]);
if (isERC5115) {
mvData.liqRequests[i].interimToken = address(asset);
}
if (amount_ >= totalAssetsInVaults) {
mvData.amounts[i] = spBalances[i];
mvData.outputAmounts[i] = _tolerance(isERC5115, assetBalances[i]);
} else {
uint256 amountOut = amount_.mulDiv(weights[i], TOTAL_WEIGHT, Math.Rounding.Down);
mvData.outputAmounts[i] = _tolerance(isERC5115, amountOut);
mvData.amounts[i] = IBaseForm(superform).previewDepositTo(amountOut);
if (mvData.amounts[i] > spBalances[i]) {
mvData.amounts[i] = spBalances[i];
}
}
mvData.maxSlippages[i] = MAX_SLIPPAGE;
unchecked {
++i;
}
}
return mvData;
}
function _prepareSingleVaultWithdrawData(uint256 amount_) internal view returns (SingleVaultSFData memory svData) {
svData.superformId = superformIds[0];
(address superform,,) = svData.superformId.getSuperform();
// Get current balances
uint256 spBalance = _getSuperPositionBalance(_SUPER_POSITIONS, svData.superformId);
uint256 assetBalance = IBaseForm(superform).previewRedeemFrom(spBalance);
// Set up basic data
svData.liqRequest.token = address(asset);
bool isERC5115 = _isERC5115Vault(svData.superformId);
if (isERC5115) {
svData.liqRequest.interimToken = address(asset);
}
// Calculate withdrawal amounts
if (amount_ >= assetBalance) {
svData.amount = spBalance;
svData.outputAmount = _tolerance(isERC5115, assetBalance);
} else {
svData.outputAmount = _tolerance(isERC5115, amount_);
svData.amount = IBaseForm(superform).previewDepositTo(amount_);
if (svData.amount > spBalance) {
svData.amount = spBalance;
}
}
svData.maxSlippage = MAX_SLIPPAGE;
svData.hasDstSwap = false;
svData.retain4626 = false;
svData.receiverAddress = address(this);
svData.receiverAddressSP = address(this);
return svData;
}
/// @notice Checks if a vault is ERC5115 and validates form implementation IDs
/// @param superformId_ The superform ID to check
/// @return isERC5115 True if the vault is ERC5115
function _isERC5115Vault(uint256 superformId_) internal view returns (bool isERC5115) {
ISuperformFactoryMinimal factory = ISuperformFactoryMinimal(_SUPERFORM_FACTORY);
address erc5115Implementation = factory.getFormImplementation(ERC5115FormImplementationId);
(address superform,,) = superformId_.getSuperform();
uint256 superFormId = factory.vaultFormImplCombinationToSuperforms(
keccak256(abi.encode(erc5115Implementation, IBaseForm(superform).getVaultAddress()))
);
if (superFormId == superformId_) {
isERC5115 = true;
}
}
/// @dev returns the address for id_ from super registry
function _getAddress(bytes32 id_) internal view returns (address) {
return superRegistry.getAddress(id_);
}
/// @notice Sets approval for multiple SuperPositions
/// @param router_ The router address to approve
/// @param superformIds_ The superform IDs to approve
/// @param amounts_ The amounts to approve
function _setSuperPositionsApproval(
address router_,
uint256[] memory superformIds_,
uint256[] memory amounts_
)
internal
{
ISuperPositions(_SUPER_POSITIONS).setApprovalForMany(router_, superformIds_, amounts_);
}
/// @notice Sets approval for a single SuperPosition
/// @param router_ The router address to approve
/// @param superformId_ The superform ID to approve
/// @param amount_ The amount to approve
function _setSuperPositionApproval(address router_, uint256 superformId_, uint256 amount_) internal {
ISuperPositions(_SUPER_POSITIONS).setApprovalForOne(router_, superformId_, amount_);
}
/// @notice Gets the current balance of the asset token held by this contract
/// @return balance The current balance of the asset token
function _getAssetBalance(IERC20 token_) internal view returns (uint256) {
return token_.balanceOf(address(this));
}
function _getSuperPositionBalance(address superPositions, uint256 superformId) internal view returns (uint256) {
return ISuperPositions(superPositions).balanceOf(address(this), superformId);
}
/// @notice Prepares rebalance arguments for Superform Router Plus
/// @param superformIdsRebalanceFrom_ Array of Superform IDs to rebalance from
/// @param amountsRebalanceFrom_ Array of amounts to rebalance from
/// @param finalSuperformIds_ Array of Superform IDs to rebalance to
/// @param weightsOfRedestribution_ Array of weights for redestribution
/// @param slippage_ Maximum allowed slippage
function _prepareRebalanceArgs(
uint256[] calldata superformIdsRebalanceFrom_,
uint256[] calldata amountsRebalanceFrom_,
uint256[] calldata finalSuperformIds_,
uint256[] calldata weightsOfRedestribution_,
uint256 slippage_
)
internal
view
returns (ISuperformRouterPlus.RebalanceMultiPositionsSyncArgs memory args)
{
args.ids = superformIdsRebalanceFrom_;
args.sharesToRedeem = amountsRebalanceFrom_;
args.interimAsset = address(asset); // Assuming 'asset' is the interim token
args.slippage = slippage_; // 1% slippage, adjust as needed
args.receiverAddressSP = address(this);
(SingleDirectMultiVaultStateReq memory req, uint256 totalOutputAmount) =
_prepareSingleDirectMultiVaultStateReq(superformIdsRebalanceFrom_, amountsRebalanceFrom_, slippage_, true);
/// @dev prepare callData for rebalance from
args.callData = abi.encodeWithSelector(IBaseRouter.singleDirectMultiVaultWithdraw.selector, req);
/// @dev create a filtered version of superformIdsRebalanceTo
(uint256[] memory filteredSuperformIds, uint256[] memory filteredWeights) =
_filterNonZeroWeights(finalSuperformIds_, weightsOfRedestribution_);
(req,) = _prepareSingleDirectMultiVaultStateReq(
filteredSuperformIds, _calculateAmounts(totalOutputAmount, filteredWeights), slippage_, false
);
/// @dev prepare rebalanceToCallData
args.rebalanceToCallData = abi.encodeWithSelector(IBaseRouter.singleDirectMultiVaultDeposit.selector, req);
args.expectedAmountToReceivePostRebalanceFrom = totalOutputAmount;
}
/// @notice Prepares single direct multi-vault state request
/// @param superformIds_ Array of Superform IDs
/// @param amounts_ Array of amounts
/// @param slippage_ Maximum allowed slippage
/// @param isWithdraw_ True if withdrawing, false if depositing
/// @return req The prepared single direct multi-vault state request
/// @return totalOutputAmount The total output amount
function _prepareSingleDirectMultiVaultStateReq(
uint256[] memory superformIds_,
uint256[] memory amounts_,
uint256 slippage_,
bool isWithdraw_
)
internal
view
returns (SingleDirectMultiVaultStateReq memory req, uint256 totalOutputAmount)
{
MultiVaultSFData memory data;
data.superformIds = superformIds_;
data.amounts = amounts_;
address routerPlus = _getAddress(keccak256("SUPERFORM_ROUTER_PLUS"));
uint256 _numberOfSuperforms_ = superformIds_.length;
data.outputAmounts = new uint256[](_numberOfSuperforms_);
data.maxSlippages = new uint256[](_numberOfSuperforms_);
data.liqRequests = new LiqRequest[](_numberOfSuperforms_);
bytes[] memory dataToEncode = new bytes[](_numberOfSuperforms_);
for (uint256 i; i < _numberOfSuperforms_;) {
(address superform,,) = superformIds_[i].getSuperform();
if (isWithdraw_) {
// Check if vault is ERC5115
bool isERC5115 = _isERC5115Vault(superformIds_[i]);
if (isERC5115) {
data.liqRequests[i].interimToken = address(asset);
}
uint256 amountOut = IBaseForm(superform).previewRedeemFrom(amounts_[i]);
data.outputAmounts[i] = _tolerance(isERC5115, amountOut);
} else {
dataToEncode[i] = _prepareDepositExtraFormDataForSuperform(superformIds_[i]);
data.outputAmounts[i] = IBaseForm(superform).previewDepositTo(amounts_[i]);
}
totalOutputAmount += data.outputAmounts[i];
data.maxSlippages[i] = slippage_;
data.liqRequests[i].token = address(asset);
data.liqRequests[i].liqDstChainId = CHAIN_ID;
unchecked {
++i;
}
}
data.hasDstSwaps = new bool[](_numberOfSuperforms_);
data.retain4626s = data.hasDstSwaps;
/// @dev routerPlus receives assets to continue the rebalance
data.receiverAddress = routerPlus;
/// @dev in case of withdraw failure, this vault receives the superPositions back
data.receiverAddressSP = address(this);
if (!isWithdraw_) {
data.extraFormData = abi.encode(_numberOfSuperforms_, dataToEncode);
}
req.superformData = data;
}
/// @notice Prepares deposit extra form data for a single superform
/// @param superformId_ The superform ID
/// @return bytes Encoded data for the superform
function _prepareDepositExtraFormDataForSuperform(uint256 superformId_) internal view returns (bytes memory) {
// For ERC4626 vaults, no extra data needed
// For ERC5115 vaults, include asset address
bytes memory extraData = _isERC5115Vault(superformId_) ? abi.encode(address(asset)) : bytes("");
return abi.encode(superformId_, extraData);
}
/// @notice Calculates amounts based on total output amount and weights
/// @param totalOutputAmount_ The total output amount
/// @param weights_ Array of weights
/// @return amounts Array of calculated amounts
function _calculateAmounts(
uint256 totalOutputAmount_,
uint256[] memory weights_
)
internal
pure
returns (uint256[] memory amounts)
{
amounts = new uint256[](weights_.length);
for (uint256 i; i < weights_.length; ++i) {
amounts[i] = totalOutputAmount_.mulDiv(weights_[i], TOTAL_WEIGHT, Math.Rounding.Down);
}
}
/// @notice Filters out zero weights and returns corresponding superform IDs and weights
/// @param superformIds_ Array of Superform IDs
/// @param weights_ Array of weights
/// @return filteredIds Array of filtered Superform IDs
/// @return filteredWeights Array of filtered weights
function _filterNonZeroWeights(
uint256[] calldata superformIds_,
uint256[] calldata weights_
)
internal
pure
returns (uint256[] memory filteredIds, uint256[] memory filteredWeights)
{
uint256 count;
uint256 length = weights_.length;
for (uint256 i; i < length; ++i) {
if (weights_[i] != 0) {
count++;
}
}
filteredIds = new uint256[](count);
filteredWeights = new uint256[](count);
uint256 j;
uint256 totalWeight;
for (uint256 i; i < length; ++i) {
if (weights_[i] != 0) {
filteredIds[j] = superformIds_[i];
filteredWeights[j] = weights_[i];
totalWeight += weights_[i];
j++;
}
}
if (totalWeight != TOTAL_WEIGHT) revert INVALID_WEIGHTS();
}
/// @notice Updates the SuperVault data after rebalancing
/// @param superPositions_ Address of the SuperPositions contract
/// @param finalSuperformIds_ Array of Superform IDs to rebalance to
function _updateSVData(address superPositions_, uint256[] memory finalSuperformIds_) internal {
// Cache current superform IDs
uint256[] memory currentSuperformIds = superformIds;
// For each current superform ID
uint256 numSuperforms = currentSuperformIds.length;
uint256 numFinalSuperforms = finalSuperformIds_.length;
for (uint256 i; i < numSuperforms;) {
bool found;
// Check if it exists in finalSuperformIds_
for (uint256 j; j < numFinalSuperforms;) {
if (currentSuperformIds[i] == finalSuperformIds_[j]) {
found = true;
break;
}
unchecked {
++j;
}
}
// If not found in final IDs, it should be fully rebalanced
if (!found) {
if (_getSuperPositionBalance(superPositions_, currentSuperformIds[i]) != 0) {
revert INVALID_SP_FULL_REBALANCE(currentSuperformIds[i]);
}
}
unchecked {
++i;
}
}
uint256 totalWeight;
uint256 length = finalSuperformIds_.length;
if (length == 0) revert ZERO_SUPERFORMS();
uint256[] memory newWeights = new uint256[](length);
/// @dev check if finalSuperformIds are present in superform factory and support the asset
ISuperformFactory factory = ISuperformFactory(_SUPERFORM_FACTORY);
address superform;
uint256 value;
address assetCache = address(asset);
/// @dev calculate total value and individual values
for (uint256 i; i < length;) {
if (!factory.isSuperform(finalSuperformIds_[i])) {
revert SUPERFORM_DOES_NOT_EXIST(finalSuperformIds_[i]);
}
(superform,,) = finalSuperformIds_[i].getSuperform();
if (IBaseForm(superform).getVaultAsset() != assetCache) {
revert SUPERFORM_DOES_NOT_SUPPORT_ASSET();
}
uint256 balance = _getSuperPositionBalance(superPositions_, finalSuperformIds_[i]);
value = IBaseForm(superform).previewRedeemFrom(balance);
newWeights[i] = value;
totalWeight += value;
unchecked {
++i;
}
}
/// @dev calculate new weights as percentages
uint256 totalAssignedWeight;
for (uint256 i; i < length - 1;) {
newWeights[i] = newWeights[i].mulDiv(TOTAL_WEIGHT, totalWeight, Math.Rounding.Down);
totalAssignedWeight += newWeights[i];
unchecked {
++i;
}
}
/// @notice assign remaining weight to the last index
newWeights[length - 1] = TOTAL_WEIGHT - totalAssignedWeight;
/// @dev update SV data
weights = newWeights;
superformIds = finalSuperformIds_;
numberOfSuperforms = length;
emit RebalanceComplete(finalSuperformIds_, newWeights);
}
/// @notice Changes the whitelist for a Superform ID
/// @param superformId_ The Superform ID to change
/// @param isWhitelisted_ Whether to whitelist or blacklist
function _changeSuperformWhitelist(uint256 superformId_, bool isWhitelisted_) internal {
bool currentlyWhitelisted = whitelistedSuperformIdsSet.contains(superformId_);
// Only process if there's an actual change
if (currentlyWhitelisted != isWhitelisted_) {
if (isWhitelisted_) {
_addToWhitelist(superformId_);
} else {
_removeFromWhitelist(superformId_);
}
emit SuperformWhitelisted(superformId_, isWhitelisted_);
}
}
/// @notice Adds a superform ID to the whitelist array
/// @param superformId The Superform ID to add
function _addToWhitelist(uint256 superformId) internal {
whitelistedSuperformIdsSet.add(superformId);
}
/// @notice Removes a superform ID from the whitelist array
/// @param superformId The Superform ID to remove
function _removeFromWhitelist(uint256 superformId) internal {
whitelistedSuperformIdsSet.remove(superformId);
}
/// @notice Calculates the tolerance for ERC5115 vaults
/// @param isERC5115 Whether the vault is ERC5115
/// @param amount The amount to calculate tolerance for
/// @return The calculated tolerance