GovernorVotes.sol 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (governance/extensions/GovernorVotes.sol)
  3. pragma solidity ^0.8.20;
  4. import {Governor} from "../Governor.sol";
  5. import {IVotes} from "../utils/IVotes.sol";
  6. import {IERC5805} from "../../interfaces/IERC5805.sol";
  7. import {SafeCast} from "../../utils/math/SafeCast.sol";
  8. /**
  9. * @dev Extension of {Governor} for voting weight extraction from an {ERC20Votes} token, or since v4.5 an {ERC721Votes} token.
  10. */
  11. abstract contract GovernorVotes is Governor {
  12. IERC5805 public immutable token;
  13. constructor(IVotes tokenAddress) {
  14. token = IERC5805(address(tokenAddress));
  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.getPastVotes(account, timepoint);
  47. }
  48. }