Skip to content
Lattice

GovernorLib

Git Source

Title: GovernorLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/governance/Governor.sol)

Library implementing the full on-chain governance lifecycle: proposal creation, voting (For/Against/Abstain with quorum), optional timelock queuing, execution, and cancellation. Includes EIP-712 castVoteBySig support.

Three-layer pattern: this library holds all logic and state. The stateless Governor facet delegates every external call here.

Constants

QUORUM_DENOMINATOR

Quorum denominator: quorum = (totalSupply * quorumNumerator) / QUORUM_DENOMINATOR.

uint256 internal constant QUORUM_DENOMINATOR = 100;

GRACE_PERIOD

How long a queued proposal stays executable after its ETA. Past eta + GRACE_PERIOD the proposal is Expired, which is terminal: state reports it, execute refuses it, and checkTimelockOperation makes a colocated timelock refuse to run its operation directly.

uint256 internal constant GRACE_PERIOD = 14 days;

Functions

governorStorage

Returns a storage reference to the GovernorStorage struct.

function governorStorage() internal pure returns (GovernorStorage storage $);

Returns

NameTypeDescription
$GovernorStorage

registerInterface

Registers support for the IGovernor interface via ERC-165.

function registerInterface() internal;

__Governor_init

Initializes the Governor module.

Must be called between preInitializer and postInitializer.

function __Governor_init(
        string memory name_,
        address token_,
        address timelock_,
        uint48 votingDelay_,
        uint32 votingPeriod_,
        uint256 proposalThreshold_,
        uint256 quorumNumerator_
    ) internal;

Parameters

NameTypeDescription
name_stringThe human-readable name of the governor.
token_addressThe IVotes-compatible token used for voting power.
timelock_addressOptional TimelockController address (address(0) for direct execution).
votingDelay_uint48Delay in clock units before voting starts (can be 0).
votingPeriod_uint32Duration in clock units of the voting window (must be > 0).
proposalThreshold_uint256Minimum voting power required to create a proposal.
quorumNumerator_uint256Quorum as a percentage of total supply (0–100).

name

Returns the human-readable name of this governor.

function name() internal view returns (string memory);

Returns

NameTypeDescription
<none>string

version

Returns the version string ("1").

function version() internal pure returns (string memory);

Returns

NameTypeDescription
<none>string

token

Returns the IVotes token address.

function token() internal view returns (address);

Returns

NameTypeDescription
<none>address

timelock

Returns the configured TimelockController address (address(0) if none).

function timelock() internal view returns (address);

Returns

NameTypeDescription
<none>address

clock

Returns the current clock value, delegating to the token's EIP-6372 clock. Falls back to block.timestamp if the token does not implement IERC6372.

function clock() internal view returns (uint48);

Returns

NameTypeDescription
<none>uint48

CLOCK_MODE

Returns the machine-readable clock mode string, delegating to the token's CLOCK_MODE. Falls back to "mode=timestamp" if the token does not implement IERC6372.

function CLOCK_MODE() internal view returns (string memory);

Returns

NameTypeDescription
<none>string

COUNTING_MODE

Returns the counting mode string (Bravo: For/Against/Abstain).

function COUNTING_MODE() internal pure returns (string memory);

Returns

NameTypeDescription
<none>string

votingDelay

Returns the voting delay in clock units.

function votingDelay() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

votingPeriod

Returns the voting period duration in clock units.

function votingPeriod() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

proposalThreshold

Returns the proposal threshold (minimum voting power to propose).

function proposalThreshold() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

quorumNumerator

Returns the current quorum numerator.

function quorumNumerator() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

quorumNumeratorAt

Returns the quorum numerator at a given timepoint.

Simplified: returns the current numerator (no historical tracking).

function quorumNumeratorAt(
        uint256 /* timepoint */
    )
        internal
        view
        returns (uint256);

Parameters

NameTypeDescription
_uint256

Returns

