Skip to content

Commit

Permalink
chore: update roles and name (#36)
Browse files Browse the repository at this point in the history
* chore: update roles and name

* feat: remove strategy

* feat: role setter
  • Loading branch information
Schlagonia authored Feb 12, 2024
1 parent 46ac9e5 commit b5f0610
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 70 deletions.
78 changes: 41 additions & 37 deletions contracts/Managers/RoleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ contract RoleManager is Governance2Step {
roles: uint96(
Roles.REPORTING_MANAGER |
Roles.DEBT_MANAGER |
Roles.QUEUE_MANAGER
Roles.QUEUE_MANAGER |
Roles.DEBT_PURCHASER
)
});

Expand All @@ -162,7 +163,7 @@ contract RoleManager is Governance2Step {
// The keeper can process reports and update debt.
_positions[KEEPER] = Position({
holder: _keeper,
roles: uint96(Roles.REPORTING_MANAGER | Roles.DEBT_MANAGER)
roles: uint96(Roles.REPORTING_MANAGER)
});

// Set just the roles for a debt allocator.
Expand Down Expand Up @@ -255,9 +256,14 @@ contract RoleManager is Governance2Step {

// Create the name and symbol to be standardized based on rating.
string memory ratingString = ratingToString[_rating];
// Name is "{SYMBOL} yVault-{RATING}"
// Name is "{SYMBOL}-{RATING} yVault"
string memory _name = string(
abi.encodePacked(ERC20(_asset).symbol(), " yVault-", ratingString)
abi.encodePacked(
ERC20(_asset).symbol(),
"-",
ratingString,
" yVault"
)
);
// Symbol is "yv{SYMBOL}-{RATING}".
string memory _symbol = string(
Expand Down Expand Up @@ -345,46 +351,44 @@ contract RoleManager is Governance2Step {
address _vault,
address _debtAllocator
) internal virtual {
// Cache positionInfo to be reused for each setter.
Position memory positionInfo = _positions[DADDY];

// Set the roles for daddy.
IVault(_vault).set_role(
positionInfo.holder,
uint256(positionInfo.roles)
);
_setRole(_vault, _positions[DADDY]);

// Set the roles for Brain.
positionInfo = _positions[BRAIN];
IVault(_vault).set_role(
positionInfo.holder,
uint256(positionInfo.roles)
);
_setRole(_vault, _positions[BRAIN]);

// Set the roles for Security.
positionInfo = _positions[SECURITY];
IVault(_vault).set_role(
positionInfo.holder,
uint256(positionInfo.roles)
);
_setRole(_vault, _positions[SECURITY]);

// Set the roles for the Keeper.
positionInfo = _positions[KEEPER];
IVault(_vault).set_role(
positionInfo.holder,
uint256(positionInfo.roles)
);
_setRole(_vault, _positions[KEEPER]);

// Set the roles for the Strategy Manager.
positionInfo = _positions[STRATEGY_MANAGER];
IVault(_vault).set_role(
positionInfo.holder,
uint256(positionInfo.roles)
);
_setRole(_vault, _positions[STRATEGY_MANAGER]);

// Give the specific debt allocator its roles.
positionInfo = _positions[DEBT_ALLOCATOR];
IVault(_vault).set_role(_debtAllocator, uint256(positionInfo.roles));
_setRole(
_vault,
Position(_debtAllocator, _positions[DEBT_ALLOCATOR].roles)
);
}

/**
* @dev Used internally to set the roles on a vault for a given position.
* Will not set the roles if the position holder is address(0).
* This does not check that the roles are !=0 because it is expected that
* the holder will be set to 0 if the position is not being used.
*
* @param _vault Address of the vault.
* @param _position Holder address and roles to set.
*/
function _setRole(
address _vault,
Position memory _position
) internal virtual {
if (_position.holder != address(0)) {
IVault(_vault).set_role(_position.holder, uint256(_position.roles));
}
}

/**
Expand Down Expand Up @@ -537,12 +541,12 @@ contract RoleManager is Governance2Step {
require(vaultConfig[_vault].asset != address(0), "vault not added");

// Remove the roles from the old allocator.
IVault(_vault).set_role(vaultConfig[_vault].debtAllocator, 0);
_setRole(_vault, Position(vaultConfig[_vault].debtAllocator, 0));

// Give the new debt allocator the relevant roles.
IVault(_vault).set_role(
_debtAllocator,
getPositionRoles(DEBT_ALLOCATOR)
_setRole(
_vault,
Position(_debtAllocator, _positions[DEBT_ALLOCATOR].roles)
);

// Update the vaults config.
Expand Down
59 changes: 52 additions & 7 deletions contracts/debtAllocators/DebtAllocator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ contract DebtAllocator {
uint256 newTotalDebtRatio
);

/// @notice An event emitted when a strategy is added or removed.
event StrategyChanged(address indexed strategy, Status status);

/// @notice An event emitted when the minimum time to wait is updated.
event UpdateMinimumWait(uint256 newMinimumWait);

Expand All @@ -49,20 +52,29 @@ contract DebtAllocator {
/// @notice An event emitted when the max debt update loss is updated.
event UpdateMaxDebtUpdateLoss(uint256 newMaxDebtUpdateLoss);

/// @notice Status when a strategy is added or removed from the allocator.
enum Status {
NULL,
ADDED,
REMOVED
}

/// @notice Struct for each strategies info.
struct Config {
// Flag to set when a strategy is added.
bool added;
// The ideal percent in Basis Points the strategy should have.
uint16 targetRatio;
// The max percent of assets the strategy should hold.
uint16 maxRatio;
// Timestamp of the last time debt was updated.
// The debt updates must be done through this allocator
// for this to be used.
uint128 lastUpdate;
// We have an extra 96 bits in the slot.
uint96 lastUpdate;
// We have an extra 120 bits in the slot.
// So we declare the variable in the struct so it can be
// used if this contract is inherited.
uint96 open;
uint120 open;
}

/// @notice Make sure the caller is governance.
Expand Down Expand Up @@ -200,7 +212,7 @@ contract DebtAllocator {
}

// Update the last time the strategies debt was updated.
_configs[_strategy].lastUpdate = uint128(block.timestamp);
_configs[_strategy].lastUpdate = uint96(block.timestamp);
}

/**
Expand All @@ -215,6 +227,12 @@ contract DebtAllocator {
function shouldUpdateDebt(
address _strategy
) public view virtual returns (bool, bytes memory) {
// Get the strategy specific debt config.
Config memory config = getConfig(_strategy);

// Make sure the strategy has been added to the allocator.
if (!config.added) return (false, bytes("!added"));

// Check the base fee isn't too high.
if (!DebtAllocatorFactory(factory).isCurrentBaseFeeAcceptable()) {
return (false, bytes("Base Fee"));
Expand All @@ -227,9 +245,6 @@ contract DebtAllocator {
// Make sure its an active strategy.
require(params.activation != 0, "!active");

// Get the strategy specific debt config.
Config memory config = getConfig(_strategy);

if (block.timestamp - config.lastUpdate <= minimumWait) {
return (false, bytes("min wait"));
}
Expand Down Expand Up @@ -395,6 +410,12 @@ contract DebtAllocator {
// Get the current config.
Config memory config = getConfig(_strategy);

// Set added flag if not set yet.
if (!config.added) {
config.added = true;
emit StrategyChanged(_strategy, Status.ADDED);
}

// Get what will be the new total debt ratio.
uint256 newTotalDebtRatio = totalDebtRatio -
config.targetRatio +
Expand All @@ -419,6 +440,30 @@ contract DebtAllocator {
);
}

/**
* @notice Remove a strategy from this debt allocator.
* @dev Will delete the full config for the strategy
* @param _strategy Address of the address ro remove.
*/
function removeStrategy(address _strategy) external virtual onlyManagers {
Config memory config = getConfig(_strategy);
require(config.added, "!added");

uint256 target = config.targetRatio;

// Remove any debt ratio the strategy holds.
if (target != 0) {
totalDebtRatio -= target;
emit UpdateStrategyDebtRatio(_strategy, 0, 0, totalDebtRatio);
}

// Remove the full config including the `added` flag.
delete _configs[_strategy];

// Emit Event.
emit StrategyChanged(_strategy, Status.REMOVED);
}

/**
* @notice Set the minimum change variable for a strategy.
* @dev This is the minimum amount of debt to be
Expand Down
Loading

0 comments on commit b5f0610

Please sign in to comment.