GovernorVotes.sol 1.8 KB

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