Skill 4.2.1

Explain how a contract creation transaction deploys code

Key Concepts

A contract is deployed by a transaction with no to address. Its data field contains the creation bytecode followed by the ABI-encoded constructor arguments. The EVM runs the creation code, which executes the constructor and returns the runtime bytecode; the network stores that runtime code at the new address. The receipt's contractAddress field gives the address.

With CREATE, the address is the last 20 bytes of the Keccak-256 hash of the RLP-encoded deployer address and nonce, so the same account deploying at nonce 0 always gets the same address on any chain. With CREATE2, used by factory contracts, the address depends on the factory address, a 32-byte salt and the hash of the creation code, so it can be computed before deployment and reproduced across chains.

Deployment is expensive: on top of the 21,000 base cost, contract creation adds 32,000 gas, the creation code's calldata is charged per byte, and storing the runtime code costs 200 gas per byte. Runtime code is limited to 24,576 bytes (EIP-170).