VotesExtended.sol 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Checkpoints} from "../../utils/structs/Checkpoints.sol";
  4. import {Votes} from "./Votes.sol";
  5. import {SafeCast} from "../../utils/math/SafeCast.sol";
  6. /**
  7. * @dev Extension of {Votes} that adds checkpoints for delegations and balances.
  8. *
  9. * WARNING: While this contract extends {Votes}, valid uses of {Votes} may not be compatible with
  10. * {VotesExtended} without additional considerations. This implementation of {_transferVotingUnits} must
  11. * run AFTER the voting weight movement is registered, such that it is reflected on {_getVotingUnits}.
  12. *
  13. * Said differently, {VotesExtended} MUST be integrated in a way that calls {_transferVotingUnits} AFTER the
  14. * asset transfer is registered and balances are updated:
  15. *
  16. * ```solidity
  17. * contract VotingToken is Token, VotesExtended {
  18. * function transfer(address from, address to, uint256 tokenId) public override {
  19. * super.transfer(from, to, tokenId); // <- Perform the transfer first ...
  20. * _transferVotingUnits(from, to, 1); // <- ... then call _transferVotingUnits.
  21. * }
  22. *
  23. * function _getVotingUnits(address account) internal view override returns (uint256) {
  24. * return balanceOf(account);
  25. * }
  26. * }
  27. * ```
  28. *
  29. * {ERC20Votes} and {ERC721Votes} follow this pattern and are thus safe to use with {VotesExtended}.
  30. */
  31. abstract contract VotesExtended is Votes {
  32. using Checkpoints for Checkpoints.Trace160;
  33. using Checkpoints for Checkpoints.Trace208;
  34. mapping(address delegator => Checkpoints.Trace160) private _userDelegationCheckpoints;
  35. mapping(address account => Checkpoints.Trace208) private _userVotingUnitsCheckpoints;
  36. /**
  37. * @dev Returns the delegate of an `account` at a specific moment in the past. If the `clock()` is
  38. * configured to use block numbers, this will return the value at the end of the corresponding block.
  39. *
  40. * Requirements:
  41. *
  42. * - `timepoint` must be in the past. If operating using block numbers, the block must be already mined.
  43. */
  44. function getPastDelegate(address account, uint256 timepoint) public view virtual returns (address) {
  45. return address(_userDelegationCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint)));
  46. }
  47. /**
  48. * @dev Returns the `balanceOf` of an `account` at a specific moment in the past. If the `clock()` is
  49. * configured to use block numbers, this will return the value at the end of the corresponding block.
  50. *
  51. * Requirements:
  52. *
  53. * - `timepoint` must be in the past. If operating using block numbers, the block must be already mined.
  54. */
  55. function getPastBalanceOf(address account, uint256 timepoint) public view virtual returns (uint256) {
  56. return _userVotingUnitsCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint));
  57. }
  58. /// @inheritdoc Votes
  59. function _delegate(address account, address delegatee) internal virtual override {
  60. super._delegate(account, delegatee);
  61. _userDelegationCheckpoints[account].push(clock(), uint160(delegatee));
  62. }
  63. /// @inheritdoc Votes
  64. function _transferVotingUnits(address from, address to, uint256 amount) internal virtual override {
  65. super._transferVotingUnits(from, to, amount);
  66. if (from != to) {
  67. if (from != address(0)) {
  68. _userVotingUnitsCheckpoints[from].push(clock(), SafeCast.toUint208(_getVotingUnits(from)));
  69. }
  70. if (to != address(0)) {
  71. _userVotingUnitsCheckpoints[to].push(clock(), SafeCast.toUint208(_getVotingUnits(to)));
  72. }
  73. }
  74. }
  75. }