Skip to content

Commit

Permalink
feat: create new role to update pool weights
Browse files Browse the repository at this point in the history
  • Loading branch information
blockgroot committed Aug 27, 2024
1 parent c2b620a commit 5a2da49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 11 additions & 2 deletions contracts/PoolSelector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ contract PoolSelector is IPoolSelector, AccessControlUpgradeable {

mapping(uint8 => uint256) public poolWeights;

bytes32 public constant POOL_WEIGHTS_OPERATOR = keccak256("POOL_WEIGHTS_OPERATOR");
error CallerNotPoolWeightsOperator();

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
Expand Down Expand Up @@ -108,12 +111,14 @@ contract PoolSelector is IPoolSelector, AccessControlUpgradeable {

/**
* @notice update the target weights of existing pools
* @dev only `Manager` can call,
* @dev only `POOL_WEIGHTS_OPERATOR's` can call,
* @param _poolTargets new target weights of pools
* `_poolTargets` array provide pool target in the same order of poolIDs that are stored in poolIdArray of poolUtils
*/
function updatePoolWeights(uint256[] calldata _poolTargets) external {
UtilLib.onlyManagerRole(msg.sender, staderConfig);
if (!onlyPoolWeightsOperatorRole(msg.sender)) {
revert CallerNotPoolWeightsOperator();
}
uint8[] memory poolIdArray = IPoolUtils(staderConfig.getPoolUtils()).getPoolIdArray();
uint256 poolCount = poolIdArray.length;
uint256 poolTargetLength = _poolTargets.length;
Expand Down Expand Up @@ -145,4 +150,8 @@ contract PoolSelector is IPoolSelector, AccessControlUpgradeable {
staderConfig = IStaderConfig(_staderConfig);
emit UpdatedStaderConfig(_staderConfig);
}

function onlyPoolWeightsOperatorRole(address account) private view returns (bool) {
return hasRole(POOL_WEIGHTS_OPERATOR, account);
}
}
5 changes: 4 additions & 1 deletion test/foundry_tests/PoolSelector.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ contract PoolSelectorTest is Test {
PoolSelector poolSelector;
PoolUtilsMockForDepositFlow poolUtils;

error CallerNotPoolWeightsOperator();

function setUp() public {
vm.clearMockedCalls();
staderAdmin = vm.addr(100);
Expand Down Expand Up @@ -55,6 +57,7 @@ contract PoolSelectorTest is Test {
poolUtils = new PoolUtilsMockForDepositFlow(address(0), address(staderConfig));

vm.startPrank(staderAdmin);
poolSelector.grantRole(poolSelector.POOL_WEIGHTS_OPERATOR(), staderManager);
staderConfig.updatePoolUtils(address(poolUtils));
staderConfig.updateStakePoolManager(staderStakePoolManager);
staderConfig.grantRole(staderConfig.MANAGER(), staderManager);
Expand Down Expand Up @@ -95,7 +98,7 @@ contract PoolSelectorTest is Test {
invalidSizePoolWeight[1] = 4000;
invalidSizePoolWeight[2] = 4000;

vm.expectRevert(UtilLib.CallerNotManager.selector);
vm.expectRevert(CallerNotPoolWeightsOperator.selector);
poolSelector.updatePoolWeights(poolWeight);

vm.startPrank(staderManager);
Expand Down

0 comments on commit 5a2da49

Please sign in to comment.