This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
generated from storyprotocol/solidity-template
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing Governance support into Protocol (#35)
- Loading branch information
1 parent
8836092
commit a46562d
Showing
16 changed files
with
566 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// See https://github.com/storyprotocol/protocol-contracts/blob/main/StoryProtocol-AlphaTestingAgreement-17942166.3.pdf | ||
pragma solidity ^0.8.23; | ||
|
||
import { Errors } from "contracts/lib/Errors.sol"; | ||
import { IGovernance } from "contracts/interfaces/governance/IGovernance.sol"; | ||
import { IGovernable } from "../interfaces/governance/IGovernable.sol"; | ||
import { ERC165Checker } from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol"; | ||
import { GovernanceLib } from "contracts/lib/GovernanceLib.sol"; | ||
/// @title Governable | ||
/// @dev All contracts managed by governance should inherit from this contract. | ||
abstract contract Governable is IGovernable { | ||
/// @notice The address of the governance. | ||
address public governance; | ||
|
||
/// @dev Ensures that the function is called by the protocol admin. | ||
modifier onlyProtocolAdmin() { | ||
if(!IGovernance(governance).hasRole(GovernanceLib.PROTOCOL_ADMIN, msg.sender)) { | ||
revert Errors.Governance__OnlyProtocolAdmin(); | ||
} | ||
_; | ||
} | ||
|
||
modifier whenNotPaused() { | ||
if (IGovernance(governance).getState() == GovernanceLib.ProtocolState.Paused) { | ||
revert Errors.Governance__ProtocolPaused(); | ||
} | ||
_; | ||
} | ||
|
||
/// @notice Constructs a new Governable contract. | ||
/// @param governance_ The address of the governance. | ||
constructor(address governance_) { | ||
if (governance_ == address(0)) revert Errors.Governance__ZeroAddress(); | ||
governance = governance_; | ||
emit GovernanceUpdated(governance); | ||
} | ||
|
||
/// @notice Sets a new governance address. | ||
/// @param newGovernance The address of the new governance. | ||
function setGovernance(address newGovernance) external onlyProtocolAdmin { | ||
if (newGovernance == address(0)) revert Errors.Governance__ZeroAddress(); | ||
if (!ERC165Checker.supportsInterface(newGovernance, type(IGovernance).interfaceId)) | ||
revert Errors.Governance__UnsupportedInterface("IGovernance"); | ||
if (IGovernance(newGovernance).getState() != IGovernance(governance).getState()) | ||
revert Errors.Governance__InconsistentState(); | ||
governance = newGovernance; | ||
emit GovernanceUpdated(newGovernance); | ||
} | ||
|
||
/// @notice Returns the current governance address. | ||
/// @return The address of the current governance. | ||
function getGovernance() external view returns (address) { | ||
return governance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// See https://github.com/storyprotocol/protocol-contracts/blob/main/StoryProtocol-AlphaTestingAgreement-17942166.3.pdf | ||
pragma solidity ^0.8.23; | ||
|
||
import { Errors } from "contracts/lib/Errors.sol"; | ||
import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol"; | ||
import { IGovernance } from "contracts/interfaces/governance/IGovernance.sol"; | ||
import { GovernanceLib } from "contracts/lib/GovernanceLib.sol"; | ||
|
||
/// @title Governance | ||
/// @dev This contract is used for governance of the protocol. | ||
contract Governance is AccessControl, IGovernance { | ||
GovernanceLib.ProtocolState internal state; | ||
|
||
/// @notice Creates a new Governance contract. | ||
/// @param admin The address of the initial admin. | ||
constructor(address admin) { | ||
if (admin == address(0)) revert Errors.Governance__ZeroAddress(); | ||
_grantRole(GovernanceLib.PROTOCOL_ADMIN, admin); | ||
} | ||
|
||
/// @notice Sets the state of the protocol. | ||
/// @param newState The new state of the protocol. | ||
function setState(GovernanceLib.ProtocolState newState) external override { | ||
if (!hasRole(GovernanceLib.PROTOCOL_ADMIN, msg.sender)) revert Errors.Governance__OnlyProtocolAdmin(); | ||
if (newState == state) revert Errors.Governance__NewStateIsTheSameWithOldState(); | ||
emit StateSet(msg.sender, state, newState, block.timestamp); | ||
state = newState; | ||
} | ||
|
||
/// @notice Returns the current state of the protocol. | ||
/// @return The current state of the protocol. | ||
function getState() external view override returns (GovernanceLib.ProtocolState) { | ||
return state; | ||
} | ||
|
||
/// @notice Checks if the contract supports a specific interface. | ||
/// @param interfaceId The id of the interface. | ||
/// @return True if the contract supports the interface, false otherwise. | ||
function supportsInterface(bytes4 interfaceId) public view override returns (bool) { | ||
return (interfaceId == type(IGovernance).interfaceId || super.supportsInterface(interfaceId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// See https://github.com/storyprotocol/protocol-contracts/blob/main/StoryProtocol-AlphaTestingAgreement-17942166.3.pdf | ||
pragma solidity ^0.8.23; | ||
|
||
|
||
/// @title IGovernable | ||
/// @notice This is the interface for the Lens Protocol main governance functions. | ||
interface IGovernable { | ||
/// @notice Emitted when the governance is updated | ||
/// @param newGovernance The address of the new governance | ||
event GovernanceUpdated(address indexed newGovernance); | ||
/// @notice Sets the governance address | ||
/// @param newGovernance The address of the new governance | ||
function setGovernance(address newGovernance) external; | ||
/// @notice Returns the current governance address | ||
/// @return The address of the current governance | ||
function getGovernance() external view returns (address); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// See https://github.com/storyprotocol/protocol-contracts/blob/main/StoryProtocol-AlphaTestingAgreement-17942166.3.pdf | ||
pragma solidity ^0.8.23; | ||
|
||
import { IAccessControl } from "@openzeppelin/contracts/access/IAccessControl.sol"; | ||
import { GovernanceLib } from "contracts/lib/GovernanceLib.sol"; | ||
|
||
/// @title IGovernance | ||
/// @dev This interface defines the governance functionality for the protocol. | ||
interface IGovernance is IAccessControl { | ||
/// @notice Emitted when the protocol state is set | ||
/// @param account The address that triggered the state change | ||
/// @param prevState The previous state of the protocol | ||
/// @param newState The new state of the protocol | ||
/// @param timestamp The time when the state change occurred | ||
event StateSet( | ||
address indexed account, | ||
GovernanceLib.ProtocolState prevState, | ||
GovernanceLib.ProtocolState newState, | ||
uint256 timestamp | ||
); | ||
|
||
/// @notice Sets the state of the protocol | ||
/// @dev This function can only be called by an account with the appropriate role | ||
/// @param newState The new state to set for the protocol | ||
function setState(GovernanceLib.ProtocolState newState) external; | ||
|
||
/// @notice Returns the current state of the protocol | ||
/// @return The current state of the protocol | ||
function getState() external view returns (GovernanceLib.ProtocolState); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// See https://github.com/storyprotocol/protocol-contracts/blob/main/StoryProtocol-AlphaTestingAgreement-17942166.3.pdf | ||
pragma solidity ^0.8.23; | ||
|
||
/// @title Governance | ||
/// @dev This library provides types for Story Protocol Governance. | ||
library GovernanceLib { | ||
|
||
bytes32 public constant PROTOCOL_ADMIN = bytes32(0); | ||
|
||
/// @notice An enum containing the different states the protocol can be in. | ||
/// @param Unpaused The unpaused state. | ||
/// @param Paused The paused state. | ||
enum ProtocolState { | ||
Unpaused, | ||
Paused | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.