Contract Design – Do Immutable Variables Influence the Contract Bytecode?

bytecodecontract-designcontract-developmentimmutablestorage

Suppose you have a contract like this:

pragma solidity >=0.8.19;

contract Bar {
    uint256 public immutable value;

    constructor(uint256 value_) {
        value = value_;
    }
}

Will Bar have different bytecodes if different values are passed to the constructor?

Best Answer

Yes. Bar's bytecode depends on value_, because immutable variables are not part of contract storage.

We can prove this with the following Foundry test:

pragma solidity >=0.8.19 <0.9.0;

import { PRBTest } from "@prb/test/PRBTest.sol";

import { Bar } from "src/Bar.sol";

contract BarTest is PRBTest {
    Bar internal bar0;
    Bar internal bar1;

    function setUp() public virtual {
        bar0 = new Bar(42);
        bar1 = new Bar(43);
    }

    // fails because the bytecodes don't match
    function test_Bytecode() external {
        bytes memory code0 = address(bar0).code;
        bytes memory code1 = address(bar1).code;
        assertEq(code0, code1, "bytecodes don't match");
    }
}

The bytecode of bar1 will differ from that of bar0 just by one character - in the 162nd position, bar0 has a, whereas bar1 has b.

// bar0
0x6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60537f000000000000000000000000000000000000000000000000000000000000002a81565b60405190815260200160405180910390f3
// bar1
0x6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60537f000000000000000000000000000000000000000000000000000000000000002b81565b60405190815260200160405180910390f3
Related Topic