Skip to main content

Contract metadata

Metadata

Solidity compiler generates an output file called metadata that contains information about the contract and the compilation (see Solidity docs). This includes the compiler settings, contract ABI, NatSpec contract documentation, and used source files and their hashes. Sourcify takes the full advantage of this information to fully verify a deployed contract i.e. a byte-by-byte match of the contract. Metadata file is required for the Sourcify verification alongside the Solidity source files.

To see metadata in action check out the Metadata Playground.

Check Matches to understand how Sourcify uses the metadata for verification

Where to find

Solidity compiler

You can pass the --metadata flag to the Solidity command line compiler to get the metadata output printed.

solc --metadata MyContract.sol

Write the metadata into a file with

solc --metadata MyContract.sol > metadata.json

Standard JSON Input-Output

To have the metadata in the ouput of the compilation with Solidity's standard JSON, add metadata to the outputSelection of the compilation input JSON. Below example outputs metadata for all contracts compiled.

    "outputSelection": {
"*": {
"*": [
"metadata", // <--
"evm.bytecode",
"evm.bytecode.sourceMap"
],
...
},
...
}

Truffle

Truffle stores the contract compilation outputs to build/contracts/ folder. The .json file contains the metadata of the contract. You don't need to extract the metadata manually.

Simply drag and drop the output .json file of the contract you want to verify to the sourcify.dev UI.

Hardhat

Hardhat stores the outputs of the compilations under the artifacts/build-info/ folder inside the project. The .json file under the folder contains the Standard JSON Input-Output of the Solidity compiler for all contracts.

Since v2.6.8, Hardhat outputs the metadata files of each contract by default under the JSON's .output.contracts.<contract-path>.<contract-name>.metadata field. You don't have to extract the metadata manually. To verify via the Hardhat output:

Hardhat - earlier than v2.6.8

If you use an earlier version of Hardhat than 2.6.8 you can modify the hardhat-config.js manually to output metadata inside the build-info/ file:

// hardat-config.js
module.exports = {
...
solidity: {
settings: {
version: ...,
optimizer: {...},
outputSelection: {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"metadata", // <-- add this
...
]
},
},
},
},
...
};

In theory, if you haven't changed any of the compilation settings, it should result in the same metadata file and you should get a full match. Otherwise you will be able to get a partial match only.