From 6c4d5ff2c30c6cdae52af57dc926fcf6508c3484 Mon Sep 17 00:00:00 2001 From: Carlo Miguel Dy Date: Wed, 2 Mar 2022 14:39:20 +0800 Subject: [PATCH 1/5] Add sol files for royalties implementation --- contract/contracts/@eip2981/ERC2981Base.sol | 27 ++++++++++++++++ .../@eip2981/ERC2981ContractWideRoyalties.sol | 32 +++++++++++++++++++ .../contracts/@eip2981/IERC2981Royalties.sol | 17 ++++++++++ 3 files changed, 76 insertions(+) create mode 100644 contract/contracts/@eip2981/ERC2981Base.sol create mode 100644 contract/contracts/@eip2981/ERC2981ContractWideRoyalties.sol create mode 100644 contract/contracts/@eip2981/IERC2981Royalties.sol diff --git a/contract/contracts/@eip2981/ERC2981Base.sol b/contract/contracts/@eip2981/ERC2981Base.sol new file mode 100644 index 0000000..9e0d7e9 --- /dev/null +++ b/contract/contracts/@eip2981/ERC2981Base.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GNU General Public License v3.0 +pragma solidity ^0.8.10; + +import "@openzeppelin/contracts/utils/introspection/ERC165.sol"; + +import "./IERC2981Royalties.sol"; + +/// @dev This is a contract used to add ERC2981 support to ERC721 and 1155 +abstract contract ERC2981Base is ERC165, IERC2981Royalties { + struct RoyaltyInfo { + address recipient; + uint24 amount; + } + + /// @inheritdoc ERC165 + function supportsInterface(bytes4 interfaceId) + public + view + virtual + override + returns (bool) + { + return + interfaceId == type(IERC2981Royalties).interfaceId || + super.supportsInterface(interfaceId); + } +} diff --git a/contract/contracts/@eip2981/ERC2981ContractWideRoyalties.sol b/contract/contracts/@eip2981/ERC2981ContractWideRoyalties.sol new file mode 100644 index 0000000..ec52e8a --- /dev/null +++ b/contract/contracts/@eip2981/ERC2981ContractWideRoyalties.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GNU General Public License v3.0 +pragma solidity ^0.8.10; + +import "@openzeppelin/contracts/utils/introspection/ERC165.sol"; + +import "./ERC2981Base.sol"; + +/// @dev This is a contract used to add ERC2981 support to ERC721 and 1155 +/// @dev This implementation has the same royalties for each and every tokens +abstract contract ERC2981ContractWideRoyalties is ERC2981Base { + RoyaltyInfo private _royalties; + + /// @dev Sets token royalties + /// @param recipient recipient of the royalties + /// @param value percentage (using 2 decimals - 10000 = 100, 0 = 0) + function _setRoyalties(address recipient, uint256 value) internal { + require(value <= 10000, "ERC2981Royalties: Too high"); + _royalties = RoyaltyInfo(recipient, uint24(value)); + } + + /// @inheritdoc IERC2981Royalties + function royaltyInfo(uint256, uint256 value) + external + view + override + returns (address receiver, uint256 royaltyAmount) + { + RoyaltyInfo memory royalties = _royalties; + receiver = royalties.recipient; + royaltyAmount = (value * royalties.amount) / 10000; + } +} diff --git a/contract/contracts/@eip2981/IERC2981Royalties.sol b/contract/contracts/@eip2981/IERC2981Royalties.sol new file mode 100644 index 0000000..28dafa1 --- /dev/null +++ b/contract/contracts/@eip2981/IERC2981Royalties.sol @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GNU General Public License v3.0 +pragma solidity ^0.8.10; + +/// @title IERC2981Royalties +/// @dev Interface for the ERC2981 - Token Royalty standard +interface IERC2981Royalties { + /// @notice Called with the sale price to determine how much royalty + // is owed and to whom. + /// @param _tokenId - the NFT asset queried for royalty information + /// @param _value - the sale price of the NFT asset specified by _tokenId + /// @return _receiver - address of who should be sent the royalty payment + /// @return _royaltyAmount - the royalty payment amount for value sale price + function royaltyInfo(uint256 _tokenId, uint256 _value) + external + view + returns (address _receiver, uint256 _royaltyAmount); +} From 5b7f17c23a690ac5aee60a137dad97c4e30eaca7 Mon Sep 17 00:00:00 2001 From: Carlo Miguel Dy Date: Wed, 2 Mar 2022 15:04:24 +0800 Subject: [PATCH 2/5] Implement contract wide royalties --- contract/contracts/PixelAvatars.sol | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/contract/contracts/PixelAvatars.sol b/contract/contracts/PixelAvatars.sol index e905636..61f7bad 100644 --- a/contract/contracts/PixelAvatars.sol +++ b/contract/contracts/PixelAvatars.sol @@ -5,13 +5,15 @@ import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol"; import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721EnumerableUpgradeable.sol"; import "@openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol"; +import "./@eip2981/ERC2981ContractWideRoyalties.sol"; /// @author Developer DAO /// @title The Pixel Avatar smart contract that is compliant to ERC721 standard and is upgradeable contract PixelAvatars is ERC721EnumerableUpgradeable, ReentrancyGuardUpgradeable, - OwnableUpgradeable + OwnableUpgradeable, + ERC2981ContractWideRoyalties { string public baseURI; uint256 public mintPrice; @@ -29,6 +31,8 @@ contract PixelAvatars is __ERC721_init("Pixel Avatars", "PXLDEV"); __Ownable_init(); + setRoyalties(10000); // On initialize set 10% royalty fee + baseURI = "ipfs://QmZdT5R5XGQVwGnnpiS6dGjUHZh6z8JjpuHhsqcLMMeWiC/"; mintPrice = 12 ether; @@ -37,8 +41,11 @@ contract PixelAvatars is // mintPrice = 0.01 ether; } - function _baseURI() internal view virtual override returns (string memory) { - return baseURI; + /** + * @param amount The amount of royalties to be set. + */ + function setRoyalties(uint256 amount) external onlyOwner { + _setRoyalties(msg.sender, amount); } function setBaseURI(string memory _newBaseURI) external onlyOwner { @@ -103,4 +110,8 @@ contract PixelAvatars is function withdraw() external onlyOwner { payable(msg.sender).transfer(address(this).balance); } + + function _baseURI() internal view virtual override returns (string memory) { + return baseURI; + } } From efd59488eb741b8dd8a30a8161e67d926e224d3c Mon Sep 17 00:00:00 2001 From: Carlo Miguel Dy <45052332+carlomigueldy@users.noreply.github.com> Date: Fri, 18 Mar 2022 21:00:41 +0800 Subject: [PATCH 3/5] Set royalties to 1000 -> 10% --- contract/contracts/PixelAvatars.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contract/contracts/PixelAvatars.sol b/contract/contracts/PixelAvatars.sol index 61f7bad..46f3d5f 100644 --- a/contract/contracts/PixelAvatars.sol +++ b/contract/contracts/PixelAvatars.sol @@ -31,7 +31,7 @@ contract PixelAvatars is __ERC721_init("Pixel Avatars", "PXLDEV"); __Ownable_init(); - setRoyalties(10000); // On initialize set 10% royalty fee + setRoyalties(1000); // On initialize set 10% royalty fee baseURI = "ipfs://QmZdT5R5XGQVwGnnpiS6dGjUHZh6z8JjpuHhsqcLMMeWiC/"; mintPrice = 12 ether; From ca885e503635aba4a2873539a429e423223683c9 Mon Sep 17 00:00:00 2001 From: Carlo Miguel Dy <45052332+carlomigueldy@users.noreply.github.com> Date: Fri, 18 Mar 2022 21:03:42 +0800 Subject: [PATCH 4/5] Update IERC2981Royalties.sol --- contract/contracts/@eip2981/IERC2981Royalties.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contract/contracts/@eip2981/IERC2981Royalties.sol b/contract/contracts/@eip2981/IERC2981Royalties.sol index 28dafa1..eabf3e5 100644 --- a/contract/contracts/@eip2981/IERC2981Royalties.sol +++ b/contract/contracts/@eip2981/IERC2981Royalties.sol @@ -4,8 +4,7 @@ pragma solidity ^0.8.10; /// @title IERC2981Royalties /// @dev Interface for the ERC2981 - Token Royalty standard interface IERC2981Royalties { - /// @notice Called with the sale price to determine how much royalty - // is owed and to whom. + /// @notice Called with the sale price to determine how much royalty is owed and to whom. /// @param _tokenId - the NFT asset queried for royalty information /// @param _value - the sale price of the NFT asset specified by _tokenId /// @return _receiver - address of who should be sent the royalty payment From e55183f4a205332a37fd1dbb7a5f4744a336d646 Mon Sep 17 00:00:00 2001 From: Carlo Miguel Dy <45052332+carlomigueldy@users.noreply.github.com> Date: Fri, 18 Mar 2022 21:04:23 +0800 Subject: [PATCH 5/5] Update PixelAvatars.sol --- contract/contracts/PixelAvatars.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contract/contracts/PixelAvatars.sol b/contract/contracts/PixelAvatars.sol index 46f3d5f..aba069b 100644 --- a/contract/contracts/PixelAvatars.sol +++ b/contract/contracts/PixelAvatars.sol @@ -41,9 +41,7 @@ contract PixelAvatars is // mintPrice = 0.01 ether; } - /** - * @param amount The amount of royalties to be set. - */ + /// @param amount The amount of royalties to be set function setRoyalties(uint256 amount) external onlyOwner { _setRoyalties(msg.sender, amount); }