Table of Contents
Metadata is information about data that describes its content, structure, or purpose. It provides details like the author, creation date, file size, and keywords, making data easier to find, organize, and use. Metadata is essential for managing digital content, powering search engines, and organizing complex databases.
In the Ethereum Virtual Machine (EVM) ecosystem, smart contract metadata refers to information included in a smart contract that provides context, documentation, and technical details about its structure and functionality. This information allows developers, users, and tools to interact with the contract.
Smart contract metadata consists of several elements that define the contract’s behavior and structure:
Smart contract metadata delivers several functions:
The Solidity compiler automatically generates a JSON file containing the contract’s metadata. This file includes:
The compiler appends a hash of this metadata to the contract's bytecode. If the metadata is published to decentralized storage systems like IPFS or Swarm, it can be retrieved and authenticated using this hash.
To ensure smart contract metadata is secure and useful, developers should follow these best practices:
Below is a basic smart contract written in Solidity, along with associated metadata generated during its compilation.
//SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
contract Metadata {
function print() public pure returns(string memory){
return "Cyfrin is the best";
}
}
This contract contains a single function: print() that simply returns the string "Cyfrin is the best." While the code is simple, the metadata it generates gives developers and tools the information they need to interact with the contract.
{
"compiler": { "version": "0.8.27+commit.40a35a09" },
"language": "Solidity",
"output": {
"abi": [
{
"inputs": [],
"stateMutability": "pure",
"type": "function",
"name": "print",
"outputs": [
{ "internalType": "string", "name": "", "type": "string" }
]
}
],
"devdoc": { "kind": "dev", "methods": {}, "version": 1 },
"userdoc": { "kind": "user", "methods": {}, "version": 1 }
},
"settings": {
"remappings": ["forge-std/=lib/forge-std/src/"],
"optimizer": { "enabled": true, "runs": 200 },
"metadata": { "bytecodeHash": "ipfs" },
"compilationTarget": { "src/CyfrinMetadata.sol": "Metadata" },
"evmVersion": "cancun",
"libraries": {}
},
"sources": {
"src/CyfrinMetadata.sol": {
"keccak256": "0x0c00ac87e1e47cb3a95c2f272e4d3f0043e21b22e9610c83c5c6d089fbc6c8a5",
"urls": [
"bzz-raw://72e613b8d3a1c26d247ebc5d97019fc28eaa1ee35f80e989d7fccf2c8fc3759d",
"dweb:/ipfs/QmPtPkryuAKyFLGMFxg8sF6a4Kuhqytwx7Fhgfuti2A6vU"
],
"license": "MIT"
}
},
"version": 1
}
Components of this smart contract’s metadata include: