diff --git a/EIPS/eip-6786.md b/EIPS/eip-6786.md new file mode 100644 index 00000000000000..a9d3d5bc861c3c --- /dev/null +++ b/EIPS/eip-6786.md @@ -0,0 +1,105 @@ +--- +eip: 6786 +title: Registry for royalties payment for NFTs +description: A registry used for paying royalties for any NFT with information about the creator +author: Otniel Nicola (@OT-kthd), Bogdan Popa (@BogdanKTHD) +discussions-to: https://ethereum-magicians.org/t/eip-5666-royalty-debt-registry/13569 +status: Draft +type: Standards Track +category: ERC +created: 2023-03-27 +requires: 165, 2981 +--- + +## Abstract + +This standard allows anyone to pay royalties for a certain NFT and also to keep track of the royalties amount paid. It will cumulate the value each time a payment is executed through it and make the information public. + +## Motivation + +There are many marketplaces which do not enforce any royalty payment to the NFT creator every time the NFT is sold or re-sold and/or providing a way for doing it. There are some marketplaces which use specific system of royalties, however that system is applicable for the NFTs creates on their platform. + +In this context, there is a need of a way for paying royalties, as it is a strong incentive for creators to keep contributing to the NFTs ecosystem. + +Additionally, this standard will provide a way of computing the amount of royalties paid to a creator for a certain NFT. This could be useful in the context of categorising NFTs in terms of royalties. The term “debt“ is used because the standard aims to provide a way of knowing if there are any royalties left unpaid for the NFTs trades that took place in a marketplace that does not support them and, in that case, expose a way of paying them. + +Not only the owner of it, but anyone could pay royalties for a certain NFT. This could be a way of supporting a creator for his work. + +## Specification + +The keywords “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119. + +Every contract compliant with [ERC-6786](./eip-6786.md) MUST implement the interface defined as follows: + +### Contract Interface + +```solidity +// @title Royalty Debt Registry +/// Note: the ERC-165 identifier for this interface is 0x253b27b0 + +interface IERC6786 { + + // Logged when royalties were paid for a NFT + /// @notice Emitted when royalties are paid for the NFT with address tokenAddress and id tokenId + event RoyaltiesPaid(address indexed tokenAddress, uint256 indexed tokenId, uint256 amount); + + /// @notice sends msg.value to the creator of a NFT + /// @dev Throws if there are no on-chain informations about the creator + /// @param tokenAddress The address of NFT contract + /// @param tokenId The NFT id + function payRoyalties(address tokenAddress, uint256 tokenId) external payable; + + /// @notice Get the amount of royalties which was paid for a NFT + /// @dev + /// @param tokenAddress The address of NFT contract + /// @param tokenId The NFT id + /// @return The amount of royalties paid for the NFT + function getPaidRoyalties(address tokenAddress, uint256 tokenId) external view returns (uint256); +} +``` + +All functions defined as view MAY be implemented as pure or view + +Function payRoyalties MAY be implemented as public or external + +The event RoyaltiesPaid MUST be emitted when the payRoyalties function is called + +The supportsInterface method MUST return true when called with `0x253b27b0` + +## Rationale + +With a lot of places made for trading NFTs dropping down the royalty payment or having a centralised approach, we want to provide a way for anyone to pay royalties to the creators. + +The payment can be made in native coins, so it is easy to aggregate the amount of paid royalties. We want this information to be public, so anyone could tell if a creator received royalties in case of under the table trading or in case of marketplaces which don’t support royalties. + +The function used for payment can be called by anyone (not only the NFTs owner) to support the creator at any time. There is a way of seeing the amount of paid royalties in any token, also available for anyone. + +For fetching creator on-chain data we will use [ERC-2981](./eip-2981.md), but any other on-chain method of getting the creator address is accepted. + +## Backwards Compatibility + +This ERC is not introducing any backward incompatibilities. + +## Test Cases + +Tests are included in [`ERC6786.test.js`](../assets/eip-6786/test/ERC6786.test.js). + +To run them in terminal, you can use the following commands: + +``` +cd ../assets/eip-6786 +npm install +npx hardhat test +``` + +## Reference Implementation + +See [`ERC6786.sol`](../assets/eip-6786/contracts/ERC6786.sol). + +## Security Considerations + +There are no security considerations related directly to the implementation of this standard. + +## Copyright + +Copyright and related rights waived via [CC0](../assets/eip-6786/LICENSE). diff --git a/assets/eip-6786/README.md b/assets/eip-6786/README.md new file mode 100644 index 00000000000000..cc3a5481a726d1 --- /dev/null +++ b/assets/eip-6786/README.md @@ -0,0 +1,27 @@ +
+ +# ERC6786 Royalty Debt Registry + +
+ +This project provides a reference implementation of the proposed `ERC-6786 Royalty Debt Registry`. + +## Install + +In order to install the required dependencies you need to execute: +```shell +npm install +``` + +## Compile + +In order to compile the solidity contracts you need to execute: +```shell +npx hardhat compile +``` + +## Tests + +```shell +npx hardhat test +``` \ No newline at end of file diff --git a/assets/eip-6786/contracts/ERC6786.sol b/assets/eip-6786/contracts/ERC6786.sol new file mode 100644 index 00000000000000..e332f79887d6f3 --- /dev/null +++ b/assets/eip-6786/contracts/ERC6786.sol @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: CC0 +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/utils/introspection/ERC165.sol"; +import "./IERC6786.sol"; +import "./utils/IERC2981.sol"; + +contract ERC6786 is IERC6786, ERC165 { + + //Mapping from token (address and id) to the amount of paid royalties + mapping(address => mapping(uint256 => uint256)) private _paidRoyalties; + + /* + * bytes4(keccak256('payRoyalties(address,uint256)')) == 0xf511f0e9 + * bytes4(keccak256('getPaidRoyalties(address,uint256)')) == 0xd02ad759 + * + * => 0xf511f0e9 ^ 0xd02ad759 == 0x253b27b0 + */ + bytes4 private constant _INTERFACE_ID_ERC6786 = 0x253b27b0; + + /* + * bytes4(keccak256('royaltyInfo(uint256,uint256)')) == 0x2a55205a + */ + bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a; + + /// @notice This is thrown when there is no creator related information + /// @param tokenAddress -> the address of the contract + /// @param tokenId -> the id of the NFT + error CreatorError(address tokenAddress, uint256 tokenId); + + /// @notice This is thrown when the payment fails + /// @param creator -> the address of the creator + /// @param amount -> the amount to pay + error PaymentError(address creator, uint256 amount); + + function checkRoyalties(address _contract) internal view returns (bool) { + (bool success) = IERC165(_contract).supportsInterface(_INTERFACE_ID_ERC2981); + return success; + } + + /** + * @dev See {IERC165-supportsInterface}. + */ + function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { + return + interfaceId == type(IERC6786).interfaceId || + super.supportsInterface(interfaceId); + } + + function payRoyalties(address tokenAddress, uint256 tokenId) external override payable { + if (!checkRoyalties(tokenAddress)) { + revert CreatorError(tokenAddress, tokenId); + } + (address creator,) = IERC2981(tokenAddress).royaltyInfo(tokenId, 0); + (bool success,) = payable(creator).call{value : msg.value}(""); + if(!success) { + revert PaymentError(creator, msg.value); + } + _paidRoyalties[tokenAddress][tokenId] += msg.value; + + emit RoyaltiesPaid(tokenAddress, tokenId, msg.value); + } + + function getPaidRoyalties(address tokenAddress, uint256 tokenId) external view override returns (uint256) { + return _paidRoyalties[tokenAddress][tokenId]; + } +} diff --git a/assets/eip-6786/contracts/IERC6786.sol b/assets/eip-6786/contracts/IERC6786.sol new file mode 100644 index 00000000000000..2d7fcebbee7a35 --- /dev/null +++ b/assets/eip-6786/contracts/IERC6786.sol @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: CC0 +pragma solidity ^0.8.0; + +interface IERC6786 { + + /// @dev Logged when royalties were paid for a NFT + /// @notice Emitted when royalties are paid for the NFT with address tokenAddress and id tokenId + event RoyaltiesPaid(address indexed tokenAddress, uint256 indexed tokenId, uint256 amount); + + /// @notice sends msg.value to the creator for a NFT + /// @dev Throws if there is no on-chain information about the creator + /// @param tokenAddress The address of NFT contract + /// @param tokenId The NFT id + function payRoyalties(address tokenAddress, uint256 tokenId) external payable; + + /// @notice Get the amount of royalties which was paid for + /// @param tokenAddress The address of NFT contract + /// @param tokenId The NFT id + /// @return The amount of royalties paid for the NFT in paymentToken + function getPaidRoyalties(address tokenAddress, uint256 tokenId) external view returns (uint256); +} diff --git a/assets/eip-6786/contracts/token/ERC721Royalty.sol b/assets/eip-6786/contracts/token/ERC721Royalty.sol new file mode 100644 index 00000000000000..17bf60c792765c --- /dev/null +++ b/assets/eip-6786/contracts/token/ERC721Royalty.sol @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: CC0 +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; +import "../utils/IERC2981.sol"; + +contract ERC721Royalty is ERC721, IERC2981 { + + address public constant DEFAULT_CREATOR_ADDRESS = 0x4fF5DDB196A32e3dC604abD5422805ecAD22c468; + + constructor(string memory name_, string memory symbol_) ERC721(name_, symbol_) {} + + /** + * @dev See {IERC165-supportsInterface}. + */ + function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, IERC165) returns (bool) { + return + interfaceId == type(IERC721).interfaceId || + interfaceId == type(IERC2981).interfaceId || + super.supportsInterface(interfaceId); + } + + function royaltyInfo( + uint256 _tokenId, + uint256 _salePrice + ) external view override returns ( + address receiver, + uint256 royaltyAmount + ) { + receiver = DEFAULT_CREATOR_ADDRESS; + royaltyAmount = _salePrice / 10000; + } +} diff --git a/assets/eip-6786/contracts/utils/IERC2981.sol b/assets/eip-6786/contracts/utils/IERC2981.sol new file mode 100644 index 00000000000000..406d2a3ea1673e --- /dev/null +++ b/assets/eip-6786/contracts/utils/IERC2981.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/utils/introspection/IERC165.sol"; + +interface IERC2981 is IERC165 { + function royaltyInfo( + uint256 _tokenId, + uint256 _salePrice + ) external view returns ( + address receiver, + uint256 royaltyAmount + ); +} diff --git a/assets/eip-6786/test/ERC6786.test.js b/assets/eip-6786/test/ERC6786.test.js new file mode 100644 index 00000000000000..07a03e33bdb865 --- /dev/null +++ b/assets/eip-6786/test/ERC6786.test.js @@ -0,0 +1,65 @@ +const {deployContracts} = require("../scripts/deploy_contracts"); +const {expect} = require('chai'); + +describe("ERC6786", () => { + + let erc721Royalty; + let erc721; + let royaltyDebtRegistry; + const tokenId = 666; + + beforeEach(async () => { + const contracts = await deployContracts(); + erc721Royalty = contracts.erc721Royalty; + erc721 = contracts.erc721; + royaltyDebtRegistry = contracts.royaltyDebtRegistry; + }) + + it('should support ERC6786 interface', async () => { + await expect(await royaltyDebtRegistry.supportsInterface("0x253b27b0")).to.be.true; + }) + + it('should allow paying royalties for a ERC2981 NFT', async () => { + await expect(royaltyDebtRegistry.payRoyalties( + erc721Royalty.address, + tokenId, + {value: 1000} + )).to.emit(royaltyDebtRegistry, 'RoyaltiesPaid') + .withArgs(erc721Royalty.address, tokenId, 1000); + }) + + it('should not allow paying royalties for a non-ERC2981 NFT', async () => { + await expect(royaltyDebtRegistry.payRoyalties( + erc721.address, + tokenId, + {value: 1000} + )).to.be.revertedWithCustomError(royaltyDebtRegistry,'CreatorError') + .withArgs(erc721.address, tokenId); + }) + + it('should allow retrieving initial royalties amount for a NFT', async () => { + await expect(await royaltyDebtRegistry.getPaidRoyalties( + erc721Royalty.address, + tokenId + )).to.equal(0); + }) + + it('should allow retrieving royalties amount after payments for a NFT', async () => { + await royaltyDebtRegistry.payRoyalties( + erc721Royalty.address, + tokenId, + {value: 2000} + ); + + await royaltyDebtRegistry.payRoyalties( + erc721Royalty.address, + tokenId, + {value: 3666} + ) + + await expect(await royaltyDebtRegistry.getPaidRoyalties( + erc721Royalty.address, + tokenId + )).to.equal(5666); + }) +});