NameTypeDescription
<none>uint256

quorumDenominator

Returns the quorum denominator (100).

function quorumDenominator() internal pure returns (uint256);

Returns

NameTypeDescription
<none>uint256

quorum

Returns the quorum (in token units) required at a given past timepoint.

function quorum(uint256 timepoint) internal view returns (uint256);

Parameters

NameTypeDescription
timepointuint256The past clock value to query.

Returns

NameTypeDescription
<none>uint256

getVotes

Returns the voting power of an account at a past timepoint.

function getVotes(address account, uint256 timepoint) internal view returns (uint256);

Parameters

NameTypeDescription
accountaddress
timepointuint256

Returns

NameTypeDescription
<none>uint256

hashProposal

Computes the proposal ID from targets, values, calldatas, and description hash.

function hashProposal(
        address[] memory targets,
        uint256[] memory values,
        bytes[] memory calldatas,
        bytes32 descriptionHash
    ) internal pure returns (uint256);

Parameters

NameTypeDescription
targetsaddress[]
valuesuint256[]
calldatasbytes[]
descriptionHashbytes32

Returns

NameTypeDescription
<none>uint256

proposalSnapshot

Returns the clock value at which voting starts for a proposal.

function proposalSnapshot(uint256 proposalId) internal view returns (uint256);

Parameters

NameTypeDescription
proposalIduint256

Returns

NameTypeDescription
<none>uint256

proposalDeadline

Returns the clock value at which voting ends for a proposal.

function proposalDeadline(uint256 proposalId) internal view returns (uint256);

Parameters

NameTypeDescription
proposalIduint256

Returns

NameTypeDescription
<none>uint256

proposalProposer

Returns the address that created a proposal.

function proposalProposer(uint256 proposalId) internal view returns (address);

Parameters

NameTypeDescription
proposalIduint256

Returns

NameTypeDescription
<none>address

proposalEta

Returns the timelock ETA for a proposal (0 if not queued).

function proposalEta(uint256 proposalId) internal view returns (uint256);

Parameters

NameTypeDescription
proposalIduint256

Returns

NameTypeDescription
<none>uint256

proposalVotes

Returns the vote totals for a proposal.

function proposalVotes(uint256 proposalId)
        internal
        view
        returns (uint256 againstVotes, uint256 forVotes, uint256 abstainVotes);

Parameters

NameTypeDescription
proposalIduint256

Returns

NameTypeDescription
againstVotesuint256
forVotesuint256
abstainVotesuint256

hasVoted

Returns whether an account has voted on a proposal.

function hasVoted(uint256 proposalId, address account) internal view returns (bool);

Parameters

NameTypeDescription
proposalIduint256
accountaddress

Returns

NameTypeDescription
<none>bool

state

Returns the current state of a proposal.

State transitions (ordered by finality): Executed → Canceled → Pending → Active → Defeated → Succeeded → Queued → Expired Expired (queued, still pending in the timelock, and past eta + GRACE_PERIOD) is terminal: execute refuses it, and a timelock in the same diamond refuses its operation (see checkTimelockOperation).

function state(uint256 proposalId) internal view returns (IGovernor.ProposalState);

Parameters

NameTypeDescription
proposalIduint256

Returns

NameTypeDescription
<none>IGovernor.ProposalState

propose

Creates a new proposal.

function propose(
        address[] memory targets,
        uint256[] memory values,
        bytes[] memory calldatas,
        string memory description
    ) internal returns (uint256);

Parameters

NameTypeDescription
targetsaddress[]Contract addresses to call.
valuesuint256[]ETH values for each call.
calldatasbytes[]Encoded calldata for each call.
descriptionstringHuman-readable description of the proposal.

Returns

NameTypeDescription
<none>uint256proposalId The unique ID of the new proposal.

_storeAndEmitProposal

Stores proposal core data and emits ProposalCreated. Extracted to avoid stack-too-deep.

