Skip to main content

Contract Metadata

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

The ContractMetadata smart contract is an extension usable with any base smart contract. It lets you associate arbitrary metadata with your smart contract. Additionally, ContractMetadata is necessary for NFT contracts that want royalties to get distributed on OpenSea.

Availability in base contracts

The ContractMetadata extension is already available in the following base contracts:

  • All ERC20, ERC721 and ERC1155 base contracts.

Unlocked Features

Once deployed, you can use the features made available by these contracts on the SDK and dashboard:

Implementing the Contract

The ContractMetadata extension is an abstract contract, and expects you to implement the following functions by yourself:

NameTypeParametersReturnsDescription
_canSetContractURIinternal view virtualn/aboolRuns on every attempt to set the metadata of the contract. Returns whether contract metadata can be set in the given execution context.
// 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;
}
}

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 is false, the setContractURI 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 call setContractURI.