ERC2981: NFT Royalty Standard. What is it, and How do You Use it?
Royalties across marketplaces give problems. ERC2981 is here to fix that.
As the subtitle suggests, royalties across marketplaces give you a headache. Since the start of NFTs, each marketplace implements its own way of giving and saving royalty data. EIP2981 was brought into life to fix this issue.
With ERC2981, your smart contract (NFT or otherwise) can save its royalty data straight on-chain. Marketplaces and users can read this data and act accordingly.
Note: Keep in mind that not every marketplace implements this yet, but it's starting to get used more.
What is ERC2981
ERC2981 is an implementation of the Royalty standard defined in EIP2981. It can be used to set a global royalty for each token, or a specific royalty for each different token.
The royalty to be paid will be stored on-chain in your own smart contract. But ERC2981 will not enforce the payment of the royalty. It does allow for each marketplace to work with a single standard of defining royalties which is great for the future of NFTs.
We can implement ERC2981 by using the smart contract from OpenZeppelin. We only need to (optionally) create a single function to set the royalties, or we can set the royalties at deployment by setting the values from within the constructor.
Implementing ERC2981
Implementing the royalty standard requires us to only import OpenZeppelin’s smart contract and create some way to set the default royalty we want to receive.
In a smart contract, you want to receive a royalty on, add the following code.
In this snippet, I use two ways of setting royalty, and you could implement both at the same time if you’d want to.
We set a default royalty within the constructor, and if we want, we can change the royalties afterward by using the function. But be sure to add some security to the function such as the onlyOwner
modifier from Ownable.sol
.
Conclusion
If you want to receive royalties, ERC2981 is an easy way of letting marketplaces know you want to. Unfortunately, not all marketplaces support this yet, but I think they will.
Thank you so much for reading and have an excellent day.
Consider supporting me by getting a Medium membership. It helps me out a lot, it won’t cost you anything extra, and you can read as many Medium articles as you like!
Follow me on Twitter and gm.xyz to keep up with my projects.
Check out Pixel Pizzas on Polygon.