basiliskawakens.xyz

How to Verify a Smart Contract's Source Code Matches Its Deployed Bytecode

You can verify that a contract's published source code matches the code actually running on-chain by recompiling the source with the same compiler settings and comparing the resulting bytecode against what's stored on the network. If the two match, you know the source code you're reading is the code that's executing. If they don't, you're likely looking at a different program than what users are interacting with.

This is one of the most important checks you can do before using an unfamiliar DeFi protocol, a new token, or any contract that holds funds. A mismatch doesn't automatically mean something is malicious - developers sometimes forget to include a file or use a different compiler version than they claim. But it does mean you cannot trust what you're reading, and that alone is reason to walk away.

Why a mismatch happens

The Ethereum Virtual Machine (EVM) doesn't store human-readable Solidity. It stores bytecode - a long string of hexadecimal characters that the EVM executes directly. When a developer deploys a contract, only that bytecode is written to the blockchain. The source code you see on Etherscan or a similar explorer is something the developer submitted separately, after deployment, and asked the explorer to "verify."

Explorers verify source code by recompiling it locally and comparing the resulting bytecode to the deployed bytecode. If they match, the explorer labels the contract as "verified" and shows the source code with a checkmark. If they don't match, the explorer shows the bytecode but no source.

A mismatch can happen for several reasons:

None of these are necessarily signs of fraud. But you cannot tell the difference between a harmless omission and a deliberate backdoor just by looking at the source. The only way to be sure is to do the verification yourself.

What you need to verify manually

You'll need three things:

  1. The source code (from the explorer or the project's GitHub).
  2. The compiler version and settings (from the explorer's "Contract" tab, under "Compiler" and "Settings").
  3. A local Solidity compiler - solc - or a tool like Foundry's forge that wraps it.

You'll also need the constructor arguments, because the bytecode stored on-chain includes the constructor's initialization code plus the arguments passed to it at deployment time. Explorers often show these in the "Contract Creator" section, but you can also find them in the transaction that created the contract.

Step-by-step manual verification

  1. Get the deployed bytecode. On Etherscan, go to the "Contract" tab, then "Bytecode." Copy the full bytecode string. It starts with 0x. Save it to a file, say deployed.txt.

  2. Get the compiler version and settings. On the same page, look under "Compiler" for something like 0.8.20+commit.a1b79de6. Under "Settings," note the EVM version (e.g., berlin, london, paris), whether optimization is enabled, and the optimizer runs (e.g., 200). Write these down.

  3. Get the source code. If the contract is verified, you'll see a "Contract Source Code" tab. Copy every .sol file and every imported file. Save them into a directory with the same structure as the original project. You'll also need the constructor arguments - the hex string after the bytecode in the creation transaction.

  4. Recompile. The exact command depends on your tool. With solc, a minimal invocation looks like:

bash solc --bin --optimize --optimize-runs 200 --evm-version paris --abi MyContract.sol

But you also need to pass the constructor arguments. With forge, you'd use forge create or forge verify-contract with the --constructor-args flag. If you're doing it by hand, you can append the constructor arguments to the compiled bytecode and then compare.

  1. Compare. The compiled bytecode includes the initialization code plus the constructor arguments. The deployed bytecode does not include the constructor arguments - those are consumed during deployment and are not stored on-chain. So you need to strip the first chunk of the compiled bytecode (the initialization code) and compare the remainder to the deployed bytecode.

In practice, people compare the deployed bytecode from the explorer against the --bin-runtime output from solc, which gives you exactly the runtime portion. Append the constructor arguments to the end of the runtime code only if you want to verify the constructor itself.

  1. Check for metadata hash. Solidity appends a metadata hash to the end of the bytecode. This hash is derived from the source code and compiler settings. If your local compilation produces a different metadata hash, the bytecode will differ in the last 60 or so bytes even if the logic is identical. Many verification tools ignore this by comparing only the "creation code" or by using a metadata hash comparison. For manual checks, compare everything except the final 64 bytes, or use a tool that strips the metadata automatically.

What to do when you find a mismatch

If your locally compiled bytecode doesn't match what's on-chain, stop. Do not interact with the contract. Check whether the explorer itself flags the contract as verified or not. If it's not verified, that's a red flag in itself. If it is verified but your local build differs, you may have made an error in the compiler settings - double-check the optimizer settings and the Solidity version. Some contracts are compiled with a nightly build or a pre-release version that solc won't reproduce exactly.

Also check whether the contract is a proxy. If the "Contract" tab shows a proxy pattern, the bytecode on-chain is just a delegatecall to another address. In that case, you need to verify the implementation contract at the address the proxy points to, not the proxy itself. The proxy's source code is often trivial and matches easily; the real logic lives elsewhere.

Tools that automate most of this

If manual verification sounds tedious, you're not alone. Tools like forge verify-contract, sourcify, and explorer-specific "verify" buttons do most of this automatically. But they rely on the same underlying assumptions: that the source code you provide is the source code that was deployed. The only way to be absolutely certain is to recompile from a clean checkout of the project's repository, using the exact commit that the developer claims to have deployed.

That's why the final step of any serious verification is to check the project's GitHub. Find the commit hash that matches the deployed contract, check it out, and build from there. If the project doesn't publish a commit hash or a tagged release, consider that a warning sign.

A practical shortcut for most users

You don't need to verify every contract yourself. For most tokens and simple contracts, the fact that the explorer shows a verified contract with a checkmark is sufficient - the explorer already did the compilation check. The manual process matters when:

In those cases, spend the twenty minutes to do the check. It's the only way to be sure the code you're reading is the code that's running.

Not financial advice. basiliskawakens.xyz publishes market data and general information about digital assets. Crypto assets are volatile and you can lose everything you put in. Nothing here is a recommendation to buy, sell or hold, and we make no price predictions.

Prices are sourced from third parties and may be delayed or wrong. Verify anything you intend to act on against a primary source.

Back to on-chain data