|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +// Interface |
| 5 | +import "./interface/ITWRouter.sol"; |
| 6 | +import "./interface/IExtensionRegistry.sol"; |
| 7 | + |
| 8 | +// Extensions & libraries |
| 9 | +import "../extension/Multicall.sol"; |
| 10 | + |
| 11 | +// Extension pattern imports |
| 12 | +import "lib/dynamic-contracts/src/presets/utils/StringSet.sol"; |
| 13 | +import "lib/dynamic-contracts/src/core/Router.sol"; |
| 14 | +import "lib/dynamic-contracts/src/presets/utils/DefaultExtensionSet.sol"; |
| 15 | +import "lib/dynamic-contracts/src/presets/utils/ExtensionState.sol"; |
| 16 | + |
| 17 | +abstract contract TWRouter is ITWRouter, Multicall, ExtensionState, Router { |
| 18 | + using StringSet for StringSet.Set; |
| 19 | + |
| 20 | + /*/////////////////////////////////////////////////////////////// |
| 21 | + State variables |
| 22 | + //////////////////////////////////////////////////////////////*/ |
| 23 | + |
| 24 | + /// @notice The DefaultExtensionSet that stores default extensions of the router. |
| 25 | + address public immutable defaultExtensionSet; |
| 26 | + |
| 27 | + /// @notice The ExtensionRegistry that stores all latest, vetted extensions available to router. |
| 28 | + address public immutable extensionRegistry; |
| 29 | + |
| 30 | + /*/////////////////////////////////////////////////////////////// |
| 31 | + Constructor |
| 32 | + //////////////////////////////////////////////////////////////*/ |
| 33 | + |
| 34 | + constructor(address _extensionRegistry, string[] memory _extensionNames) { |
| 35 | + extensionRegistry = _extensionRegistry; |
| 36 | + |
| 37 | + DefaultExtensionSet map = new DefaultExtensionSet(); |
| 38 | + defaultExtensionSet = address(map); |
| 39 | + |
| 40 | + uint256 len = _extensionNames.length; |
| 41 | + |
| 42 | + for (uint256 i = 0; i < len; i += 1) { |
| 43 | + Extension memory extension = IExtensionRegistry(_extensionRegistry).getExtension(_extensionNames[i]); |
| 44 | + map.setExtension(extension); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /*/////////////////////////////////////////////////////////////// |
| 49 | + External functions |
| 50 | + //////////////////////////////////////////////////////////////*/ |
| 51 | + |
| 52 | + /// @dev Adds a new extension to the router. |
| 53 | + function addExtension(string memory _extensionName) external { |
| 54 | + require(_canSetExtension(), "TWRouter: caller not authorized"); |
| 55 | + |
| 56 | + Extension memory extension = IExtensionRegistry(extensionRegistry).getExtension(_extensionName); |
| 57 | + |
| 58 | + _addExtension(extension); |
| 59 | + } |
| 60 | + |
| 61 | + /// @dev Updates an existing extension in the router, or overrides a default extension. |
| 62 | + function updateExtension(string memory _extensionName) external { |
| 63 | + require(_canSetExtension(), "TWRouter: caller not authorized"); |
| 64 | + |
| 65 | + Extension memory extension = IExtensionRegistry(extensionRegistry).getExtension(_extensionName); |
| 66 | + |
| 67 | + _updateExtension(extension); |
| 68 | + } |
| 69 | + |
| 70 | + /// @dev Removes an existing extension from the router. |
| 71 | + function removeExtension(string memory _extensionName) external { |
| 72 | + require(_canSetExtension(), "TWRouter: caller not authorized"); |
| 73 | + |
| 74 | + _removeExtension(_extensionName); |
| 75 | + } |
| 76 | + |
| 77 | + /*/////////////////////////////////////////////////////////////// |
| 78 | + View functions |
| 79 | + //////////////////////////////////////////////////////////////*/ |
| 80 | + |
| 81 | + /** |
| 82 | + * @notice Returns all extensions stored. Override default lugins stored in router are |
| 83 | + * given precedence over default extensions in DefaultExtensionSet. |
| 84 | + */ |
| 85 | + function getAllExtensions() external view returns (Extension[] memory allExtensions) { |
| 86 | + Extension[] memory mapExtensions = IDefaultExtensionSet(defaultExtensionSet).getAllExtensions(); |
| 87 | + uint256 mapExtensionsLen = mapExtensions.length; |
| 88 | + |
| 89 | + ExtensionStateStorage.Data storage data = ExtensionStateStorage.extensionStateStorage(); |
| 90 | + string[] memory names = data.extensionNames.values(); |
| 91 | + uint256 namesLen = names.length; |
| 92 | + |
| 93 | + uint256 overrides = 0; |
| 94 | + for (uint256 i = 0; i < mapExtensionsLen; i += 1) { |
| 95 | + if (data.extensionNames.contains(mapExtensions[i].metadata.name)) { |
| 96 | + overrides += 1; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + uint256 total = (namesLen + mapExtensionsLen) - overrides; |
| 101 | + |
| 102 | + allExtensions = new Extension[](total); |
| 103 | + uint256 idx = 0; |
| 104 | + |
| 105 | + for (uint256 i = 0; i < mapExtensionsLen; i += 1) { |
| 106 | + string memory name = mapExtensions[i].metadata.name; |
| 107 | + if (!data.extensionNames.contains(name)) { |
| 108 | + allExtensions[idx] = mapExtensions[i]; |
| 109 | + idx += 1; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + for (uint256 i = 0; i < namesLen; i += 1) { |
| 114 | + allExtensions[idx] = data.extensions[names[i]]; |
| 115 | + idx += 1; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /// @dev Returns the extension metadata and functions for a given extension. |
| 120 | + function getExtension(string memory _extensionName) public view returns (Extension memory) { |
| 121 | + ExtensionStateStorage.Data storage data = ExtensionStateStorage.extensionStateStorage(); |
| 122 | + bool isLocalExtension = data.extensionNames.contains(_extensionName); |
| 123 | + |
| 124 | + return |
| 125 | + isLocalExtension |
| 126 | + ? data.extensions[_extensionName] |
| 127 | + : IDefaultExtensionSet(defaultExtensionSet).getExtension(_extensionName); |
| 128 | + } |
| 129 | + |
| 130 | + /// @dev Returns the extension's implementation smart contract address. |
| 131 | + function getExtensionImplementation(string memory _extensionName) external view returns (address) { |
| 132 | + return getExtension(_extensionName).metadata.implementation; |
| 133 | + } |
| 134 | + |
| 135 | + /// @dev Returns all functions that belong to the given extension contract. |
| 136 | + function getAllFunctionsOfExtension(string memory _extensionName) |
| 137 | + external |
| 138 | + view |
| 139 | + returns (ExtensionFunction[] memory) |
| 140 | + { |
| 141 | + return getExtension(_extensionName).functions; |
| 142 | + } |
| 143 | + |
| 144 | + /// @dev Returns the Extension metadata for a given function. |
| 145 | + function getExtensionForFunction(bytes4 _functionSelector) public view returns (ExtensionMetadata memory) { |
| 146 | + ExtensionStateStorage.Data storage data = ExtensionStateStorage.extensionStateStorage(); |
| 147 | + ExtensionMetadata memory metadata = data.extensionMetadata[_functionSelector]; |
| 148 | + |
| 149 | + bool isLocalExtension = metadata.implementation != address(0); |
| 150 | + |
| 151 | + return |
| 152 | + isLocalExtension |
| 153 | + ? metadata |
| 154 | + : IDefaultExtensionSet(defaultExtensionSet).getExtensionForFunction(_functionSelector); |
| 155 | + } |
| 156 | + |
| 157 | + /// @dev Returns the extension implementation address stored in router, for the given function. |
| 158 | + function getImplementationForFunction(bytes4 _functionSelector) |
| 159 | + public |
| 160 | + view |
| 161 | + override |
| 162 | + returns (address extensionAddress) |
| 163 | + { |
| 164 | + return getExtensionForFunction(_functionSelector).implementation; |
| 165 | + } |
| 166 | + |
| 167 | + /*/////////////////////////////////////////////////////////////// |
| 168 | + Internal functions |
| 169 | + //////////////////////////////////////////////////////////////*/ |
| 170 | + |
| 171 | + /// @dev Returns whether a extension can be set in the given execution context. |
| 172 | + function _canSetExtension() internal view virtual returns (bool); |
| 173 | +} |
0 commit comments