Contract metadata
Metadata
Since v0.4.7 (2016-12-15) 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 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"
],
...
},
...
}
Foundry
Foundry stores the contract compilation outputs to out/CONTRACT_NAME
folder. The .json
file contains the metadata of the contract under "rawMetadata"
and "metadata"
fields. However you don't need to extract the metadata manually for verification.
You can use the forge verify-contract
command to verify a contract. See Foundry verification for more details.
Simply drag and drop the output .json
file of the contract you want to verify to the sourcify.dev UI or provide it as the metadata file to the API.
Hardhat
You can use the native Hardhat verify plugin to verify your contracts. See Hardhat verification for more details.
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 you can provide the JSON containing the artifacts to Sourcify via UI or API. See Hardhat verification for more details. If the file is too large, try zippping it and uploading the zip file.
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.
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.