Create contract at deterministic address using salt - enables counterfactual deployment
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0xf5Introduced: Constantinople (EIP-1014)CREATE2 deploys a new contract with a deterministic address computed from creator, salt, and init code hash. Unlike CREATE (which uses nonce), the address is predictable before deployment, enabling counterfactual instantiation and state channels.
Specification
Stack Input:
value (wei to send)offset (memory offset of init code)length (size of init code)salt (32-byte salt for address computation)
Stack Output:
address (deployed contract address, or 0 if failed)
CREATE2 enables deliberate address collisions through init code manipulation:
// VULNERABLE: Pre-Cancun collision attackcontract VulnerableFactory { function deploy(bytes memory code, bytes32 salt) external { address addr; assembly { addr := create2(0, add(code, 32), mload(code), salt) } // Deployed contract A }}// Attack (pre-EIP-6780):// 1. Deploy Contract A with init_code_1, salt// 2. Selfdestruct Contract A// 3. Deploy Contract B with init_code_2, SAME salt// Result: Different code at same address!// Post-Cancun (EIP-6780): SELFDESTRUCT doesn't delete in same tx// Attack mitigated: Cannot redeploy at same address
Mitigation (EIP-6780): SELFDESTRUCT only deletes if created in same transaction.
Init Code Hash Importance
Address depends on init code hash - different code = different address:
// These deploy to DIFFERENT addresses even with same saltbytes memory code1 = type(Contract).creationCode;bytes memory code2 = abi.encodePacked( type(Contract).creationCode, abi.encode(123) // Constructor argument);// Different init code hash = different addressaddress addr1 = create2(0, add(code1, 32), mload(code1), salt);address addr2 = create2(0, add(code2, 32), mload(code2), salt);// addr1 != addr2
Salt Reuse
Same salt with same code fails:
// VULNERABLE: Deployment fails if salt reusedfunction deploy(bytes memory code, bytes32 salt) external { address addr; assembly { addr := create2(0, add(code, 32), mload(code), salt) } // Reverts if contract already exists at computed address}// SAFE: Include unique data in saltfunction deployUnique(bytes memory code, uint256 nonce) external { bytes32 salt = keccak256(abi.encodePacked(msg.sender, nonce)); // Address unique per user + nonce}
Metamorphic Contracts
Contracts that change code after deployment:
// DANGEROUS: Metamorphic pattern (pre-Cancun)contract Metamorphic { function morph() external { // Change implementation selfdestruct(payable(address(this))); // Then redeploy different code at same address via CREATE2 }}// Post-Cancun: EIP-6780 prevents this// SELFDESTRUCT doesn't delete code unless created in same tx
Frontrunning
Deterministic addresses enable frontrunning:
// VULNERABLE: Public salt + code = frontrunnabalefunction deploy(bytes memory code, bytes32 salt) external { // Attacker sees tx in mempool // Frontruns with higher gas price // Deploys at target address first! address addr; assembly { addr := create2(0, add(code, 32), mload(code), salt) }}// MITIGATION: Include msg.sender in saltbytes32 salt = keccak256(abi.encodePacked(msg.sender, userProvidedSalt));// Address unique per deployer
Constructor Reentrancy
Same reentrancy risks as CREATE:
contract Vulnerable { mapping(address => bool) public trusted; function deployAndTrust(bytes memory code, bytes32 salt) external { address deployed; assembly { deployed := create2(0, add(code, 32), mload(code), salt) } // VULNERABLE: Constructor could re-enter here trusted[deployed] = true; }}
Implementation
TypeScript
/** * CREATE2 opcode (0xf5) * Create contract at deterministic address */export function create2(frame: FrameType): EvmError | null { // EIP-214: Cannot be called in static context if (frame.isStatic) { return { type: "WriteProtection" }; } // Pop 4 arguments const value = popStack(frame); const offset = popStack(frame); const length = popStack(frame); const salt = popStack(frame); // Calculate gas cost const words = Math.ceil(Number(length) / 32); let gasCost = 32000n; // Base gasCost += BigInt(words * 2); // Init code (EIP-3860) gasCost += BigInt(words * 6); // Keccak256 for address // Memory expansion if (length > 0) { const end = Number(offset) + Number(length); gasCost += memoryExpansionCost(frame, end); updateMemorySize(frame, end); } // Charge gas consumeGas(frame, gasCost); // Read init code const initCode = readMemory(frame, offset, length); // Compute deterministic address const initCodeHash = keccak256(initCode); const preimage = new Uint8Array(1 + 20 + 32 + 32); preimage[0] = 0xff; preimage.set(frame.address, 1); preimage.set(saltToBytes(salt), 21); preimage.set(initCodeHash, 53); const addressHash = keccak256(preimage); const address = addressHash.slice(12); // Check collision if (accountExists(address)) { pushStack(frame, 0n); // Failure frame.returnData = new Uint8Array(0); return null; } // Calculate forwarded gas (63/64 rule) const afterCharge = frame.gasRemaining; const maxForward = afterCharge - afterCharge / 64n; // Execute deployment const result = executeCreate({ address: address, initCode: initCode, value: value, gas: maxForward }); // Refund unused gas frame.gasRemaining += result.gasLeft; // Set return_data frame.returnData = result.success ? new Uint8Array(0) : result.output; // Push address (0 if failed) pushStack(frame, result.success ? addressToBigInt(address) : 0n); frame.pc += 1; return null;}