# Compiled bytecode differs depending on unrelated contracts - cannot verify contract

**URL:** <https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164>\
**Category:** Contracts\
**Tags:** etherscan-verify\
**Created:** [August 18, 2021, 11:18pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164 "2021-08-18T23:18:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimsum](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/dimsum/32/8497_2.png) [@dimsum](https://forum.openzeppelin.com/u/dimsum)\
**Post date:** [August 18, 2021, 11:18pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/1 "2021-08-18T23:18:48Z")

</div>

Hi people,

I've been reading as much around this as possible, but cannot verify a contract which relies on openzeppelin imports, because the compiled bytecode differs from the deployed bytecode.

All compiled and deployed with hardhat, solc 0.8.0

1: a contract was compiled and deployed to rinkeby and mainnet, while there were other contracts (i.e. a backup version of the main contract) in the same project.  
2: I did not verify the contract immediately (silly I know)  
3: some small changes are made to the non-dependent files in the same project (i.e. backup .sol files are renamed and replaced with the deployed contract)  
4: Trying to verify the deployed contract some days later fails (following @abcoathup 's tutorial using hardhat-etherscan, and via direct verification on etherscan with flattened contract)

I have read and used this guide (thanks for writing it!)

> [@How to verify with Hardhat or Truffle a smart contract using OpenZeppelin Contracts](https://forum.openzeppelin.com/t/verify-smart-contract-inheriting-from-openzeppelin-contracts/4119):
>
> For a complete list of resources regarding verification visit [How to verify a contract on Etherscan/BscScan/PolygonScan](https://forum.openzeppelin.com/t/how-to-verify-a-contract-on-etherscan-bscscan-polygonscan/14225). The recommended way to verify a smart contract inheriting from OpenZeppelin Contracts is as a "multi-file" contract (corresponding to the Standard-Json-Input compiler type on Etherscan). It is easier to read, imports are maintained, licenses are maintained. Verified using Hardhat Verified using Truffle Hardhat Hardhat has an Etherscan plugin: [Hardhat Etherscan plugin](https://github.com/nomiclabs/hardhat/tree/master/packages/hardhat-etherscan) N…

I have tried directly verifying on etherscan, with a flattened contract (manually copied all dependencies into the same Contract.sol)

I have tried the hardhat-etherscan method:  
`npx hardhat verify --network network contractAddress constructorArgs)`

In all attempts I get the same error:

```auto
"Error in plugin @nomiclabs/hardhat-etherscan: The address provided as argument contains a contract, but its bytecode doesn't match any of your local contracts.

Possible causes are:
  - Contract code changed after the deployment was executed. This includes code for seemingly unrelated contracts.
  - A solidity file was added, moved, deleted or renamed after the deployment was executed. This includes files for seemingly unrelated contracts.
  - Solidity compiler settings were modified after the deployment was executed (like the optimizer, target EVM, etc.).
  - The given address is wrong.
  - The selected network (mainnet) is wrong."

```

Or from etherscan:  
`unable to find a matching bytecode`

So my question is: does the state of the project directory at compile/deployment (i.e. the existence of other .sol files) affect the deployed bytecode for a contract?

If so, is it impossible to reproduce the same bytecode for verification if the non-dependent file structure or non-dependent file names have changed?

I'm beginning to tear my hair out over this one - any help would be really appreciated!

Thanks everyone 😀

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [August 19, 2021, 12:49am UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/2 "2021-08-19T00:49:20Z")

</div>

> [@dimsum](#):
>
> does the state of the project directory at compile/deployment (i.e. the existence of other .sol files) affect the deployed bytecode for a contract?

It actually can affect the bytecode but in a way that Etherscan (and Hardhat) know to ignore.

I don't understand from your post, what method did you use the deploy the contract initially?

---

<div class="post-metadata">

**Author:** ![dimsum](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/dimsum/32/8497_2.png) [@dimsum](https://forum.openzeppelin.com/u/dimsum)\
**Post date:** [August 19, 2021, 10:09am UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/3 "2021-08-19T10:09:41Z")

</div>

Thanks @frangio  
I deployed with hardhat-ethers via a .js script in the command line:  
`npx hardhat run ./scripts/deploy.js --network mainnet`

```auto
// ./scripts/deploy.js 

async function main() {
	const Contract = await ethers.getContractFactory("MyContract");
	const contract = await Contract.deploy(constructorArg);

```

...subsequent changes to other .sol files in the same project...

then have been attempting verification with hardhat-etherscan:  
`npx hardhat verify --network mainnet <contract address> <constructorArg>`

Do you have any ideas? I am CERTAIN the contract has not been modified since deployment. Confirmed with version control rollbacks etc.

Issues [https://github.com/ethereum/solidity/issues/9573](https://github.com/ethereum/solidity/issues/9573)  
and [https://github.com/nomiclabs/hardhat/issues/804](https://github.com/nomiclabs/hardhat/issues/804)  
are similar, but are considered fixed by solc 0.7.0...

Thanks for your help! 👍

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [August 19, 2021, 2:24pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/4 "2021-08-19T14:24:36Z")

</div>

Are you also sure that the compilation configuration (e.g. optimizer) hasn't changed? Are you sure you're providing constructor arguments correctly?

---

<div class="post-metadata">

**Author:** ![dimsum](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/dimsum/32/8497_2.png) [@dimsum](https://forum.openzeppelin.com/u/dimsum)\
**Post date:** [August 19, 2021, 3:03pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/5 "2021-08-19T15:03:39Z")

</div>

Thanks - optimizer was left to default, so ought to be the same for deployment compilation and verification. I've just tried again with `{optimizer: true, runs: 200}` and get the same result.  
Constructor arguments are the same - this part of the bytecode actually matches 😅

Any other ideas? I'm starting to lean towards redeployment and this being a LESSON. But would rather exhaust every possibility before doing that!

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [August 19, 2021, 5:32pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/6 "2021-08-19T17:32:10Z")

</div>

No more ideas, sorry.

---

<div class="post-metadata">

**Author:** ![dimsum](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/dimsum/32/8497_2.png) [@dimsum](https://forum.openzeppelin.com/u/dimsum)\
**Post date:** [August 19, 2021, 5:39pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/7 "2021-08-19T17:39:09Z")

</div>

No worries mate, thank you for helping!

---

<div class="post-metadata">

**Author:** ![cameel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/cameel/32/4358_2.png) [@cameel](https://forum.openzeppelin.com/u/cameel)\
**Post date:** [August 28, 2021, 11:21pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/8 "2021-08-28T23:21:41Z")

</div>

Do you have the contract metadata from the original deployment? It should be possible to track down the thing that's different by comparing it with the metadata you are getting now. If the compiler options are different or some specific file changed, you'll see it there. And if the metadata is identical, you should be getting identical bytecode.

It's probably obvious but I'll ask just in case: are you using the same exact compiler release? The bytecode produced by 0.8.0 is not guaranteed to be the same as from another 0.8.x release. They're only compatible at the source code level.

Finally, do you explicitly set the EVM version? If not and the tools you are using are automatically setting it to the latest version (rather than relying on compiler's default), and you happened to deploy just before the recent London hard fork, you might be getting different bytecode after it.

---

<div class="post-metadata">

**Author:** ![dimsum](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/dimsum/32/8497_2.png) [@dimsum](https://forum.openzeppelin.com/u/dimsum)\
**Post date:** [September 9, 2021, 11:24am UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/9 "2021-09-09T11:24:51Z")

</div>

Thank you very much for your reply - I ended up re-deploying and verifying immediately afterwards 😅

The compiler was the same version, but I did not set the EVM version - that may have been it.

I'm happy to move on and chalk this up as a lesson in version control...

---

<div class="post-metadata">

**Author:** ![Salmaan\_Khan](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/salmaan_khan/32/13035_2.png) [@Salmaan\_Khan](https://forum.openzeppelin.com/u/Salmaan_Khan)\
**Post date:** [April 29, 2022, 4:26pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/10 "2022-04-29T16:26:28Z")

</div>

Continuing the discussion from [Compiled bytecode differs depending on unrelated contracts - cannot verify contract](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/9):

Hey there, I'm a facing exact same issue that you faced, I read that you ended up redeploying but still want to know if you discovered the solution later on.

---

<div class="post-metadata">

**Author:** ![Shawn\_Nguyen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/shawn_nguyen/32/12761_2.png) [@Shawn\_Nguyen](https://forum.openzeppelin.com/u/Shawn_Nguyen)\
**Post date:** [May 3, 2022, 4:52am UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/11 "2022-05-03T04:52:09Z")

</div>

I'm in a similar situation as the original post, the only difference is I deployed my contract via **REMIX**.  
Likewise, I didn't verify the contract immediately, I only tried to verify it after a few weeks.

The methods that I've tried to verify the contract:

1. Directly on Etherscan (flattened contract)
2. Using Hardhat
3. Using Remix with a plugin called "ETHERSCAN - CONTRACT VERIFICATION"

I can't redeploy the contract, I was wondering is there anyone or company providing services of reversing the bytecode to see what went wrong (I will provide the source code), so that it can be rectified and get the contract verified?

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [May 3, 2022, 4:48pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/12 "2022-05-03T16:48:20Z")

</div>

All we know about verification is in this post:

> [@How to verify a contract on Etherscan/BscScan/PolygonScan](https://forum.openzeppelin.com/t/how-to-verify-a-contract-on-etherscan-bscscan-polygonscan/14225):
>
> Make sure to read this post before asking a question about verification. If your issue is not addressed here, just leave a comment below. If you want to pay someone to do the verification for you, create a post in #Smart Contracts > Developer Wanted. This guide applies to [all Etherscan instances](https://etherscan.io/eaas), including all of the following as well as their testnet explorers: [etherscan.io](http://etherscan.io) (Ethereum) [bscscan.com](http://bscscan.com) (Binance Smart Chain) [polygonscan.com](http://polygonscan.com) (Polygon) [snowtrace.io](http://snowtrace.io) (Avalanche) Recommendations Prac…

I don't know of companies offering the service you mention but you can ask in #Smart Contracts > Developer Wanted, where some community members can try to do the verification.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [May 3, 2022, 4:48pm UTC](https://forum.openzeppelin.com/t/compiled-bytecode-differs-depending-on-unrelated-contracts-cannot-verify-contract/14164/13 "2022-05-03T16:48:25Z")

</div>


