Skip to content

Instantly share code, notes, and snippets.

@yann300
Created July 11, 2025 12:05
Show Gist options
  • Save yann300/a84f5da4cb8b3ef06f9aa02a339d0063 to your computer and use it in GitHub Desktop.
Save yann300/a84f5da4cb8b3ef06f9aa02a339d0063 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.30+commit.73712a01.js&optimize=false&runs=200&gist=
{
"query":"generate Create a Voting contract",
"response":"Source: openzeppelin-contracts/contracts/mocks/VotesMock.sol\nContent: // SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import {Votes} from \"../governance/utils/Votes.sol\"; abstract contract VotesMock is Votes { mapping(address voter => uint256) private _votingUnits; function getTotalSupply() public view returns (uint256) { return _getTotalSupply(); } function delegate(address account, address newDelegation) public { return _delegate(account, newDelegation); } function _getVotingUnits(address account) internal view override returns (uint256) { return _votingUnits[account]; } function _mint(address account, uint256 votes) internal { _votingUnits[account] += votes; _transferVotingUnits(address(0), account, votes); } function _burn(address account, uint256 votes) internal { _votingUnits[account] += votes; _transferVotingUnits(account, address(0), votes); } } abstract contract VotesTimestampMock is VotesMock { function clock() public view override returns (uint48) { return uint48(block.timestamp); } // solhint-disable-next-line func-name-mixedcase function CLOCK_MODE() public view virtual override returns (string memory) { return \"mode=timestamp\"; } }\n\nSource: openzeppelin-contracts/contracts/governance/README.adoc\nContent: = Governance [.readme-notice] NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/governance This directory includes primitives for on-chain governance. == Governor This modular system of Governor contracts allows the deployment on-chain voting protocols similar to https://compound.finance/docs/governance[Compound's Governor Alpha & Bravo] and beyond, through the ability to easily customize multiple aspects of the protocol. [TIP] ==== For a guided experience, set up your Governor contract using https://wizard.openzeppelin.com/#governor[Contracts Wizard]. For a written walkthrough, check out our guide on xref:ROOT:governance.adoc[How to set up on-chain governance]. ==== * {Governor}: The core contract that contains all the logic and primitives. It is abstract and requires choosing one of each of the modules below, or custom ones. Votes modules determine the source of voting power, and sometimes quorum number. * {GovernorVotes}: Extracts voting weight from an {IVotes} contract. * {GovernorVotesQuorumFraction}: Combines with `GovernorVotes` to set the quorum as a fraction of the total token supply. * {GovernorVotesSuperQuorumFraction}: Combines `GovernorSuperQuorum` with `GovernorVotesQuorumFraction` to set the super quorum as a fraction of the total token supply. Counting modules determine valid voting options. * {GovernorCountingSimple}: Simple voting mechanism with 3 voting options: Against, For and Abstain. * {GovernorCountingFractional}: A more modular voting system that allows a user to vote with only part of its voting power, and to split that weight arbitrarily between the 3 different options (Against, For and Abstain). * {GovernorCountingOverridable}: An extended version of `GovernorCountingSimple` which allows delegatees to override their delegates while the vote is live. Must be used in conjunction with {VotesExtended}. Timelock extensions add a delay for governance decisions to be executed. The workflow is extended to require\n\nSource: openzeppelin-contracts/contracts/mocks/VotesExtendedMock.sol\nContent: // SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import {VotesExtended} from \"../governance/utils/VotesExtended.sol\"; abstract contract VotesExtendedMock is VotesExtended { mapping(address voter => uint256) private _votingUnits; function getTotalSupply() public view returns (uint256) { return _getTotalSupply(); } function delegate(address account, address newDelegation) public { return _delegate(account, newDelegation); } function _getVotingUnits(address account) internal view override returns (uint256) { return _votingUnits[account]; } function _mint(address account, uint256 votes) internal { _votingUnits[account] += votes; _transferVotingUnits(address(0), account, votes); } function _burn(address account, uint256 votes) internal { _votingUnits[account] += votes; _transferVotingUnits(account, address(0), votes); } } abstract contract VotesExtendedTimestampMock is VotesExtendedMock { function clock() public view override returns (uint48) { return uint48(block.timestamp); } // solhint-disable-next-line func-name-mixedcase function CLOCK_MODE() public view virtual override returns (string memory) { return \"mode=timestamp\"; } }\n\nSource: openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Votes.sol\nContent: // SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/extensions/ERC20Votes.sol) pragma solidity ^0.8.20; import {ERC20} from \"../ERC20.sol\"; import {Votes} from \"../../../governance/utils/Votes.sol\"; import {Checkpoints} from \"../../../utils/structs/Checkpoints.sol\"; /** * @dev Extension of ERC-20 to support Compound-like voting and delegation. This version is more generic than Compound's, * and supports token supply up to 2^208^ - 1, while COMP is limited to 2^96^ - 1. * * NOTE: This contract does not provide interface compatibility with Compound's COMP token. * * This extension keeps a history (checkpoints) of each account's vote power. Vote power can be delegated either * by calling the {Votes-delegate} function directly, or by providing a signature to be used with {Votes-delegateBySig}. Voting * power can be queried through the public accessors {Votes-getVotes} and {Votes-getPastVotes}. * * By default, token balance does not account for voting power. This makes transfers cheaper. The downside is that it * requires users to delegate to themselves in order to activate checkpoints and have their voting power tracked. */ abstract contract ERC20Votes is ERC20, Votes { /** * @dev Total supply cap has been exceeded, introducing a risk of votes overflowing. */ error ERC20ExceededSafeSupply(uint256 increasedSupply, uint256 cap); /** * @dev Maximum token supply. Defaults to `type(uint208).max` (2^208^ - 1). * * This maximum is enforced in {_update}. It limits the total supply of the token, which is otherwise a uint256, * so that checkpoints can be stored in the Trace208 structure used by {Votes}. Increasing this value will not * remove the underlying limitation, and will cause {_update} to fail because of a math overflow in * {Votes-_transferVotingUnits}. An override could be used to further restrict the total supply (to a lower value) if * additional logic requires it. When resolving override conflicts on this function, the minimum\n\nSource: openzeppelin-contracts/contracts/mocks/governance/GovernorVoteMock.sol\nContent: // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {GovernorCountingSimple} from \"../../governance/extensions/GovernorCountingSimple.sol\"; import {GovernorVotes} from \"../../governance/extensions/GovernorVotes.sol\"; abstract contract GovernorVoteMocks is GovernorVotes, GovernorCountingSimple { function quorum(uint256) public pure override returns (uint256) { return 0; } function votingDelay() public pure override returns (uint256) { return 4; } function votingPeriod() public pure override returns (uint256) { return 16; } }\n\nSource: openzeppelin-contracts/CHANGELOG.md\nContent: # Changelog ### Breaking changes - Update minimum pragma to 0.8.24 in `SignatureChecker`, `Governor` and Governor's extensions. ([#5716](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5716)). ## 5.3.0 (2025-04-09) ### Breaking Changes - Replace `GovernorCountingOverridable.VoteReceipt` struct parameter member names `hasOverriden` and `overridenWeight` for `hasOverridden` and `overriddenWeight` respectively. #### Custom error changes - Replace `GovernorAlreadyOverridenVote` with `GovernorAlreadyOverriddenVote`. - Replace `GovernorOnlyProposer` with `GovernorUnableToCancel`. ### Changes by category #### Account - `ERC4337Utils`: Update the `hash` function to call `getUserOpHash` on the specified entrypoint and add an `ENTRYPOINT_V08` constant. ([#5614](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5614)) - `ERC7579Utils`: Add ABI decoding checks on calldata bounds within `decodeBatch`. ([#5371](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5371)) - `ERC7579Utils`: Replace `address(0)` with `address(this)` during execution for calldata compression efficiency. ([#5614](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5614)) #### Governance - `IGovernor`: Add the `getProposalId` function to the governor interface. ([#5290](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5290)) - `GovernorProposalGuardian`: Add a governance extension that defines a proposal guardian who can cancel proposals at any stage in their lifecycle. ([#5303](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5303)) - `GovernorSequentialProposalId`: Adds a `Governor` extension that sequentially numbers proposal ids instead of using the hash. ([#5290](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5290)) - `GovernorSuperQuorum`: Add a governance extension to support a super quorum. Proposals that meet the super quorum (and have a majority of for votes) advance to the `Succeeded` state before the proposal deadline. (",
"retrieved_documents":[
{
"content":"// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import {Votes} from \"../governance/utils/Votes.sol\"; abstract contract VotesMock is Votes { mapping(address voter => uint256) private _votingUnits; function getTotalSupply() public view returns (uint256) { return _getTotalSupply(); } function delegate(address account, address newDelegation) public { return _delegate(account, newDelegation); } function _getVotingUnits(address account) internal view override returns (uint256) { return _votingUnits[account]; } function _mint(address account, uint256 votes) internal { _votingUnits[account] += votes; _transferVotingUnits(address(0), account, votes); } function _burn(address account, uint256 votes) internal { _votingUnits[account] += votes; _transferVotingUnits(account, address(0), votes); } } abstract contract VotesTimestampMock is VotesMock { function clock() public view override returns (uint48) { return uint48(block.timestamp); } // solhint-disable-next-line func-name-mixedcase function CLOCK_MODE() public view virtual override returns (string memory) { return \"mode=timestamp\"; } }",
"source":"openzeppelin-contracts/contracts/mocks/VotesMock.sol"
},
{
"content":"= Governance [.readme-notice] NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/governance This directory includes primitives for on-chain governance. == Governor This modular system of Governor contracts allows the deployment on-chain voting protocols similar to https://compound.finance/docs/governance[Compound's Governor Alpha & Bravo] and beyond, through the ability to easily customize multiple aspects of the protocol. [TIP] ==== For a guided experience, set up your Governor contract using https://wizard.openzeppelin.com/#governor[Contracts Wizard]. For a written walkthrough, check out our guide on xref:ROOT:governance.adoc[How to set up on-chain governance]. ==== * {Governor}: The core contract that contains all the logic and primitives. It is abstract and requires choosing one of each of the modules below, or custom ones. Votes modules determine the source of voting power, and sometimes quorum number. * {GovernorVotes}: Extracts voting weight from an {IVotes} contract. * {GovernorVotesQuorumFraction}: Combines with `GovernorVotes` to set the quorum as a fraction of the total token supply. * {GovernorVotesSuperQuorumFraction}: Combines `GovernorSuperQuorum` with `GovernorVotesQuorumFraction` to set the super quorum as a fraction of the total token supply. Counting modules determine valid voting options. * {GovernorCountingSimple}: Simple voting mechanism with 3 voting options: Against, For and Abstain. * {GovernorCountingFractional}: A more modular voting system that allows a user to vote with only part of its voting power, and to split that weight arbitrarily between the 3 different options (Against, For and Abstain). * {GovernorCountingOverridable}: An extended version of `GovernorCountingSimple` which allows delegatees to override their delegates while the vote is live. Must be used in conjunction with {VotesExtended}. Timelock extensions add a delay for governance decisions to be executed. The workflow is extended to require",
"source":"openzeppelin-contracts/contracts/governance/README.adoc"
},
{
"content":"// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import {VotesExtended} from \"../governance/utils/VotesExtended.sol\"; abstract contract VotesExtendedMock is VotesExtended { mapping(address voter => uint256) private _votingUnits; function getTotalSupply() public view returns (uint256) { return _getTotalSupply(); } function delegate(address account, address newDelegation) public { return _delegate(account, newDelegation); } function _getVotingUnits(address account) internal view override returns (uint256) { return _votingUnits[account]; } function _mint(address account, uint256 votes) internal { _votingUnits[account] += votes; _transferVotingUnits(address(0), account, votes); } function _burn(address account, uint256 votes) internal { _votingUnits[account] += votes; _transferVotingUnits(account, address(0), votes); } } abstract contract VotesExtendedTimestampMock is VotesExtendedMock { function clock() public view override returns (uint48) { return uint48(block.timestamp); } // solhint-disable-next-line func-name-mixedcase function CLOCK_MODE() public view virtual override returns (string memory) { return \"mode=timestamp\"; } }",
"source":"openzeppelin-contracts/contracts/mocks/VotesExtendedMock.sol"
},
{
"content":"// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/extensions/ERC20Votes.sol) pragma solidity ^0.8.20; import {ERC20} from \"../ERC20.sol\"; import {Votes} from \"../../../governance/utils/Votes.sol\"; import {Checkpoints} from \"../../../utils/structs/Checkpoints.sol\"; /** * @dev Extension of ERC-20 to support Compound-like voting and delegation. This version is more generic than Compound's, * and supports token supply up to 2^208^ - 1, while COMP is limited to 2^96^ - 1. * * NOTE: This contract does not provide interface compatibility with Compound's COMP token. * * This extension keeps a history (checkpoints) of each account's vote power. Vote power can be delegated either * by calling the {Votes-delegate} function directly, or by providing a signature to be used with {Votes-delegateBySig}. Voting * power can be queried through the public accessors {Votes-getVotes} and {Votes-getPastVotes}. * * By default, token balance does not account for voting power. This makes transfers cheaper. The downside is that it * requires users to delegate to themselves in order to activate checkpoints and have their voting power tracked. */ abstract contract ERC20Votes is ERC20, Votes { /** * @dev Total supply cap has been exceeded, introducing a risk of votes overflowing. */ error ERC20ExceededSafeSupply(uint256 increasedSupply, uint256 cap); /** * @dev Maximum token supply. Defaults to `type(uint208).max` (2^208^ - 1). * * This maximum is enforced in {_update}. It limits the total supply of the token, which is otherwise a uint256, * so that checkpoints can be stored in the Trace208 structure used by {Votes}. Increasing this value will not * remove the underlying limitation, and will cause {_update} to fail because of a math overflow in * {Votes-_transferVotingUnits}. An override could be used to further restrict the total supply (to a lower value) if * additional logic requires it. When resolving override conflicts on this function, the minimum",
"source":"openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Votes.sol"
},
{
"content":"// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {GovernorCountingSimple} from \"../../governance/extensions/GovernorCountingSimple.sol\"; import {GovernorVotes} from \"../../governance/extensions/GovernorVotes.sol\"; abstract contract GovernorVoteMocks is GovernorVotes, GovernorCountingSimple { function quorum(uint256) public pure override returns (uint256) { return 0; } function votingDelay() public pure override returns (uint256) { return 4; } function votingPeriod() public pure override returns (uint256) { return 16; } }",
"source":"openzeppelin-contracts/contracts/mocks/governance/GovernorVoteMock.sol"
},
{
"content":"# Changelog ### Breaking changes - Update minimum pragma to 0.8.24 in `SignatureChecker`, `Governor` and Governor's extensions. ([#5716](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5716)). ## 5.3.0 (2025-04-09) ### Breaking Changes - Replace `GovernorCountingOverridable.VoteReceipt` struct parameter member names `hasOverriden` and `overridenWeight` for `hasOverridden` and `overriddenWeight` respectively. #### Custom error changes - Replace `GovernorAlreadyOverridenVote` with `GovernorAlreadyOverriddenVote`. - Replace `GovernorOnlyProposer` with `GovernorUnableToCancel`. ### Changes by category #### Account - `ERC4337Utils`: Update the `hash` function to call `getUserOpHash` on the specified entrypoint and add an `ENTRYPOINT_V08` constant. ([#5614](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5614)) - `ERC7579Utils`: Add ABI decoding checks on calldata bounds within `decodeBatch`. ([#5371](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5371)) - `ERC7579Utils`: Replace `address(0)` with `address(this)` during execution for calldata compression efficiency. ([#5614](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5614)) #### Governance - `IGovernor`: Add the `getProposalId` function to the governor interface. ([#5290](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5290)) - `GovernorProposalGuardian`: Add a governance extension that defines a proposal guardian who can cancel proposals at any stage in their lifecycle. ([#5303](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5303)) - `GovernorSequentialProposalId`: Adds a `Governor` extension that sequentially numbers proposal ids instead of using the hash. ([#5290](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5290)) - `GovernorSuperQuorum`: Add a governance extension to support a super quorum. Proposals that meet the super quorum (and have a majority of for votes) advance to the `Succeeded` state before the proposal deadline. (",
"source":"openzeppelin-contracts/CHANGELOG.md"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment