bg

How to Deploy your First Smart Contract on Optimism?

Updated at: September 20, 202411 Mins Read

Author: QuillAudits Team

So, you’ve heard all the hype about Optimism and are itching to write and deploy your very first smart contract? Perfect!

In this guide, I’ll walk you through how to set up everything from MetaMask to the Remix IDE, and how to deploy your first smart contract to the OP Sepolia testnet. Let’s turn you into a Web3 wizard!


Step 1: Setting Up MetaMask — Your Gateway to Optimism

MetaMask is the most widely used Ethereum wallet, and it’s an essential tool for interacting with blockchain applications, managing your crypto assets, and, of course, deploying smart contracts. We’ll be using MetaMask to interact with the Optimism network, but first, let’s set it up.



Meta Mask wallet
  1. Install MetaMask:

    • Visit metamask.io from your browser (Chrome, Brave, or Firefox are all good choices).
       
    • Click the “Download” button, and choose the correct extension for your browser. If you're using a Chromium-based browser (like Chrome or Brave), you might be prompted to install MetaMask via the Chrome Web Store.
       
    • Once installed, you’ll see a cute little fox icon at the top right of your browser. Click it to get started with wallet setup.

     

  2. Create Your MetaMask Wallet:
  • After launching the extension, click “Create a new wallet”. You’ll be asked to accept MetaMask’s terms of service and privacy policy.
     
  • MetaMask will then ask if you want to share anonymous usage data. You can choose to “I Agree” or “No Thanks” based on your preference (either option will let you proceed).
     
  • Now, create a strong password for your wallet. This password encrypts your MetaMask wallet locally, so make sure it’s something you can remember. You might want to store it in a password manager to keep things safe.

 

3. Secure Your Wallet:

  • MetaMask will now display a 12-word secret recovery phrase. This phrase is critical: it can be used to recover your wallet if your browser extension ever gets corrupted or lost.
     
  • Write down this phrase on a piece of paper and store it somewhere safe. For development purposes, you might want to store it in a password manager, but NEVER share it with anyone.
     
  • After writing down the recovery phrase, MetaMask will ask you to confirm it by selecting a few words in the correct order. Once that’s done, click “Complete”. Your wallet is now ready to use!
     

4. Final Touches:

You’re now inside the MetaMask wallet! If you want to explore other wallet options later, Ethereum.org has a great Find a Wallet feature. But for now, MetaMask is our ticket to Optimism.

 

Secure Your Optimism Smart Contracts with QuillAudits

Ready to secure your Optimism smart contracts? Take the first step towards a safer blockchain journey. Request an Audit with QuillAudits today & ensure your contracts are robust and secure!

Check Our Pricingarrow

Step 2: Adding OP Sepolia Testnet to MetaMask

Optimism is a Layer 2 scaling solution that drastically reduces the cost of transactions compared to Ethereum’s mainnet. We’ll start by deploying your contract to OP Sepolia, Optimism’s testnet. Once you’re comfortable, you can move on to the OP Mainnet.

 

  1. Connecting MetaMask to OP Sepolia:
  • By default, MetaMask only connects to the Ethereum mainnet, so we need to add the OP Sepolia testnet as a custom network.

    2
    Go to the OP Sepolia connection page. It’ll display the connection details for Sepolia and give you an option to add it to MetaMask.

    Once you’re ready, click the “Connect” button to add Sepolia to your MetaMask networks. A MetaMask popup will appear, asking for permission to add the network. Click “Approve”, and then “Switch network” to complete the process.

2. Fauceting for Test ETH:

To deploy a contract, you need ETH to pay for gas fees, even on a testnet. Luckily, you don’t need to buy test ETH — you can get it for free from a faucet.

  • Go to the Optimism Superchain Faucet and request some test ETH for Sepolia. If this faucet is down, don’t worry; there are other options available, and a quick search for "Sepolia ETH faucet" should give you alternatives.
     
  • Once you receive the test ETH, check your wallet balance in MetaMask to make sure it updated. If you don’t see any ETH, ensure that MetaMask is connected to OP Sepolia.
     

Step 3: Writing Your First Smart Contract in Remix

Now that your wallet is ready and you’ve connected to the OP Sepolia network, it’s time to get into the heart of smart contract development! We’ll use Remix, a browser-based Solidity IDE that makes it super simple to write and deploy smart contracts.

2

