Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
haythemsellami committed Jul 23, 2024
1 parent 6af81ca commit 4bc449c
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 41 deletions.
17 changes: 8 additions & 9 deletions src/core/EulerAggregationVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ contract EulerAggregationVault is
/// @dev See {RewardsModule-disableBalanceForwarder}.
function disableBalanceForwarder() external override use(rewardsModule) {}

function executeRebalance(address[] calldata _strategies) external override use(rebalanceModule) {}
function rebalance(address[] calldata _strategies) external override use(rebalanceModule) {}

/// @notice Harvest all the strategies. Any positive yiled should be gupled by calling gulp() after harvesting.
/// @dev This function will loop through the strategies following the withdrawal queue order and harvest all.
Expand Down Expand Up @@ -247,11 +247,11 @@ contract EulerAggregationVault is

if (_receiver == address(this)) revert Errors.CanNotReceiveWithdrawnAsset();

super._withdraw(_caller, _receiver, _owner, _assets, _shares);

AggregationVaultStorage storage $ = Storage._getAggregationVaultStorage();
$.totalAssetsDeposited -= _assets;

super._withdraw(_caller, _receiver, _owner, _assets, _shares);

_gulp();
}

Expand Down Expand Up @@ -423,7 +423,7 @@ contract EulerAggregationVault is
}

/// @dev Loop through stratgies, aggregate positive yield and loss and account for net amount.
/// @dev Loss socialization will be taken out from interest left first, if not enough, sozialize on deposits.
/// @dev Loss socialization will be taken out from interest left first, if not enough, socialize on deposits.
function _harvest() internal {
// gulp any extra tokens to cover in case of loss socialization
_gulp();
Expand Down Expand Up @@ -482,13 +482,12 @@ contract EulerAggregationVault is
underlyingBalance -= accruedPerformanceFee;
yield -= accruedPerformanceFee;
}

$.strategies[_strategy].allocated = uint120(underlyingBalance);
} else {
loss = strategyAllocatedAmount - underlyingBalance;

$.strategies[_strategy].allocated = uint120(underlyingBalance);
}

$.strategies[_strategy].allocated = uint120(underlyingBalance);

emit Events.ExecuteHarvest(_strategy, underlyingBalance, strategyAllocatedAmount);

