Lesson 2 of 4 Deploying Smart Contracts

How Contract Deployment Works

Deployment is a transaction like any other, with special rules. This lesson explains the contract creation transaction, how constructor arguments are passed, how CREATE and CREATE2 determine addresses and what deployment costs.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the contract creation transaction.
  • Explain how CREATE and CREATE2 determine addresses.
  • Identify the components of deployment cost.
  • Explain how constructor arguments are passed.

The contract creation transaction

A contract is deployed by a transaction whose to field is empty. The following sequence shows what happens.

  1. Build

    The deployer's tool concatenates the creation bytecode and the ABI-encoded constructor arguments as the transaction data.

  2. Execute

    The EVM runs the creation code, which executes the constructor, initialising storage and immutables.

  3. Return runtime code

    The creation code returns the runtime bytecode.

  4. Store

    The network stores the runtime code at the new address and charges 200 gas per byte.

  5. Report

    The receipt's contractAddress field gives the new address.

If the constructor reverts or the transaction runs out of gas, no contract is created, but the gas used is still paid.

The following diagram shows the steps of a deployment and how the address is derived.

What happens when a contract is deployed. The transaction's to field is empty, and its data is 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 the runtime code at the new address, charging 200 gas per byte, and the receipt's contractAddress field reports the address.
The creation code runs once; the runtime code stays.
Put in order

What a deployment does

Exam skills 4.2.1

Arrange the steps of a contract creation transaction.

Log in to try this activity Interactive activities are free with an account.

Contract addresses

The address of a new contract is computed, not chosen. The following comparison shows the two methods.

CREATE
Used byEvery deployment transaction; new C() in Solidity
Address depends onDeployer address and nonce
Predictable in advanceYes, if the nonce is known
Same address on other chainsOnly if the deployer has the same nonce there
CREATE2
Used byFactories using new C{salt: s}()
Address depends onFactory address, salt and hash of creation code
Predictable in advanceYes, and independent of nonce
Same address on other chainsYes, if the factory exists at the same address

Because a CREATE address depends only on the deployer and its nonce, the first contract deployed by a fresh account is always at the same address. On Hardhat Network, the first account's first deployment is always at 0x5FbDB2315678afecb367f032d93F642f64180aa3, which is why tutorials can hard-code it.

What deployment costs

Deployment is usually the most expensive transaction a contract sees. The following table breaks down the cost.

ComponentGas
Base transaction cost21,000
Contract creation32,000
Creation code as calldata4 per zero byte, 16 per non-zero byte
Constructor executionVaries; storage writes dominate
Code deposit200 per byte of runtime code

A small contract of a few hundred bytes costs around 150,000 gas to deploy; a large DeFi contract near the 24,576-byte limit costs several million. At mainnet gas prices this can be significant, so teams measure deployment gas and may use the optimizer's low-runs setting or libraries to reduce code size.

Fill in the number

Estimating deployment gas

Exam skills 4.2.1 · 5.2.1

Add up the main fixed parts of a deployment's gas cost.

Log in to try this activity Interactive activities are free with an account.
Worked example

Constructor arguments

A token's constructor is constructor(string memory name, uint256 supply). A developer deploys it with web3.js using contract.deploy({ data: bytecode }).send({ from }) and the transaction reverts or the token has an empty name. What is wrong?

The constructor arguments were not passed. They must be given as contract.deploy({ data: bytecode, arguments: ["Club Token", 1000000n] }), so that web3.js ABI-encodes them and appends them to the bytecode. The same arguments are needed later to verify the source.

Worked example

Predicting an address

A deployer account has sent 4 transactions on Sepolia. Its next transaction will deploy a contract. Can the team know the contract's address before sending it?

Yes. The next nonce is 4, and the address is the last 20 bytes of keccak256(rlp([deployer, 4])); libraries provide helpers for this calculation. If any other transaction from the account is sent first, the nonce and therefore the address change.

Check yourself

Where does a deployer find the address of a newly created contract?

Show answer

In the transaction receipt's contractAddress field. It can also be computed in advance from the deployer address and nonce (CREATE).

Exam Tips

Remember for the exam

  • Contract creation: a transaction with no to address, carrying creation bytecode and encoded constructor arguments.
  • The receipt's contractAddress gives the new address.
  • CREATE addresses depend on deployer and nonce; CREATE2 on factory, salt and code.
  • Deployment pays 21,000 + 32,000 + calldata + constructor + 200 gas per byte of code.