|
| 1 | +// SPDX-License-Identifier: GPL-3.0-only |
| 2 | +pragma solidity ^0.8.21; |
| 3 | + |
| 4 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 5 | +import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 6 | +import "@openzeppelin/contracts/access/Ownable2Step.sol"; |
| 7 | +import {ZeroAddress} from "./utils/Errors.sol"; |
| 8 | +import "./stBTC.sol"; |
| 9 | +import "./interfaces/IDispatcher.sol"; |
| 10 | + |
| 11 | +/// @title IMezoPortal |
| 12 | +/// @dev Interface for the Mezo's Portal contract. |
| 13 | +interface IMezoPortal { |
| 14 | + /// @notice DepositInfo keeps track of the deposit balance and unlock time. |
| 15 | + /// Each deposit is tracked separately and associated with a specific |
| 16 | + /// token. Some tokens can be deposited but can not be locked - in |
| 17 | + /// that case the unlockAt is the block timestamp of when the deposit |
| 18 | + /// was created. The same is true for tokens that can be locked but |
| 19 | + /// the depositor decided not to lock them. |
| 20 | + struct DepositInfo { |
| 21 | + uint96 balance; |
| 22 | + uint32 unlockAt; |
| 23 | + } |
| 24 | + |
| 25 | + /// @notice Deposit and optionally lock tokens for the given period. |
| 26 | + /// @dev Lock period will be normalized to weeks. If non-zero, it must not |
| 27 | + /// be shorter than the minimum lock period and must not be longer than |
| 28 | + /// the maximum lock period. |
| 29 | + /// @param token token address to deposit |
| 30 | + /// @param amount amount of tokens to deposit |
| 31 | + /// @param lockPeriod lock period in seconds, 0 to not lock the deposit |
| 32 | + function deposit(address token, uint96 amount, uint32 lockPeriod) external; |
| 33 | + |
| 34 | + /// @notice Withdraw deposited tokens. |
| 35 | + /// Deposited lockable tokens can be withdrawn at any time if |
| 36 | + /// there is no lock set on the deposit or the lock period has passed. |
| 37 | + /// There is no way to withdraw locked deposit. Tokens that are not |
| 38 | + /// lockable can be withdrawn at any time. Deposit can be withdrawn |
| 39 | + /// partially. |
| 40 | + /// @param token deposited token address |
| 41 | + /// @param depositId id of the deposit |
| 42 | + /// @param amount amount of the token to be withdrawn from the deposit |
| 43 | + function withdraw(address token, uint256 depositId, uint96 amount) external; |
| 44 | + |
| 45 | + /// @notice The number of deposits created. Includes the deposits that |
| 46 | + /// were fully withdrawn. This is also the identifier of the most |
| 47 | + /// recently created deposit. |
| 48 | + function depositCount() external view returns (uint256); |
| 49 | + |
| 50 | + /// @notice Get the balance and unlock time of a given deposit. |
| 51 | + /// @param depositor depositor address |
| 52 | + /// @param token token address to get the balance |
| 53 | + /// @param depositId id of the deposit |
| 54 | + function getDeposit( |
| 55 | + address depositor, |
| 56 | + address token, |
| 57 | + uint256 depositId |
| 58 | + ) external view returns (DepositInfo memory); |
| 59 | +} |
| 60 | + |
| 61 | +/// @notice MezoAllocator routes tBTC to/from MezoPortal. |
| 62 | +contract MezoAllocator is IDispatcher, Ownable2Step { |
| 63 | + using SafeERC20 for IERC20; |
| 64 | + |
| 65 | + /// @notice Address of the MezoPortal contract. |
| 66 | + IMezoPortal public immutable mezoPortal; |
| 67 | + /// @notice tBTC token contract. |
| 68 | + IERC20 public immutable tbtc; |
| 69 | + /// @notice stBTC token vault contract. |
| 70 | + stBTC public immutable stbtc; |
| 71 | + /// @notice Keeps track of the addresses that are allowed to trigger deposit |
| 72 | + /// allocations. |
| 73 | + mapping(address => bool) public isMaintainer; |
| 74 | + /// @notice List of maintainers. |
| 75 | + address[] public maintainers; |
| 76 | + /// @notice keeps track of the latest deposit ID assigned in Mezo Portal. |
| 77 | + uint256 public depositId; |
| 78 | + /// @notice Keeps track of the total amount of tBTC allocated to MezoPortal. |
| 79 | + uint96 public depositBalance; |
| 80 | + |
| 81 | + /// @notice Emitted when tBTC is deposited to MezoPortal. |
| 82 | + event DepositAllocated( |
| 83 | + uint256 indexed oldDepositId, |
| 84 | + uint256 indexed newDepositId, |
| 85 | + uint256 addedAmount, |
| 86 | + uint256 newDepositAmount |
| 87 | + ); |
| 88 | + /// @notice Emitted when tBTC is withdrawn from MezoPortal. |
| 89 | + event DepositWithdrawn(uint256 indexed depositId, uint256 amount); |
| 90 | + /// @notice Emitted when the maintainer address is updated. |
| 91 | + event MaintainerAdded(address indexed maintainer); |
| 92 | + /// @notice Emitted when the maintainer address is updated. |
| 93 | + event MaintainerRemoved(address indexed maintainer); |
| 94 | + /// @notice Emitted when tBTC is released from MezoPortal. |
| 95 | + event DepositReleased(uint256 indexed depositId, uint256 amount); |
| 96 | + /// @notice Reverts if the caller is not a maintainer. |
| 97 | + error CallerNotMaintainer(); |
| 98 | + /// @notice Reverts if the caller is not the stBTC contract. |
| 99 | + error CallerNotStbtc(); |
| 100 | + /// @notice Reverts if the maintainer is not registered. |
| 101 | + error MaintainerNotRegistered(); |
| 102 | + /// @notice Reverts if the maintainer has been already registered. |
| 103 | + error MaintainerAlreadyRegistered(); |
| 104 | + |
| 105 | + modifier onlyMaintainer() { |
| 106 | + if (!isMaintainer[msg.sender]) { |
| 107 | + revert CallerNotMaintainer(); |
| 108 | + } |
| 109 | + _; |
| 110 | + } |
| 111 | + |
| 112 | + /// @notice Initializes the MezoAllocator contract. |
| 113 | + /// @param _mezoPortal Address of the MezoPortal contract. |
| 114 | + /// @param _tbtc Address of the tBTC token contract. |
| 115 | + constructor( |
| 116 | + address _mezoPortal, |
| 117 | + IERC20 _tbtc, |
| 118 | + stBTC _stbtc |
| 119 | + ) Ownable(msg.sender) { |
| 120 | + if (_mezoPortal == address(0)) { |
| 121 | + revert ZeroAddress(); |
| 122 | + } |
| 123 | + if (address(_tbtc) == address(0)) { |
| 124 | + revert ZeroAddress(); |
| 125 | + } |
| 126 | + if (address(_stbtc) == address(0)) { |
| 127 | + revert ZeroAddress(); |
| 128 | + } |
| 129 | + mezoPortal = IMezoPortal(_mezoPortal); |
| 130 | + tbtc = _tbtc; |
| 131 | + stbtc = _stbtc; |
| 132 | + } |
| 133 | + |
| 134 | + /// @notice Allocate tBTC to MezoPortal. Each allocation creates a new "rolling" |
| 135 | + /// deposit meaning that the previous Acre's deposit is fully withdrawn |
| 136 | + /// before a new deposit with added amount is created. This mimics a |
| 137 | + /// "top up" functionality with the difference that a new deposit id |
| 138 | + /// is created and the previous deposit id is no longer in use. |
| 139 | + /// @dev This function can be invoked periodically by a maintainer. |
| 140 | + function allocate() external onlyMaintainer { |
| 141 | + if (depositBalance > 0) { |
| 142 | + // Free all Acre's tBTC from MezoPortal before creating a new deposit. |
| 143 | + // slither-disable-next-line reentrancy-no-eth |
| 144 | + mezoPortal.withdraw(address(tbtc), depositId, depositBalance); |
| 145 | + } |
| 146 | + |
| 147 | + // Fetch unallocated tBTC from stBTC contract. |
| 148 | + uint256 addedAmount = tbtc.balanceOf(address(stbtc)); |
| 149 | + // slither-disable-next-line arbitrary-send-erc20 |
| 150 | + tbtc.safeTransferFrom(address(stbtc), address(this), addedAmount); |
| 151 | + |
| 152 | + // Create a new deposit in the MezoPortal. |
| 153 | + depositBalance = uint96(tbtc.balanceOf(address(this))); |
| 154 | + tbtc.forceApprove(address(mezoPortal), depositBalance); |
| 155 | + // 0 denotes no lock period for this deposit. |
| 156 | + mezoPortal.deposit(address(tbtc), depositBalance, 0); |
| 157 | + uint256 oldDepositId = depositId; |
| 158 | + // MezoPortal doesn't return depositId, so we have to read depositCounter |
| 159 | + // which assigns depositId to the current deposit. |
| 160 | + depositId = mezoPortal.depositCount(); |
| 161 | + |
| 162 | + // slither-disable-next-line reentrancy-events |
| 163 | + emit DepositAllocated( |
| 164 | + oldDepositId, |
| 165 | + depositId, |
| 166 | + addedAmount, |
| 167 | + depositBalance |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + /// @notice Withdraws tBTC from MezoPortal and transfers it to stBTC. |
| 172 | + /// This function can withdraw partial or a full amount of tBTC from |
| 173 | + /// MezoPortal for a given deposit id. |
| 174 | + /// @param amount Amount of tBTC to withdraw. |
| 175 | + function withdraw(uint256 amount) external { |
| 176 | + if (msg.sender != address(stbtc)) revert CallerNotStbtc(); |
| 177 | + |
| 178 | + emit DepositWithdrawn(depositId, amount); |
| 179 | + mezoPortal.withdraw(address(tbtc), depositId, uint96(amount)); |
| 180 | + // slither-disable-next-line reentrancy-benign |
| 181 | + depositBalance -= uint96(amount); |
| 182 | + tbtc.safeTransfer(address(stbtc), amount); |
| 183 | + } |
| 184 | + |
| 185 | + /// @notice Releases deposit in full from MezoPortal. |
| 186 | + /// @dev This is a special function that can be used to migrate funds during |
| 187 | + /// allocator upgrade or in case of emergencies. |
| 188 | + function releaseDeposit() external onlyOwner { |
| 189 | + uint96 amount = mezoPortal |
| 190 | + .getDeposit(address(this), address(tbtc), depositId) |
| 191 | + .balance; |
| 192 | + |
| 193 | + emit DepositReleased(depositId, amount); |
| 194 | + depositBalance = 0; |
| 195 | + mezoPortal.withdraw(address(tbtc), depositId, amount); |
| 196 | + tbtc.safeTransfer(address(stbtc), tbtc.balanceOf(address(this))); |
| 197 | + } |
| 198 | + |
| 199 | + /// @notice Updates the maintainer address. |
| 200 | + /// @param maintainerToAdd Address of the new maintainer. |
| 201 | + function addMaintainer(address maintainerToAdd) external onlyOwner { |
| 202 | + if (maintainerToAdd == address(0)) { |
| 203 | + revert ZeroAddress(); |
| 204 | + } |
| 205 | + if (isMaintainer[maintainerToAdd]) { |
| 206 | + revert MaintainerAlreadyRegistered(); |
| 207 | + } |
| 208 | + maintainers.push(maintainerToAdd); |
| 209 | + isMaintainer[maintainerToAdd] = true; |
| 210 | + |
| 211 | + emit MaintainerAdded(maintainerToAdd); |
| 212 | + } |
| 213 | + |
| 214 | + /// @notice Removes the maintainer address. |
| 215 | + /// @param maintainerToRemove Address of the maintainer to remove. |
| 216 | + function removeMaintainer(address maintainerToRemove) external onlyOwner { |
| 217 | + if (!isMaintainer[maintainerToRemove]) { |
| 218 | + revert MaintainerNotRegistered(); |
| 219 | + } |
| 220 | + delete (isMaintainer[maintainerToRemove]); |
| 221 | + |
| 222 | + for (uint256 i = 0; i < maintainers.length; i++) { |
| 223 | + if (maintainers[i] == maintainerToRemove) { |
| 224 | + maintainers[i] = maintainers[maintainers.length - 1]; |
| 225 | + // slither-disable-next-line costly-loop |
| 226 | + maintainers.pop(); |
| 227 | + break; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + emit MaintainerRemoved(maintainerToRemove); |
| 232 | + } |
| 233 | + |
| 234 | + /// @notice Returns the total amount of tBTC allocated to MezoPortal. |
| 235 | + function totalAssets() external view returns (uint256) { |
| 236 | + return depositBalance; |
| 237 | + } |
| 238 | + |
| 239 | + /// @notice Returns the list of maintainers. |
| 240 | + function getMaintainers() external view returns (address[] memory) { |
| 241 | + return maintainers; |
| 242 | + } |
| 243 | +} |
0 commit comments