Skip to content

Commit

Permalink
Merge pull request #7 from euler-xyz/fix/adjustAllocationPoints
Browse files Browse the repository at this point in the history
Fix adjustAllocationPoints() and test allocation state management
  • Loading branch information
haythemsellami authored May 14, 2024
2 parents ddbc1f4 + c46e749 commit 09758f3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/FourSixTwoSixAgg.sol
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 +408,18 @@ contract FourSixTwoSixAgg is EVCUtil, ERC4626, AccessControlEnumerable {
nonReentrant
onlyRole(ALLOCATION_ADJUSTER_ROLE)
{
Strategy memory strategyData = strategies[strategy];
Strategy memory strategyDataCache = strategies[strategy];
uint256 totalAllocationPointsCache = totalAllocationPoints;

if (strategyData.active = false) {
if (!strategyDataCache.active) {
revert InactiveStrategy();
}

strategies[strategy].allocationPoints = uint120(newPoints);
if (newPoints > strategyData.allocationPoints) {
uint256 diff = newPoints - strategyData.allocationPoints;
totalAllocationPoints + totalAllocationPointsCache + diff;
} else if (newPoints < strategyData.allocationPoints) {
uint256 diff = strategyData.allocationPoints - newPoints;
totalAllocationPoints = totalAllocationPointsCache - diff;
}

totalAllocationPoints = (newPoints > strategyDataCache.allocationPoints)
? totalAllocationPointsCache + (newPoints - strategyDataCache.allocationPoints)
: totalAllocationPointsCache - (strategyDataCache.allocationPoints - newPoints);
}

/// @notice Swap two strategies indexes in the withdrawal queue.
Expand Down
40 changes: 40 additions & 0 deletions test/fuzz/AdjustAllocationPointsFuzzTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import {FourSixTwoSixAggBase, FourSixTwoSixAgg} from "../common/FourSixTwoSixAggBase.t.sol";

contract AdjustAllocationsPointsFuzzTest is FourSixTwoSixAggBase {
function setUp() public virtual override {
super.setUp();

uint256 initialStrategyAllocationPoints = 500e18;
_addStrategy(manager, address(eTST), initialStrategyAllocationPoints);
}

function testFuzzAdjustAllocationPoints(uint256 _newAllocationPoints) public {
_newAllocationPoints = bound(_newAllocationPoints, 0, type(uint120).max);

uint256 strategyAllocationPoints = (fourSixTwoSixAgg.getStrategy(address(eTST))).allocationPoints;
uint256 totalAllocationPointsBefore = fourSixTwoSixAgg.totalAllocationPoints();
uint256 withdrawalQueueLengthBefore = fourSixTwoSixAgg.withdrawalQueueLength();

vm.prank(manager);
fourSixTwoSixAgg.adjustAllocationPoints(address(eTST), _newAllocationPoints);

FourSixTwoSixAgg.Strategy memory strategy = fourSixTwoSixAgg.getStrategy(address(eTST));

if (_newAllocationPoints < strategyAllocationPoints) {
assertEq(
fourSixTwoSixAgg.totalAllocationPoints(),
totalAllocationPointsBefore - (strategyAllocationPoints - _newAllocationPoints)
);
} else {
assertEq(
fourSixTwoSixAgg.totalAllocationPoints(),
totalAllocationPointsBefore + (_newAllocationPoints - strategyAllocationPoints)
);
}
assertEq(fourSixTwoSixAgg.withdrawalQueueLength(), withdrawalQueueLengthBefore);
assertEq(strategy.allocationPoints, _newAllocationPoints);
}
}
50 changes: 50 additions & 0 deletions test/unit/AdjustAllocationPointsTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import {FourSixTwoSixAggBase, FourSixTwoSixAgg} from "../common/FourSixTwoSixAggBase.t.sol";

contract AdjustAllocationsPointsTest is FourSixTwoSixAggBase {
uint256 initialStrategyAllocationPoints = 500e18;

function setUp() public virtual override {
super.setUp();

_addStrategy(manager, address(eTST), initialStrategyAllocationPoints);
}

function testAdjustAllocationPoints() public {
uint256 newAllocationPoints = 859e18;
uint256 totalAllocationPointsBefore = fourSixTwoSixAgg.totalAllocationPoints();
uint256 withdrawalQueueLengthBefore = fourSixTwoSixAgg.withdrawalQueueLength();

vm.prank(manager);
fourSixTwoSixAgg.adjustAllocationPoints(address(eTST), newAllocationPoints);

FourSixTwoSixAgg.Strategy memory strategy = fourSixTwoSixAgg.getStrategy(address(eTST));

assertEq(
fourSixTwoSixAgg.totalAllocationPoints(),
totalAllocationPointsBefore + (newAllocationPoints - initialStrategyAllocationPoints)
);
assertEq(fourSixTwoSixAgg.withdrawalQueueLength(), withdrawalQueueLengthBefore);
assertEq(strategy.allocationPoints, newAllocationPoints);
}

function testAdjustAllocationPoints_FromUnauthorizedAddress() public {
uint256 newAllocationPoints = 859e18;

vm.startPrank(deployer);
vm.expectRevert();
fourSixTwoSixAgg.adjustAllocationPoints(address(eTST), newAllocationPoints);
vm.stopPrank();
}

function testAdjustAllocationPoints_InactiveStrategy() public {
uint256 newAllocationPoints = 859e18;

vm.startPrank(manager);
vm.expectRevert(FourSixTwoSixAgg.InactiveStrategy.selector);
fourSixTwoSixAgg.adjustAllocationPoints(address(eTST2), newAllocationPoints);
vm.stopPrank();
}
}

0 comments on commit 09758f3

Please sign in to comment.