forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 6
Milestone
Description
Factory Contracts & Bytecode Dependencies in Resolc
Overview
When using the resolc compiler, factory contracts that use the new keyword behave differently compared to traditional EVM. Understanding how to manage external contract bytecode is critical to ensuring successful deployments.
Key Difference from EVM
In traditional EVM environments:
new Child(...)compiles to aCREATEopcode that embeds the full child contract bytecode in the transaction.
In resolc:
new Child(...)compiles to aCREATEcall that references a hash of the child contract’s bytecode.- This means the child contract's bytecode must be uploaded beforehand, otherwise instantiation will fail.
Compiler Behavior
resolc analyzes the Solidity code and:
- Detects usage of
new. - Extracts and includes necessary factory dependencies in the output.
Example Output Snippet
{
"factoryDependencies": {
"0100001949af973d893f480abced5ef67da7704ff5e87db3a2df35bdf5e2fd10": "MyFactory.sol:Child"
}
}This indicates that the factory contract depends on a Child contract with that bytecode hash.
Bytecode Availability
Using the --standard-json compiler option, the output includes:
- Bytecode for all contracts (e.g., factory and children).
- The factoryDependencies field for deployment tools to process.
Deployment Process
A deployment tool (like Foundry) must:
- Parse factoryDependencies.
- Match each bytecode hash to the compiled child artifact.
- Check if the hash is already deployed on-chain.
- Upload missing dependencies.
- Deploy the factory contract only after all dependencies are present.
Solidity Example
contract Child {
uint public data;
constructor(uint _data) {
data = _data;
}
}
contract Parent {
function deployChild(uint val) external {
// Only works if Child is pre-uploaded!
Child c = new Child(val);
}
}
To check
zksync additionally outputs factoryDependenciesUnlinked - to check if it is needed and do we need to align our compiler version
factoryDependenciesUnlinked":["MyFactory.sol:Child"]
Metadata
Metadata
Assignees
Labels
No labels