Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Deploying to L2 networks in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Deploying to L2 networks
Write Smart Contract
Compile Contract
Connect to L2 Network
Deploy Contract
Confirm Deployment
Interact with Contract on L2
This flow shows the steps to deploy a smart contract on a Layer 2 blockchain network, from writing code to interacting after deployment.
Execution Sample
Blockchain / Solidity
const contract = await ethers.getContractFactory("MyContract");
const deployed = await contract.deploy();
await deployed.deployed();
console.log("Deployed to L2 at", deployed.address);
This code deploys a smart contract to an L2 network and logs its address after deployment.
Execution Table
StepActionEvaluationResult
1Get contract factoryethers.getContractFactory("MyContract")Contract factory object ready
2Deploy contractcontract.deploy()Transaction sent to L2 network
3Wait for deploymentdeployed.deployed()Deployment confirmed on L2
4Log addressconsole.log(...)Prints deployed contract address
💡 Deployment confirmed and contract address logged, process complete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
contractundefinedContract factory objectContract factory objectContract factory objectContract factory object
deployedundefinedundefinedDeployed contract instanceDeployed contract instanceDeployed contract instance
Key Moments - 2 Insights
Why do we wait for 'deployed.deployed()' before using the contract address?
Waiting for 'deployed.deployed()' ensures the contract is fully deployed on the L2 network before interacting, as shown in execution_table step 3.
What does 'ethers.getContractFactory' do in deployment?
'ethers.getContractFactory' prepares the contract code for deployment, creating a factory object as seen in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'contract' after step 2?
AContract factory object
BDeployed contract instance
CDeployment transaction object
DUndefined
💡 Hint
Check the 'contract' variable in variable_tracker after step 2
At which step does the deployment get confirmed on the L2 network?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
See execution_table step 3 description
If we skip waiting for 'deployed.deployed()', what might happen?
AContract address will be undefined
BDeployment will fail immediately
CWe might interact with a contract not yet deployed
DNothing changes, safe to skip
💡 Hint
Refer to key_moments about waiting for deployment confirmation
Concept Snapshot
Deploying to L2 networks:
1. Write and compile your smart contract.
2. Connect to the L2 network using a provider.
3. Use contract factory to deploy.
4. Wait for deployment confirmation with deployed.deployed().
5. Interact with the contract using its L2 address.
Full Transcript
Deploying to Layer 2 networks involves writing your smart contract code, compiling it, and then connecting to the Layer 2 blockchain network. Using a contract factory, you deploy the contract by sending a transaction to the network. It is important to wait for the deployment to be confirmed on the network before interacting with the contract. This ensures the contract is fully available and avoids errors. After confirmation, you can use the contract address to call functions or send transactions on the L2 network.

Practice

(1/5)
1. What is the main benefit of deploying smart contracts to Layer 2 (L2) networks?
easy
A. Increased decentralization over Layer 1
B. More complex contract code execution
C. Faster transactions and lower fees compared to Layer 1
D. Unlimited storage capacity for contracts

Solution

  1. Step 1: Understand Layer 2 purpose

    Layer 2 networks are designed to improve blockchain scalability by handling transactions off the main chain.
  2. Step 2: Identify benefits of L2

    This results in faster transaction speeds and lower fees compared to Layer 1.
  3. Final Answer:

    Faster transactions and lower fees compared to Layer 1 -> Option C
  4. Quick Check:

    L2 improves speed and cost [OK]
Hint: L2 means faster and cheaper transactions than L1 [OK]
Common Mistakes:
  • Confusing L2 with increased decentralization
  • Thinking L2 allows unlimited storage
  • Assuming L2 makes contracts more complex
2. Which of the following is the correct syntax to specify an L2 network in a deployment script using Hardhat?
easy
A. "network: 'layer2'" inside the config file
B. "network: 'l1'" inside the deployment script
C. "network: 'mainnet'" inside the config file
D. network: 'layer2'" without quotes in the script

Solution

  1. Step 1: Recognize correct network naming

    In Hardhat config, network names are strings and must be quoted.
  2. Step 2: Identify L2 network syntax

    Using "network: 'layer2'" inside the config file is the correct way to specify the L2 network.
  3. Final Answer:

    "network: 'layer2'" inside the config file -> Option A
  4. Quick Check:

    Network names are strings in config [OK]
Hint: Network names must be strings in config files [OK]
Common Mistakes:
  • Using unquoted network names causing syntax errors
  • Confusing L1 and L2 network names
  • Placing network setting inside deployment script instead of config
3. Given this deployment snippet for an L2 network:
const network = 'layer2';
console.log(`Deploying to ${network}`);
What will be the output?
medium
A. Deploying to ${network}
B. Deploying to layer2
C. Deploying to network
D. Error: network not defined

Solution

  1. Step 1: Understand template literals

    The backticks and ${} syntax insert variable values into strings.
  2. Step 2: Substitute variable value

    Here, ${network} becomes 'layer2', so the output is 'Deploying to layer2'.
  3. Final Answer:

    Deploying to layer2 -> Option B
  4. Quick Check:

    Template literal inserts variable [OK]
Hint: Backticks with ${} insert variables in strings [OK]
Common Mistakes:
  • Confusing template literals with normal quotes
  • Expecting literal ${network} output
  • Assuming variable is undefined
4. You try to deploy a contract to an L2 testnet but get an error: "Invalid RPC URL". What is the most likely cause?
medium
A. Your private key is invalid
B. The contract code has syntax errors
C. You forgot to compile the contract
D. The RPC URL for the L2 network is missing or incorrect in the config

Solution

  1. Step 1: Understand RPC URL role

    The RPC URL connects your deployment tool to the blockchain network.
  2. Step 2: Link error to cause

    An "Invalid RPC URL" error means the URL is missing or wrong in the config file.
  3. Final Answer:

    The RPC URL for the L2 network is missing or incorrect in the config -> Option D
  4. Quick Check:

    Invalid RPC URL means wrong/missing URL [OK]
Hint: Check RPC URL correctness in config first [OK]
Common Mistakes:
  • Assuming contract code errors cause RPC URL errors
  • Ignoring network config settings
  • Not verifying private key separately
5. You want to deploy a contract to an L2 network but keep your private key secure. Which approach is best?
hard
A. Store the private key in environment variables and load it securely
B. Use a public key instead of a private key for deployment
C. Share the private key in the project README for easy access
D. Hardcode the private key in the deployment script

Solution

  1. Step 1: Understand private key security

    Private keys must be kept secret to prevent unauthorized access.
  2. Step 2: Identify secure storage method

    Using environment variables keeps keys out of code and version control, enhancing security.
  3. Final Answer:

    Store the private key in environment variables and load it securely -> Option A
  4. Quick Check:

    Env vars keep keys secret [OK]
Hint: Never hardcode keys; use environment variables [OK]
Common Mistakes:
  • Hardcoding keys in scripts
  • Sharing keys publicly
  • Confusing public and private keys