ERC2981 NFT Royalties Implementation in a ERC721 Marketplace

I've read already many posts regarding this topic, but I just can't wrap my head around this Royalty implementation thing.
Pls help me out, I don't know how I should play with this thing.
what is feeNumerator? _setDefaultRoyalty(address receiver, uint96 feeNumerator)
is it NFT Price? Nft Royalty Percent?
Pls take a look at the following contract and help me with how I can implement NftRoyalty in this contract. Am I doing it right? if not then pls correct me. Thanks.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.8;

import "hardhat/console.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/common/ERC2981.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract PodShip is ERC721URIStorage, ERC2981, Ownable {
    using Counters for Counters.Counter;
    Counters.Counter private _podcastId;
    Counters.Counter private _nftId;

    struct Podcast {
        address nftCreator;
        address nftOwner;
        string nftName;
        string nftDescription;
        uint256 nftPrice;
        uint256 nftRoyaltyPercent;
        uint256 nftId;
        bool listed;
    }


    mapping(uint256 => Podcast) podcastId;

    constructor() ERC721("PodShip NFTs", "PODSHIP") {}

    function mintNft(
        string memory ipfsURI,
        string memory _nftName, 
        string memory _nftDescription, 
        uint256 _nftPrice, 
        uint256 _nftRoyaltyPercent) public returns(uint256) {
            require(_nftPrice > 0, "NFT Price not set correctly");
            require(_nftRoyaltyPercent > 0 && _nftRoyaltyPercent <= 50, "NFT Royalty should be in-between 1 & 50");

            _podcastId.increment();
            _nftId.increment();
            uint256 nft_Id = _nftId.current();

            _safeMint(msg.sender, nft_Id);
            _setTokenURI(nft_Id, ipfsURI);

            podcastId[_podcastId.current()] = Podcast(
            payable(msg.sender), 
            payable(msg.sender), 
            _nftName, 
            _nftDescription, 
            _nftPrice, 
            _nftRoyaltyPercent,
            nft_Id,
            false
        );
        console.log(nft_Id);
        console.log(_podcastId.current());

        return nft_Id;
    }

    function listNFT(uint256 _podcastID) public {
        require(msg.sender == podcastId[_podcastID].nftOwner && msg.sender == podcastId[_podcastID].nftCreator, "Only Owner can list his NFT");
        podcastId[_podcastID].listed = true;
        approve(address(this), podcastId[_podcastID].nftId);
        setApprovalForAll(address(this), true);
        _setDefaultRoyalty(podcastId[_podcastID].nftCreator, uint96(podcastId[_podcastID].nftRoyaltyPercent));
    }

Hi,

Look at the feeNumerator as the percentage of the price that corresponds to the royalties, meaning, royalty is specified as a fraction of sale price. if you take a look at the royaltyInfo function of the royalties contract you will se how the royalty price is computed and returned:


function royaltyInfo(uint256 _tokenId, uint256 _salePrice) public view virtual override returns (address, uint256) {
        RoyaltyInfo memory royalty = _tokenRoyaltyInfo[_tokenId];

        if (royalty.receiver == address(0)) {
            royalty = _defaultRoyaltyInfo;
        }

        uint256 royaltyAmount = (_salePrice * royalty.royaltyFraction) / _feeDenominator();

        return (royalty.receiver, royaltyAmount);
    }

If for example your royalties are going to be 25% of the sale price, you can set your feeNumerator to 25 and override the feeDenominator to 100, and if the sales price is 2000, 25% of that is 500, and if you apply the formula in the function:
(_salePrice * royalty.royaltyFraction) / _feeDenominator()==>(2000 * 25) / 100=500

For more information please take a look at the documentation.

1 Like

which function implements the actual transfer of royalties, in my understanding the royaltyInfo function is only responsible for displaying the royalty information.

The ERC of royalties only included how to save and fetch the info, but you will need to update your mint and transfer functions to actually include the transfer of the royalties on your own.

If for example your royalties are going to be 25% of the sale price, you can set your feeNumerator to 25 and override the feeDenominator to 100, and if the sales price is 2000, 25% of that is 500

Exactly what I was looking for
Thanks, Gal.