|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity >=0.8.27; |
| 3 | + |
| 4 | +import {Ownable} from "@oz/access/Ownable.sol"; |
| 5 | + |
| 6 | +import {IStaking} from "./../core/interfaces/IStaking.sol"; |
| 7 | +import {IMintableERC20} from "./../governance/interfaces/IMintableERC20.sol"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @title StakingAssetHandler |
| 11 | + * @notice This contract is used as a faucet for creating validators. |
| 12 | + * |
| 13 | + * It allows for anyone with the `canAddValidator` role to add validators to the rollup, |
| 14 | + * caveat being that it controls the number of validators that can be added in a time period. |
| 15 | + * |
| 16 | + * @dev For example, if minMintInterval is 60*60 and maxDepositsPerMint is 3, |
| 17 | + * then *generally* 3 validators can be added every hour. |
| 18 | + * NB: it is possible to add 1 validator at the top of the hour, and 2 validators |
| 19 | + * at the very end of the hour, then 3 validators at the top of the next hour |
| 20 | + * so the maximum "burst" rate is effectively twice the maxDepositsPerMint. |
| 21 | + * |
| 22 | + * @dev This contract must be a minter of the staking asset. |
| 23 | + * |
| 24 | + * @dev Only the owner can grant and revoke the `canAddValidator` role, and perform other administrative tasks |
| 25 | + * such as setting the rollup, deposit amount, min mint interval, max deposits per mint, and withdrawer. |
| 26 | + * |
| 27 | + */ |
| 28 | +interface IStakingAssetHandler { |
| 29 | + event ToppedUp(uint256 _amount); |
| 30 | + event ValidatorAdded( |
| 31 | + address indexed _attester, address indexed _proposer, address indexed _withdrawer |
| 32 | + ); |
| 33 | + event RollupUpdated(address indexed _rollup); |
| 34 | + event DepositAmountUpdated(uint256 _depositAmount); |
| 35 | + event IntervalUpdated(uint256 _interval); |
| 36 | + event DepositsPerMintUpdated(uint256 _depositsPerMint); |
| 37 | + event WithdrawerUpdated(address indexed _withdrawer); |
| 38 | + event AddValidatorPermissionGranted(address indexed _address); |
| 39 | + event AddValidatorPermissionRevoked(address indexed _address); |
| 40 | + |
| 41 | + error NotCanAddValidator(address _caller); |
| 42 | + error NotEnoughTimeSinceLastMint(uint256 _lastMintTimestamp, uint256 _minMintInterval); |
| 43 | + error CannotMintZeroAmount(); |
| 44 | + error MaxDepositsTooLarge(uint256 _depositAmount, uint256 _maxDepositsPerMint); |
| 45 | + |
| 46 | + function addValidator(address _attester, address _proposer) external; |
| 47 | + function setRollup(address _rollup) external; |
| 48 | + function setDepositAmount(uint256 _amount) external; |
| 49 | + function setMintInterval(uint256 _interval) external; |
| 50 | + function setDepositsPerMint(uint256 _depositsPerMint) external; |
| 51 | + function setWithdrawer(address _withdrawer) external; |
| 52 | + function grantAddValidatorPermission(address _address) external; |
| 53 | + function revokeAddValidatorPermission(address _address) external; |
| 54 | +} |
| 55 | + |
| 56 | +contract StakingAssetHandler is IStakingAssetHandler, Ownable { |
| 57 | + IMintableERC20 public immutable STAKING_ASSET; |
| 58 | + |
| 59 | + mapping(address => bool) public canAddValidator; |
| 60 | + |
| 61 | + uint256 public depositAmount; |
| 62 | + uint256 public lastMintTimestamp; |
| 63 | + uint256 public mintInterval; |
| 64 | + uint256 public depositsPerMint; |
| 65 | + |
| 66 | + IStaking public rollup; |
| 67 | + address public withdrawer; |
| 68 | + |
| 69 | + modifier onlyCanAddValidator() { |
| 70 | + require(canAddValidator[msg.sender], NotCanAddValidator(msg.sender)); |
| 71 | + _; |
| 72 | + } |
| 73 | + |
| 74 | + constructor( |
| 75 | + address _owner, |
| 76 | + address _stakingAsset, |
| 77 | + address _rollup, |
| 78 | + address _withdrawer, |
| 79 | + uint256 _depositAmount, |
| 80 | + uint256 _mintInterval, |
| 81 | + uint256 _depositsPerMint, |
| 82 | + address[] memory _canAddValidator |
| 83 | + ) Ownable(_owner) { |
| 84 | + require(_depositsPerMint > 0, CannotMintZeroAmount()); |
| 85 | + |
| 86 | + STAKING_ASSET = IMintableERC20(_stakingAsset); |
| 87 | + |
| 88 | + rollup = IStaking(_rollup); |
| 89 | + emit RollupUpdated(_rollup); |
| 90 | + |
| 91 | + withdrawer = _withdrawer; |
| 92 | + emit WithdrawerUpdated(_withdrawer); |
| 93 | + |
| 94 | + depositAmount = _depositAmount; |
| 95 | + emit DepositAmountUpdated(_depositAmount); |
| 96 | + |
| 97 | + mintInterval = _mintInterval; |
| 98 | + emit IntervalUpdated(_mintInterval); |
| 99 | + |
| 100 | + depositsPerMint = _depositsPerMint; |
| 101 | + emit DepositsPerMintUpdated(_depositsPerMint); |
| 102 | + |
| 103 | + for (uint256 i = 0; i < _canAddValidator.length; i++) { |
| 104 | + canAddValidator[_canAddValidator[i]] = true; |
| 105 | + emit AddValidatorPermissionGranted(_canAddValidator[i]); |
| 106 | + } |
| 107 | + canAddValidator[_owner] = true; |
| 108 | + emit AddValidatorPermissionGranted(_owner); |
| 109 | + } |
| 110 | + |
| 111 | + function addValidator(address _attester, address _proposer) external override onlyCanAddValidator { |
| 112 | + bool needsToMint = STAKING_ASSET.balanceOf(address(this)) < depositAmount; |
| 113 | + bool canMint = block.timestamp - lastMintTimestamp >= mintInterval; |
| 114 | + |
| 115 | + require(!needsToMint || canMint, NotEnoughTimeSinceLastMint(lastMintTimestamp, mintInterval)); |
| 116 | + if (needsToMint) { |
| 117 | + STAKING_ASSET.mint(address(this), depositAmount * depositsPerMint); |
| 118 | + lastMintTimestamp = block.timestamp; |
| 119 | + emit ToppedUp(depositAmount * depositsPerMint); |
| 120 | + } |
| 121 | + |
| 122 | + STAKING_ASSET.approve(address(rollup), depositAmount); |
| 123 | + rollup.deposit(_attester, _proposer, withdrawer, depositAmount); |
| 124 | + emit ValidatorAdded(_attester, _proposer, withdrawer); |
| 125 | + } |
| 126 | + |
| 127 | + function setRollup(address _rollup) external override onlyOwner { |
| 128 | + rollup = IStaking(_rollup); |
| 129 | + emit RollupUpdated(_rollup); |
| 130 | + } |
| 131 | + |
| 132 | + function setDepositAmount(uint256 _amount) external override onlyOwner { |
| 133 | + depositAmount = _amount; |
| 134 | + emit DepositAmountUpdated(_amount); |
| 135 | + } |
| 136 | + |
| 137 | + function setMintInterval(uint256 _interval) external override onlyOwner { |
| 138 | + mintInterval = _interval; |
| 139 | + emit IntervalUpdated(_interval); |
| 140 | + } |
| 141 | + |
| 142 | + function setDepositsPerMint(uint256 _depositsPerMint) external override onlyOwner { |
| 143 | + require(_depositsPerMint > 0, CannotMintZeroAmount()); |
| 144 | + depositsPerMint = _depositsPerMint; |
| 145 | + emit DepositsPerMintUpdated(_depositsPerMint); |
| 146 | + } |
| 147 | + |
| 148 | + function setWithdrawer(address _withdrawer) external override onlyOwner { |
| 149 | + withdrawer = _withdrawer; |
| 150 | + emit WithdrawerUpdated(_withdrawer); |
| 151 | + } |
| 152 | + |
| 153 | + function grantAddValidatorPermission(address _address) external override onlyOwner { |
| 154 | + canAddValidator[_address] = true; |
| 155 | + emit AddValidatorPermissionGranted(_address); |
| 156 | + } |
| 157 | + |
| 158 | + function revokeAddValidatorPermission(address _address) external override onlyOwner { |
| 159 | + canAddValidator[_address] = false; |
| 160 | + emit AddValidatorPermissionRevoked(_address); |
| 161 | + } |
| 162 | +} |
0 commit comments