Project Structure
Mega CLI creates standardized project structures to help you get started quickly with blockchain development on the Mega network. This guide explains the organization and purpose of each component in the different project types.Full-Stack Project Structure
When you runmega init without flags, you get a full-stack project with both smart contract and frontend development environments. Here’s an overview of the key directories and files:
Key Components
Foundry App Directory
Thefoundry-app directory contains your smart contract code organized in the Foundry standard structure:
- lib/: External dependencies installed via Forge, primarily containing the
forge-stdstandard library - script/: Solidity scripts for deployment and other contract interactions, using the
.s.solsuffix - src/: Source code for your smart contracts, with
GmegaCounter.solincluded as an example - test/: Test files for your smart contracts, conventionally named with a
.t.solsuffix - foundry.toml: Configuration file for Foundry settings
Next.js App Directory
Thenext-app directory contains a modern Next.js application:
- src/app/: Page components and routes using the Next.js app router
- gmega/: Example application demonstrating integration with Mega blockchain
- src/components/: Reusable React components, including a
Navbar.js - src/config/: Configuration files for the application
- src/constants/: JavaScript constants, likely including contract addresses
- src/context/: React context providers for state management
- public/: Static assets including logos and images
Frontend-Only Project Structure
When you create a frontend-only project withmega init --frontend, you get a structure similar to the next-app directory in the full-stack project:
Foundry-Only Project Structure
When you create a Foundry-only project withmega init --foundry, you get the standard Foundry structure:
Understanding File Naming Conventions
Solidity Files
- Contract files: Placed in the
src/directory with a.solextension - Test files: Placed in the
test/directory with a.t.solextension - Script files: Placed in the
script/directory with a.s.solextension
Next.js Files
- Page components: Named
page.jsin the appropriate directory undersrc/app/ - Layout components: Named
layout.jsin the appropriate directory undersrc/app/ - Components: Organized in the
src/components/directory
Generated Directories
Some directories are generated during the build process and should not be edited directly:- out/: Contains compiled Solidity artifacts
- cache/: Contains cache files for faster compilation
- node_modules/: Contains JavaScript dependencies (for Next.js projects)
Best Practices
- Keep contract code in the
src/directory and only test code in thetest/directory - Follow the Foundry naming conventions for test files (
.t.sol) and script files (.s.sol) - Use the
script/directory for deployment scripts rather than ad-hoc deployment - Store deployed contract addresses in the frontend’s
src/constants/directory - Customize the
app/gmega/example to build your application’s functionality
Next Steps
- See detailed examples of common workflows
- Check out the Foundry Book for more about Foundry project structure

