AgentSkillsCN

compact-reviewer:best-practices

当您审查Compact合约,探寻其中的惯用模式、学习推荐的最佳实践,或识别在Midnight智能合约开发中应避免的常见错误时,可选用此功能。

SKILL.md
--- frontmatter
name: compact-reviewer:best-practices
description: Use when reviewing Compact contracts for idiomatic patterns, learning recommended practices, or identifying common mistakes to avoid in Midnight smart contract development.

Best Practices Skill

Guidance on idiomatic Compact patterns and common mistakes to avoid.

When to Use

This skill activates for queries about:

  • Idiomatic Compact code
  • Best practices and conventions
  • Common mistakes
  • Recommended patterns
  • Library usage

Trigger words: best practices, idiomatic, convention, pattern, recommended, should, ought

Quick Reference

Idiomatic Patterns

PatternGoodAvoid
Authorizationrequire_owner() helperInline auth in every circuit
ConstantsNamed constantsMagic numbers
Error handlingDescriptive assertionsSilent failures
State accessControlled via helpersDirect ledger access everywhere
Namingverb_noun for circuitsCryptic abbreviations

Common Mistakes

compact
// ❌ Missing language pragma
// Should be at the top of every file
export circuit example(): [] { }

// ✅ With pragma
pragma language_version >= 0.18.0;
export circuit example(): [] { }
compact
// ❌ Not using Counter for counts
ledger count: Cell<Uint<64>>;
export circuit increment(): [] {
    count.write(count.read() + 1);
}

// ✅ Using Counter ADT
ledger count: Counter;
export circuit increment(): [] {
    count.increment(1);
}

Review Process

1. Pragma and Imports

Check file structure:

  • Pragma present and version specified
  • Imports at top (when supported)
  • Logical ordering of declarations

2. Type Choices

Verify appropriate types:

  • Counter for counts
  • Map for key-value pairs
  • Set for membership
  • Cell for single values
  • Vector for fixed-size arrays

3. Pattern Compliance

Check against idioms:

  • Authorization extracted to helpers
  • Constants defined at top
  • Consistent naming
  • Documented public interfaces

References

Related Skills