|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; |
| 5 | +import "@openzeppelin-upgrades/contracts/access/AccessControlEnumerableUpgradeable.sol"; |
| 6 | +import "../interfaces/IPausable.sol"; |
| 7 | +import "./ProtocolRegistryStorage.sol"; |
| 8 | + |
| 9 | +contract ProtocolRegistry is Initializable, AccessControlEnumerableUpgradeable, ProtocolRegistryStorage { |
| 10 | + using ShortStringsUpgradeable for *; |
| 11 | + using EnumerableMap for EnumerableMap.UintToAddressMap; |
| 12 | + |
| 13 | + /** |
| 14 | + * |
| 15 | + * INITIALIZING FUNCTIONS |
| 16 | + * |
| 17 | + */ |
| 18 | + constructor() ProtocolRegistryStorage() { |
| 19 | + _disableInitializers(); |
| 20 | + } |
| 21 | + |
| 22 | + /// @inheritdoc IProtocolRegistry |
| 23 | + function initialize(address initialAdmin, address pauserMultisig) external initializer { |
| 24 | + _grantRole(DEFAULT_ADMIN_ROLE, initialAdmin); |
| 25 | + _grantRole(PAUSER_ROLE, pauserMultisig); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * |
| 30 | + * INITIALIZING FUNCTIONS |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | + /// @inheritdoc IProtocolRegistry |
| 35 | + function ship( |
| 36 | + address[] calldata addresses, |
| 37 | + DeploymentConfig[] calldata configs, |
| 38 | + string[] calldata names, |
| 39 | + string calldata semanticVersion |
| 40 | + ) external onlyRole(DEFAULT_ADMIN_ROLE) { |
| 41 | + // Update the semantic version. |
| 42 | + _updateSemanticVersion(semanticVersion); |
| 43 | + for (uint256 i = 0; i < addresses.length; ++i) { |
| 44 | + // Append each provided |
| 45 | + _appendDeployment(addresses[i], configs[i], names[i]); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// @inheritdoc IProtocolRegistry |
| 50 | + function configure(address addr, DeploymentConfig calldata config) external onlyRole(DEFAULT_ADMIN_ROLE) { |
| 51 | + // Update the config |
| 52 | + _deploymentConfigs[addr] = config; |
| 53 | + // Emit the event. |
| 54 | + emit DeploymentConfigured(addr, config); |
| 55 | + } |
| 56 | + |
| 57 | + /// @inheritdoc IProtocolRegistry |
| 58 | + function pauseAll() external onlyRole(PAUSER_ROLE) { |
| 59 | + uint256 length = totalDeployments(); |
| 60 | + // Iterate over all stored deployments. |
| 61 | + for (uint256 i = 0; i < length; ++i) { |
| 62 | + (, address addr) = _deployments.at(i); |
| 63 | + DeploymentConfig memory config = _deploymentConfigs[addr]; |
| 64 | + // Only attempt to pause deployments marked as pausable. |
| 65 | + if (config.pausable && !config.deprecated) { |
| 66 | + IPausable(addr).pauseAll(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * |
| 73 | + * HELPER FUNCTIONS |
| 74 | + * |
| 75 | + */ |
| 76 | + |
| 77 | + /// @dev Updates the semantic version of the protocol. |
| 78 | + function _updateSemanticVersion( |
| 79 | + string calldata semanticVersion |
| 80 | + ) internal { |
| 81 | + string memory previousSemanticVersion = _semanticVersion.toString(); |
| 82 | + _semanticVersion = semanticVersion.toShortString(); |
| 83 | + emit SemanticVersionUpdated(previousSemanticVersion, semanticVersion); |
| 84 | + } |
| 85 | + |
| 86 | + /// @dev Appends a deployment. |
| 87 | + function _appendDeployment(address addr, DeploymentConfig calldata config, string calldata name) internal { |
| 88 | + // Store name => address mapping |
| 89 | + _deployments.set({key: _unwrap(name.toShortString()), value: addr}); |
| 90 | + // Store deployment config |
| 91 | + _deploymentConfigs[addr] = config; |
| 92 | + // Emit the events. |
| 93 | + emit DeploymentShipped(addr, config); |
| 94 | + } |
| 95 | + |
| 96 | + /// @dev Unwraps a ShortString to a uint256. |
| 97 | + function _unwrap( |
| 98 | + ShortString shortString |
| 99 | + ) internal pure returns (uint256) { |
| 100 | + return uint256(ShortString.unwrap(shortString)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * |
| 105 | + * VIEW FUNCTIONS |
| 106 | + * |
| 107 | + */ |
| 108 | + |
| 109 | + /// @inheritdoc IProtocolRegistry |
| 110 | + function version() external view virtual returns (string memory) { |
| 111 | + return _semanticVersion.toString(); |
| 112 | + } |
| 113 | + |
| 114 | + /// @inheritdoc IProtocolRegistry |
| 115 | + function majorVersion() external view returns (string memory) { |
| 116 | + bytes memory v = bytes(_semanticVersion.toString()); |
| 117 | + return string(abi.encodePacked(v[0])); |
| 118 | + } |
| 119 | + |
| 120 | + /// @inheritdoc IProtocolRegistry |
| 121 | + function getAddress( |
| 122 | + string calldata name |
| 123 | + ) external view returns (address) { |
| 124 | + return _deployments.get(_unwrap(name.toShortString())); |
| 125 | + } |
| 126 | + |
| 127 | + /// @inheritdoc IProtocolRegistry |
| 128 | + function getDeployment( |
| 129 | + string calldata name |
| 130 | + ) external view returns (address addr, DeploymentConfig memory config) { |
| 131 | + addr = _deployments.get(_unwrap(name.toShortString())); |
| 132 | + config = _deploymentConfigs[addr]; |
| 133 | + return (addr, config); |
| 134 | + } |
| 135 | + |
| 136 | + /// @inheritdoc IProtocolRegistry |
| 137 | + function getAllDeployments() |
| 138 | + external |
| 139 | + view |
| 140 | + returns (string[] memory names, address[] memory addresses, DeploymentConfig[] memory configs) |
| 141 | + { |
| 142 | + uint256 length = totalDeployments(); |
| 143 | + names = new string[](length); |
| 144 | + addresses = new address[](length); |
| 145 | + configs = new DeploymentConfig[](length); |
| 146 | + |
| 147 | + for (uint256 i = 0; i < length; ++i) { |
| 148 | + (uint256 nameShortString, address addr) = _deployments.at(i); |
| 149 | + names[i] = ShortString.wrap(bytes32(nameShortString)).toString(); |
| 150 | + addresses[i] = addr; |
| 151 | + configs[i] = _deploymentConfigs[addr]; |
| 152 | + } |
| 153 | + |
| 154 | + return (names, addresses, configs); |
| 155 | + } |
| 156 | + |
| 157 | + /// @inheritdoc IProtocolRegistry |
| 158 | + function totalDeployments() public view returns (uint256) { |
| 159 | + return _deployments.length(); |
| 160 | + } |
| 161 | +} |
0 commit comments