Skip to main content

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.

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

SDK Usage

By adding this extension to a smart contract, the following features, hooks and functions are unlocked in the SDK:

Base Contracts Implementing This Extension

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

Full API Reference

royaltyInfo
function royaltyInfo(uint256 tokenId, uint256 salePrice)
external
view
virtual
returns (address receiver, uint256 royaltyAmount)
  • Returns royalty recipient and amount for a given NFT and its sale price.
  • Parameter tokenId: The tokenID of the NFT for which to query royalty info.
  • Parameter salePrice: Sale price of the NFT.
getRoyaltyInfoForToken
function getRoyaltyInfoForToken(uint256 tokenId) public view override returns (address, uint16);
  • Returns the royalty recipient and BPS for a given NFT.
  • Parameter tokenId: The tokenID of the NFT for which to query royalty info.
getDefaultRoyaltyInfo
function getDefaultRoyaltyInfo() external view override returns (address, uint16);
  • Returns the default royalty recipient and BPS for the contract's NFTs.
setDefaultRoyaltyInfo
function setDefaultRoyaltyInfo(address royaltyRecipient, uint256 royaltyBps) external;
  • Lets an authorized wallet set the default royalty recipient and BPS for the contract's NFT.
  • Parameter royaltyRecipient: The address to set as the new default royalty recipient.
  • Parameter royaltyBps: The value to set as the new default royalty BPS.
  • The _canSetRoyaltyInfo function is run on every call to this function. If the return value of _canSetRoyaltyInfo is false, the setDefaultRoyaltyInfo call will revert.
setRoyaltyInfoForToken
function setRoyaltyInfoForToken(
uint256 tokenId,
address recipient,
uint256 bps
) external
  • Lets an authorized wallet set the royalty recipient and BPS for a particular NFT of the contract.
  • Parameter tokenId: The tokenId of the NFT for which to set royalty info.
  • Parameter recipient: The address to set as the new royalty recipient for the NFT.
  • Parameter bps: The value to set as the new royalty BPS for the NFT.
  • The _canSetRoyaltyInfo function is run on every call to this function. If the return value of _canSetRoyaltyInfo is false, the setRoyaltyInfoForToken call will revert.
_setupDefaultRoyaltyInfo
function _setupDefaultRoyaltyInfo(address royaltyRecipient, uint256 royaltyBps) internal;
  • Sets the default royalty recipient and BPS for the contract's NFT.
  • Parameter royaltyRecipient: The address to set as the new default royalty recipient.
  • Parameter royaltyBps: The value to set as the new default royalty BPS.
  • The _canSetRoyaltyInfo function is not run on a call to this function.
_setupRoyaltyInfoForToken
function _setupDefaultRoyaltyInfo(address royaltyRecipient, uint256 royaltyBps) internal;
  • Sets the royalty recipient and BPS for a particular NFT of the contract.
  • Parameter tokenId: The tokenId of the NFT for which to set royalty info.
  • Parameter recipient: The address to set as the new royalty recipient for the NFT.
  • Parameter bps: The value to set as the new royalty BPS for the NFT.
  • The _canSetRoyaltyInfo function is not run on a call to this function.
_canSetRoyaltyInfo
function _canSetRoyaltyInfo() internal view virtual returns (bool);
  • Runs on every setDefaultRoyaltyInfo and setRoyaltyInfoForToken function calls.
  • Returns whether the relevant royalty info can be set in the given execution context.
  • For example, this function can check whether the wallet calling setDefaultRoyaltyInfo is the contract owner, and enforce that only the owner should be able to successfully call setDefaultRoyaltyInfo.