Project Structure
Understanding the directory structure of Mega CLI projects
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 run mega 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
The foundry-app
directory contains your smart contract code organized in the Foundry standard structure:
- lib/: External dependencies installed via Forge, primarily containing the
forge-std
standard library - script/: Solidity scripts for deployment and other contract interactions, using the
.s.sol
suffix - src/: Source code for your smart contracts, with
GmegaCounter.sol
included as an example - test/: Test files for your smart contracts, conventionally named with a
.t.sol
suffix - foundry.toml: Configuration file for Foundry settings
Next.js App Directory
The next-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 with mega init --frontend
, you get a structure similar to the next-app
directory in the full-stack project:
This structure focuses solely on the frontend implementation, allowing you to connect to existing contracts on the Mega testnet.
Foundry-Only Project Structure
When you create a Foundry-only project with mega init --foundry
, you get the standard Foundry structure:
This structure focuses entirely on smart contract development, ideal for developers who only need to write and deploy contracts.
Understanding File Naming Conventions
Solidity Files
- Contract files: Placed in the
src/
directory with a.sol
extension - Test files: Placed in the
test/
directory with a.t.sol
extension - Script files: Placed in the
script/
directory with a.s.sol
extension
Next.js Files
- Page components: Named
page.js
in the appropriate directory undersrc/app/
- Layout components: Named
layout.js
in 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
Was this page helpful?