GovernorVotes.sol 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 private immutable _token;
  13. constructor(IVotes tokenAddress) {
  14. _token = IERC5805(address(tokenAddress));
  15. }
  16. /**
  17. * @dev The token that voting power is sourced from.
  18. */
  19. function token() public view virtual returns (IERC5805) {
  20. return _token;
  21. }
  22. /**
  23. * @dev Clock (as specified in EIP-6372) is set to match the token's clock. Fallback to block numbers if the token
  24. * does not implement EIP-6372.
  25. */
  26. function clock() public view virtual override returns (uint48) {
  27. try token().clock() returns (uint48 timepoint) {
  28. return timepoint;
  29. } catch {
  30. return SafeCast.toUint48(block.number);
  31. }
  32. }
  33. /**
  34. * @dev Machine-readable description of the clock as specified in EIP-6372.
  35. */
  36. // solhint-disable-next-line func-name-mixedcase
  37. function CLOCK_MODE() public view virtual override returns (string memory) {
  38. try token().CLOCK_MODE() returns (string memory clockmode) {
  39. return clockmode;
  40. } catch {
  41. return "mode=blocknumber&from=default";
  42. }
  43. }
  44. /**
  45. * Read the voting weight from the token's built in snapshot mechanism (see {Governor-_getVotes}).
  46. */
  47. function _getVotes(
  48. address account,
  49. uint256 timepoint,
  50. bytes memory /*params*/
  51. ) internal view virtual override returns (uint256) {
  52. return token().getPastVotes(account, timepoint);
  53. }
  54. }