|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import { AbstractSafeModule } from "./AbstractSafeModule.sol"; |
| 5 | +import { IVault } from "../interfaces/IVault.sol"; |
| 6 | + |
| 7 | +/** |
| 8 | + * @title PermissionedRebaseModule |
| 9 | + * @notice Safe module that lets a permissioned operator drive a `rebase()` |
| 10 | + * on vaults that are kept in the rebase-paused state. For each |
| 11 | + * configured vault, the module calls (in order, via the Safe): |
| 12 | + * `unpauseRebase()` -> `rebase()` -> `pauseRebase()`. |
| 13 | + * If any sub-call fails, the whole transaction reverts so a vault |
| 14 | + * can never be left unpaused by a partial run. |
| 15 | + */ |
| 16 | +contract PermissionedRebaseModule is AbstractSafeModule { |
| 17 | + mapping(address => bool) public isVaultWhitelisted; |
| 18 | + address[] public vaults; |
| 19 | + |
| 20 | + event VaultAdded(address vault); |
| 21 | + event VaultRemoved(address vault); |
| 22 | + event PermissionedRebaseExecuted(address vault); |
| 23 | + |
| 24 | + constructor( |
| 25 | + address _safeAddress, |
| 26 | + address _operator, |
| 27 | + address[] memory _vaults |
| 28 | + ) AbstractSafeModule(_safeAddress) { |
| 29 | + _grantRole(OPERATOR_ROLE, _operator); |
| 30 | + |
| 31 | + for (uint256 i = 0; i < _vaults.length; i++) { |
| 32 | + _addVault(_vaults[i]); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @notice For every whitelisted vault, sequentially call |
| 38 | + * `unpauseRebase()`, `rebase()`, then `pauseRebase()` via the |
| 39 | + * Safe. Reverts atomically on any sub-call failure. |
| 40 | + */ |
| 41 | + function permissionedRebase() external onlyRole(OPERATOR_ROLE) { |
| 42 | + uint256 vaultsLength = vaults.length; |
| 43 | + for (uint256 i = 0; i < vaultsLength; i++) { |
| 44 | + address vault = vaults[i]; |
| 45 | + _execOnVault(vault, IVault.unpauseRebase.selector); |
| 46 | + _execOnVault(vault, IVault.rebase.selector); |
| 47 | + _execOnVault(vault, IVault.pauseRebase.selector); |
| 48 | + emit PermissionedRebaseExecuted(vault); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + function _execOnVault(address vault, bytes4 selector) internal { |
| 53 | + bool success = safeContract.execTransactionFromModule( |
| 54 | + vault, |
| 55 | + 0, // Value |
| 56 | + abi.encodeWithSelector(selector), |
| 57 | + 0 // Call |
| 58 | + ); |
| 59 | + require(success, "Vault call failed"); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @notice Add a vault to the whitelist. Only the Safe can call. |
| 64 | + */ |
| 65 | + function addVault(address _vault) external onlyRole(DEFAULT_ADMIN_ROLE) { |
| 66 | + _addVault(_vault); |
| 67 | + } |
| 68 | + |
| 69 | + function _addVault(address _vault) internal { |
| 70 | + require(_vault != address(0), "Vault is zero address"); |
| 71 | + require(!isVaultWhitelisted[_vault], "Vault already whitelisted"); |
| 72 | + isVaultWhitelisted[_vault] = true; |
| 73 | + vaults.push(_vault); |
| 74 | + emit VaultAdded(_vault); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @notice Remove a vault from the whitelist. Only the Safe can call. |
| 79 | + */ |
| 80 | + function removeVault(address _vault) external onlyRole(DEFAULT_ADMIN_ROLE) { |
| 81 | + require(isVaultWhitelisted[_vault], "Vault not whitelisted"); |
| 82 | + isVaultWhitelisted[_vault] = false; |
| 83 | + |
| 84 | + for (uint256 i = 0; i < vaults.length; i++) { |
| 85 | + if (vaults[i] == _vault) { |
| 86 | + vaults[i] = vaults[vaults.length - 1]; |
| 87 | + vaults.pop(); |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + emit VaultRemoved(_vault); |
| 93 | + } |
| 94 | +} |
0 commit comments