//SPDX-License-Identifier: CC-BY-NC-ND-4.0 pragma solidity ^0.8.22; import {ITermController} from "./interfaces/ITermController.sol"; import {ITermControllerEvents} from "./interfaces/ITermControllerEvents.sol"; import {TermAuctionResults} from "./lib/TermAuctionResults.sol"; import {AuctionMetadata} from "./lib/AuctionMetadata.sol"; import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol"; import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; import {Versionable} from "./lib/Versionable.sol"; /// @author TermLabs /// @title Term Controller /// @notice This contract manages Term Finance protocol permissions and controls /// @dev This contract operates at the protocol level and governs all instances of a Term Repo contract TermController is ITermController, ITermControllerEvents, Initializable, AccessControlUpgradeable, UUPSUpgradeable, Versionable { // ======================================================================== // = Access Roles ========================================================= // ======================================================================== bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); bytes32 public constant AUCTION_ROLE = keccak256("AUCTION_ROLE"); bytes32 public constant CONTROLLER_ADMIN_ROLE = keccak256("CONTROLLER_ADMIN_ROLE"); bytes32 public constant DEVOPS_ROLE = keccak256("DEVOPS_ROLE"); bytes32 public constant INITIALIZER_ROLE = keccak256("INITIALIZER_ROLE"); bytes32 public constant FACTORY_DEPLOYER_ROLE = keccak256("FACTORY_DEPLOYER_ROLE"); bytes32 public constant SPECIALIST_ROLE = keccak256("SPECIALIST_ROLE"); // ======================================================================== // = State Variables ====================================================== // ======================================================================== // Pause state for all Term Finance contracts bool public termContractsPaused; // Term Finance Treasury Wallet Address address internal treasuryWallet; // Term Finance Protocol Reserves address internal protocolReserveWallet; // Mapping which returns true for contract addresses deployed by Term Finance Protocol mapping(address => bool) internal termAddresses; // Mapping which returns true for term repo contract addresses deployed by factory mapping (address => bool) internal factoryDeployedAddresses; // Mapping which returns true for external contract addresses approved for integration mapping(address => bool) internal approvedExternalAddresses; // Mapping which returns true for term repo ids registered in Controller mapping(bytes32 => bool) public registeredRepoIds; // Mapping which returns true for term auction ids registered in Controller mapping(bytes32 => bool) public registeredAuctionIds; // Mapping which returns a TermAuctionResults object for each termId. mapping(bytes32 => TermAuctionResults) internal termAuctionResults; // ======================================================================== // = Modifiers =========================================================== // ======================================================================== modifier onlyInitializerOrFactoryDeployer() { require(hasRole(INITIALIZER_ROLE, msg.sender) || hasRole(FACTORY_DEPLOYER_ROLE, msg.sender), "Unauthorized"); _; } /// @custom:oz-upgrades-unsafe-allow constructor // ======================================================================== // = Deploy ============================================================== // ======================================================================== /// @custom:oz-upgrades-unsafe-allow constructor constructor() { _disableInitializers(); } function initialize( address treasuryWallet_, address protocolReserveWallet_, address controllerAdminWallet_, address devopsWallet_, address adminWallet_ ) external initializer { UUPSUpgradeable.__UUPSUpgradeable_init(); AccessControlUpgradeable.__AccessControl_init(); require( controllerAdminWallet_ != address(0), "controller admin is zero address" ); require(devopsWallet_ != address(0), "devops wallet is zero address"); require(adminWallet_ != address(0), "admin wallet is zero address"); _grantRole(CONTROLLER_ADMIN_ROLE, controllerAdminWallet_); _grantRole(DEVOPS_ROLE, devopsWallet_); _grantRole(ADMIN_ROLE, adminWallet_); require(treasuryWallet_ != address(0), "treasury is zero address"); treasuryWallet = treasuryWallet_; require( protocolReserveWallet_ != address(0), "reserve is zero address" ); protocolReserveWallet = protocolReserveWallet_; } function pairInitializer( address initializer ) external onlyRole(ADMIN_ROLE) { _grantRole(INITIALIZER_ROLE, initializer); } function pairFactoryDeployer(address factoryDeployer) external onlyRole(ADMIN_ROLE) { _grantRole(FACTORY_DEPLOYER_ROLE, factoryDeployer); } function pairAuction(address auction) external onlyInitializerOrFactoryDeployer { _grantRole(AUCTION_ROLE, auction); } // ======================================================================== // = Interface/API ======================================================== // ======================================================================== /// @notice External view function which returns contract address of treasury wallet function getTreasuryAddress() external view returns (address) { return treasuryWallet; } /// @notice External view function which returns contract address of protocol reserve /// @return The protocol reserve address function getProtocolReserveAddress() external view returns (address) { return protocolReserveWallet; } function getTermAuctionResults( bytes32 termRepoId ) external view returns (AuctionMetadata[] memory auctionMetadata, uint8 numOfAuctions) { auctionMetadata = termAuctionResults[termRepoId].auctionMetadata; numOfAuctions = termAuctionResults[termRepoId].numOfAuctions; } /// @notice External view function which returns whether contract address is deployed by Term Finance Protocol /// @param contractAddress The input contract address to query /// @return Whether the given address is deployed by Term Finance Protocol function isTermDeployed( address contractAddress ) external view returns (bool) { return _isTermDeployed(contractAddress); } /// @notice External view function which returns whether contract address is deployed by a Term Finance factory /// @param contractAddress The input contract address to query /// @return Whether the given address is deployed by a Term Finance factory function isFactoryDeployed( address contractAddress ) external view returns (bool) { return _isFactoryDeployed(contractAddress); } /// @notice External view function which returns whether contract address is approved for integration /// @param contractAddress The input contract address to query /// @return Whether the given address is approved for integration function isTermApproved( address contractAddress ) external view returns (bool) { return _isTermApproved(contractAddress); } /// @param authedUser The address of user to check access for mint exposure function verifyMintExposureAccess( address authedUser ) external view returns (bool) { return hasRole(SPECIALIST_ROLE, authedUser); } // ======================================================================== // = Admin Functions ====================================================== // ======================================================================== /// @notice Admin function to update the Term Finance treasury wallet address /// @param newTreasuryWallet The new treasury address function updateTreasuryAddress( address newTreasuryWallet ) external onlyRole(DEVOPS_ROLE) { require( newTreasuryWallet != treasuryWallet, "No change in treasury address" ); address oldTreasuryWallet = treasuryWallet; treasuryWallet = newTreasuryWallet; emit TreasuryAddressUpdated(oldTreasuryWallet, treasuryWallet); } /// @notice Admin function to update the Term Finance protocol reserve wallet address /// @param newProtocolReserveWallet The new protocol reserve wallet address function updateProtocolReserveAddress( address newProtocolReserveWallet ) external onlyRole(DEVOPS_ROLE) { require( newProtocolReserveWallet != protocolReserveWallet, "No change in protocol reserve address" ); address oldProtocolReserveWallet = protocolReserveWallet; protocolReserveWallet = newProtocolReserveWallet; emit ProtocolReserveAddressUpdated( oldProtocolReserveWallet, protocolReserveWallet ); } /// @notice Admin function to update the designated controller admin wallet that calls markTermDeployed /// @param oldControllerAdminWallet The current controller admin wallet to revoke permissions for /// @param newControllerAdminWallet The new controller admin wallet to grant permissions for function updateControllerAdminWallet( address oldControllerAdminWallet, address newControllerAdminWallet ) external onlyRole(DEVOPS_ROLE) { require( oldControllerAdminWallet != address(0), "Old Controller Admin Wallet cannot be zero address" ); require( newControllerAdminWallet != address(0), "New Controller Admin Wallet cannot be zero address" ); require( hasRole(CONTROLLER_ADMIN_ROLE, oldControllerAdminWallet), "incorrect old controller admin wallet address" ); _revokeRole(CONTROLLER_ADMIN_ROLE, oldControllerAdminWallet); _grantRole(CONTROLLER_ADMIN_ROLE, newControllerAdminWallet); } /// @notice Admin function to add a new Term Finance contract to Controller /// @param termContract The new term contract address function markTermDeployed( address termContract ) external onlyRole(CONTROLLER_ADMIN_ROLE) { require(!_isTermDeployed(termContract), "Contract is already in Term"); termAddresses[termContract] = true; } /// @notice Admin function to remove a contract from Controller /// @param termContract The new term contract address function unmarkTermDeployed( address termContract ) external onlyRole(CONTROLLER_ADMIN_ROLE) { require(_isTermDeployed(termContract), "Contract is not in Term"); delete termAddresses[termContract]; } /// @notice Admin function to add a new factory-deployed contract to Controller /// @param factoryDeployedContract The factory-deployed contract address function markTermFactoryDeployed( address factoryDeployedContract ) external onlyRole(FACTORY_DEPLOYER_ROLE) { require(!_isFactoryDeployed(factoryDeployedContract), "Contract is already marked deployed by factory"); factoryDeployedAddresses[factoryDeployedContract] = true; } /// @notice Admin function to remove a factory-deployed contract from Controller /// @param factoryDeployedContract The factory-deployed contract address function unmarkTermFactoryDeployed( address factoryDeployedContract ) external onlyRole(CONTROLLER_ADMIN_ROLE) { require(_isFactoryDeployed(factoryDeployedContract), "Contract is not marked deployed by factory"); delete factoryDeployedAddresses[factoryDeployedContract]; } /// @notice Admin function to add a new term repo id to Controller /// @param repoId The new term repo id function registerRepoId(bytes32 repoId) external onlyInitializerOrFactoryDeployer { registeredRepoIds[repoId] = true; } /// @notice Admin function to add a new term auction id to Controller /// @param auctionId The new term auction id function registerAuctionId(bytes32 auctionId) external onlyInitializerOrFactoryDeployer { registeredAuctionIds[auctionId] = true; } /// @notice Admin function to add a new external contract to Controller /// @param externalContract The new external contract address function markTermApproved( address externalContract ) external onlyRole(ADMIN_ROLE) { require(!approvedExternalAddresses[externalContract], "Contract is already approved"); approvedExternalAddresses[externalContract] = true; } /// @notice Admin function to remove an external contract from Controller /// @param externalContract The new external contract address function unmarkTermApproved( address externalContract ) external onlyRole(ADMIN_ROLE) { require(approvedExternalAddresses[externalContract], "Contract is not approved"); delete approvedExternalAddresses[externalContract]; } /// @param authedUser The address of user granted access to create mint exposure function grantMintExposureAccess( address authedUser ) external onlyRole(ADMIN_ROLE) { _grantRole(SPECIALIST_ROLE, authedUser); } /// @param revokedUser The address of user to revoke access to create mint exposure function revokeMintExposureAccess( address revokedUser ) external onlyRole(ADMIN_ROLE) { _revokeRole(SPECIALIST_ROLE, revokedUser); } /// @notice Pauses all Term Finance contracts /// @dev Sets the global pause state to true, halting contract operations function pauseTermContracts() external onlyRole(ADMIN_ROLE) { termContractsPaused = true; } /// @notice Unpauses all Term Finance contracts /// @dev Sets the global pause state to false, resuming contract operations function unpauseTermContracts() external onlyRole(ADMIN_ROLE) { termContractsPaused = false; } function recordAuctionResult( bytes32 termRepoId, bytes32 termAuctionId, uint256 auctionClearingRate ) external onlyRole(AUCTION_ROLE) { TermAuctionResults storage term = termAuctionResults[termRepoId]; AuctionMetadata memory auctionMetadata = AuctionMetadata({ termAuctionId: termAuctionId, auctionClearingRate: auctionClearingRate, auctionClearingBlockTimestamp: block.timestamp }); term.auctionMetadata.push(auctionMetadata); term.numOfAuctions++; } function _isTermDeployed( address contractAddress ) private view returns (bool) { return termAddresses[contractAddress]; } function _isFactoryDeployed( address contractAddress ) private view returns (bool) { return factoryDeployedAddresses[contractAddress]; } function _isTermApproved( address contractAddress ) private view returns (bool) { return approvedExternalAddresses[contractAddress]; } // ======================================================================== // = Upgrades ============================================================= // ======================================================================== // solhint-disable no-empty-blocks ///@dev required override by the OpenZeppelin UUPS module function _authorizeUpgrade( address ) internal view override onlyRole(DEVOPS_ROLE) {} // solhint-enable no-empty-blocks }