Deploy Your First Contract

Overview

This tutorial will guide you through deploying your first smart contract on OPN Chain. We'll create a simple storage contract, deploy it, and interact with it using various tools.

What you'll learn:

  • Writing a basic smart contract

  • Compiling with Solidity

  • Deploying to OPN testnet

  • Interacting with your contract

  • Verifying your contract

Time required: 20 minutes

Prerequisites

Before starting, ensure you have:

  • [x] MetaMask installed and connected to OPN testnet

  • [x] Test OPN tokens from the faucet

  • [x] Node.js 16+ installed

  • [x] Basic command line knowledge

Step 1: Set Up Your Project

Create Project Directory

Install Dependencies

Initialize Hardhat

Select:

  • "Create a JavaScript project"

  • Press Enter for all prompts

  • Type 'y' to install sample project dependencies

Configure Hardhat for OPN

Create/update hardhat.config.js:

Set Up Environment Variables

Create .env file:

⚠️ Security Note: Never commit your .env file! Add it to .gitignore:

Step 2: Write Your Smart Contract

Create the Contract

Delete the sample contracts and create contracts/SimpleStorage.sol:

Understanding the Contract

This contract:

  • Stores a single number (storedValue)

  • Allows anyone to update the value

  • Emits events when the value changes

  • Provides functions to read and modify the value

Step 3: Compile the Contract

Compile

Expected output:

The compiled artifacts are saved in artifacts/ directory.

Check Compilation

You should see SimpleStorage.json containing the ABI and bytecode.

Step 4: Write Tests

Create Test File

Create test/SimpleStorage.test.js:

Run Tests

Expected output:

Step 5: Deploy to OPN Testnet

Create Deployment Script

Create scripts/deploy.js:

Deploy

Expected output:

🎉 Congratulations! You've deployed your first smart contract on OPN Chain!

Step 6: Interact with Your Contract

Create Interaction Script

Create scripts/interact.js:

Run Interaction

Step 7: Interact via Web Interface

Create Simple Web Interface

Create interface.html:

Update Contract Address

  1. Open deployment-info.json

  2. Copy the contract address

  3. Replace YOUR_CONTRACT_ADDRESS_HERE in the HTML file

  4. Open interface.html in your browser

Step 8: Verify Your Contract (Optional)

Why Verify?

Contract verification:

  • Makes source code public

  • Enables direct interaction on block explorer

  • Builds trust with users

  • Allows easy contract reading

Manual Verification Steps

Until automated verification is available:

  1. Prepare verification info:

  1. Save flattened contract:

  1. When block explorer launches:

    • Navigate to your contract

    • Click "Verify Contract"

    • Paste flattened source

    • Enter constructor arguments

    • Submit verification

Advanced Topics

Gas Optimization

Optimize your contract for OPN's gas costs:

Using Events Efficiently

Upgradeable Contracts

For production, consider upgradeable patterns:

Troubleshooting

Common Issues

"Insufficient funds" error:

  • Check your wallet has OPN tokens

  • Visit the faucet for more tokens

"Network error" when deploying:

  • Verify RPC URL: https://testnet-rpc.iopn.tech

  • Check your internet connection

  • Try increasing gas limit

"Nonce too high" error:

Contract not verified:

  • Ensure exact compiler version match

  • Check constructor arguments encoding

  • Try flattening the contract

Getting Help

If you encounter issues:

  1. Check error messages carefully

  2. Review FAQs

Summary

In this tutorial, you:

  • ✅ Set up a Hardhat project

  • ✅ Wrote a smart contract

  • ✅ Compiled and tested it

  • ✅ Deployed to OPN testnet

  • ✅ Interacted via scripts and web interface

  • ✅ Learned about verification

You're now ready to build more complex applications on OPN Chain!


Share your success! Tweet your deployed contract address with #OPNChain

Last updated