Skip to main content

RoyaltyMod

ERC-2981 royalty logic for NFTs

Key Features
  • Implements ERC-2981 royaltyInfo logic.
  • Supports setting default and token-specific royalty information.
  • Utilizes diamond storage for state persistence.
  • Internal functions ensure composability within facets.
Module Usage

This module provides internal functions for use in your custom facets. Import it to access shared logic and storage.

Overview

This module provides internal functions for managing ERC-2981 royalty information within a diamond. Facets can use these functions to set, retrieve, and reset both default and token-specific royalties, leveraging shared diamond storage. The royalty logic is transparently applied via the royaltyInfo function.


Storage

RoyaltyInfo

Structure containing royalty information. Properties

Definition
struct RoyaltyInfo {
address receiver;
uint96 royaltyFraction;
}

RoyaltyStorage

storage-location: erc8042:compose.erc2981

Definition
struct RoyaltyStorage {
RoyaltyInfo defaultRoyaltyInfo;
mapping(uint256 tokenId => RoyaltyInfo) tokenRoyaltyInfo;
}

State Variables

PropertyTypeDescription
STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("compose.erc2981"))
FEE_DENOMINATORuint96(Value: 10000)

Functions

deleteDefaultRoyalty

Removes default royalty information. After calling this function, royaltyInfo will return (address(0), 0) for tokens without specific royalty.

function deleteDefaultRoyalty() ;

getStorage

Returns the royalty storage struct from its predefined slot. Uses inline assembly to access diamond storage location.

function getStorage() pure returns (RoyaltyStorage storage s);

Returns:

PropertyTypeDescription
sRoyaltyStorageThe storage reference for royalty state variables.

resetTokenRoyalty

Resets royalty information for a specific token to use the default setting. Clears token-specific royalty storage, causing fallback to default royalty.

function resetTokenRoyalty(uint256 _tokenId) ;

Parameters:

PropertyTypeDescription
_tokenIduint256The token ID to reset royalty configuration for.

royaltyInfo

Queries royalty information for a given token and sale price. Returns token-specific royalty or falls back to default royalty. Royalty amount is calculated as a percentage of the sale price using basis points. Implements the ERC-2981 royaltyInfo function logic.

function royaltyInfo(uint256 _tokenId, uint256 _salePrice) view returns (address receiver, uint256 royaltyAmount);

Parameters:

PropertyTypeDescription
_tokenIduint256The NFT asset queried for royalty information.
_salePriceuint256The sale price of the NFT asset.

Returns:

PropertyTypeDescription
receiveraddressThe address designated to receive the royalty payment.
royaltyAmountuint256The royalty payment amount for _salePrice.

setDefaultRoyalty

Sets the default royalty information that applies to all tokens. Validates receiver and fee, then updates default royalty storage.

function setDefaultRoyalty(address _receiver, uint96 _feeNumerator) ;

Parameters:

PropertyTypeDescription
_receiveraddressThe royalty recipient address.
_feeNumeratoruint96The royalty fee in basis points.

setTokenRoyalty

Sets royalty information for a specific token, overriding the default. Validates receiver and fee, then updates token-specific royalty storage.

function setTokenRoyalty(uint256 _tokenId, address _receiver, uint96 _feeNumerator) ;

Parameters:

PropertyTypeDescription
_tokenIduint256The token ID to configure royalty for.
_receiveraddressThe royalty recipient address.
_feeNumeratoruint96The royalty fee in basis points.

Errors

Best Practices

Best Practice
  • Ensure receiver addresses are valid and fee numerators are within acceptable bounds (e.g., 0-10000) before calling set functions.
  • Use resetTokenRoyalty to revert token-specific royalties to the default.
  • Call royaltyInfo to retrieve royalty details, which automatically handles fallback to default royalties.

Integration Notes

Shared Storage

This module interacts with diamond storage at a specific, predefined slot identified by keccak256("compose.erc2981"). It manages a RoyaltyStorage struct containing defaultRoyaltyInfo. Functions like setDefaultRoyalty and setTokenRoyalty directly modify this shared storage. The royaltyInfo function reads from this storage to provide royalty details, falling back to default settings when token-specific settings are absent. All modifications are immediately visible to other facets accessing the same diamond storage.

Was this helpful?
Last updated: