GovernorVotes.sol 2.0 KB

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