create-fhevm-example
v1.5.4
Published
Create FHEVM example projects with a single command - A comprehensive toolkit for building privacy-preserving smart contracts
Maintainers
Readme
📖 Technical Deep Dive: See OVERVIEW.md for architecture decisions, design patterns, and detailed module documentation.
Overview
This project provides tools and examples for building privacy-preserving smart contracts using FHEVM by Zama.
Key Features
- 🎯 33 Examples - encryption, decryption, operations, OpenZeppelin and more
- 🛠️ Interactive CLI - Modern command-line interface with beautiful prompts
- 📦 Standalone Projects - Generate, runnable Hardhat projects from any example
- 📚 Auto Documentation - GitBook-formatted documentation automatically
- 🔗 Hardhat Template - Pre-configured template with all FHEVM dependencies
Quick Start
💡 Recommended: Use
npx create-fhevm-example- no installation needed!
Option 1: NPX (Recommended)
# Interactive mode
npx create-fhevm-example
# Quick commands
npx create-fhevm-example --example fhe-counter
npx create-fhevm-example --category basic --output ./my-project
# Add example to existing Hardhat project
npx create-fhevm-example --addOption 2: Clone Repository
For local development and contributing:
git clone https://github.com/NecipAkgz/fhevm-example-factory.git
cd fhevm-example-factory
npm installInteractive Mode:
npm run createQuick Commands:
# Generate a single example
npm run create:example fhe-counter ./my-fhe-counter
# Generate a category bundle
npm run create:category basic ./my-basic-examples
# Generate documentation
npm run generate:docs [example] # No arg = all docs, with name = specific doc
# Show help
npm run help:create📦 NPM Package
Published as create-fhevm-example on NPM, this package allows you to create FHEVM projects without cloning this repository.
Advantages:
- 🚀 No Repository Clone - Install and run directly via
npx - ⚡ Offline Capable - All files bundled, no network needed during scaffolding
- 🔧 Works Anywhere - No local dependencies or setup required
- 🎯 Production Ready - Ideal for scaffolding new dApps or integrating into existing projects
Interactive Mode
npx create-fhevm-exampleQuick Commands
# Create single example
npx create-fhevm-example --example fhe-counter
# Create category project
npx create-fhevm-example --category basic --output ./my-projectAdd to Existing Project
Already have a Hardhat project? Inject FHEVM capabilities without starting from scratch:
# Add FHEVM example to existing Hardhat project
npx create-fhevm-example --add
npx create-fhevm-example --add --target ./my-existing-projectThis will:
- ✅ Detect your Hardhat project
- ✅ Add FHEVM dependencies to
package.json - ✅ Update
hardhat.config.tswith FHEVM plugin - ✅ Add an example contract and test of your choice
- ✅ Handle file conflicts intelligently (skip/overwrite/rename)
Note: The
--addfeature is available throughnpx create-fhevm-exampleonly.
📋 Available Examples
33 examples total - Click to expand each category:
fhe-counter- Simple encrypted counter demonstrating FHE basicsencrypt-single-value- FHE encryption mechanism and common pitfallsencrypt-multiple-values- Handling multiple encrypted values in one transaction
user-decrypt-single-value- User decryption with permission requirementsuser-decrypt-multiple-values- Decrypting multiple values for a userpublic-decrypt-single-value- On-chain public decryption of a single valuepublic-decrypt-multiple-values- On-chain public decryption of multiple values
fhe-add- Addition operations on encrypted valuesfhe-arithmetic- All arithmetic: add, sub, mul, div, rem, min, maxfhe-comparison- All comparisons: eq, ne, gt, lt, ge, le, selectfhe-if-then-else- Conditional operations on encrypted values
fhe-access-control-FHE.allow,FHE.allowThis,FHE.allowTransientpatternsfhe-input-proof- Input proof validation, batched inputsfhe-handles- Handle creation, computation, immutabilityfhe-edge-cases- Overflow, underflow, gas benchmarks, permission edge cases
Anti-Patterns:
control-flow- Conditional logic and loop anti-patterns with encrypted valuespermissions- Permission management mistakes (allowThis, allow, cross-contract)operations-gas-noise- Performance issues, side-channels, inefficient operations
rock-paper-scissors- Encrypted moves with FHE commit-reveal patternencrypted-lottery- Private lottery with encrypted ticket numbersencrypted-poker- Texas Hold'em with hidden hole cardsencrypted-blackjack- Blackjack with encrypted cards and bust detection
erc7984- Confidential token standard (ERC7984)erc7984-erc20-wrapper- Wrap ERC20 into confidential ERC7984swap-erc7984-to-erc20- Swap confidential to public tokensswap-erc7984-to-erc7984- Fully confidential atomic swapsvesting-wallet- Linear vesting with encrypted amounts
blind-auction- Encrypted bids, winner computed via FHE.gt/selecthidden-voting- Homomorphic vote tallying, private ballotsprivate-payroll- Confidential salary payments with encrypted amountsencrypted-escrow- Secure escrow with hidden amounts until releaseprivate-kyc- Identity verification with predicate proofs (age, credit score)private-order-book- MEV-resistant trading with encrypted orders
🔧 Automation Tools
For local development, the repository provides automation tools:
1. Standalone Project Generator
Creates a production-ready Hardhat environment tailored for a single example:
- 🏗️ Scaffolds a new clean project using the official
fhevm-hardhat-template - 📋 Copies contracts and tests from the bundled package
- ⚙️ Configures Hardhat and generates deployment scripts automatically
- 🔧 Handles contract dependencies and npm packages automatically
npm run create:example fhe-counter ./my-counter
# Or use the interactive CLI:
npm run create2. Category Bundle Generator
Generates a unified workspace containing all examples from a specific category:
- 📦 Bundles multiple contracts into a single project
- 🧪 Consolidates all test files ensuring they run in harmony
- 🔗 Manages shared dependencies across examples automatically
npm run create:category openzeppelin ./my-oz-examples
# Or use the interactive CLI:
npm run create3. Documentation Engine
Automatically builds GitBook-ready markdown files directly from your source code.
- 🔍 Extracts code snippets from Contracts and Tests
- 🔐 Auto-generates FHE API Reference (functions & types used)
- 🎨 Formats content into clean, tabbed markdown views
- 📑 Updates the documentation index (
SUMMARY.md)
npm run generate:docs fhe-counter # Single example
npm run generate:docs # All examples💻 CLI Reference
Local Development:
npm run create- Interactive CLInpm run create:example [name] [path]- Generate single examplenpm run create:category [name] [path]- Generate category projectnpm run generate:docs [example]- Generate docs (all or specific)npm run generate:config- Auto-discover contracts and generate confignpm run test- Test examples (interactive selection)npm run test:all- Test all examples directlynpm run doctor- Validate environment, submodule status, and config integritynpm run help:create- Show help information
NPM Package:
npx create-fhevm-example- Interactive modenpx create-fhevm-example --example <name>- Create single examplenpx create-fhevm-example --category <name>- Create category projectnpx create-fhevm-example --add- Add to existing Hardhat projectnpx create-fhevm-example --help- Show help
🔄 Maintenance & Testing
Test Examples
Test selected examples in a unified project for fast, efficient verification:
# Interactive mode - select with space, confirm with enter
npm run test
# Run all tests directly
npm run test:all
# Quick mode - test specific examples
npm run test fhe-counter,fhe-add💡 Selected examples are bundled into a single project, so dependencies install once and all tests run together.
📂 Project Structure
├── 📁 fhevm-hardhat-template/ # Base Hardhat template (git submodule)
│ ├── contracts/ # Template contract
│ ├── test/ # Template tests
│ ├── deploy/ # Deployment scripts
│ └── hardhat.config.ts # Hardhat configuration
│
├── 📁 contracts/ # All example contracts
│ ├── basic/ # Basic FHE operations
│ ├── concepts/ # Critical FHEVM concepts
│ └── openzeppelin/ # OpenZeppelin integration
│
├── 📁 test/ # All test files (mirrors contracts/)
│
├── 📁 docs/ # Generated GitBook documentation
│
├── 📁 scripts/ # CLI source code
│ ├── index.ts # Main CLI entry point
│ ├── shared/ # Shared utilities
│ │ ├── config.ts # Auto-generated example registry
│ │ ├── utils.ts # Core utilities (logging, naming)
│ │ ├── generators.ts # Template & code generation
│ │ ├── builders.ts # Project scaffolding logic
│ │ └── ui.ts # Interactive prompts
│ └── commands/ # CLI commands
│ ├── add-mode.ts # Add FHEVM to existing projects
│ ├── doctor.ts # Environment health checker
│ ├── generate-config.ts # Auto-discover contracts
│ ├── generate-docs.ts # Documentation generator
│ └── maintenance.ts # Test all examples runner
│
└── README.md # This file🛠️ Creating a New Example
For contributors adding new examples:
Create Folder (if new category)
mkdir -p contracts/your-category mkdir -p test/your-category💡 Convention-Based Categories: Folder name becomes category name
contracts/gaming/→ "Gaming" categorycontracts/defi-lending/→ "Defi Lending" category
Write Contract in
contracts/<category>/YourExample.sol/** * @notice Your contract description here - this becomes the example description! * * @dev Technical details... */ contract YourExample { // Implementation }⚠️ Required:
@noticetag is mandatory for auto-discoveryWrite Tests in
test/<category>/YourExample.tsGenerate Configuration (Auto-Discovery)
npm run generate:config # Scans contracts, extracts @notice tags📝 Note: If your example requires external dependencies, manually add them to
scripts/shared/config.ts:"your-example": { npmDependencies: { "@openzeppelin/contracts": "^5.4.0" // NPM packages }, dependencies: [ "contracts/mocks/SomeMock.sol" // Contract files ], }Test Standalone Repository
npm run create:example your-example ./test-output cd test-output npm install && npm run compile && npm run test
🔄 Updating FHEVM Dependencies
When @fhevm/solidity or related packages release new versions:
Update the submodule template:
git submodule update --remote --mergeTest all examples for compatibility:
npm run test:allRegenerate documentation if APIs changed:
npm run generate:docs
🔗 Resources
🤝 Contributing
Contributions are welcome! When adding examples:
- ✅ Follow existing patterns and structure
- ✅ Include comprehensive inline comments
- ✅ Add
@noticetag to contract for auto-discovery - ✅ Demonstrate both correct and incorrect usage
- ✅ Run
npm run generate:configto auto-generate configuration - ✅ Test generated standalone repository
- ✅ Verify documentation renders correctly
