|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.25; |
| 3 | + |
| 4 | +/* solhint-disable comprehensive-interface */ |
| 5 | + |
| 6 | +import { |
| 7 | + AccessControlDefaultAdminRulesUpgradeable |
| 8 | +} from "@openzeppelin/contracts-upgradeable/access/extensions/AccessControlDefaultAdminRulesUpgradeable.sol"; |
| 9 | +import {ICollateralManagement} from "./interfaces/ICollateralManagement.sol"; |
| 10 | +import {IFlyoverDiscovery} from "./interfaces/IFlyoverDiscovery.sol"; |
| 11 | +import {Flyover} from "./libraries/Flyover.sol"; |
| 12 | + |
| 13 | +/// @title FlyoverDiscovery |
| 14 | +/// @notice Registry and discovery of Liquidity Providers (LPs) for Flyover |
| 15 | +/// @dev Keeps LP metadata and consults `ICollateralManagement` to decide listing and operational status |
| 16 | +contract FlyoverDiscovery is |
| 17 | + AccessControlDefaultAdminRulesUpgradeable, |
| 18 | + IFlyoverDiscovery |
| 19 | +{ |
| 20 | + |
| 21 | + // ------------------------------------------------------------ |
| 22 | + // FlyoverDiscovery State Variables |
| 23 | + // ------------------------------------------------------------ |
| 24 | + |
| 25 | + mapping(uint => Flyover.LiquidityProvider) private _liquidityProviders; |
| 26 | + ICollateralManagement private _collateralManagement; |
| 27 | + uint public lastProviderId; |
| 28 | + |
| 29 | + // ------------------------------------------------------------ |
| 30 | + // FlyoverDiscovery Public Functions and Modifiers |
| 31 | + // ------------------------------------------------------------ |
| 32 | + |
| 33 | + /// @notice Initializes the contract with admin configuration |
| 34 | + /// @dev Uses OZ upgradeable admin rules. Must be called only once |
| 35 | + /// @param owner The Default Admin and initial owner address |
| 36 | + /// @param initialDelay The initial admin delay for `AccessControlDefaultAdminRulesUpgradeable` |
| 37 | + /// @param collateralManagement The address of the `ICollateralManagement` contract |
| 38 | + function initialize( |
| 39 | + address owner, |
| 40 | + uint48 initialDelay, |
| 41 | + address collateralManagement |
| 42 | + ) external initializer { |
| 43 | + if (collateralManagement.code.length == 0) revert Flyover.NoContract(collateralManagement); |
| 44 | + __AccessControlDefaultAdminRules_init(initialDelay, owner); |
| 45 | + _collateralManagement = ICollateralManagement(collateralManagement); |
| 46 | + } |
| 47 | + |
| 48 | + /// @inheritdoc IFlyoverDiscovery |
| 49 | + function register( |
| 50 | + string calldata name, |
| 51 | + string calldata apiBaseUrl, |
| 52 | + bool status, |
| 53 | + Flyover.ProviderType providerType |
| 54 | + ) external payable returns (uint) { |
| 55 | + |
| 56 | + _validateRegistration(name, apiBaseUrl, providerType, msg.sender, msg.value); |
| 57 | + |
| 58 | + ++lastProviderId; |
| 59 | + _liquidityProviders[lastProviderId] = Flyover.LiquidityProvider({ |
| 60 | + id: lastProviderId, |
| 61 | + providerAddress: msg.sender, |
| 62 | + name: name, |
| 63 | + apiBaseUrl: apiBaseUrl, |
| 64 | + status: status, |
| 65 | + providerType: providerType |
| 66 | + }); |
| 67 | + emit Register(lastProviderId, msg.sender, msg.value); |
| 68 | + _addCollateral(providerType, msg.sender, msg.value); |
| 69 | + return (lastProviderId); |
| 70 | + } |
| 71 | + |
| 72 | + /// @inheritdoc IFlyoverDiscovery |
| 73 | + function setProviderStatus( |
| 74 | + uint providerId, |
| 75 | + bool status |
| 76 | + ) external { |
| 77 | + if (msg.sender != owner() && msg.sender != _liquidityProviders[providerId].providerAddress) { |
| 78 | + revert NotAuthorized(msg.sender); |
| 79 | + } |
| 80 | + _liquidityProviders[providerId].status = status; |
| 81 | + emit IFlyoverDiscovery.ProviderStatusSet(providerId, status); |
| 82 | + } |
| 83 | + |
| 84 | + /// @inheritdoc IFlyoverDiscovery |
| 85 | + function updateProvider(string calldata name, string calldata apiBaseUrl) external { |
| 86 | + if (bytes(name).length < 1 || bytes(apiBaseUrl).length < 1) revert InvalidProviderData(name, apiBaseUrl); |
| 87 | + Flyover.LiquidityProvider storage lp; |
| 88 | + address providerAddress = msg.sender; |
| 89 | + for (uint i = 1; i < lastProviderId + 1; ++i) { |
| 90 | + lp = _liquidityProviders[i]; |
| 91 | + if (providerAddress == lp.providerAddress) { |
| 92 | + lp.name = name; |
| 93 | + lp.apiBaseUrl = apiBaseUrl; |
| 94 | + emit IFlyoverDiscovery.ProviderUpdate(providerAddress, lp.name, lp.apiBaseUrl); |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | + revert Flyover.ProviderNotRegistered(providerAddress); |
| 99 | + } |
| 100 | + |
| 101 | + /// @inheritdoc IFlyoverDiscovery |
| 102 | + function getProviders() external view returns (Flyover.LiquidityProvider[] memory) { |
| 103 | + uint count = 0; |
| 104 | + Flyover.LiquidityProvider storage lp; |
| 105 | + for (uint i = 1; i < lastProviderId + 1; ++i) { |
| 106 | + if (_shouldBeListed(_liquidityProviders[i])) { |
| 107 | + ++count; |
| 108 | + } |
| 109 | + } |
| 110 | + Flyover.LiquidityProvider[] memory providersToReturn = new Flyover.LiquidityProvider[](count); |
| 111 | + count = 0; |
| 112 | + for (uint i = 1; i < lastProviderId + 1; ++i) { |
| 113 | + lp = _liquidityProviders[i]; |
| 114 | + if (_shouldBeListed(lp)) { |
| 115 | + providersToReturn[count] = lp; |
| 116 | + ++count; |
| 117 | + } |
| 118 | + } |
| 119 | + return providersToReturn; |
| 120 | + } |
| 121 | + |
| 122 | + /// @inheritdoc IFlyoverDiscovery |
| 123 | + function getProvider(address providerAddress) external view returns (Flyover.LiquidityProvider memory) { |
| 124 | + return _getProvider(providerAddress); |
| 125 | + } |
| 126 | + |
| 127 | + /// @inheritdoc IFlyoverDiscovery |
| 128 | + function isOperational(Flyover.ProviderType providerType, address addr) external view returns (bool) { |
| 129 | + return _getProvider(addr).status && |
| 130 | + _collateralManagement.isCollateralSufficient(providerType, addr); |
| 131 | + } |
| 132 | + |
| 133 | + // ------------------------------------------------------------ |
| 134 | + // Getter Functions |
| 135 | + // ------------------------------------------------------------ |
| 136 | + |
| 137 | + /// @inheritdoc IFlyoverDiscovery |
| 138 | + function getProvidersId() external view returns (uint) { |
| 139 | + return lastProviderId; |
| 140 | + } |
| 141 | + |
| 142 | + // ------------------------------------------------------------ |
| 143 | + // FlyoverDiscovery Private Functions |
| 144 | + // ------------------------------------------------------------ |
| 145 | + |
| 146 | + /// @notice Adds collateral to the collateral management contract based on provider type |
| 147 | + /// @dev Distributes collateral between peg-in and peg-out based on provider type |
| 148 | + /// @param providerType The type of provider (PegIn, PegOut, or Both) |
| 149 | + /// @param providerAddress The address of the provider |
| 150 | + /// @param collateralAmount The total amount of collateral to add |
| 151 | + function _addCollateral( |
| 152 | + Flyover.ProviderType providerType, |
| 153 | + address providerAddress, |
| 154 | + uint256 collateralAmount |
| 155 | + ) private { |
| 156 | + if (providerType == Flyover.ProviderType.PegIn) { |
| 157 | + _collateralManagement.addPegInCollateralTo{value: collateralAmount}(providerAddress); |
| 158 | + } else if (providerType == Flyover.ProviderType.PegOut) { |
| 159 | + _collateralManagement.addPegOutCollateralTo{value: collateralAmount}(providerAddress); |
| 160 | + } else if (providerType == Flyover.ProviderType.Both) { |
| 161 | + uint256 halfAmount = collateralAmount / 2; |
| 162 | + uint256 remainder = collateralAmount % 2; |
| 163 | + _collateralManagement.addPegInCollateralTo{value: halfAmount + remainder}(providerAddress); |
| 164 | + _collateralManagement.addPegOutCollateralTo{value: halfAmount}(providerAddress); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /// @notice Checks if a liquidity provider should be listed in the public provider list |
| 169 | + /// @dev A provider is listed if it is registered and has status enabled |
| 170 | + /// @param lp The liquidity provider storage reference |
| 171 | + /// @return True if the provider should be listed, false otherwise |
| 172 | + function _shouldBeListed(Flyover.LiquidityProvider storage lp) private view returns(bool){ |
| 173 | + return _collateralManagement.isRegistered(lp.providerType, lp.providerAddress) && lp.status; |
| 174 | + } |
| 175 | + |
| 176 | + /// @notice Validates registration parameters and requirements |
| 177 | + /// @dev Checks EOA status, data validity, provider type, registration status, and collateral requirements |
| 178 | + /// @param name The provider name to validate |
| 179 | + /// @param apiBaseUrl The provider API URL to validate |
| 180 | + /// @param providerType The provider type to validate |
| 181 | + /// @param providerAddress The provider address to validate |
| 182 | + /// @param collateralAmount The collateral amount to validate against minimum requirements |
| 183 | + function _validateRegistration( |
| 184 | + string memory name, |
| 185 | + string memory apiBaseUrl, |
| 186 | + Flyover.ProviderType providerType, |
| 187 | + address providerAddress, |
| 188 | + uint256 collateralAmount |
| 189 | + ) private view { |
| 190 | + if (providerAddress != msg.sender || providerAddress.code.length != 0) revert NotEOA(providerAddress); |
| 191 | + |
| 192 | + if ( |
| 193 | + bytes(name).length < 1 || |
| 194 | + bytes(apiBaseUrl).length < 1 |
| 195 | + ) { |
| 196 | + revert InvalidProviderData(name, apiBaseUrl); |
| 197 | + } |
| 198 | + |
| 199 | + if (providerType > type(Flyover.ProviderType).max) revert InvalidProviderType(providerType); |
| 200 | + |
| 201 | + if ( |
| 202 | + _collateralManagement.getPegInCollateral(providerAddress) > 0 || |
| 203 | + _collateralManagement.getPegOutCollateral(providerAddress) > 0 || |
| 204 | + _collateralManagement.getResignationBlock(providerAddress) != 0 |
| 205 | + ) { |
| 206 | + revert AlreadyRegistered(providerAddress); |
| 207 | + } |
| 208 | + |
| 209 | + // Check minimum collateral requirement |
| 210 | + uint256 minCollateral = _collateralManagement.getMinCollateral(); |
| 211 | + if (providerType == Flyover.ProviderType.Both) { |
| 212 | + if (collateralAmount < minCollateral * 2) { |
| 213 | + revert InsufficientCollateral(collateralAmount); |
| 214 | + } |
| 215 | + } else { |
| 216 | + if (collateralAmount < minCollateral) { |
| 217 | + revert InsufficientCollateral(collateralAmount); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + /// @notice Retrieves a liquidity provider by address |
| 223 | + /// @dev Searches through all registered providers to find a match |
| 224 | + /// @param providerAddress The address of the provider to find |
| 225 | + /// @return The liquidity provider record, reverts if not found |
| 226 | + function _getProvider(address providerAddress) private view returns (Flyover.LiquidityProvider memory) { |
| 227 | + for (uint i = 1; i < lastProviderId + 1; ++i) { |
| 228 | + if (_liquidityProviders[i].providerAddress == providerAddress) { |
| 229 | + return _liquidityProviders[i]; |
| 230 | + } |
| 231 | + } |
| 232 | + revert Flyover.ProviderNotRegistered(providerAddress); |
| 233 | + } |
| 234 | +} |
0 commit comments