AgentSkillsCN

compact-core:contract-patterns

当您需要实现常见的Compact合约模式,例如投票系统、托管合约、代币转账、访问控制、拍卖、注册表,或在Midnight区块链开发中寻找可复用的合约模板与设计模式时,可选用此功能。

SKILL.md
--- frontmatter
name: compact-core:contract-patterns
description: Use when implementing common Compact contract patterns such as voting systems, escrow, token transfers, access control, auctions, registries, or when looking for reusable contract templates and design patterns for Midnight blockchain development.

Contract Patterns

Reusable Compact contract patterns and best practices for Midnight development

Overview

This skill provides a comprehensive catalog of Compact contract patterns for the Midnight blockchain. Patterns range from simple building blocks to comprehensive multi-contract systems.

Pattern Categories

  1. Simple Patterns (~10 focused examples)

    • Basic state management
    • Access control primitives
    • Time-based logic
    • Cryptographic commitments
  2. Deep-Dive Implementations (3 comprehensive systems)

    • Private Voting System
    • Multi-Party Token Escrow
    • Role-Based Access Registry

Quick Navigation

Simple Patterns

PatternFileUse Case
Counterexamples/simple/counter.compactBasic state increments
Ownershipexamples/simple/ownership.compactSingle-owner control
Time Lockexamples/simple/time-lock.compactDelayed execution
Whitelistexamples/simple/whitelist.compactMembership verification
Rate Limitexamples/simple/rate-limit.compactAction throttling
Multi-Sigexamples/simple/multi-sig.compactN-of-M approvals
Pausableexamples/simple/pausable.compactEmergency stops
Upgradeableexamples/simple/upgradeable.compactLogic migration
Fee Collectorexamples/simple/fee-collector.compactPayment handling
Random Selectionexamples/simple/random-selection.compactCommit-reveal randomness

Deep-Dive Systems

SystemDirectoryDescription
Private Votingexamples/deep-dives/private-voting/Complete anonymous voting with ZK proofs
Token Escrowexamples/deep-dives/token-escrow/Multi-party custody with conditions
Access Registryexamples/deep-dives/access-registry/Role-based access with Merkle proofs

References

Usage Examples

Finding a Pattern

"Show me the counter pattern" → Read examples/simple/counter.compact

Understanding Complex Systems

"How do I implement private voting?" → Read references/private-voting.md then explore examples/deep-dives/private-voting/

Adapting Patterns

"I need time-locked multi-sig" → Combine time-lock.compact + multi-sig.compact patterns

Best Practices

  1. Start Simple: Use simple patterns as building blocks
  2. Understand Privacy: Know what's public vs private in each pattern
  3. Test Thoroughly: Each pattern includes test considerations
  4. Combine Carefully: When mixing patterns, verify privacy guarantees
  5. Document Intent: Add comments explaining business logic

Dependencies

All patterns use standard library imports:

compact
import CompactStandardLibrary;

Some advanced patterns may require additional imports:

compact
import CompactStandardLibrary.Merkle;
import CompactStandardLibrary.Crypto;