ERC721Votes.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/extensions/ERC721Votes.sol)
  3. pragma solidity ^0.8.24;
  4. import {ERC721} from "../ERC721.sol";
  5. import {Votes} from "../../../governance/utils/Votes.sol";
  6. /**
  7. * @dev Extension of ERC-721 to support voting and delegation as implemented by {Votes}, where each individual NFT counts
  8. * as 1 vote unit.
  9. *
  10. * Tokens do not count as votes until they are delegated, because votes must be tracked which incurs an additional cost
  11. * on every transfer. Token holders can either delegate to a trusted representative who will decide how to make use of
  12. * the votes in governance decisions, or they can delegate to themselves to be their own representative.
  13. */
  14. abstract contract ERC721Votes is ERC721, Votes {
  15. /**
  16. * @dev See {ERC721-_update}. Adjusts votes when tokens are transferred.
  17. *
  18. * Emits a {IVotes-DelegateVotesChanged} event.
  19. */
  20. function _update(address to, uint256 tokenId, address auth) internal virtual override returns (address) {
  21. address previousOwner = super._update(to, tokenId, auth);
  22. _transferVotingUnits(previousOwner, to, 1);
  23. return previousOwner;
  24. }
  25. /**
  26. * @dev Returns the balance of `account`.
  27. *
  28. * WARNING: Overriding this function will likely result in incorrect vote tracking.
  29. */
  30. function _getVotingUnits(address account) internal view virtual override returns (uint256) {
  31. return balanceOf(account);
  32. }
  33. /**
  34. * @dev See {ERC721-_increaseBalance}. We need that to account tokens that were minted in batch.
  35. */
  36. function _increaseBalance(address account, uint128 amount) internal virtual override {
  37. super._increaseBalance(account, amount);
  38. _transferVotingUnits(address(0), account, amount);
  39. }
  40. }