Mesh LogoMesh
MidnightMidnight Contracts Wizard

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 documentation

Source 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 source

Managed 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 contract

Important: 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"
  }
}
ScriptPurpose
npm run buildCompile TypeScript to JavaScript
npm run compileCompile Compact contracts to TypeScript
npm run cleanRemove 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:

CircuitPurpose
mintCreate new tokens with hidden amount
transferMove tokens with hidden sender/amount
burnDestroy tokens with hidden amount
balanceProve balance threshold without revealing exact amount

Working with the project

Modify contracts

  1. Edit .compact files in src/[contract]/
  2. Recompile with npm run compile
  3. Build with npm run build

Add new contracts

  1. Create a new directory in src/
  2. Add your .compact file
  3. 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 compile

TypeScript errors in managed files

Problem: Type errors in src/managed/*.ts.

Solution:

  1. Delete src/managed/
  2. Recompile contracts: npm run compile
  3. Ensure Compact compiler version matches dependencies

Build fails

Problem: npm run build fails with errors.

Solutions:

  1. Run npm install to ensure dependencies are installed
  2. Check that src/managed/ contains compiled contracts
  3. Verify tsconfig.json is valid JSON

Next steps

On this page