Project Structure
Understand the files and directories generated by the Midnight Contracts Wizard
When you create a project with the Midnight Contracts Wizard, it generates a complete directory structure with all necessary configuration files. This guide explains each component and how to work with it.
Directory overview
my-project/
├── src/
│ ├── tokenization/ # Contract source (if selected)
│ │ └── token.compact
│ ├── staking/ # Contract source (if selected)
│ │ └── staking.compact
│ ├── identity/ # Contract source (if selected)
│ │ └── identity.compact
│ ├── oracle/ # Contract source (if selected)
│ │ └── oracle.compact
│ ├── lending/ # Contract source (if selected)
│ │ └── lending.compact
│ └── managed/ # Compiled output (auto-generated)
│ └── *.ts
├── dist/ # Build output
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── tsconfig.build.json # Production build config
└── README.md # Project documentationSource directory (src/)
The src/ directory contains your contract source files and compiled output.
Contract directories
Each selected contract gets its own directory with .compact source files:
src/
├── tokenization/
│ └── token.compact # Tokenization contract source
├── identity/
│ └── identity.compact # Identity contract source
└── lending/
└── lending.compact # Lending contract sourceManaged directory (src/managed/)
The managed/ directory contains compiled TypeScript files generated from your .compact contracts.
src/managed/
├── tokenization.ts # Compiled tokenization contract
├── identity.ts # Compiled identity contract
└── lending.ts # Compiled lending contractImportant: Do not edit files in src/managed/ manually. They are regenerated each time you compile your contracts.
Configuration files
package.json
Contains project metadata, dependencies, and build scripts:
{
"name": "my-project",
"version": "1.0.0",
"type": "module",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"compile": "compact-cli compile src/**/*.compact",
"clean": "rm -rf dist src/managed"
},
"dependencies": {
"@midnight-ntwrk/compact-runtime": "^0.8.1",
"@midnight-ntwrk/midnight-js-types": "^2.0.2"
},
"devDependencies": {
"typescript": "^5.0.0"
}
}| Script | Purpose |
|---|---|
npm run build | Compile TypeScript to JavaScript |
npm run compile | Compile Compact contracts to TypeScript |
npm run clean | Remove generated files |
tsconfig.json
Main TypeScript configuration for development:
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["ES2020"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"rootDir": "./src",
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}tsconfig.build.json
Extended configuration for production builds:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"exclude": ["**/*.test.ts", "**/*.spec.ts"]
}Build output (dist/)
After running npm run build, compiled JavaScript and type definitions appear in dist/:
dist/
├── tokenization.js # Compiled JavaScript
├── tokenization.d.ts # Type definitions
├── identity.js
├── identity.d.ts
└── ...Contract source files
Compact file structure
Each .compact file contains the contract definition and ZK circuits:
// Example: src/tokenization/token.compact
contract Token {
// State variables
ledger balances: Map<Address, Secret<u64>>;
// ZK circuits
circuit mint(amount: Secret<u64>) -> Public<bool> {
// Minting logic with ZK proof
}
circuit transfer(to: Address, amount: Secret<u64>) -> Public<bool> {
// Transfer logic with ZK proof
}
// Additional circuits...
}Understanding circuits
Each circuit in the contract corresponds to a ZK proof:
| Circuit | Purpose |
|---|---|
mint | Create new tokens with hidden amount |
transfer | Move tokens with hidden sender/amount |
burn | Destroy tokens with hidden amount |
balance | Prove balance threshold without revealing exact amount |
Working with the project
Modify contracts
- Edit
.compactfiles insrc/[contract]/ - Recompile with
npm run compile - Build with
npm run build
Add new contracts
- Create a new directory in
src/ - Add your
.compactfile - Compile and build
Integrate with your dApp
Import compiled contracts in your application:
import { TokenizationContract } from './managed/tokenization';
import { MidnightSetupAPI } from '@meshsdk/midnight-setup';
const contract = new TokenizationContract({
// Contract configuration
});
const api = await MidnightSetupAPI.deployContract(providers, contract);Troubleshooting
Missing managed files
Problem: src/managed/ is empty or missing files.
Solution: Run the Compact compiler:
npm run compileTypeScript errors in managed files
Problem: Type errors in src/managed/*.ts.
Solution:
- Delete
src/managed/ - Recompile contracts:
npm run compile - Ensure Compact compiler version matches dependencies
Build fails
Problem: npm run build fails with errors.
Solutions:
- Run
npm installto ensure dependencies are installed - Check that
src/managed/contains compiled contracts - Verify
tsconfig.jsonis valid JSON
Next steps
- Contracts - Learn about each contract template
- Midnight Setup - Deploy contracts to the network
- API Reference - Integrate with your dApp