Skip to content

Instantly share code, notes, and snippets.

@shobhitic
Last active June 23, 2023 15:39
Show Gist options
  • Select an option

  • Save shobhitic/50518080ca7cb29072d72730873ff54a to your computer and use it in GitHub Desktop.

Select an option

Save shobhitic/50518080ca7cb29072d72730873ff54a to your computer and use it in GitHub Desktop.
Royalty implementation for NFTs. https://www.youtube.com/watch?v=h6Fb_dPZCd0
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "@openzeppelin/contracts@4.4.2/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@4.4.2/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts@4.4.2/access/Ownable.sol";
contract MyToken is ERC721, ERC721Enumerable, Ownable {
string public contractURI;
uint96 royaltyFeesInBips;
address royaltyAddress;
constructor(uint96 _royaltyFeesInBips, string memory _contractURI) ERC721("MyToken", "MTK") {
royaltyFeesInBips = _royaltyFeesInBips;
royaltyAddress = owner();
contractURI = _contractURI;
// setRoyaltyInfo(msg.sender, _royaltyFeesInBips);
}
function safeMint(address to, uint256 tokenId) public onlyOwner {
_safeMint(to, tokenId);
}
function setRoyaltyInfo(address _receiver, uint96 _royaltyFeesInBips) public onlyOwner {
royaltyAddress = _receiver;
royaltyFeesInBips = _royaltyFeesInBips;
}
function setContractURI(string calldata _contractURI) public onlyOwner {
contractURI = _contractURI;
}
// The following functions are overrides required by Solidity.
function _beforeTokenTransfer(address from, address to, uint256 tokenId)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId);
}
function royaltyInfo(uint256 _tokenId, uint256 _salePrice)
external
view
virtual
override
returns (address, uint256)
{
return (royaltyAddress, calculateRoyalty(_salePrice));
}
function calculateRoyalty(uint256 _salePrice) pure public returns (uint256) {
return (_salePrice / 10000) * royaltyFeesInBips;
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return interfaceId == 0x2a55205a || super.supportsInterface(interfaceId);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/common/ERC2981.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract MyToken is ERC721, ERC721Enumerable, ERC2981, Ownable {
string public contractURI;
constructor(uint256 _royaltyFeesInBips, string memory _contractURI) ERC721("MyToken", "MTK") {
setRoyaltyInfo(owner(), _royaltyFeesInBips);
contractURI = _contractURI;
}
function safeMint(address to, uint256 tokenId) public onlyOwner {
_safeMint(to, tokenId);
}
function setRoyaltyInfo(address _receiver, uint96 _royaltyFeesInBips) public onlyOwner {
_setDefaultRoyalty(_receiver, _royaltyFeesInBips);
}
function setContractURI(string calldata _contractURI) public onlyOwner {
contractURI = _contractURI;
}
// The following functions are overrides required by Solidity.
function _beforeTokenTransfer(address from, address to, uint256 tokenId)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable, ERC2981)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}
@arkaydeus

Copy link
Copy Markdown

_setDefaultRoyalty takes a uint96 for the second argument so i think line 22 needs to change _royaltyFeesInBips to uint96 from uint256

@shobhitic

Copy link
Copy Markdown
Author

@arkaydeus good catch, fixed it, thanks!

@MadManwithaBlueBox

Copy link
Copy Markdown

Line 60 of the first example above has an extra ) at the end than there should be.

@nir-cohe

nir-cohe commented Mar 1, 2022

Copy link
Copy Markdown

what the difference between both of the contracts?

@logistic-people-deutschland

Copy link
Copy Markdown

get at ERC2981NFTCustom.sol
can someone please help
an error:

DeclarationError: Function with same name and parameter types defined twice.
--> contracts/ERC2981NFTdrawo.sol:43:5:
|
43 | function supportsInterface(bytes4 interfaceId)
| ^ (Relevant source part starts here and spans across multiple lines).
Note: Other declaration is here:
--> contracts/ERC2981NFTdrawo.sol:67:5:
|
67 | function supportsInterface(bytes4 interfaceId)
| ^ (Relevant source part starts here and spans across multiple lines).

TypeError: Function has override specified but does not override anything.
--> contracts/ERC2981NFTdrawo.sol:57:9:
|
57 | override
| ^^^^^^^^

@bertil291utn

Copy link
Copy Markdown

@logistic-people-deutschland on ERC2981NFTCustom.sol file there are supportsInterface function twice, so remove it the last one and should work

@shobhitic

Copy link
Copy Markdown
Author

what the difference between both of the contracts?

@Nir-Cohen one uses Openzeppelin's implementation and one is custom. Accompanying video - https://www.youtube.com/watch?v=h6Fb_dPZCd0

@RoamWang

RoamWang commented May 4, 2022

Copy link
Copy Markdown

Hi, sir. I have add setRoyaltyInfo to my ERC2981 contract, but it doesn't work.

contract ALPHA is ERC721Enumerable, ERC2981, Ownable{
constructor()
ERC721("ALPHA Coin", "ALPHA")
{
manager = msg.sender;
// set royalty of all NFTs to 10%
_setDefaultRoyalty(manager, 1000);
}
}

@RoamWang

RoamWang commented May 6, 2022

Copy link
Copy Markdown

sir, the Creator Fees doesn't show on test opensea, is that right?

@mardommah

Copy link
Copy Markdown

line 13 on constructor _royaltyFeesInBips should be uint96

@GoldevLab

GoldevLab commented Jul 8, 2022

Copy link
Copy Markdown

what about an ERC1155 implementation?

@rabTAI

rabTAI commented Sep 23, 2022

Copy link
Copy Markdown

image
It should be uint96 on constructor too on second contract.

@rom102393

Copy link
Copy Markdown

I am using the first file ERC2981NFTCustom.sol. This is still valid but please help if you can fix this error.
01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment