Skip to content

Instantly share code, notes, and snippets.

@shobhitic
Last active June 23, 2023 15:39
Show Gist options
  • Save shobhitic/50518080ca7cb29072d72730873ff54a to your computer and use it in GitHub Desktop.
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);
}
}
@rom102393
Copy link

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