1

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));
    }

1 Answer 1

2

All info can be found here: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/common/ERC2981.sol#L9-L23

Basically the royalty fee is calculated as a fraction of the sale price. The fraction is calculated as feeNumerator/10000. So if the price is 1 ETH and feeNumerator = 500, the royalty is 0.05 ETH.

_setDefaultRoyalty(address receiver, uint96 feeNumerator) sets the royalty for all NFTs in the collection.

In your code, I believe you want to set different royalties for different token ids. In this case you should use:

_setTokenRoyalty(podcastId[_podcastID].nftId, podcastId[_podcastID].nftCreator, uint96(podcastId[_podcastID].nftRoyaltyPercent));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.