GovernorVotesComp.sol 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.6.0) (governance/extensions/GovernorVotesComp.sol)
  3. pragma solidity ^0.8.0;
  4. import "../Governor.sol";
  5. import "../../token/ERC20/extensions/ERC20VotesComp.sol";
  6. /**
  7. * @dev Extension of {Governor} for voting weight extraction from a Comp token.
  8. *
  9. * _Available since v4.3._
  10. */
  11. abstract contract GovernorVotesComp is Governor {
  12. ERC20VotesComp public immutable token;
  13. constructor(ERC20VotesComp token_) {
  14. token = token_;
  15. }
  16. /**
  17. * @dev Clock (as specified in EIP-6372) is set to match the token's clock. Fallback to block numbers if the token
  18. * does not implement EIP-6372.
  19. */
  20. function clock() public view virtual override returns (uint48) {
  21. try token.clock() returns (uint48 timepoint) {
  22. return timepoint;
  23. } catch {
  24. return SafeCast.toUint48(block.number);
  25. }
  26. }
  27. /**
  28. * @dev Machine-readable description of the clock as specified in EIP-6372.
  29. */
  30. // solhint-disable-next-line func-name-mixedcase
  31. function CLOCK_MODE() public view virtual override returns (string memory) {
  32. try token.CLOCK_MODE() returns (string memory clockmode) {
  33. return clockmode;
  34. } catch {
  35. return "mode=blocknumber&from=default";
  36. }
  37. }
  38. /**
  39. * Read the voting weight from the token's built-in snapshot mechanism (see {Governor-_getVotes}).
  40. */
  41. function _getVotes(
  42. address account,
  43. uint256 timepoint,
  44. bytes memory /*params*/
  45. ) internal view virtual override returns (uint256) {
  46. return token.getPriorVotes(account, timepoint);
  47. }
  48. }