ContractMetadata
import "@thirdweb-dev/contracts/extension/ContractMetadata.sol";
The ContractMetadata
smart contract is an extension usable with any smart contract. It lets you define metadata for your smart contract. Additionally, ContractMetadata
is necessary to enable royalties on NFT contracts on OpenSea.
Usage
The ContractMetadata
extension is an abstract contract, and expects you to implement the following functions by yourself:
Name | Type | Returns | Description |
---|---|---|---|
_canSetContractURI | internal view virtual | bool | Runs on every attempt to set the metadata of the contract. Returns whether contract metadata 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/ContractMetadata.sol";
contract MyContract is ContractMetadata {
/**
* 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 `ContractMetadata` extension.
*/
address public deployer;
constructor() {
deployer = msg.sender;
}
/**
* This function returns who is authorized to set the metadata for your metadata.
*
* As an EXAMPLE, we'll only allow the contract deployer to set the contract's metadata.
*
* You MUST complete the body of this function to use the `ContractMetadata` extension.
*/
function _canSetContractURI() 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 of the base contracts implement this extension.
Full API reference
contractURI
function contractURI() external view returns (string memory);
- Returns the URI for the metadata associated with the smart contract. Generally, an IPFS URI.
- Set this value using the
setContractURI
function.
setContractURI
function setContractURI(string calldata _uri) external;
- Sets the metadata of the smart contract. This value can be read from the
contractURI
function. - Parameter
_uri
: The URI of the metadata to associate with the contract. - The
_canSetContractURI
function is run on every call to this function. If the return value of_canSetContractURI
isfalse
, thesetContractURI
call will revert.
_setupContractURI
function _setupContractURI(string memory _uri) internal;
- Sets the metadata of the smart contract. This value can be read from the
contractURI
function. - Parameter
_uri
: The URI of the metadata to associate with the contract. - The
_canSetContractURI
function is not run on a call to this function.
_canSetContractURI
function _canSetContractURI() internal view virtual returns (bool);
- Runs on every
setContractURI
function call. - Returns whether contract metadata can be set in the given execution context.
- For example, this function can check whether the wallet calling
setContractURI
is the contract owner, and enforce that only the owner should be able to successfully callsetContractURI
.