return (yield, loss);
Expand Down Expand Up @@ -545,6 +544,6 @@ contract EulerAggregationVault is
function _isCallerWithdrawalQueue() internal view {
AggregationVaultStorage storage $ = Storage._getAggregationVaultStorage();

if (_msgSender() != $.withdrawalQueue) revert Errors.NotWithdrawaQueue();
if (msg.sender != $.withdrawalQueue) revert Errors.NotWithdrawaQueue();
}
}
10 changes: 6 additions & 4 deletions src/core/common/Shared.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,21 @@ abstract contract Shared {
function _interestAccruedFromCache() internal view returns (uint256) {
AggregationVaultStorage storage $ = Storage._getAggregationVaultStorage();

uint40 interestSmearEndCached = $.interestSmearEnd;
// If distribution ended, full amount is accrued
if (block.timestamp >= $.interestSmearEnd) {
if (block.timestamp >= interestSmearEndCached) {
return $.interestLeft;
}

uint40 lastInterestUpdateCached = $.lastInterestUpdate;
// If just updated return 0
if ($.lastInterestUpdate == block.timestamp) {
if (lastInterestUpdateCached == block.timestamp) {
return 0;
}

// Else return what has accrued
uint256 totalDuration = $.interestSmearEnd - $.lastInterestUpdate;
uint256 timePassed = block.timestamp - $.lastInterestUpdate;
uint256 totalDuration = interestSmearEndCached - lastInterestUpdateCached;
uint256 timePassed = block.timestamp - lastInterestUpdateCached;

return $.interestLeft * timePassed / totalDuration;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/module/Rebalance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract contract RebalanceModule is ContextUpgradeable, Shared {

/// @notice Rebalance strategies allocation for a specific curated vault.
/// @param _strategies Strategies addresses.
function executeRebalance(address[] calldata _strategies) external virtual nonReentrant {
function rebalance(address[] calldata _strategies) external virtual nonReentrant {
_gulp();

for (uint256 i; i < _strategies.length; ++i) {
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/DepositRebalanceHarvestWithdrawE2ETest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ contract DepositRebalanceHarvestWithdrawE2ETest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down Expand Up @@ -141,7 +141,7 @@ contract DepositRebalanceHarvestWithdrawE2ETest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down Expand Up @@ -240,7 +240,7 @@ contract DepositRebalanceHarvestWithdrawE2ETest is EulerAggregationVaultBase {
strategiesToRebalance[0] = address(eTST);
strategiesToRebalance[1] = address(eTSTsecondary);
vm.prank(user1);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(
eulerAggregationVault.totalAllocated(), expectedeTSTStrategyCash + expectedeTSTsecondaryStrategyCash
Expand Down Expand Up @@ -330,7 +330,7 @@ contract DepositRebalanceHarvestWithdrawE2ETest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down Expand Up @@ -434,7 +434,7 @@ contract DepositRebalanceHarvestWithdrawE2ETest is EulerAggregationVaultBase {
strategiesToRebalance[0] = address(eTST);
strategiesToRebalance[1] = address(eTSTsecondary);
vm.prank(user1);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(
eulerAggregationVault.totalAllocated(), expectedeTSTStrategyCash + expectedeTSTsecondaryStrategyCash
Expand Down Expand Up @@ -566,7 +566,7 @@ contract DepositRebalanceHarvestWithdrawE2ETest is EulerAggregationVaultBase {
strategiesToRebalance[0] = address(eTST);
strategiesToRebalance[1] = address(eTSTsecondary);
vm.prank(user1);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(
eulerAggregationVault.totalAllocated(), expectedeTSTStrategyCash + expectedeTSTsecondaryStrategyCash
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/HarvestRedeemE2ETest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract HarvestRedeemE2ETest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/PerformanceFeeE2ETest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ contract PerformanceFeeE2ETest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/StrategyCapE2ETest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ contract StrategyCapE2ETest is EulerAggregationVaultBase {

vm.prank(user1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand All @@ -113,7 +113,7 @@ contract StrategyCapE2ETest is EulerAggregationVaultBase {
uint256 strategyAllocatedBefore = (eulerAggregationVault.getStrategy(address(eTST))).allocated;

strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);
vm.stopPrank();

assertEq(strategyAllocatedBefore, (eulerAggregationVault.getStrategy(address(eTST))).allocated);
Expand Down Expand Up @@ -158,7 +158,7 @@ contract StrategyCapE2ETest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), cap);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), cap);
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/ToggleStrategyEmergencyStatusE2ETest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ contract ToggleStrategyEmergencyStatusE2ETest is EulerAggregationVaultBase {
strategiesToRebalance[0] = address(eTST);
strategiesToRebalance[1] = address(eTSTsecondary);
vm.prank(user1);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(
eulerAggregationVault.totalAllocated(), expectedeTSTStrategyCash + expectedeTSTsecondaryStrategyCash
Expand Down Expand Up @@ -224,7 +224,7 @@ contract ToggleStrategyEmergencyStatusE2ETest is EulerAggregationVaultBase {
strategiesToRebalance[0] = address(eTST);
strategiesToRebalance[1] = address(eTSTsecondary);
vm.prank(user1);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(
eulerAggregationVault.totalAllocated(), expectedeTSTStrategyCash + expectedeTSTsecondaryStrategyCash
Expand Down
4 changes: 2 additions & 2 deletions test/invariant/handler/EulerAggregationVaultHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ contract EulerAggregationVaultHandler is Test {
assertEq(eulerAggVault.totalAllocationPoints(), ghost_totalAllocationPoints);
}

function executeRebalance(uint256 _actorIndexSeed) external {
function rebalance(uint256 _actorIndexSeed) external {
(currentActor, currentActorIndex) = actorUtil.fetchActor(_actorIndexSeed);

(address[] memory strategiesToRebalance, uint256 strategiesCounter) = withdrawalQueue.getWithdrawalQueueArray();
(currentActor, success, returnData) = actorUtil.initiateActorCall(
_actorIndexSeed,
address(eulerAggVault),
abi.encodeWithSelector(EulerAggregationVault.executeRebalance.selector, strategiesToRebalance)
abi.encodeWithSelector(EulerAggregationVault.rebalance.selector, strategiesToRebalance)
);

for (uint256 i; i < strategiesCounter; i++) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/GulpTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract GulpTest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/HarvestTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ contract HarvestTest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down
18 changes: 9 additions & 9 deletions test/unit/RebalanceTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract RebalanceTest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down Expand Up @@ -107,7 +107,7 @@ contract RebalanceTest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), eTSTMaxDeposit);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), eTSTMaxDeposit);
Expand Down Expand Up @@ -142,7 +142,7 @@ contract RebalanceTest is EulerAggregationVaultBase {
vm.warp(block.timestamp + 86400);
vm.prank(user1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

// create new strategy & add it
IEVault eTSTsecondary;
Expand Down Expand Up @@ -176,7 +176,7 @@ contract RebalanceTest is EulerAggregationVaultBase {

vm.prank(user1);
strategiesToRebalance[0] = address(eTSTsecondary);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

// assertEq(eulerAggregationVault.totalAllocated(), eTSTsecondaryMaxDeposit);
assertEq(
Expand Down Expand Up @@ -226,7 +226,7 @@ contract RebalanceTest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), strategyBefore.allocated);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), strategyBefore.allocated);
Expand Down Expand Up @@ -259,7 +259,7 @@ contract RebalanceTest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

// decrease allocation points
uint256 newAllocationPoints = 300e18;
Expand All @@ -277,7 +277,7 @@ contract RebalanceTest is EulerAggregationVaultBase {

vm.prank(user1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down Expand Up @@ -313,7 +313,7 @@ contract RebalanceTest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

// decrease allocation points
uint256 newAllocationPoints = 300e18;
Expand All @@ -339,7 +339,7 @@ contract RebalanceTest is EulerAggregationVaultBase {

vm.prank(user1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

// TODO: check this
// assertEq(eulerAggregationVault.totalAllocated(), strategyBefore.allocated - eTSTMaxWithdraw);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/RemoveStrategy.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ contract RemoveStrategyTest is EulerAggregationVaultBase {
vm.prank(user1);
address[] memory strategiesToRebalance = new address[](1);
strategiesToRebalance[0] = address(eTST);
eulerAggregationVault.executeRebalance(strategiesToRebalance);
eulerAggregationVault.rebalance(strategiesToRebalance);

assertEq(eulerAggregationVault.totalAllocated(), expectedStrategyCash);
assertEq(eTST.convertToAssets(eTST.balanceOf(address(eulerAggregationVault))), expectedStrategyCash);
Expand Down

0 comments on commit 4bc449c

Please sign in to comment.