Solidity – Difference Between `bytecode` and `deployedBytecode` for Contracts with Constructor Args

bytecodedeployed-bytecodehardhatsolidity

Suppose I have a contract like

pragma solidity ^0.8.17;

contract MyContract {
    constructor(string arg1, uint arg2) {
        ...
    }    
    ...
}

When I compile the solidity contract (actually I'm using hardhat), we get an artifact file like artifacts/contracts/MyContract.sol/MyContract.json, which includes bytecode and deployedBytecode fields. What are the differences?

I've googled and found some docs but its not yet clear, especially because bytecode and deployedBytecode are generated without constructor parameters.

  • the concatenation of bytecode and consctuctor parameters are send in the deployed transaction?
  • the byte code of the deployed stored on Ethereum is deployedBytecode and constructor parameters?

Best Answer

According to hardhat document : deployedBytecode: A "0x"-prefixed hex string of the unlinked runtime/deployed bytecode. If the contract is not deployable, this has the string "0x". It means deployed bytecode is runtime bytecode.

  1. What is the difference : the initialize code and its parameters: and boostrap the contract runtime bytecode and its initial environment (e.g. state storages, including running the constructor logic).
  2. Yes encoded params are appended to the end of bytecode. Read more: https://docs.blockscout.com/for-users/abi-encoded-constructor-arguments
  3. deployedBytecode or runtime bytecode is what you see on etherscan or when you query eth_getCode using ethereum node's RPC: Does "eth_getCode" return the init bytecode or the deployed bytecode?

Hope the above helps, you can read more about that here Difference between bytecode and runtime bytecode

Related Topic