UNPKG

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
# ๐ŸŒ™ Midnight Smart Contract Scaffold [![Midnight Network](https://img.shields.io/badge/Midnight-Network-blue)](https://midnight.network) [![TypeScript](https://img.shields.io/badge/TypeScript-5.8-blue)](https://typescriptlang.org) [![Node.js](https://img.shields.io/badge/Node.js-18+-green)](https://nodejs.org) [![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](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.