Web3.js – web3.eth.getCode Not Matching Etherscan Data

bytecodeetherscanweb3js

For example, let's take contract address 0xdac17f958d2ee523a2206206994597c13d831ec7.

Does anybody have an idea why the following two are totally different:

  1. The string returned from await web3.eth.getCode(0xdac17f958d2ee523a2206206994597c13d831ec7)

  2. The string under Contract Creation Code at
    https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code

I am using Web3.js v1.2.1.

Thank you!

Best Answer

That's because they are not the same thing.

When you execute the getCode(...) function, you get the deployed bytecode of a specific address, as the docs says. The bytecode on the blockchain is the result of the execution of the compiled bytecode of your contract, which includes initialization code.

About the contract you provide, which is verified:

  1. await web3.eth.getCode(0xdac17f958d2ee523a2206206994597c13d831ec7)

Returns the bytecode of the deployed contract.

  1. Contract Creation Code

This is the input of the transaction that creates the contract, as you can see here in the Input Data field. This bytecode includes initialization code and will result in the bytecode deployed on the blockchain.


Let's see an example about a non verified contract.

With: web3.eth.getCode("0x004c8981FdDA3219d4F1319a50b2EfC9F52D36B3")

What you will get is the bytecode of the contract on the blockchain:

https://rinkeby.etherscan.io/address/0x004c8981FdDA3219d4F1319a50b2EfC9F52D36B3#code

Which is not the same as the input of the transaction that creates the contract, which is the contract creation code.

https://rinkeby.etherscan.io/tx/0xea40ca2f6be3f146b7a4f80c97319e6658f5cc1118330f4b4bf76a7df10c6f0f

Probably here is explained better:

Related Topic