ERC2981Upgradeable.sol 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.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)
  43. external
  44. view
  45. virtual
  46. override
  47. returns (address, uint256)
  48. {
  49. RoyaltyInfo memory royalty = _tokenRoyaltyInfo[_tokenId];
  50. if (royalty.receiver == address(0)) {
  51. royalty = _defaultRoyaltyInfo;
  52. }
  53. uint256 royaltyAmount = (_salePrice * royalty.royaltyFraction) / _feeDenominator();
  54. return (royalty.receiver, royaltyAmount);
  55. }
  56. /**
  57. * @dev The denominator with which to interpret the fee set in {_setTokenRoyalty} and {_setDefaultRoyalty} as a
  58. * fraction of the sale price. Defaults to 10000 so fees are expressed in basis points, but may be customized by an
  59. * override.
  60. */
  61. function _feeDenominator() internal pure virtual returns (uint96) {
  62. return 10000;
  63. }
  64. /**
  65. * @dev Sets the royalty information that all ids in this contract will default to.
  66. *
  67. * Requirements:
  68. *
  69. * - `receiver` cannot be the zero address.
  70. * - `feeNumerator` cannot be greater than the fee denominator.
  71. */
  72. function _setDefaultRoyalty(address receiver, uint96 feeNumerator) internal virtual {
  73. require(feeNumerator <= _feeDenominator(), "ERC2981: royalty fee will exceed salePrice");
  74. require(receiver != address(0), "ERC2981: invalid receiver");
  75. _defaultRoyaltyInfo = RoyaltyInfo(receiver, feeNumerator);
  76. }
  77. /**
  78. * @dev Removes default royalty information.
  79. */
  80. function _deleteDefaultRoyalty() internal virtual {
  81. delete _defaultRoyaltyInfo;
  82. }
  83. /**
  84. * @dev Sets the royalty information for a specific token id, overriding the global default.
  85. *
  86. * Requirements:
  87. *
  88. * - `tokenId` must be already minted.
  89. * - `receiver` cannot be the zero address.
  90. * - `feeNumerator` cannot be greater than the fee denominator.
  91. */
  92. function _setTokenRoyalty(
  93. uint256 tokenId,
  94. address receiver,
  95. uint96 feeNumerator
  96. ) internal virtual {
  97. require(feeNumerator <= _feeDenominator(), "ERC2981: royalty fee will exceed salePrice");
  98. require(receiver != address(0), "ERC2981: Invalid parameters");
  99. _tokenRoyaltyInfo[tokenId] = RoyaltyInfo(receiver, feeNumerator);
  100. }
  101. /**
  102. * @dev Resets royalty information for the token id back to the global default.
  103. */
  104. function _resetTokenRoyalty(uint256 tokenId) internal virtual {
  105. delete _tokenRoyaltyInfo[tokenId];
  106. }
  107. /**
  108. * This empty reserved space is put in place to allow future versions to add new
  109. * variables without shifting down storage in the inheritance chain.
  110. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  111. */
  112. uint256[48] private __gap;
  113. }