Skip to main content

Examples

This section provides practical examples and complete workflows to help you get the most out of Mega CLI.

Smart Contract Development Workflow

This example shows a complete workflow for developing, testing, and deploying a smart contract locally and to MegaETH testnet.

1. Initialize a Foundry Project

2. Create a Simple Contract

Create a file named src/Counter.sol:

3. Write a Test

Create a file named test/Counter.t.sol:

4. Compile and Test

5. Deploy Locally

You can use the mega deploy command like so:
For more complex deployment scenarios, you can still create deployment scripts in the script/ directory, but for this example, we’ll use the direct deployment approach.

6. Create an Account

7. Get Testnet Tokens

8. Deploy to Testnet

9. Deploy to Mainnet

When you are ready and have a funded MegaETH mainnet account:
You can override a network’s default RPC while retaining chain ID validation:

Full-Stack App Development Workflow

This example demonstrates how to create and run a full-stack application.

1. Initialize a Full-Stack Project

2. Start the Development Environment

3. Deploy a Contract to the Local Chain

In a new terminal:

4. Connect the Frontend to the Contract

Update next-app/src/constants/index.js with your contract address:

5. Test the Application

After starting the development environment with mega dev, you can:
  1. Visit http://localhost:3000/gmega in your browser
  2. Connect your wallet
  3. Interact with the deployed contract