create-midnight-app
Version:
๐ Scaffold for building Midnight smart contracts with dynamic type detection and automated CLI generation
431 lines (324 loc) โข 13.2 kB
Markdown
# ๐ Midnight Smart Contract Scaffold
[](https://midnight.network)
[](https://typescriptlang.org)
[](https://nodejs.org)
[](LICENSE)
**The complete development scaffold for Midnight smart contracts.** Write your `.compact` contract and instantly get a full development environment with zero configuration. The system automatically generates a dynamic CLI, handles deployment pipelines, manages wallet operations, and provides a complete testing framework.
**๐ From contract to production in minutes, not hours.**
## โจ What You Get
- ๐ **Auto-generating CLI** that adapts to your contract functions
- ๐ **Seamless testnet deployment** with automated wallet management
- ๐ฐ **Built-in wallet tools** for balance checking and faucet requests
- ๐ **Smart contract analysis** with automatic type generation
- ๐ **Comprehensive documentation** and debugging guides
## ๐ Quick Start
**Get started with Midnight development in under 2 minutes:**
```bash
# Clone the scaffold
git clone https://github.com/kaleababayneh/scaffold-midnight.git
cd scaffold-midnight
# Install dependencies
npm install
# Create your first contract
touch my-contract.compact
# set up your wallet env
mv .env.example .env
# Deploy and test (one command!)
npm run deploy
```
### ๐ Prerequisites
**System Requirements:**
- **Node.js 20**
- **Compact Compiler**
- **Docker Desktop**
### ๐ **Write Your First Contract**
Edit your `.compact` file in the project root:
```compact
pragma language_version 0.15;
import CompactStandardLibrary;
export ledger counter: Counter;
export circuit increment(value: Uint<16>): [] {
counter.increment(value);
}
export circuit get_count(): Uint<64> {
return counter;
}
```
### ๐ฏ **Generate Everything Automatically**
```bash
npm run dev
```
This single command:
- ๐ Syncs your contract to the build system
- ๐จ Compiles contract and generates ZK keys
- ๐ Creates TypeScript types and API functions
- ๐ฅ๏ธ Builds a dynamic CLI that adapts to your contract
- โ
Everything ready to use!
### ๐ **Deploy & Test**
**For testnet deployment:**
```bash
npm run wallet
```
## ๐ Features
### โจ **Zero-Configuration Development**
- **Auto-Detection**: Automatically finds and analyzes your `.compact` contracts
- **Zero Manual Updates**: Change functions โ regenerate โ everything adapts automatically
- **Smart Contract Analysis**: Works with any contract structure and function names
- **Witness Function Support**: Automatically detects and includes private state functions
### ๐ **Complete Deployment Pipeline**
- **One-Command Local Deployment**: `npm run deploy` for full local development setup
- **Docker Integration**: Automatically manages local Midnight node containers
- **Testnet Deployment**: Seamless testnet integration with `npm run wallet`
- **Dry Run Support**: Preview deployment with `--dry-run` flag
### ๐ง **Developer-Friendly Workflow**
- **Root-Level Editing**: Edit contracts directly in project root for easy access
- **Auto-Sync Pipeline**: Automatically syncs contracts to build directories
- **Clean Builds**: Removes old artifacts before rebuilding
- **Hot Reload**: Instant updates when contract changes
- **Comprehensive Compilation**: Handles ZK proof generation and TypeScript building
### ๐ฐ **Wallet & Balance Management**
- **Automatic Wallet Generation**: Creates secure wallets with one command
- **Balance Checking**: Real-time testnet balance monitoring
- **Faucet Integration**: Automated and manual token requesting
- **Transaction Management**: Full transaction lifecycle support
## ๐ Project Structure
```
example-counter/
โโโ my-contract.compact # ๐ Edit your contract here (root level)
โโโ package.json # ๐ฆ Main project configuration
โโโ .env # ๐ Wallet configuration (auto-generated)
โโโ boilerplate/
โ โโโ contract/ # ๐จ Contract compilation workspace
โ โ โโโ src/ # ๐ Auto-synced contracts & witnesses
โ โ โโโ my-contract.compact # โ Synced from root
โ โ โโโ witnesses.ts # ๐ Private state functions
โ โ โโโ managed/ # ๐๏ธ Compiled output & ZK keys
โ โโโ contract-cli/ # ๐ฅ๏ธ Generated CLI application
โ โ โโโ src/ # ๐ฏ Dynamic CLI code
โ โ โ โโโ api.ts # ๐ Contract interaction API
โ โ โ โโโ cli.ts # ๐ฅ๏ธ Interactive CLI interface
โ โ โ โโโ enhanced-api.ts # ๐ Contract metadata & analysis
โ โ โโโ standalone.yml # ๐ณ Docker configuration for local node
โ โโโ scripts/ # โ๏ธ Build & deployment automation
โ โโโ auto-generator.js # ๐ Core auto-generation engine
โ โโโ deploy.js # ๐ Deployment orchestrator
โ โโโ check-balance.js # ๐ฐ Wallet balance checker
โ โโโ request-faucet.js # ๐ฐ Testnet token requests
โโโ README.md # ๐ This documentation
```
### ๐ **How the Auto-Sync Works**
1. **๐ Edit**: Modify your `.compact` contract in the project root
2. **๐ Sync**: `npm run dev` copies it to `boilerplate/contract/src/`
3. **๐จ Compile**: Contract compiles to `managed/` with ZK keys
4. **๐ Generate**: TypeScript types and API functions auto-generated
5. **๐ฅ๏ธ Build**: CLI updates with new contract functions
6. **โ
Ready**: Everything synchronized and ready to use
## ๐ Development Workflow
### **1. Edit Contract (Root Level)**
```bash
# Edit your contract file in the project root
nano my-contract.compact # Use any editor you prefer
```
### **2. Auto-Generate Everything**
```bash
npm run dev
```
This command automatically:
- ๐ Syncs your contract from root to build directory
- ๐จ Compiles contract with `compactc` and generates ZK keys
- ๐ Updates TypeScript types and API functions
- ๐ฅ๏ธ Rebuilds CLI with new contract functions
- ๐ Detects witness functions and includes them
- โ
Everything synchronized and ready!
### **3. Deploy & Test**
**Local Development (Recommended):**
```bash
npm run deploy
```
- Starts local Midnight node in Docker
- Deploys your contract locally
- Launches interactive CLI for testing
**Testnet Deployment:**
```bash
npm run wallet
```
- Connects to live Midnight testnet
- Handles wallet creation and funding
- Deploys to public testnet
### **4. Iterate and Improve**
```bash
# Make changes to your contract
vim my-contract.compact
# Regenerate CLI (fast)
npm run dev
# Test changes
npm run deploy
```
## ๐ง Troubleshooting
### ๐จ Common Issues & Solutions
| Issue | Symptoms | Solution |
|-------|----------|----------|
| **Contract not detected** | "No .compact files found" | Ensure `.compact` file is in project root |
| **Compilation errors** | Build fails, ZK key generation errors | Check `pragma language_version` directive |
| **CLI generation failed** | Missing menu options | Verify `export circuit` function syntax |
| **Docker issues** | Local deployment fails | Ensure Docker Desktop is running |
| **Testnet connection** | Wallet sync timeouts | Check internet connection, try `npm run balance` |
| **Zero balance** | Deployment fails with "insufficient funds" | Run `npm run faucet` or use manual faucet |
| **Permission errors** | File system access denied | Run `sudo npm run deploy` or fix file permissions |
### ๐ Debug Commands
```bash
# Check system requirements
node --version && docker --version && git --version
# Verify contract syntax
npm run build
# Check wallet status
npm run balance
# View detailed logs
npm run deploy --dry-run
# Test local environment
docker ps
```
## ๐๏ธ Commands Reference
| Command | Purpose | Environment |
|---------|---------|-------------|
| `npm run dev` | ๐ Regenerate CLI from contract changes | Local |
| `npm run deploy` | ๐ Deploy to Midnight testnet | Testnet |
| `npm run wallet` | ๐ ๐ Generate new wallet seed and address | Testnet |
| `npm run balance` | ๐ฐ Check current wallet balance | Testnet |
| `npm run faucet` | ๐ฐ Request testnet tokens | Testnet |
| `npm run build` | ๐จ Build all workspaces | Local |
## ๐ Deployment Guide
**Quick testnet deployment:**
```bash
npm run deploy
```
This command:
1. ๐จ Compiles your contract and generates CLI
3. ๐ฆ Deploys contract to midnight testnet network
4. ๐ฏ Launches interactive CLI for testing
### ๐ Testnet Deployment
**Interactive testnet deployment:**
```bash
npm run wallet
```
**Automated testnet deployment:**
```bash
# Deploy new contract automatically
npm run deploy --new
# Join existing contract
npm run deploy --join
# Preview commands (dry run)
npm run deploy --dry-run
```
### ๐ง Deployment Flags
| Flag | Behavior | Use Case |
|------|----------|----------|
| `--new` | Auto-deploy new contract | CI/CD pipelines |
| `--join` | Auto-join existing contract | Team collaboration |
| `--dry-run` | Preview commands only | Testing scripts |
| `--help` | Show detailed help | Documentation |
## ๐๏ธ How It Works
### **Auto-Detection System**
The system automatically:
1. **Scans** the root directory for `.compact` files
2. **Copies** them to the contract source directory (replacing old ones)
3. **Analyzes** contract functions and ledger state
4. **Generates** TypeScript types and API functions
5. **Builds** a dynamic CLI that adapts to your contract
### **Function Discovery**
```javascript
// Automatically detected from your contract:
export circuit increment(value: Uint<16>): [] { ... }
export circuit get_count(): Uint<64> { ... }
// Becomes CLI options:
// 1. Increment (1 param)
// 2. Get Count (read-only)
```
### **Smart CLI Generation**
- **Parameter Detection**: Automatically detects function parameters
- **Type Safety**: Generates TypeScript interfaces
- **Read-Only Functions**: Identifies and marks query functions
- **Interactive Menus**: Creates numbered options for all functions
## โ๏ธ Advanced Configuration
### ๐จ Custom Contract Examples
**Voting Contract:**
```compact
pragma language_version 0.15;
import CompactStandardLibrary;
export ledger votes: VotingState;
export circuit vote_for(candidate: Uint<8>): [] {
votes.vote(candidate);
}
export circuit get_results(): VotingResults {
return votes.getResults();
}
```
### ๐ง Environment Variables
Create a `.env` file for configuration:
```bash
# Wallet Configuration
WALLET_SEED=your-64-character-hex-seed-phrase
WALLET_ADDRESS=your-wallet-address
## ๐ฐ Wallet Management
### ๐ Wallet Creation
**Option 1: Auto-generate wallet (Recommended)**
```bash
npm run wallet
```
**Option 2: Manual setup**
```bash
# Copy example configuration
cp .env.example .env
# Edit .env file and add your seed
WALLET_SEED=your-64-character-hex-seed-phrase-here
```
### ๐ฐ Balance Checking
**Check wallet balance:**
```bash
npm run balance
```
### ๐ฐ Getting Testnet Tokens
**Automated faucet request:**
```bash
npm run faucet
```
## ๐ ๏ธ Architecture
### **Auto-Generator Pipeline**
```mermaid
graph LR
A[Root .compact] --> B[Sync to src/]
B --> C[Compile Contract]
C --> D[Generate ZK Keys]
D --> E[Update TypeScript]
E --> F[Build CLI]
F --> G[Ready to Use!]
```
### **CLI Components**
- **Contract Analyzer**: Parses contract functions and types
- **Dynamic Generator**: Creates CLI menus and handlers
- **API Layer**: Handles contract interactions
- **Wallet Integration**: Manages testnet connections
## ๐ค Contributing
We welcome contributions! Here's how to get started:
### ๐ Contribution Guidelines
**Types of contributions welcome:**
- ๐ **Bug fixes** - Fix issues with the scaffold
- โจ **New features** - Add functionality to improve developer experience
- ๐ **Documentation** - Improve guides, examples, and API docs
- ๐จ **UI/UX** - Enhance CLI interface and user experience
- ๐งช **Tests** - Add test coverage and improve reliability
## ๐ Project Status
| Feature | Status | Notes |
|---------|--------|-------|
| โ
Auto-CLI Generation | Complete | Supports all contract types |
| โ
Local Development | Complete | Docker-based Midnight node |
| โ
Testnet Deployment | Complete | Automated wallet management |
| โ
Deployment Flags | Complete | |
| โ
Wallet Tools | Complete | Balance checking, faucet |
| โ
Documentation | Complete | Comprehensive guides |
| ๐ Multi-Contract Support | Planned | Support multiple contracts |
| ๐ GUI Interface | Planned | Web-based contract interaction |
| ๐ Contract Templates | Planned | Pre-built contract examples |
**Last Updated**: June 13, 2025
Built with โค๏ธ for the Midnight ecosystem ๐
Empowering developers to build privacy-preserving applications with zero-knowledge proofs.