CREATE2 (EIP-1014) computes a contract's address as the last 20 bytes of keccak256(0xff ++ deployer ++ salt ++ keccak256(initCode)). The deployer's nonce plays no part. So if the same factory address deploys the same init code (creation bytecode plus constructor arguments, byte for byte) with the same salt, the contract lands at the same address on every EVM chain. In practice you use a factory that already sits at one address everywhere, such as Arachnid's deterministic deployment proxy at 0x4e59b44847b379578588920cA78FbF26c0B4956C (Foundry's default), the Safe Singleton Factory or CreateX. Then you keep constructor arguments identical across chains, or use CREATE3 when they must differ.
Why doesn't a normal deployment give the same address?
A regular CREATE deployment, which is what you get from sending a transaction with no to field or from new Contract() without a salt, derives the address from the sender and their nonce: keccak256(rlp([sender, nonce])). Deploy from the same key on two chains and you only get matching addresses if that key has sent exactly the same number of transactions on both. One stray transfer, a failed deployment or a test transaction on one chain and the addresses diverge for good. (If nonces are new to you, how nonces work covers them.)
Teams used to keep a dedicated "deployer key" untouched on every chain for exactly this reason. It is fragile, and if the key leaks, its future nonces can be used to deploy anything at your expected addresses.
How does CREATE2 compute the address?
The CREATE2 opcode (0xF5, added in Ethereum's Constantinople upgrade in 2019) takes a 32-byte salt alongside the init code:
address = keccak256( 0xff ++ deployer ++ salt ++ keccak256(initCode) )[12:]
0xffis a constant prefix that keeps CREATE2 addresses from colliding with CREATE ones.deployeris the address of the contract executing CREATE2, not your wallet.saltis any 32 bytes you choose.initCodeis the creation bytecode with ABI-encoded constructor arguments appended.
Every input can be computed before anything is deployed, which is also why account abstraction wallets can show you a "counterfactual" address before the account exists, and why Uniswap can compute pool addresses without storing them. You can predict the address off-chain:
import { concat, encodeAbiParameters, getContractAddress, keccak256, toHex } from "viem";
const FACTORY = "0x4e59b44847b379578588920cA78FbF26c0B4956C";
const salt = keccak256(toHex("myproject.registry.v1"));
const initCode = concat([
registryBytecode, // creation bytecode from your build artifact
encodeAbiParameters([{ type: "address" }], [owner]),
]);
const predicted = getContractAddress({ opcode: "CREATE2", from: FACTORY, salt, bytecode: initCode });
(ethers v6 has the same calculation as getCreate2Address(factory, salt, keccak256(initCode)).)
Which factory should I use?
The address formula includes the factory, so the factory itself must exist at the same address on every chain. That's a chicken-and-egg problem, and the common factories solve it in different ways:
| Factory | Address | How it gets to the same address | Notes |
|---|---|---|---|
| Arachnid deterministic deployment proxy | 0x4e59b448...4956C | A keyless, pre-signed transaction anyone can broadcast | Foundry's default; calldata is salt ++ initCode |
| Safe Singleton Factory | 0x914d7Fec...643d7 | Safe deploys it per chain from a controlled key | Used for Safe contracts; request it for new chains |
| CreateX | 0xba5Ed099...ba5Ed | Pre-signed transactions plus a funded deployer | CREATE2 and CREATE3, salt protection options |
| Your own factory | Whatever you deploy | Deployed via one of the above | Lets you add access control |
The Arachnid proxy was deployed with a pre-EIP-155 transaction (no chain ID in the signature) from an address whose key nobody knows, so the same signed transaction can be replayed on any new chain. Some chains and many RPC providers reject transactions without replay protection, and chains with non-standard gas costs can make the fixed-gas transaction fail. Those chains end up without the proxy, or with it only as a genesis predeploy. Check before you plan:
cast code 0x4e59b44847b379578588920cA78FbF26c0B4956C --rpc-url $RPC_URL # non-empty = available
cast code 0xba5Ed099633D3B313e4D5F7bdc1305d3c28ba5Ed --rpc-url $RPC_URL
New chains built on the OP Stack since the Ecotone upgrade ship all three as preinstalls, along with Multicall3, Permit2 and the ERC-4337 EntryPoints, which is one benefit of shared rollup stacks like the OP Stack.
How do I deploy with Foundry?
In a Foundry script, new Contract{salt: ...}(...) inside a broadcast is sent through the CREATE2 deployer automatically:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Script, console} from "forge-std/Script.sol";
import {Registry} from "../src/Registry.sol";
contract Deploy is Script {
bytes32 constant SALT = keccak256("myproject.registry.v1");
function run() external {
address owner = vm.envAddress("OWNER"); // must be the same on every chain
bytes memory initCode = abi.encodePacked(type(Registry).creationCode, abi.encode(owner));
address predicted = vm.computeCreate2Address(SALT, keccak256(initCode));
if (predicted.code.length > 0) {
console.log("already deployed at", predicted);
return;
}
vm.startBroadcast();
Registry registry = new Registry{salt: SALT}(owner);
vm.stopBroadcast();
require(address(registry) == predicted, "address mismatch");
console.log("Registry deployed at", predicted);
}
}
Then run it once per chain, with RPC aliases defined in foundry.toml:
for chain in mainnet base arbitrum optimism; do
forge script script/Deploy.s.sol --rpc-url "$chain" --broadcast --verify --account deployer
done
Without Foundry's helper, call the Arachnid proxy directly. Its calldata is the salt followed by the init code, and it returns the new address:
FACTORY=0x4e59b44847b379578588920cA78FbF26c0B4956C
SALT=$(cast keccak "myproject.registry.v1")
ARGS=$(cast abi-encode "f(address)" "$OWNER") # constructor arguments
INIT_CODE=$(cast concat-hex "$(forge inspect Registry bytecode)" "$ARGS")
cast call "$FACTORY" "$(cast concat-hex "$SALT" "$INIT_CODE")" --rpc-url "$RPC_URL" # dry run, prints address
cast send "$FACTORY" "$(cast concat-hex "$SALT" "$INIT_CODE")" --rpc-url "$RPC_URL" --account deployer
For a vanity prefix, cast create2 --starts-with 0x0000 --deployer $FACTORY --init-code-hash $HASH searches for a salt that produces one.
Why did my contract land at a different address?
If the address differs between chains, some input differs. The usual suspects:
Constructor arguments. Anything chain-specific in the constructor (a WETH address, a router, a chain ID, an oracle) changes the init code. Keep constructor arguments identical and set chain-specific values later through an access-controlled setter, or use CREATE3 (below).
Compiler output. Init code must match to the byte. A different solc version, optimiser runs, via_ir setting or evm_version produces different bytecode. So does the metadata hash that Solidity appends, which changes when comments, file paths or unrelated sources in the build change. Build once, deploy the same artifact everywhere, and pin the settings:
# foundry.toml
[profile.default]
solc_version = "0.8.28"
evm_version = "cancun" # the lowest EVM version every target chain supports
optimizer = true
optimizer_runs = 200
bytecode_hash = "none" # drop the metadata hash so cosmetic changes don't alter bytecode
Choosing evm_version matters if some target chains lag behind Ethereum's hard forks. Bytecode built for Cancun can contain PUSH0 or MCOPY, which older chains reject. What the zkEVM types mean covers how rollups differ here.
A different factory. Foundry lets you override the CREATE2 deployer, and a teammate's script might use CreateX while yours uses the Arachnid proxy.
Non-standard chains. ZKsync Era derives CREATE2 addresses with its own formula over a bytecode hash, so the same salt gives a different address there. See why Solidity behaves differently on ZKsync Era.
When should I use CREATE3?
CREATE3 makes the address depend only on the factory and the salt, not on the init code. The factory uses CREATE2 to deploy a tiny proxy with fixed bytecode, and that proxy uses plain CREATE (at nonce 1) to deploy your contract. Since the proxy's code never changes, neither does the final address.
That lets you pass different constructor arguments per chain, or even deploy a different version later, at the same address. CreateX's deployCreate3(bytes32 salt, bytes initCode) is the usual way to do it. Be careful with CreateX's salt flags: setting byte 21 of the salt to 0x01 enables cross-chain redeploy protection by mixing in the chain ID, which deliberately gives different addresses per chain. For the same address everywhere, use 0x00 there, and put your deployer address in the first 20 bytes so only you can use that salt.
The trade-off is trust: the address no longer commits to the code. Anyone checking that "the contract at this address is the one we audited" has to compare bytecode on each chain.
Security pitfalls
msg.sender in the constructor is the factory. Ownable(msg.sender) makes the Arachnid proxy the owner, and since it has no functions for calling your contract, ownership is lost forever. Always pass the owner explicitly, ideally the same Safe multisig that holds your admin keys.
Anyone can deploy your contract first. Public factories are permissionless. Someone who sees your salt and init code can deploy them on a chain you haven't reached yet. They get exactly the contract you would have deployed, which is harmless if the constructor sets all state, but dangerous if the contract needs a separate initialize call. For proxies, pass initialisation data to the proxy constructor so deployment and initialisation are atomic, as described in how upgradeable proxies work, or use a permissioned salt.
Same address does not mean same code. Before trusting a deployment on a new chain, compare cast codehash $ADDRESS across chains. On a chain where you never deployed, an attacker using CREATE3 or a different factory might occupy an address users expect to be yours.
Metamorphic contracts are mostly gone. Before the Dencun upgrade, a contract could SELFDESTRUCT and be redeployed with different code at the same CREATE2 address. EIP-6780 removed that except within the creating transaction, so code at a CREATE2 address is now effectively permanent.
Checklist
- Check the factory exists on every target chain with
cast code. - Build once with pinned compiler settings,
bytecode_hash = "none"and the lowest commonevm_version. - Keep constructor arguments identical across chains; pass the owner explicitly.
- Predict the address with
vm.computeCreate2Addressor viem, and assert it after deploying. - Use CREATE3 only when arguments must differ, and with CreateX set the salt for same-address, permissioned deployment.
- Make initialisation atomic so front-running a deployment gains nothing.
- Compare
cast codehashon every chain, then verify the source on each block explorer.
Get the weekly commit
New blockchain deep dives every week.