ERC2981.sol 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (token/common/ERC2981.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC2981} from "../../interfaces/IERC2981.sol";
  5. import {IERC165, ERC165} from "../../utils/introspection/ERC165.sol";
  6. /**
  7. * @dev Implementation of the NFT Royalty Standard, a standardized way to retrieve royalty payment information.
  8. *
  9. * Royalty information can be specified globally for all token ids via {_setDefaultRoyalty}, and/or individually for
  10. * specific token ids via {_setTokenRoyalty}. The latter takes precedence over the first.
  11. *
  12. * Royalty is specified as a fraction of sale price. {_feeDenominator} is overridable but defaults to 10000, meaning the
  13. * fee is specified in basis points by default.
  14. *
  15. * IMPORTANT: ERC-2981 only specifies a way to signal royalty information and does not enforce its payment. See
  16. * https://eips.ethereum.org/EIPS/eip-2981#optional-royalty-payments[Rationale] in the ERC. Marketplaces are expected to
  17. * voluntarily pay royalties together with sales, but note that this standard is not yet widely supported.
  18. */
  19. abstract contract ERC2981 is IERC2981, ERC165 {
  20. struct RoyaltyInfo {
  21. address receiver;
  22. uint96 royaltyFraction;
  23. }
  24. RoyaltyInfo private _defaultRoyaltyInfo;
  25. mapping(uint256 tokenId => RoyaltyInfo) private _tokenRoyaltyInfo;
  26. /**
  27. * @dev The default royalty set is invalid (eg. (numerator / denominator) >= 1).
  28. */
  29. error ERC2981InvalidDefaultRoyalty(uint256 numerator, uint256 denominator);
  30. /**
  31. * @dev The default royalty receiver is invalid.
  32. */
  33. error ERC2981InvalidDefaultRoyaltyReceiver(address receiver);
  34. /**
  35. * @dev The royalty set for an specific `tokenId` is invalid (eg. (numerator / denominator) >= 1).
  36. */
  37. error ERC2981InvalidTokenRoyalty(uint256 tokenId, uint256 numerator, uint256 denominator);
  38. /**
  39. * @dev The royalty receiver for `tokenId` is invalid.
  40. */
  41. error ERC2981InvalidTokenRoyaltyReceiver(uint256 tokenId, address receiver);
  42. /**
  43. * @dev See {IERC165-supportsInterface}.
  44. */
  45. function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC165) returns (bool) {
  46. return interfaceId == type(IERC2981).interfaceId || super.supportsInterface(interfaceId);
  47. }
  48. /**
  49. * @inheritdoc IERC2981
  50. */
  51. function royaltyInfo(
  52. uint256 tokenId,
  53. uint256 salePrice
  54. ) public view virtual returns (address receiver, uint256 amount) {
  55. RoyaltyInfo storage _royaltyInfo = _tokenRoyaltyInfo[tokenId];
  56. address royaltyReceiver = _royaltyInfo.receiver;
  57. uint96 royaltyFraction = _royaltyInfo.royaltyFraction;
  58. if (royaltyReceiver == address(0)) {
  59. royaltyReceiver = _defaultRoyaltyInfo.receiver;
  60. royaltyFraction = _defaultRoyaltyInfo.royaltyFraction;
  61. }
  62. uint256 royaltyAmount = (salePrice * royaltyFraction) / _feeDenominator();
  63. return (royaltyReceiver, royaltyAmount);
  64. }
  65. /**
  66. * @dev The denominator with which to interpret the fee set in {_setTokenRoyalty} and {_setDefaultRoyalty} as a
  67. * fraction of the sale price. Defaults to 10000 so fees are expressed in basis points, but may be customized by an
  68. * override.
  69. */
  70. function _feeDenominator() internal pure virtual returns (uint96) {
  71. return 10000;
  72. }
  73. /**
  74. * @dev Sets the royalty information that all ids in this contract will default to.
  75. *
  76. * Requirements:
  77. *
  78. * - `receiver` cannot be the zero address.
  79. * - `feeNumerator` cannot be greater than the fee denominator.
  80. */
  81. function _setDefaultRoyalty(address receiver, uint96 feeNumerator) internal virtual {
  82. uint256 denominator = _feeDenominator();
  83. if (feeNumerator > denominator) {
  84. // Royalty fee will exceed the sale price
  85. revert ERC2981InvalidDefaultRoyalty(feeNumerator, denominator);
  86. }
  87. if (receiver == address(0)) {
  88. revert ERC2981InvalidDefaultRoyaltyReceiver(address(0));
  89. }
  90. _defaultRoyaltyInfo = RoyaltyInfo(receiver, feeNumerator);
  91. }
  92. /**
  93. * @dev Removes default royalty information.
  94. */
  95. function _deleteDefaultRoyalty() internal virtual {
  96. delete _defaultRoyaltyInfo;
  97. }
  98. /**
  99. * @dev Sets the royalty information for a specific token id, overriding the global default.
  100. *
  101. * Requirements:
  102. *
  103. * - `receiver` cannot be the zero address.
  104. * - `feeNumerator` cannot be greater than the fee denominator.
  105. */
  106. function _setTokenRoyalty(uint256 tokenId, address receiver, uint96 feeNumerator) internal virtual {
  107. uint256 denominator = _feeDenominator();
  108. if (feeNumerator > denominator) {
  109. // Royalty fee will exceed the sale price
  110. revert ERC2981InvalidTokenRoyalty(tokenId, feeNumerator, denominator);
  111. }
  112. if (receiver == address(0)) {
  113. revert ERC2981InvalidTokenRoyaltyReceiver(tokenId, address(0));
  114. }
  115. _tokenRoyaltyInfo[tokenId] = RoyaltyInfo(receiver, feeNumerator);
  116. }
  117. /**
  118. * @dev Resets royalty information for the token id back to the global default.
  119. */
  120. function _resetTokenRoyalty(uint256 tokenId) internal virtual {
  121. delete _tokenRoyaltyInfo[tokenId];
  122. }
  123. }