Docs

Royalty

import "@thirdweb-dev/contracts/extension/Royalty.sol";

Royalty implements EIP-2981 NFT royalty standard for royalty support on NFT marketplaces, allowing you to take a percentage fee of secondary sales of your NFTs.

View on GitHub

Usage

The Royalty extension is an abstract contract, and expects you to implement the following functions by yourself:

NameTypeReturnsDescription
_canSetRoyaltyInfointernal view virtualboolRuns on every attempt to set a new royalty recipient or BPS. Returns whether this info can be set in the given execution context.

This is an example smart contract demonstrating how to inherit from this extension and override the functions to add (optional) custom functionality.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@thirdweb-dev/contracts/extension/Royalty.sol";
contract MyContract is Royalty {
/**
* We store the contract deployer's address only for the purposes of the example
* in the code comment below.
*
* Doing this is not necessary to use the `Royalty` extension.
*/
address public deployer;
constructor() {
deployer = msg.sender;
}
function supportsInterface(bytes4 interfaceId)
public
view
virtual
returns (bool)
{
return type(IERC2981).interfaceId == interfaceId;
}
/**
* This function returns who is authorized to set royalty info for your NFT contract.
*
* As an EXAMPLE, we'll only allow the contract deployer to set the royalty info.
*
* You MUST complete the body of this function to use the `Royalty` extension.
*/
function _canSetRoyaltyInfo()
internal
view
virtual
override
returns (bool)
{
return msg.sender == deployer;
}
}

Base Contracts Implementing This Extension

All NFT contracts (ERC721 or ERC1155) implement this extension.

Full API Reference