|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.16; |
| 3 | + |
| 4 | +import '../library/UtilLib.sol'; |
| 5 | +import '../VaultProxy.sol'; |
| 6 | +import '../interfaces/IVaultFactory.sol'; |
| 7 | +import '../interfaces/IStaderConfig.sol'; |
| 8 | + |
| 9 | +import '@openzeppelin/contracts-upgradeable/proxy/ClonesUpgradeable.sol'; |
| 10 | +import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol'; |
| 11 | + |
| 12 | +contract VaultFactory is IVaultFactory, AccessControlUpgradeable { |
| 13 | + IStaderConfig public staderConfig; |
| 14 | + address public vaultProxyImplementation; |
| 15 | + |
| 16 | + bytes32 public constant override NODE_REGISTRY_CONTRACT = keccak256('NODE_REGISTRY_CONTRACT'); |
| 17 | + |
| 18 | + /// @custom:oz-upgrades-unsafe-allow constructor |
| 19 | + constructor() { |
| 20 | + _disableInitializers(); |
| 21 | + } |
| 22 | + |
| 23 | + function initialize(address _admin, address _staderConfig) external initializer { |
| 24 | + UtilLib.checkNonZeroAddress(_admin); |
| 25 | + UtilLib.checkNonZeroAddress(_staderConfig); |
| 26 | + __AccessControl_init_unchained(); |
| 27 | + |
| 28 | + staderConfig = IStaderConfig(_staderConfig); |
| 29 | + vaultProxyImplementation = address(new VaultProxy()); |
| 30 | + |
| 31 | + _grantRole(DEFAULT_ADMIN_ROLE, _admin); |
| 32 | + } |
| 33 | + |
| 34 | + function deployWithdrawVault( |
| 35 | + uint8 _poolId, |
| 36 | + uint256 _operatorId, |
| 37 | + uint256 _validatorCount, |
| 38 | + uint256 _validatorId |
| 39 | + ) external override onlyRole(NODE_REGISTRY_CONTRACT) returns (address) { |
| 40 | + bytes32 salt = sha256(abi.encode(_poolId, _operatorId, _validatorCount)); |
| 41 | + address withdrawVaultAddress = ClonesUpgradeable.cloneDeterministic(vaultProxyImplementation, salt); |
| 42 | + VaultProxy(payable(withdrawVaultAddress)).initialise(true, _poolId, _validatorId, address(staderConfig)); |
| 43 | + |
| 44 | + emit WithdrawVaultCreated(withdrawVaultAddress); |
| 45 | + return withdrawVaultAddress; |
| 46 | + } |
| 47 | + |
| 48 | + function deployNodeELRewardVault(uint8 _poolId, uint256 _operatorId) |
| 49 | + external |
| 50 | + override |
| 51 | + onlyRole(NODE_REGISTRY_CONTRACT) |
| 52 | + returns (address) |
| 53 | + { |
| 54 | + bytes32 salt = sha256(abi.encode(_poolId, _operatorId)); |
| 55 | + address nodeELRewardVaultAddress = ClonesUpgradeable.cloneDeterministic(vaultProxyImplementation, salt); |
| 56 | + VaultProxy(payable(nodeELRewardVaultAddress)).initialise(false, _poolId, _operatorId, address(staderConfig)); |
| 57 | + |
| 58 | + emit NodeELRewardVaultCreated(nodeELRewardVaultAddress); |
| 59 | + return nodeELRewardVaultAddress; |
| 60 | + } |
| 61 | + |
| 62 | + function computeWithdrawVaultAddress( |
| 63 | + uint8 _poolId, |
| 64 | + uint256 _operatorId, |
| 65 | + uint256 _validatorCount |
| 66 | + ) external view override returns (address) { |
| 67 | + bytes32 salt = sha256(abi.encode(_poolId, _operatorId, _validatorCount)); |
| 68 | + return ClonesUpgradeable.predictDeterministicAddress(vaultProxyImplementation, salt); |
| 69 | + } |
| 70 | + |
| 71 | + function computeNodeELRewardVaultAddress(uint8 _poolId, uint256 _operatorId) |
| 72 | + external |
| 73 | + view |
| 74 | + override |
| 75 | + returns (address) |
| 76 | + { |
| 77 | + bytes32 salt = sha256(abi.encode(_poolId, _operatorId)); |
| 78 | + return ClonesUpgradeable.predictDeterministicAddress(vaultProxyImplementation, salt); |
| 79 | + } |
| 80 | + |
| 81 | + function getValidatorWithdrawCredential(address _withdrawVault) external pure override returns (bytes memory) { |
| 82 | + return abi.encodePacked(bytes1(0x01), bytes11(0x0), address(_withdrawVault)); |
| 83 | + } |
| 84 | + |
| 85 | + //update the address of staderConfig |
| 86 | + function updateStaderConfig(address _staderConfig) external override onlyRole(DEFAULT_ADMIN_ROLE) { |
| 87 | + UtilLib.checkNonZeroAddress(_staderConfig); |
| 88 | + staderConfig = IStaderConfig(_staderConfig); |
| 89 | + emit UpdatedStaderConfig(_staderConfig); |
| 90 | + } |
| 91 | + |
| 92 | + //update the implementation address of vaultProxy contract |
| 93 | + function updateVaultProxyAddress(address _vaultProxyImpl) external override onlyRole(DEFAULT_ADMIN_ROLE) { |
| 94 | + UtilLib.checkNonZeroAddress(_vaultProxyImpl); |
| 95 | + vaultProxyImplementation = _vaultProxyImpl; |
| 96 | + emit UpdatedVaultProxyImplementation(vaultProxyImplementation); |
| 97 | + } |
| 98 | +} |
0 commit comments