What is metadata
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.
What is smart contract metadata?
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.
Key components of smart contract metadata
Smart contract metadata consists of several elements that define the contract’s behavior and structure:
- Application Binary Interface (ABI): The ABI is a JSON-formatted description of the contract's functions and events. It defines how to encode function calls and decode function outputs, enabling external applications to interact with the contract.
- Bytecode: The compiled, machine-readable version of the smart contract. The EVM executes this bytecode.
- Source code: The original, human-readable code written in high-level languages like Solidity or Vyper. Bytecode is compiled from source code.
- Compiler information: Detail about the compiler version and settings used to generate the bytecode.
- Contract name: The name or identifier given to the smart contract.
- Constructor arguments: Where applicable, any parameters passed to the contract’s constructor during deployment.
Purpose of smart contract metadata
Smart contract metadata delivers several functions:
- Interaction: The ABI makes it possible for other contracts, decentralized applications (dApps), and tools to understand how to call the contract’s functions and interpret its events.
- Verification: Metadata enables developers to verify deployed contracts by comparing the on-chain bytecode with the compiled source code.
- Documentation: Metadata provides information about the contract’s structure, behavior, and functionality, enabling developers and users to understand its mechanics.
- Upgradability: In cases like proxy contracts or when metadata stores configuration parameters, updates can be made to adjust various aspects. These aspects include implementation logic, access controls, or external references such as oracles or IPFS hashes, allowing contracts to evolve without requiring redeployment.
Smart contract metadata storage and access
The Solidity compiler automatically generates a JSON file containing the contract’s metadata. This file includes:
- Compiler version and settings
- Source code or references to the source files (e.g., IPFS or Swarm URLs)
- ABI
- Documentation (if written with NatSpec comments, which are a standard way of writing comments in your smart contract code)
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.
Best practices for smart contract metadata
To ensure smart contract metadata is secure and useful, developers should follow these best practices:
- Version control: Always include compiler version information to ensure reproducibility and compatibility.
- Documentation: Use NatSpec comments in the source code to generate comprehensive, developer-friendly documentation.
- Security: Do not include sensitive information, as metadata is publicly accessible once deployed.
- Verification: Publish metadata so third-party services can verify the contract's source code and compilation settings.
Example of smart contract metadata:
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.
Generated metadata from the Solidity smart 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
}
Metadata breakdown
Components of this smart contract’s metadata include:
- Compiler version: The contract was compiled using Solidity 0.8.27.
- ABI: This describes the print() function, including its inputs (none), outputs (a string), and state mutability (pure).
- Optimization settings: The optimizer was enabled and configured for 200 runs (the number of passes the Solidity optimizer performs on the smart contract's bytecode), which can improve performance when the contract is executed. This standard practice enhances efficiency, reduces gas, removes redundant instructions, and optimizes storage.
- Source references: The metadata includes the Keccak256 hash of the source code and links to its location in decentralized storage systems like IPFS.
- EVM version: The contract is designed to run on the cancun version of the EVM.
- License: The contract is licensed under MIT, allowing others to use and modify it freely.