RoyaltyMod
ERC-2981 royalty logic for NFTs
- Implements ERC-2981
royaltyInfologic. - Supports setting default and token-specific royalty information.
- Utilizes diamond storage for state persistence.
- Internal functions ensure composability within facets.
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
RoyaltyStorage
storage-location: erc8042:compose.erc2981
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("compose.erc2981")) |
FEE_DENOMINATOR | uint96 | (Value: 10000) |
Functions
deleteDefaultRoyalty
Removes default royalty information. After calling this function, royaltyInfo will return (address(0), 0) for tokens without specific royalty.
getStorage
Returns the royalty storage struct from its predefined slot. Uses inline assembly to access diamond storage location.
Returns:
| Property | Type | Description |
|---|---|---|
s | RoyaltyStorage | The 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.
Parameters:
| Property | Type | Description |
|---|---|---|
_tokenId | uint256 | The 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.
Parameters:
| Property | Type | Description |
|---|---|---|
_tokenId | uint256 | The NFT asset queried for royalty information. |
_salePrice | uint256 | The sale price of the NFT asset. |
Returns:
| Property | Type | Description |
|---|---|---|
receiver | address | The address designated to receive the royalty payment. |
royaltyAmount | uint256 | The 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.
Parameters:
| Property | Type | Description |
|---|---|---|
_receiver | address | The royalty recipient address. |
_feeNumerator | uint96 | The 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.
Parameters:
| Property | Type | Description |
|---|---|---|
_tokenId | uint256 | The token ID to configure royalty for. |
_receiver | address | The royalty recipient address. |
_feeNumerator | uint96 | The royalty fee in basis points. |
Errors
Best Practices
- Ensure receiver addresses are valid and fee numerators are within acceptable bounds (e.g., 0-10000) before calling set functions.
- Use
resetTokenRoyaltyto revert token-specific royalties to the default. - Call
royaltyInfoto retrieve royalty details, which automatically handles fallback to default royalties.
Integration Notes
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.