function _storeAndEmitProposal(
        uint256 proposalId,
        address proposer,
        address[] memory targets,
        uint256[] memory values,
        bytes[] memory calldatas,
        string memory description
    ) private;

Parameters

NameTypeDescription
proposalIduint256
proposeraddress
targetsaddress[]
valuesuint256[]
calldatasbytes[]
descriptionstring

queue

Queue a succeeded proposal in the TimelockController.

function queue(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
        internal
        returns (uint256);

Parameters

NameTypeDescription
targetsaddress[]Contract addresses to call.
valuesuint256[]ETH values for each call.
calldatasbytes[]Encoded calldata for each call.
descriptionHashbytes32keccak256 hash of the description string.

Returns

NameTypeDescription
<none>uint256proposalId The proposal ID.

execute

Execute a proposal (succeeded with no timelock, or queued+ready with timelock and within the grace period).

function execute(
        address[] memory targets,
        uint256[] memory values,
        bytes[] memory calldatas,
        bytes32 descriptionHash
    ) internal returns (uint256);

Parameters

NameTypeDescription
targetsaddress[]Contract addresses to call.
valuesuint256[]ETH values for each call.
calldatasbytes[]Encoded calldata for each call.
descriptionHashbytes32keccak256 hash of the description string.

Returns

NameTypeDescription
<none>uint256proposalId The proposal ID.

cancel

Cancel a proposal. Only its proposer can cancel, and only while it is Pending (before voting starts), as in OpenZeppelin's Governor. Once voting has started, no single account can veto the proposal through the Governor; a queued operation can still be cancelled in the timelock by a CANCELLER_ROLE holder, after which state reports Canceled.

function cancel(
        address[] memory targets,
        uint256[] memory values,
        bytes[] memory calldatas,
        bytes32 descriptionHash
    ) internal returns (uint256);

Parameters

NameTypeDescription
targetsaddress[]Contract addresses for the proposal.
valuesuint256[]ETH values for the proposal.
calldatasbytes[]Calldata for the proposal.
descriptionHashbytes32keccak256 hash of the description string.

Returns

NameTypeDescription
<none>uint256proposalId The proposal ID.

castVote

Cast a vote on an active proposal.

function castVote(uint256 proposalId, uint8 support) internal returns (uint256);

Parameters

NameTypeDescription
proposalIduint256The proposal to vote on.
supportuint8Vote type: 0=Against, 1=For, 2=Abstain.

Returns

NameTypeDescription
<none>uint256weight The voter's voting power at the proposal snapshot.

castVoteWithReason

Cast a vote with a reason string.

function castVoteWithReason(uint256 proposalId, uint8 support, string calldata reason) internal returns (uint256);

Parameters

NameTypeDescription
proposalIduint256The proposal to vote on.
supportuint8Vote type: 0=Against, 1=For, 2=Abstain.
reasonstringHuman-readable reason for the vote.

Returns

NameTypeDescription
<none>uint256weight The voter's voting power at the proposal snapshot.

castVoteWithReasonAndParams

Cast a vote with a reason string and extra params.

function castVoteWithReasonAndParams(
        uint256 proposalId,
        uint8 support,
        string calldata reason,
        bytes calldata params
    ) internal returns (uint256);

Parameters

NameTypeDescription
proposalIduint256The proposal to vote on.
supportuint8Vote type: 0=Against, 1=For, 2=Abstain.
reasonstringHuman-readable reason for the vote.
paramsbytesExtra encoded parameters (e.g. fractional voting split).

Returns

NameTypeDescription
<none>uint256weight The voter's voting power at the proposal snapshot.

castVoteBySig

Cast a vote via an EIP-712 signature.

function castVoteBySig(uint256 proposalId, uint8 support, address voter, bytes memory signature)
        internal
        returns (uint256);

Parameters

NameTypeDescription
proposalIduint256The proposal to vote on.
supportuint8Vote type: 0=Against, 1=For, 2=Abstain.
voteraddressThe address that signed the vote.
signaturebytesEIP-712 signature over the Ballot struct.

Returns

NameTypeDescription
<none>uint256weight The voter's voting power at the proposal snapshot.

setVotingDelay

Update the voting delay. Callable only via a passed governance proposal.

function setVotingDelay(uint48 newVotingDelay) internal;

Parameters

NameTypeDescription
newVotingDelayuint48The new voting delay in clock units.

setVotingPeriod

Update the voting period. Callable only via a passed governance proposal.

function setVotingPeriod(uint32 newVotingPeriod) internal;

Parameters

NameTypeDescription
newVotingPerioduint32The new voting period in clock units (must be > 0).

setProposalThreshold

Update the proposal threshold. Callable only via a passed governance proposal.

function setProposalThreshold(uint256 newProposalThreshold) internal;

Parameters

NameTypeDescription
newProposalThresholduint256The new minimum voting power to propose.

updateQuorumNumerator

Update the quorum numerator. Callable only via a passed governance proposal.

function updateQuorumNumerator(uint256 newQuorumNumerator) internal;

Parameters

NameTypeDescription
newQuorumNumeratoruint256New quorum percentage (0–100).

updateTimelock

Update the timelock address. Callable only via a passed governance proposal.

function updateTimelock(address newTimelock) internal;

Parameters

NameTypeDescription
newTimelockaddressThe new TimelockController address (or address(0) to disable).

checkTimelockOperation

Reverts if timelockId is the operation of a proposal this governor queued and the proposal is past its grace period (Expired).

Called by TimelockControllerLib before it runs an operation, so the grace is enforced even when the timelock's executor role is open and the operation is run directly, bypassing execute. It reads this diamond's Governor storage, so it only takes effect when the Governor and TimelockController facets share a diamond (as in GovernedVaultInit). An operation the governor did not queue maps to proposal 0 and passes. A TimelockController deployed apart from its governor cannot see the grace; grant its EXECUTOR_ROLE to the governor alone so every execution goes through execute.

function checkTimelockOperation(bytes32 timelockId) internal view;

Parameters

NameTypeDescription
timelockIdbytes32The timelock operation id about to be executed.

_castVote

Internal implementation for all vote casting paths.

function _castVote(uint256 proposalId, address voter, uint8 support, string memory reason, bytes memory params)
        internal
        returns (uint256 weight);

Parameters

NameTypeDescription
proposalIduint256
voteraddress
supportuint8
reasonstring
paramsbytes

Returns

NameTypeDescription
weightuint256

_voteSucceeded

Returns true if forVotes > againstVotes.

function _voteSucceeded(uint256 proposalId) internal view returns (bool);

Parameters

NameTypeDescription
proposalIduint256

Returns

NameTypeDescription
<none>bool

_quorumReached

Returns true if forVotes + abstainVotes >= quorum(snapshot).

function _quorumReached(uint256 proposalId) internal view returns (bool);

Parameters

NameTypeDescription
proposalIduint256

Returns

NameTypeDescription
<none>bool

_onlyGovernance

Reverts unless the caller is the configured executor: the timelock when one is set, otherwise this governor (direct execution). When a timelock relays the call, the exact calldata must have been authorized by a governor execute (see the _governanceCall bookkeeping) — this prevents the timelock from being used to invoke governor admin functions outside a passed proposal.

function _onlyGovernance() internal;

_executor

Returns the address responsible for executing passed proposals: the configured timelock if set, otherwise this governor.

function _executor() internal view returns (address);

Returns

NameTypeDescription
<none>address

_encodeStateBitmap

Encodes a single ProposalState as a 1-bit bitmask for error reporting.

function _encodeStateBitmap(IGovernor.ProposalState proposalState) internal pure returns (bytes32);

Parameters

NameTypeDescription
proposalStateIGovernor.ProposalState

Returns

NameTypeDescription
<none>bytes32