UNPKG

create-midnight-app

Version:

๐ŸŒ™ Scaffold for building Midnight smart contracts with dynamic type detection and automated CLI generation

430 lines (318 loc) โ€ข 15.2 kB
# ๐ŸŒ™ Create Midnight App [![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-20-green)](https://nodejs.org) [![NPM](https://img.shields.io/badge/NPM-2.0.0-red)](https://npmjs.com/package/create-midnight-app) **The fastest way to build Midnight smart contracts with automated CLI generation.** Create a new Midnight smart contract project in seconds with zero configuration. This scaffold provides everything you need to build, test, and deploy smart contracts on the Midnight blockchain with enhanced data type support and professional tooling. The scaffold comes with pre-generated wallet. **๐Ÿš€ From contract to production in minutes, not hours.** ## โœจ Developer Quote > **"It feels almost like magic :)"** > โ€” *Midnight developers on* `create-midnight-app` ## โœจ What You Get - ๐Ÿš€ **Zero Configuration** - Just create a `.compact` file and everything auto-generates - ๐Ÿ”„ **Auto-generating CLI** that adapts to your contract functions with intelligent parameter detection - ๐ŸŒ **Seamless testnet deployment** with automated wallet management and balance checking - ๐Ÿ’ฐ **Built-in wallet tools** for balance checking, faucet requests, and transaction management - ๐Ÿ“Š **Smart contract analysis** with automatic type generation and parameter detection - ๐Ÿ“ **Comprehensive documentation** with built-in debugging guides and troubleshooting - ๐ŸŽฏ **Enhanced Data Type Support** - Full support for all Midnight contract types including complex parameters - ๐Ÿ› ๏ธ **Professional Tooling** with error handling, validation, dry-run support, and clear feedback - ๐Ÿ” **Witness Function Support** - Automatically detects and includes private state functions ## ๐Ÿš€ Quick Start **Get started with Midnight development in under 2 minutes:** make sure you are using the latest version ```bash # Create a new project npx create-midnight-app@latest my-contract # Navigate to project cd my-contract # Set up your wallet environment mv .env.example .env # Create your smart contract touch voting.compact # deploy to testnet & start interacting npm run deploy โœ… That is ALL! ``` > โš ๏ธ **Make sure you are using Node 20** โ€” *this is important!* **๐Ÿ“ Write Your First Contract:** Edit your contract file: ```compact pragma language_version 0.15; import CompactStandardLibrary; export ledger votes: Counter; export circuit vote(): [] { votes.increment(1); } export circuit get_votes(): Uint<64> { return votes; } ``` **๐ŸŽฏ Deploy in an instant** ```bash # Auto-generate CLI, compile & deploy contract and start interacting npm run deploy ``` 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:** ```bash # For testnet wallet generation npm run wallet - # the generated wallet will automatically be saved into .env # For requesting token from faucet npm run faucet # For testnet deployment npm run deploy ``` ## ๐Ÿ“‹ Available Commands | Command | Description | Environment | |---------|-------------|-------------| | `npm run dev` | ๐Ÿ”„ Main development command - syncs, compiles, generates CLI | Local | | `npm run deploy` | ๐Ÿš€ Deploy to Midnight testnet | Testnet | | `npm run wallet` | ๐ŸŒ generate a new key-pair wallet on Midnight | Testnet | | `npm run balance` | ๐Ÿ’ฐ Check current testnet wallet balance | Testnet | | `npm run faucet` | ๐Ÿšฐ Request testnet tokens | Testnet | | `npm run check` | โœ… Verify scaffold is ready and show status | Local | | `npm run build` | ๐Ÿ”จ Build all workspaces | Local | | `npm run deploy:new` | ๐Ÿ”จ Deploy to Midnight testnet(default) | Testnet | | `npm run deploy:join` | ๐Ÿ”จ Auto-join existing contract | Testnet | | `npm run docs` | ๐ŸŒ Open the official Compact documentation | ### ๐Ÿ”ง Deployment Flags | Flag | Behavior | Use Case | |------|----------|----------| | `--dry-run` | Preview commands only | Testing scripts | | `--help` | Show detailed help | Documentation | ## ๐ŸŽฏ Development Workflow 1. **Create Contract** - Write your `.compact` file in the project root 2. **Auto-Generate** - Run `npm run dev` to generate everything automatically 3. **Deploy & Test** - Use `npm run deploy` for testnet deployment & `npm run wallet` a new wallet generation 4. **Iterate** - Edit contract, run `npm run dev/deploy`, repeat ### ๐Ÿ”„ How the Auto-Generation Works 1. **๐Ÿ“ Edit**: Modify your `.compact` contract in the project root 2. **๐Ÿ”„ Sync**: `npm run dev` copies it to build directory 3. **๐Ÿ”จ Compile**: Contract compiles with ZK keys generation 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 ## ๐Ÿš€ Enhanced Features ### โœจ **Zero-Configuration Development** - **Auto-Detection**: Automatically finds and analyzes your `.compact` contracts - **Smart Contract Analysis**: Works with any contract structure and function names - **Zero Manual Updates**: Change functions โ†’ regenerate โ†’ everything adapts automatically - **Witness Function Support**: Automatically detects and includes private state functions ### ๐ŸŒ **Complete Deployment Pipeline** - **One-Command Testnet Deployment**: `npm run deploy` for full testnet development setup - **Docker Integration**: Automatically manages local Midnight node containers - **Testnet Deployment**: Seamless testnet integration with automated wallet management - **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 - **Enhanced Data Type Support**: Full support for complex Midnight contract types ### ๐Ÿ’ฐ **Wallet & Balance Management** - **Automatic Wallet Generation**: Creates secure wallets with one command - **Balance Checking**: Real-time testnet balance monitoring with `npm run balance` - **Faucet Integration**: Automated and manual token requesting with `npm run faucet` - **Transaction Management**: Full transaction lifecycle support ## ๐Ÿ—๏ธ Project Structure ``` my-project/ โ”œโ”€โ”€ my-contract.compact โ† ๐Ÿ“ Your smart contract (edit here!) โ”œโ”€โ”€ package.json โ† ๐Ÿ“ฆ Main project configuration โ”œโ”€โ”€ .env โ† ๐Ÿ” Wallet configuration (auto-generated) โ”œโ”€โ”€ README.md โ† ๐Ÿ“– Project documentation โ””โ”€โ”€ boilerplate/ โ† ๐Ÿ—๏ธ Auto-generated tooling โ”œโ”€โ”€ 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 with enhanced data type support โ”‚ โ”‚ โ”œโ”€โ”€ 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 ``` ## ๐Ÿ”ง How It Works The scaffold uses **source-driven development**: 1. **Contract Analysis** - Parses your `.compact` file to find all functions 2. **Dynamic Generation** - Creates TypeScript APIs for each function 3. **CLI Creation** - Builds interactive menus for contract interaction 4. **Testnet Integration** - Provides wallet and deployment tools ## ๐Ÿ“š Examples & Contract Templates ### ๐Ÿ”ข Simple Counter ```compact pragma language_version 0.15; import CompactStandardLibrary; export ledger count: Counter; export circuit increment(value: Uint<16>): [] { count.increment(value); } export circuit decrement(value: Uint<16>): [] { count.decrement(value); } export circuit get_count(): Uint<64> { return count; } ``` ### ๐Ÿ—ณ๏ธ Voting System with Enhanced Data Types ```compact pragma language_version 0.15; import CompactStandardLibrary; export ledger votes_for: Counter; export ledger votes_against: Counter; export circuit vote_yes(): [] { votes_for.increment(1); } export circuit vote_no(): [] { votes_against.increment(1); } export circuit get_results(): [Uint<64>, Uint<64>] { return [votes_for, votes_against]; } ``` ## ๐Ÿ—๏ธ 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 with enhanced data type support 5. **Builds** a dynamic CLI that adapts to your contract ### **Function Discovery & Enhanced Data Types** ```javascript // Automatically detected from your contract: export circuit increment(value: Uint<16>): [] { ... } export circuit post(message: Opaque<"string">): [] { ... } export circuit get_count(): Uint<64> { ... } // Becomes CLI options with intelligent parameter handling: // 1. Increment (numeric parameter with validation) // 2. Post (string parameter with opaque type conversion) // 3. Get Count (read-only function) ``` ### **Smart CLI Generation** - **Parameter Detection**: Automatically detects function parameters and types - **Enhanced Data Type Support**: Handles complex Midnight types including opaque strings - **Type Safety**: Generates TypeScript interfaces with full type checking - **Read-Only Functions**: Identifies and marks query functions - **Interactive Menus**: Creates numbered options for all functions - **Input Validation**: Validates parameters before contract calls ## ๐Ÿ› ๏ธ Requirements **System Requirements:** - **Node.js 20** (It must be 20!) - **NPM** - **Midnight Development Tools** - Install `compactc` compiler - **Docker Desktop** (for local development) ### ๐Ÿ“‹ Prerequisites ```bash # Check system requirements node --version && docker --version && git --version # Install Midnight compiler (if not installed) # Follow Midnight documentation for compactc installation ``` ## ๐Ÿ”ง Troubleshooting ### ๐Ÿšจ Common Issues & Solutions | Issue | Symptoms | Solution | |-------|----------|----------| | **Contract not detected** | "No .compact files found" | Ensure `.compact` file is in project root | | **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 with appropriate permissions or check file ownership | ### ๐Ÿ” Debug Commands ```bash # 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 ``` ## โš™๏ธ Advanced Configuration ### ๐Ÿ”ง 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 **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 & Faucet Management ```bash # Check wallet balance npm run balance # Request testnet tokens npm run faucet ``` ## ๐Ÿ› ๏ธ Architecture ### **Auto-Generator Pipeline** ``` Root .compact โ†’ Sync to src/ โ†’ Compile Contract โ†’ Generate ZK Keys โ†’ Update TypeScript โ†’ Build CLI โ†’ Ready to Use! ``` ### **CLI Components** - **Contract Analyzer**: Parses contract functions and types with enhanced data type support - **Dynamic Generator**: Creates CLI menus and handlers for all parameter types - **API Layer**: Handles contract interactions with type conversion - **Wallet Integration**: Manages testnet connections and balance checking ## ๐Ÿค Contributing We welcome contributions! Here's how to get started: ### ๐Ÿ“‹ Contribution Guidelines **Types of contributions welcome:** - ๐Ÿ› **Bug fixes** - Fix issues with the scaffold or data type handling - โœจ **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 - ๐Ÿ”ง **Data Type Support** - Improve handling of complex Midnight types ## ๐Ÿ“Š Project Status | Feature | Status | Notes | |---------|--------|-------| | โœ… Auto-CLI Generation | Complete | Supports all contract types | | โœ… Enhanced Data Type Support | Complete | Full support for complex Midnight types | | โœ… Local Development | Complete | Docker-based Midnight node | | โœ… Testnet Deployment | Complete | Automated wallet management | | โœ… Deployment Flags | Complete | Dry-run, auto-deploy options | | โœ… Wallet Tools | Complete | Balance checking, faucet integration | | โœ… Documentation | Complete | Comprehensive guides and troubleshooting | | ๐Ÿ”„ Multi-Contract Support | Planned | Support multiple contracts per project | | ๐Ÿ”„ GUI Interface | Planned | Web-based contract interaction | | ๐Ÿ”„ Contract Templates | Planned | Pre-built contract examples | | ๐Ÿ”„ Midnight Playground | Planned | Online midnight experimenting tool | ## ๐Ÿ“„ License Apache 2.0 License ## ๐ŸŒŸ Why Use Create Midnight App? **Before**: Hours of manual setup, hardcoded configurations, manual CLI updates, type conversion issues **After**: Create contract โ†’ run command โ†’ deploy to testnet with full data type support This scaffold eliminates all the friction from Midnight smart contract development, handles complex data types automatically, and gets you building immediately. --- **Last Updated**: June 15, 2025 **Version**: 2.1.7 Built with โค๏ธ for the Midnight ecosystem ๐ŸŒ™ **Happy coding on Midnight! ๐ŸŒ™โœจ**