How to use ERC2981 to set royalties

This standard is still not used in much places but its a right step in direction to enforce royalties for NFTs
https://eips.ethereum.org/EIPS/eip-2981

to use Open Zeppelin version this is what to do (npm install openzeppelin/contracts ofcourse)

​​​​​​​import "@openzeppelin/contracts/token/common/ERC2981.sol";

in your smart contract and in the constructor you can add something like 

_setDefaultRoyalty(0xc8A5552031f92dEbeA5D9e61a396944Ee01BB2ca, 500);

which sets where royalties should go and in what percentage. 500 is here 5%
Why 500? well check here https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/cont…

  function _feeDenominator() internal pure virtual returns (uint96) {
        return 10000;
    }

feeDenominator is 10000 so 500 is 5% of that. You can also add function to change that 

    function setDefaultRoyalty(address receiver, uint96 numerator)
        external
        onlyOwner
    {
        ERC2981._setDefaultRoyalty(receiver, numerator);
    }

This also does raise amount of gas used for contract creation in my example it is
265678 or from 2297712 to 2563390. So as you really don't need it at this point and want to save gas, you can exclude this.

p.s

If you get this type of error TypeError: Derived contract must override function "supportsInterface". Two or more base classes define function with same name and parameter types. when compiling, this snippet could help, just replace contract names that clash

    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC2981, ERC721A) returns (bool) {
        return super.supportsInterface(interfaceId);
    }
Subscribe to N00b21337
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.