2

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?

1 Answer 1

3

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
1
  • Don't bar0 and bar1 have different bytecode simply because of the different arguments passed to the constructor (and not because those arguments are used to initialize immutable contract members)? 🙄
    – Iaroslav
    Jun 26, 2023 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.