Secure Smart Contract library for Solidity with upgradeable implementations of standards like ERC20 and ERC721, flexible role-based permissioning schemes, and reusable components for building custom contracts and complex decentralized systems.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Advanced DAO governance framework with flexible voting mechanisms, proposal management, timelock controllers, and vote delegation systems for decentralized decision-making.
The GovernorUpgradeable contract provides the foundation for DAO governance with proposal creation, voting, and execution.
/**
* @dev Core governance contract for DAOs
*/
abstract contract GovernorUpgradeable {
function __Governor_init(string memory name_) internal onlyInitializing;
/**
* @dev Name of the governor instance
*/
function name() external view returns (string memory);
/**
* @dev Version of the governor instance
*/
function version() external view returns (string memory);
/**
* @dev Hashing function used to build the proposal id from the proposal details
*/
function hashProposal(address[] calldata targets, uint256[] calldata values, bytes[] calldata calldatas, bytes32 descriptionHash) external pure returns (uint256);
/**
* @dev Current state of a proposal
*/
function state(uint256 proposalId) external view returns (ProposalState);
/**
* @dev Block number used to retrieve user's votes and quorum
*/
function proposalSnapshot(uint256 proposalId) external view returns (uint256);
/**
* @dev Block number at which votes close
*/
function proposalDeadline(uint256 proposalId) external view returns (uint256);
/**
* @dev Create a new proposal
*/
function propose(address[] calldata targets, uint256[] calldata values, bytes[] calldata calldatas, string calldata description) external returns (uint256);
/**
* @dev Queue a successful proposal
*/
function queue(address[] calldata targets, uint256[] calldata values, bytes[] calldata calldatas, bytes32 descriptionHash) external returns (uint256);
/**
* @dev Execute a successful proposal
*/
function execute(address[] calldata targets, uint256[] calldata values, bytes[] calldata calldatas, bytes32 descriptionHash) external payable returns (uint256);
/**
* @dev Cancel a proposal
*/
function cancel(address[] calldata targets, uint256[] calldata values, bytes[] calldata calldatas, bytes32 descriptionHash) external returns (uint256);
/**
* @dev Cast a vote on a proposal
*/
function castVote(uint256 proposalId, uint8 support) external returns (uint256);
/**
* @dev Cast a vote with a reason
*/
function castVoteWithReason(uint256 proposalId, uint8 support, string calldata reason) external returns (uint256);
/**
* @dev Cast a vote by signature
*/
function castVoteBySig(uint256 proposalId, uint8 support, address voter, bytes memory signature) external returns (uint256);
}
// Proposal states
enum ProposalState {
Pending,
Active,
Canceled,
Defeated,
Succeeded,
Queued,
Expired,
Executed
}
// Events
event ProposalCreated(uint256 proposalId, address proposer, address[] targets, uint256[] values, string[] signatures, bytes[] calldatas, uint256 voteStart, uint256 voteEnd, string description);
event ProposalQueued(uint256 proposalId, uint256 etaSeconds);
event ProposalExecuted(uint256 proposalId);
event ProposalCanceled(uint256 proposalId);
event VoteCast(address indexed voter, uint256 proposalId, uint8 support, uint256 weight, string reason);The VotesUpgradeable contract enables vote delegation and historical vote tracking for governance tokens.
/**
* @dev Vote delegation and tracking functionality
*/
abstract contract VotesUpgradeable {
function __Votes_init() internal onlyInitializing;
/**
* @dev Returns the current amount of votes that account has
*/
function getVotes(address account) external view returns (uint256);
/**
* @dev Returns the amount of votes that account had at timepoint
*/
function getPastVotes(address account, uint256 timepoint) external view returns (uint256);
/**
* @dev Returns the total supply of votes available at timepoint
*/
function getPastTotalSupply(uint256 timepoint) external view returns (uint256);
/**
* @dev Returns the delegate that account has chosen
*/
function delegates(address account) external view returns (address);
/**
* @dev Delegates votes from the sender to delegatee
*/
function delegate(address delegatee) external;
/**
* @dev Delegates votes from signer to delegatee
*/
function delegateBySig(address delegatee, uint256 nonce, uint256 expiry, uint8 v, bytes32 r, bytes32 s) external;
}
// Events
event DelegateChanged(address indexed delegator, address indexed fromDelegate, address indexed toDelegate);
event DelegateVotesChanged(address indexed delegate, uint256 previousVotes, uint256 newVotes);Timelock controller for delayed execution of governance proposals.
/**
* @dev Timelock controller for delayed execution
*/
contract TimelockControllerUpgradeable {
function __TimelockController_init(uint256 minDelay, address[] memory proposers, address[] memory executors, address admin) internal onlyInitializing;
/**
* @dev Returns the minimum delay for operations
*/
function getMinDelay() external view returns (uint256);
/**
* @dev Returns whether an id corresponds to a registered operation
*/
function isOperation(bytes32 id) external view returns (bool);
/**
* @dev Returns whether an operation is pending
*/
function isOperationPending(bytes32 id) external view returns (bool);
/**
* @dev Returns whether an operation is ready for execution
*/
function isOperationReady(bytes32 id) external view returns (bool);
/**
* @dev Returns whether an operation is done
*/
function isOperationDone(bytes32 id) external view returns (bool);
/**
* @dev Returns the timestamp at which an operation becomes ready
*/
function getTimestamp(bytes32 id) external view returns (uint256);
/**
* @dev Schedule an operation
*/
function schedule(address target, uint256 value, bytes calldata data, bytes32 predecessor, bytes32 salt, uint256 delay) external;
/**
* @dev Execute an operation
*/
function execute(address target, uint256 value, bytes calldata payload) external payable;
/**
* @dev Cancel an operation
*/
function cancel(bytes32 id) external;
}
// Role constants
bytes32 constant PROPOSER_ROLE = keccak256("PROPOSER_ROLE");
bytes32 constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE");
bytes32 constant CANCELLER_ROLE = keccak256("CANCELLER_ROLE");/**
* @dev Extension that allows the governor to update voting parameters
*/
abstract contract GovernorSettingsUpgradeable {
function __GovernorSettings_init(uint256 initialVotingDelay, uint256 initialVotingPeriod, uint256 initialProposalThreshold) internal onlyInitializing;
/**
* @dev Update the voting delay
*/
function setVotingDelay(uint256 newVotingDelay) external;
/**
* @dev Update the voting period
*/
function setVotingPeriod(uint256 newVotingPeriod) external;
/**
* @dev Update the proposal threshold
*/
function setProposalThreshold(uint256 newProposalThreshold) external;
}/**
* @dev Simple counting mechanism with Against/For/Abstain votes
*/
abstract contract GovernorCountingSimpleUpgradeable {
function __GovernorCountingSimple_init() internal onlyInitializing;
/**
* @dev Returns the vote totals for a proposal
*/
function proposalVotes(uint256 proposalId) external view returns (uint256 againstVotes, uint256 forVotes, uint256 abstainVotes);
/**
* @dev Returns whether account has cast a vote on proposalId
*/
function hasVoted(uint256 proposalId, address account) external view returns (bool);
}/**
* @dev Extension that uses a fraction of the token's total supply as quorum
*/
abstract contract GovernorVotesQuorumFractionUpgradeable {
function __GovernorVotesQuorumFraction_init(uint256 quorumNumeratorValue) internal onlyInitializing;
/**
* @dev Returns the current quorum numerator
*/
function quorumNumerator() external view returns (uint256);
/**
* @dev Returns the quorum denominator (fixed at 100)
*/
function quorumDenominator() external view returns (uint256);
/**
* @dev Changes the quorum numerator
*/
function updateQuorumNumerator(uint256 newQuorumNumerator) external;
}/**
* @dev Fractional voting mechanism that allows partial votes
*/
abstract contract GovernorCountingFractionalUpgradeable {
function __GovernorCountingFractional_init() internal onlyInitializing;
/**
* @dev Cast fractional votes (sum must equal weight)
*/
function castVoteWithReasonAndParams(uint256 proposalId, uint8 support, string calldata reason, bytes memory params) external returns (uint256);
}
/**
* @dev Overridable counting mechanism for delegation override
*/
abstract contract GovernorCountingOverridableUpgradeable {
function __GovernorCountingOverridable_init() internal onlyInitializing;
/**
* @dev Overrides delegated votes for specific proposals
*/
function castVoteWithReasonAndParams(uint256 proposalId, uint8 support, string calldata reason, bytes memory params) external returns (uint256);
}/**
* @dev Super quorum extension for critical proposals
*/
abstract contract GovernorSuperQuorumUpgradeable {
function __GovernorSuperQuorum_init() internal onlyInitializing;
/**
* @dev Returns whether proposal requires super quorum
*/
function requiresSuperQuorum(uint256 proposalId) external view returns (bool);
}
/**
* @dev Super quorum with fraction-based calculation
*/
abstract contract GovernorVotesSuperQuorumFractionUpgradeable {
function __GovernorVotesSuperQuorumFraction_init(uint256 superQuorumNumerator) internal onlyInitializing;
/**
* @dev Updates the super quorum numerator
*/
function updateSuperQuorumNumerator(uint256 newSuperQuorumNumerator) external;
}/**
* @dev Prevents late quorum changes to avoid governance attacks
*/
abstract contract GovernorPreventLateQuorumUpgradeable {
function __GovernorPreventLateQuorum_init(uint256 voteExtension) internal onlyInitializing;
/**
* @dev Returns the vote extension period
*/
function lateQuorumVoteExtension() external view returns (uint256);
/**
* @dev Updates the vote extension period
*/
function setLateQuorumVoteExtension(uint256 newVoteExtension) external;
}
/**
* @dev Adds guardian functionality for proposal protection
*/
abstract contract GovernorProposalGuardianUpgradeable {
function __GovernorProposalGuardian_init(address guardian) internal onlyInitializing;
/**
* @dev Returns the current guardian
*/
function guardian() external view returns (address);
/**
* @dev Updates the guardian address
*/
function setGuardian(address newGuardian) external;
/**
* @dev Guardian can cancel malicious proposals
*/
function cancel(address[] calldata targets, uint256[] calldata values, bytes[] calldata calldatas, bytes32 descriptionHash) external returns (uint256);
}/**
* @dev Sequential proposal ID generation
*/
abstract contract GovernorSequentialProposalIdUpgradeable {
function __GovernorSequentialProposalId_init() internal onlyInitializing;
/**
* @dev Returns the next proposal ID
*/
function nextProposalId() external view returns (uint256);
}
/**
* @dev Storage-based governance for reduced gas costs
*/
abstract contract GovernorStorageUpgradeable {
function __GovernorStorage_init() internal onlyInitializing;
/**
* @dev Stores proposal data in contract storage
*/
function getProposalData(uint256 proposalId) external view returns (address[] memory targets, uint256[] memory values, bytes[] memory calldatas);
}
/**
* @dev Keyed nonces for advanced signature schemes
*/
abstract contract GovernorNoncesKeyedUpgradeable {
function __GovernorNoncesKeyed_init() internal onlyInitializing;
/**
* @dev Returns nonce for account and key
*/
function nonces(address account, bytes32 key) external view returns (uint256);
/**
* @dev Cast vote by signature with keyed nonce
*/
function castVoteBySigWithKey(uint256 proposalId, uint8 support, address voter, bytes32 key, bytes memory signature) external returns (uint256);
}/**
* @dev Integration with AccessManager for timelock operations
*/
abstract contract GovernorTimelockAccessUpgradeable {
function __GovernorTimelockAccess_init(IAccessManager manager) internal onlyInitializing;
/**
* @dev Returns the access manager
*/
function accessManager() external view returns (IAccessManager);
/**
* @dev Updates the access manager
*/
function setAccessManager(IAccessManager newManager) external;
}import {GovernorUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/GovernorUpgradeable.sol";
import {GovernorSettingsUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorSettingsUpgradeable.sol";
import {GovernorCountingSimpleUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorCountingSimpleUpgradeable.sol";
import {GovernorVotesUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorVotesUpgradeable.sol";
import {GovernorVotesQuorumFractionUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorVotesQuorumFractionUpgradeable.sol";
contract MyGovernor is
GovernorUpgradeable,
GovernorSettingsUpgradeable,
GovernorCountingSimpleUpgradeable,
GovernorVotesUpgradeable,
GovernorVotesQuorumFractionUpgradeable
{
function initialize(
IVotes _token,
uint256 _votingDelay,
uint256 _votingPeriod,
uint256 _proposalThreshold,
uint256 _quorumPercentage
) initializer public {
__Governor_init("MyGovernor");
__GovernorSettings_init(_votingDelay, _votingPeriod, _proposalThreshold);
__GovernorCountingSimple_init();
__GovernorVotes_init(_token);
__GovernorVotesQuorumFraction_init(_quorumPercentage);
}
// Required overrides
function votingDelay() public view override(GovernorUpgradeable, GovernorSettingsUpgradeable) returns (uint256) {
return super.votingDelay();
}
function votingPeriod() public view override(GovernorUpgradeable, GovernorSettingsUpgradeable) returns (uint256) {
return super.votingPeriod();
}
function quorum(uint256 blockNumber) public view override(GovernorUpgradeable, GovernorVotesQuorumFractionUpgradeable) returns (uint256) {
return super.quorum(blockNumber);
}
}import {GovernorTimelockControlUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorTimelockControlUpgradeable.sol";
import {TimelockControllerUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/TimelockControllerUpgradeable.sol";
contract Timelocked Governor is MyGovernor, GovernorTimelockControlUpgradeable {
function initialize(
IVotes _token,
TimelockControllerUpgradeable _timelock,
uint256 _votingDelay,
uint256 _votingPeriod,
uint256 _proposalThreshold,
uint256 _quorumPercentage
) initializer public {
MyGovernor.initialize(_token, _votingDelay, _votingPeriod, _proposalThreshold, _quorumPercentage);
__GovernorTimelockControl_init(_timelock);
}
// Required overrides for timelock integration
function state(uint256 proposalId) public view override(GovernorUpgradeable, GovernorTimelockControlUpgradeable) returns (ProposalState) {
return super.state(proposalId);
}
function proposalNeedsQueuing(uint256 proposalId) public view override(GovernorUpgradeable, GovernorTimelockControlUpgradeable) returns (bool) {
return super.proposalNeedsQueuing(proposalId);
}
function _queueOperations(uint256 proposalId, address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash) internal override(GovernorUpgradeable, GovernorTimelockControlUpgradeable) returns (uint48) {
return super._queueOperations(proposalId, targets, values, calldatas, descriptionHash);
}
function _executeOperations(uint256 proposalId, address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash) internal override(GovernorUpgradeable, GovernorTimelockControlUpgradeable) {
super._executeOperations(proposalId, targets, values, calldatas, descriptionHash);
}
function _cancel(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash) internal override(GovernorUpgradeable, GovernorTimelockControlUpgradeable) returns (uint256) {
return super._cancel(targets, values, calldatas, descriptionHash);
}
}Install with Tessl CLI
npx tessl i tessl/npm-openzeppelin--contracts-upgradeable