|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import {Controllable, IControllable} from "./base/Controllable.sol"; |
| 5 | +import {IMetaVaultFactory} from "../interfaces/IMetaVaultFactory.sol"; |
| 6 | +import {IMetaProxy} from "../interfaces/IMetaProxy.sol"; |
| 7 | +import {MetaVaultProxy} from "./proxy/MetaVaultProxy.sol"; |
| 8 | +import {IMetaVault, EnumerableSet} from "../interfaces/IMetaVault.sol"; |
| 9 | +import {WrappedMetaVaultProxy} from "./proxy/WrappedMetaVaultProxy.sol"; |
| 10 | +import {IWrappedMetaVault} from "../interfaces/IWrappedMetaVault.sol"; |
| 11 | + |
| 12 | +/// @title Factory of MetaVaults and WrappedMetaVaults |
| 13 | +/// @author Alien Deployer (https://github.com/a17) |
| 14 | +contract MetaVaultFactory is Controllable, IMetaVaultFactory { |
| 15 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 16 | + |
| 17 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 18 | + /* CONSTANTS */ |
| 19 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 20 | + |
| 21 | + /// @inheritdoc IControllable |
| 22 | + string public constant VERSION = "1.0.0"; |
| 23 | + |
| 24 | + // keccak256(abi.encode(uint256(keccak256("erc7201:stability.MetaVaultFactory")) - 1)) & ~bytes32(uint256(0xff)); |
| 25 | + bytes32 private constant METAVAULTFACTORY_STORAGE_LOCATION = |
| 26 | + 0x58b476403d8ac8a4d0530fd874c3ac691dfe1c48aec83d57fe82186c80386c00; |
| 27 | + |
| 28 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 29 | + /* INITIALIZATION */ |
| 30 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 31 | + |
| 32 | + /// @inheritdoc IMetaVaultFactory |
| 33 | + function initialize(address platform_) public initializer { |
| 34 | + __Controllable_init(platform_); |
| 35 | + } |
| 36 | + |
| 37 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 38 | + /* RESTRICTED ACTIONS */ |
| 39 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 40 | + |
| 41 | + /// @inheritdoc IMetaVaultFactory |
| 42 | + function setMetaVaultImplementation(address newImplementation) external onlyGovernanceOrMultisig { |
| 43 | + MetaVaultFactoryStorage storage $ = _getStorage(); |
| 44 | + $.metaVaultImplementation = newImplementation; |
| 45 | + emit NewMetaVaultImplementation(newImplementation); |
| 46 | + } |
| 47 | + |
| 48 | + /// @inheritdoc IMetaVaultFactory |
| 49 | + function setWrappedMetaVaultImplementation(address newImplementation) external onlyGovernanceOrMultisig { |
| 50 | + MetaVaultFactoryStorage storage $ = _getStorage(); |
| 51 | + $.wrappedMetaVaultImplementation = newImplementation; |
| 52 | + emit NewWrappedMetaVaultImplementation(newImplementation); |
| 53 | + } |
| 54 | + |
| 55 | + /// @inheritdoc IMetaVaultFactory |
| 56 | + function deployMetaVault( |
| 57 | + bytes32 salt, |
| 58 | + string memory type_, |
| 59 | + address pegAsset_, |
| 60 | + string memory name_, |
| 61 | + string memory symbol_, |
| 62 | + address[] memory vaults_, |
| 63 | + uint[] memory proportions_ |
| 64 | + ) external onlyOperator returns (address proxy) { |
| 65 | + proxy = address(new MetaVaultProxy{salt: salt}()); |
| 66 | + IMetaProxy(proxy).initProxy(); |
| 67 | + IMetaVault(proxy).initialize(platform(), type_, pegAsset_, name_, symbol_, vaults_, proportions_); |
| 68 | + |
| 69 | + MetaVaultFactoryStorage storage $ = _getStorage(); |
| 70 | + $.metaVaults.add(proxy); |
| 71 | + |
| 72 | + emit NewMetaVault(proxy, type_, pegAsset_, name_, symbol_, vaults_, proportions_); |
| 73 | + } |
| 74 | + |
| 75 | + /// @inheritdoc IMetaVaultFactory |
| 76 | + function deployWrapper(bytes32 salt, address metaVault) external onlyOperator returns (address proxy) { |
| 77 | + proxy = address(new WrappedMetaVaultProxy{salt: salt}()); |
| 78 | + IMetaProxy(proxy).initProxy(); |
| 79 | + IWrappedMetaVault(proxy).initialize(platform(), metaVault); |
| 80 | + |
| 81 | + MetaVaultFactoryStorage storage $ = _getStorage(); |
| 82 | + require($.wrapper[metaVault] == address(0), AlreadyExist()); |
| 83 | + $.wrapper[metaVault] = proxy; |
| 84 | + |
| 85 | + emit NewWrappedMetaVault(proxy, metaVault); |
| 86 | + } |
| 87 | + |
| 88 | + /// @inheritdoc IMetaVaultFactory |
| 89 | + function upgradeMetaProxies(address[] memory metaProxies) external onlyOperator { |
| 90 | + uint len = metaProxies.length; |
| 91 | + for (uint i; i < len; ++i) { |
| 92 | + IMetaProxy(metaProxies[i]).upgrade(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 97 | + /* VIEW FUNCTIONS */ |
| 98 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 99 | + |
| 100 | + /// @inheritdoc IMetaVaultFactory |
| 101 | + function metaVaultImplementation() external view returns (address) { |
| 102 | + return _getStorage().metaVaultImplementation; |
| 103 | + } |
| 104 | + |
| 105 | + /// @inheritdoc IMetaVaultFactory |
| 106 | + function wrappedMetaVaultImplementation() external view returns (address) { |
| 107 | + return _getStorage().wrappedMetaVaultImplementation; |
| 108 | + } |
| 109 | + |
| 110 | + /// @inheritdoc IMetaVaultFactory |
| 111 | + function getMetaVaultProxyInitCodeHash() external pure returns (bytes32) { |
| 112 | + return keccak256(abi.encodePacked(type(MetaVaultProxy).creationCode)); |
| 113 | + } |
| 114 | + |
| 115 | + /// @inheritdoc IMetaVaultFactory |
| 116 | + function getWrappedMetaVaultProxyInitCodeHash() external pure returns (bytes32) { |
| 117 | + return keccak256(abi.encodePacked(type(WrappedMetaVaultProxy).creationCode)); |
| 118 | + } |
| 119 | + |
| 120 | + /// @inheritdoc IMetaVaultFactory |
| 121 | + function getCreate2Address( |
| 122 | + bytes32 salt, |
| 123 | + bytes32 initCodeHash, |
| 124 | + address thisAddress |
| 125 | + ) external pure returns (address) { |
| 126 | + return address(uint160(uint(keccak256(abi.encodePacked(bytes1(0xff), thisAddress, salt, initCodeHash))))); |
| 127 | + } |
| 128 | + |
| 129 | + /// @inheritdoc IMetaVaultFactory |
| 130 | + function metaVaults() external view returns (address[] memory) { |
| 131 | + return _getStorage().metaVaults.values(); |
| 132 | + } |
| 133 | + |
| 134 | + /// @inheritdoc IMetaVaultFactory |
| 135 | + function wrapper(address metaVault) external view returns (address) { |
| 136 | + return _getStorage().wrapper[metaVault]; |
| 137 | + } |
| 138 | + |
| 139 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 140 | + /* INTERNAL LOGIC */ |
| 141 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 142 | + |
| 143 | + function _getStorage() private pure returns (MetaVaultFactoryStorage storage $) { |
| 144 | + //slither-disable-next-line assembly |
| 145 | + assembly { |
| 146 | + $.slot := METAVAULTFACTORY_STORAGE_LOCATION |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments