What's the best way to implement royalties for an NFT collection?

Hey everyone! I’m new to NFTs and I’ve got my contract deployed. Minting works fine but I’m stuck on how to add royalties to my artwork. I’ve heard about two ways:

  1. Putting royalty fees in the smart contract (not sure how)
  2. Setting up royalties on OpenSea

Which one is better and safer? I’m testing on Rinkeby so I can tweak the contract as needed.

Here’s a newly designed example for reference:

pragma solidity ^0.8.0;

import '@openzeppelin/contracts/token/ERC721/ERC721.sol';

contract CreativeArt is ERC721 {
    uint256 public mintCost = 0.05 ether;
    uint256 public mintedTokens;
    uint256 public totalTokens;

    constructor(string memory name, string memory symbol, uint256 _totalTokens) ERC721(name, symbol) {
        totalTokens = _totalTokens;
    }

    function createToken() public payable {
        require(msg.value >= mintCost, 'Not enough ETH');
        require(mintedTokens < totalTokens, 'Exceeds total tokens');
        mintedTokens++;
        _safeMint(msg.sender, mintedTokens);
    }
}

Any advice on how to integrate royalties is much appreciated. Thanks!

Implementing royalties directly in the smart contract is generally considered the more robust approach. It ensures that royalties are enforced across all marketplaces that support the standard, not just OpenSea. To implement this, you should look into integrating the ERC2981 standard into your contract.

Here’s a brief outline of how you might modify your contract:

  1. Import the ERC2981 interface from OpenZeppelin.
  2. Implement the royaltyInfo function as specified by ERC2981.
  3. Set up a constructor parameter for the royalty percentage.
  4. Use _setDefaultRoyalty in your constructor to set the royalty recipient and percentage.

This approach provides a standardized way to handle royalties that most reputable marketplaces will respect. It’s a bit more work upfront, but it offers better long-term reliability and wider compatibility.

hey ethan, i’d go with the smart contract approach. it’s more reliable and you have full control. check out EIP-2981 for the royalty standard. you can implement it using OpenZeppelin’s ERC721Royalty. it’s pretty straightforward to add. good luck with your project!

hey ethan! im also pretty new to nfts but ive been doing some research lately. have you considered using the erc2981 standard for royalties? its supposed to be pretty cool and works across different marketplaces.

i was wondering, whats your artwork like? and how much of a royalty are you thinking of setting? i heard some artists go for like 5-10% but im not sure whats normal.

oh btw, your contract looks neat! have you thought about adding any special features to make your nfts stand out? like maybe some on-chain metadata or interactive elements? just curious cos i think that stuff is super interesting

anyway, good luck with your project! let us know how it turns out :slight_smile: