GovernorVotes.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (governance/extensions/GovernorVotes.sol)
  3. pragma solidity ^0.8.24;
  4. import {Governor} from "../Governor.sol";
  5. import {IVotes} from "../utils/IVotes.sol";
  6. import {IERC5805} from "../../interfaces/IERC5805.sol";
  7. import {Time} from "../../utils/types/Time.sol";
  8. /**
  9. * @dev Extension of {Governor} for voting weight extraction from an {ERC20Votes} token, or since v4.5 an {ERC721Votes}
  10. * token.
  11. */
  12. abstract contract GovernorVotes is Governor {
  13. IERC5805 private immutable _token;
  14. constructor(IVotes tokenAddress) {
  15. _token = IERC5805(address(tokenAddress));
  16. }
  17. /**
  18. * @dev The token that voting power is sourced from.
  19. */
  20. function token() public view virtual returns (IERC5805) {
  21. return _token;
  22. }
  23. /**
  24. * @dev Clock (as specified in ERC-6372) is set to match the token's clock. Fallback to block numbers if the token
  25. * does not implement ERC-6372.
  26. */
  27. function clock() public view virtual override returns (uint48) {
  28. try token().clock() returns (uint48 timepoint) {
  29. return timepoint;
  30. } catch {
  31. return Time.blockNumber();
  32. }
  33. }
  34. /**
  35. * @dev Machine-readable description of the clock as specified in ERC-6372.
  36. */
  37. // solhint-disable-next-line func-name-mixedcase
  38. function CLOCK_MODE() public view virtual override returns (string memory) {
  39. try token().CLOCK_MODE() returns (string memory clockmode) {
  40. return clockmode;
  41. } catch {
  42. return "mode=blocknumber&from=default";
  43. }
  44. }
  45. /**
  46. * Read the voting weight from the token's built in snapshot mechanism (see {Governor-_getVotes}).
  47. */
  48. function _getVotes(
  49. address account,
  50. uint256 timepoint,
  51. bytes memory /*params*/
  52. ) internal view virtual override returns (uint256) {
  53. return token().getPastVotes(account, timepoint);
  54. }
  55. }