Skip to main content

Contract Metadata

The ContractMetadata smart contract is an extension usable with any base smart contract. It lets you associate arbitrary metadata with your smart contract.

Import

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

Available functionality

FunctionalityDescription
contractURIReturns the metadata associated with your smart contract.
setContractURILets an authorized wallet set the metadata for your smart contract.
_canSetContractURIDefines the criteria a wallet must meet to be able to set your contract’s metadata.

Using the ContractMetadata extension on your contract lets you to add information about your contract (image, description, etc). This information is stored in IPFS and linked to your contract using the standard contractURI() method made available on your smart contract.

For an ERC721 or ERC1155 NFT smart contract, storing royalty information using the ContractMetadata extension is necessary for royalty support on Opensea.

Implementing the Contract extension

Import the contract extension and make your contract inherit it.

// 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 virtual override returns (bool){
return msg.sender == deployer;
}
}

Unlocked Features

Read, write, and update metadata on the contract itself using the dashboard or the SDK.

If you use deploy, you'll unlock the Settings tab to easily modify the contract's metadata:

Settings Tab.png

You can also edit the contract metadata programmatically using the SDK: