GovernorVotes.sol 1.8 KB

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