Source

 

  1. Open Remix IDE:

    Head to remix.ethereum.org to get started. When you first open Remix, it may ask whether you’d like to share usage data with the platform. This is optional and won’t affect how you use Remix.

    Once Remix is open, take a moment to explore the interface. The left sidebar contains tabs for managing files, compiling contracts, deploying contracts, and viewing Solidity documentation. Remix also has a built-in tutorial that’ll guide you through the basics.
     

  2. Creating Your First Contract:
    • Click the “File” icon on the left to create a new file. Name the file something like MyFirstContract.sol and hit enter.
       
    • Now, it’s time to write your first contract! Paste the following Solidity code into the file:

      3

      This simple contract has a single variable (message) that stores a string. It has two functions:
       
    • constructor: Initializes the contract with the message "Hello, Optimism!".
       
    • setMessage: Updates the message variable to a new string.

       

Step 4: Compiling Your Contract


Now that your code is written, it’s time to compile it and check for errors. Remix makes this process simple.

  1. Automatic Compilation:

    Remix automatically compiles your contract when you save the file, but you can also compile it manually if you prefer.

    • To compile manually, click the “Solidity Compiler” icon on the left (it looks like a piece of paper with a checkmark).
       
    • Then, click “Compile MyFirstContract.sol”.

    If everything’s good, you won’t see any errors. If you do run into issues, check your code for typos or missed punctuation marks.
     

  2. Check Compiler Version:

    Remix uses the Solidity version specified in your contract’s pragma statement (pragma solidity ^0.8.0). Make sure your compiler version matches that — you can select the appropriate compiler version in the Solidity Compiler tab if needed.
     

Step 5: Deploying Your Contract to OP Sepolia

Now, it’s time to see your work come to life on the blockchain! We’ll deploy your smart contract to the OP Sepolia testnet using MetaMask and Remix.

deploy smart contract

Source



  1. Open the Deploy Tab:

    In the left sidebar, click the “Deploy & run transactions” tab (Ethereum logo with an arrow pointing right). This is where you’ll manage contract deployments.

  2. Change Environment to OP Sepolia:

    Remix, by default, deploys contracts to an in-memory blockchain, but we need to deploy on OP Sepolia. In the “Environment” dropdown, select “Injected Provider - MetaMask”. This tells Remix to use MetaMask as the provider.

  3. Deploy the Contract:

    Once MetaMask connects, click the orange “Deploy” button. You’ll see a MetaMask popup asking you to confirm the transaction. This will require some gas fees (paid in Sepolia test ETH), so click “Confirm”.

    Wait a few seconds while the transaction processes. Once completed, Remix will show your deployed contract under “Deployed Contracts”. You did it!

     

Step 6: Interacting with Your Contract

Now that your contract is live, let’s interact with it! Remix provides an easy-to-use UI for calling your contract’s functions and reading its state.

  1. Expanding Your Contract:

    In the Deployed Contracts section, click the arrow next to your contract’s name to expand it. You’ll see a list of the contract’s functions, including setMessage and message.
     

  2. Setting a New Message:
    • In the input box next to the setMessage function, type a new message (something like "Web3 is the future!") and click the “setMessage” button.
    • MetaMask will pop up again to confirm the transaction, as this action modifies the state of the blockchain. Click

“Confirm” and wait for the transaction to complete.
 

3. Reading the New Message:

  • Once the transaction is confirmed, click the message function. This will read the current state of the message variable, and you should see the new message you just set! Congratulations — you’ve just interacted with your deployed smart contract.
     
  1. Switch to OP Mainnet:

    Follow the same steps you used to add OP Sepolia to MetaMask, but this time, add the Optimism Mainnet (you can find it on Chainlist).
     

  2. Deploying on Mainnet:

    With some real ETH in your MetaMask wallet, deploy your contract to the Optimism Mainnet using Remix, just like you did on OP Sepolia. Be mindful of gas fees, as they will be higher than on the testnet.
     

Report Mockup

Check Out Our Work

We've audited top DApps and DeFi protocols, ensuring they remain secure and Free from Hacks. Check out their audit reports to see our impact.

Read the Reports

Final Thoughts

Congratulations on deploying your first smart contract to Optimism! While you’re now equipped to explore more advanced topics like interacting with DeFi protocols or building decentralized applications (dApps), it’s crucial to prioritize security before deploying on the Optimism mainnet.

Make sure to get your code thoroughly audited by a reputable smart contract auditing firm like QuillAudits, or leverage AI tools like QuillShield to find and fix vulnerabilities.

Taking these steps ensures your smart contract is secure and ready for the mainnet. Good luck and happy coding!

Subscribe to our Newsletter

Your weekly dose of Web3 innovation and security, featuring blockchain updates, developer insights, curated knowledge, security resources, and hack alerts. Stay ahead in Web3!

Telegram