ERC2981Upgradeable.sol 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.0) (token/common/ERC2981.sol)
  3. pragma solidity ^0.8.0;
  4. import "../../interfaces/IERC2981Upgradeable.sol";
  5. import "../../utils/introspection/ERC165Upgradeable.sol";
  6. import "../../proxy/utils/Initializable.sol";
  7. /**
  8. * @dev Implementation of the NFT Royalty Standard, a standardized way to retrieve royalty payment information.
  9. *
  10. * Royalty information can be specified globally for all token ids via {_setDefaultRoyalty}, and/or individually for
  11. * specific token ids via {_setTokenRoyalty}. The latter takes precedence over the first.
  12. *
  13. * Royalty is specified as a fraction of sale price. {_feeDenominator} is overridable but defaults to 10000, meaning the
  14. * fee is specified in basis points by default.
  15. *
  16. * IMPORTANT: ERC-2981 only specifies a way to signal royalty information and does not enforce its payment. See
  17. * https://eips.ethereum.org/EIPS/eip-2981#optional-royalty-payments[Rationale] in the EIP. Marketplaces are expected to
  18. * voluntarily pay royalties together with sales, but note that this standard is not yet widely supported.
  19. *
  20. * _Available since v4.5._
  21. */
  22. abstract contract ERC2981Upgradeable is Initializable, IERC2981Upgradeable, ERC165Upgradeable {
  23. function __ERC2981_init() internal onlyInitializing {
  24. }
  25. function __ERC2981_init_unchained() internal onlyInitializing {
  26. }
  27. struct RoyaltyInfo {
  28. address receiver;
  29. uint96 royaltyFraction;
  30. }
  31. RoyaltyInfo private _defaultRoyaltyInfo;
  32. mapping(uint256 => RoyaltyInfo) private _tokenRoyaltyInfo;
  33. /**
  34. * @dev See {IERC165-supportsInterface}.
  35. */
  36. function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165Upgradeable, ERC165Upgradeable) returns (bool) {
  37. return interfaceId == type(IERC2981Upgradeable).interfaceId || super.supportsInterface(interfaceId);
  38. }
  39. /**
  40. * @inheritdoc IERC2981Upgradeable
  41. */
  42. function royaltyInfo(uint256 _tokenId, uint256 _salePrice) external view override returns (address, uint256) {
  43. RoyaltyInfo memory royalty = _tokenRoyaltyInfo[_tokenId];
  44. if (royalty.receiver == address(0)) {
  45. royalty = _defaultRoyaltyInfo;
  46. }
  47. uint256 royaltyAmount = (_salePrice * royalty.royaltyFraction) / _feeDenominator();
  48. return (royalty.receiver, royaltyAmount);
  49. }
  50. /**
  51. * @dev The denominator with which to interpret the fee set in {_setTokenRoyalty} and {_setDefaultRoyalty} as a
  52. * fraction of the sale price. Defaults to 10000 so fees are expressed in basis points, but may be customized by an
  53. * override.
  54. */
  55. function _feeDenominator() internal pure virtual returns (uint96) {
  56. return 10000;
  57. }
  58. /**
  59. * @dev Sets the royalty information that all ids in this contract will default to.
  60. *
  61. * Requirements:
  62. *
  63. * - `receiver` cannot be the zero address.
  64. * - `feeNumerator` cannot be greater than the fee denominator.
  65. */
  66. function _setDefaultRoyalty(address receiver, uint96 feeNumerator) internal virtual {
  67. require(feeNumerator <= _feeDenominator(), "ERC2981: royalty fee will exceed salePrice");
  68. require(receiver != address(0), "ERC2981: invalid receiver");
  69. _defaultRoyaltyInfo = RoyaltyInfo(receiver, feeNumerator);
  70. }
  71. /**
  72. * @dev Removes default royalty information.
  73. */
  74. function _deleteDefaultRoyalty() internal virtual {
  75. delete _defaultRoyaltyInfo;
  76. }
  77. /**
  78. * @dev Sets the royalty information for a specific token id, overriding the global default.
  79. *
  80. * Requirements:
  81. *
  82. * - `tokenId` must be already minted.
  83. * - `receiver` cannot be the zero address.
  84. * - `feeNumerator` cannot be greater than the fee denominator.
  85. */
  86. function _setTokenRoyalty(
  87. uint256 tokenId,
  88. address receiver,
  89. uint96 feeNumerator
  90. ) internal virtual {
  91. require(feeNumerator <= _feeDenominator(), "ERC2981: royalty fee will exceed salePrice");
  92. require(receiver != address(0), "ERC2981: Invalid parameters");
  93. _tokenRoyaltyInfo[tokenId] = RoyaltyInfo(receiver, feeNumerator);
  94. }
  95. /**
  96. * @dev Resets royalty information for the token id back to the global default.
  97. */
  98. function _resetTokenRoyalty(uint256 tokenId) internal virtual {
  99. delete _tokenRoyaltyInfo[tokenId];
  100. }
  101. /**
  102. * This empty reserved space is put in place to allow future versions to add new
  103. * variables without shifting down storage in the inheritance chain.
  104. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  105. */
  106. uint256[48] private __gap;
  107. }