4

I'm developing a contract in hardhat project. But now I want to write test by foundry. My problem is I'm using create2, but when running contract by hardhat and foundry they gave different addresses. I found the reason is the bytecode of contract from hardhat and foundry are different (10 ~ 20 bytes close the end). Here is my hardhat config + foundry toml.

hardhat.config.js

 config = {
  solidity: {
    version: "0.8.18",
    evmVersion: "london",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
    },
  },
...
}

foundry.toml

evm_version = 'london'
optimizer = true
optimizer_runs = 200
solc_version = '0.8.18'
via_ir = false

This is how they different, they have the same length, but different near the end enter image description here What reasons affect to bytecode?

6
  • This might be because of what bytecode are you trying to fetch. Maybe one of them outputs the creation code and another one simply runtime bytecode? Commented Aug 9, 2023 at 10:58
  • I get log both compiled bytecode and runtime bytecode. The compiled and runtime bytecode are the same for each foundry(in out) and hardhat(in artifacts).
    – CT95
    Commented Aug 10, 2023 at 1:35
  • I just updated the question, add image about how they r different.
    – CT95
    Commented Aug 10, 2023 at 1:51
  • I'll amplify the issue. I am unaware of this behavior. I guess this issue on Git Hub is by you, I'll amplify this issue in my connections. github.com/foundry-rs/foundry/issues/5559 Commented Aug 10, 2023 at 5:29
  • 2
    The issue can be solved by here:github.com/foundry-rs/foundry/issues/…
    – CT95
    Commented Aug 11, 2023 at 7:17

1 Answer 1

0

Foundry CREATE2 setup

this is what's happening when you CREATE2 from a script root:

    // Prepare CREATE2 Deployer
    let addr = Address::from_str("0x4e59b44847b379578588920ca78fbf26c0b4956c").unwrap();
    let code = hex::decode("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3").expect("Could not decode create2 deployer init_code").into();
    api.anvil_set_code(addr, code).await.unwrap();

Byecode Hash Output configuration settings

Foundry related Solidity compiler configs that can change bytecode hash output also include (besides optimizer runs and optimization settings/viaIR):

# {@see {@link https://github.com/foundry-rs/foundry/issues/4060} }
auto_detect_remappings = false
# Whether to store the referenced sources in the metadata as literal data. Helps with verification
use_literal_content = true
# The metadata hash is machine dependent. By default, this is set to none to allow for deterministic code.
# {@see {@link https://docs.soliditylang.org/en/latest/metadata.html} }
bytecode_hash = "none"
cbor_metadata = false
# Only the required contracts/files will be selected to be included in solc's output selection.
sparse_mode = true
ast = false
isolate = false
create2_library_salt = "0x0000000000000000000000000000000000000000000000000000000000000000"
prague = false
unchecked_cheatcode_artifacts = false

Customizing CREATE2 in Foundry

You cannot currently customize the foundry CREATE2 factory, see my issue on GitHub for the ongoing discussion: https://github.com/foundry-rs/foundry/issues/2638

Foundry cross-posted Issue

Additionally, this issue was reposted on GitHub and answered and closed: https://github.com/foundry-rs/foundry/issues/5559#issuecomment-1673454231

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.